general.py 2.33 KB
Newer Older
Lukáš Lalinský's avatar
Lukáš Lalinský committed
1
import os
2
import requests
Lukáš Lalinský's avatar
Lukáš Lalinský committed
3 4
import markdown.util
from markdown import Markdown
5 6
from flask import Blueprint, render_template, render_template_string, current_app, redirect, url_for
from acoustid.utils import generate_demo_client_api_key
Lukáš Lalinský's avatar
Lukáš Lalinský committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

general_page = Blueprint('general', __name__)


@general_page.route('/favicon.ico')
def favicon_ico():
    return redirect(url_for('static', filename='favicon.ico'))


class MarkdownFlaskUrlProcessor(markdown.util.Processor):

    def run(self, root):
        stack = [root]
        while stack:
            element = stack.pop()
            if element.tag == 'a':
                href = element.get('href')
                if href.startswith('flask:'):
                    element.set('href', url_for(href.split(':', 1)[1]))
            for child in element:
                stack.append(child)


30
def render_page(name, **context):
Lukáš Lalinský's avatar
Lukáš Lalinský committed
31 32 33
    path = os.path.join('pages', name)
    with current_app.open_resource(path) as file:
        text = file.read().decode('utf8')
34
        text = render_template_string(text, **context)
Lukáš Lalinský's avatar
Lukáš Lalinský committed
35
        md = Markdown(extensions=['meta'])
Lukáš Lalinský's avatar
Lukáš Lalinský committed
36
        md.treeprocessors.register(MarkdownFlaskUrlProcessor(md), 'flask_links', 50)
Lukáš Lalinský's avatar
Lukáš Lalinský committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
        html = md.convert(text)
        title = ' '.join(md.Meta.get('title', []))
        return render_template('page.html', content=html, title=title)


def add_page_route(name, path=None):
    if path is None:
        path = '/' + name
    general_page.add_url_rule(path, name, lambda: render_page(name + '.md'))


add_page_route('index', '/')
add_page_route('contact')
add_page_route('database')
add_page_route('docs')
add_page_route('donate')
add_page_route('faq')
add_page_route('fingerprinter')
add_page_route('license')
add_page_route('server')
Lukáš Lalinský's avatar
Lukáš Lalinský committed
57
add_page_route('applications')
Lukáš Lalinský's avatar
Lukáš Lalinský committed
58
add_page_route('about')
59 60 61 62 63 64


@general_page.route('/webservice')
def webservice():
    return render_page('webservice.md',
        client_api_key=generate_demo_client_api_key(current_app.config['SECRET_KEY']))
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79


def get_latest_chromaprint_release():
    rv = requests.get('https://api.github.com/repos/acoustid/chromaprint/releases?per_page=1')
    rv.raise_for_status()
    releases = rv.json()
    if not releases:
        return None
    return releases[0]


@general_page.route('/chromaprint')
def chromaprint():
    release = get_latest_chromaprint_release()
    return render_page('chromaprint.md', release=release)