diff options
Diffstat (limited to 'roka.py')
-rwxr-xr-x | roka.py | 68 |
1 files changed, 32 insertions, 36 deletions
@@ -5,71 +5,68 @@ import os import shutil import json from flask import Flask, request, Response, render_template, send_file, templating -from flask.globals import _app_ctx_stack +from flask.globals import app_ctx from lib.books import Books from lib.util import check_auth, escape, generate_rss, read_cache -abs_path = os.path.dirname(os.path.abspath(__file__)) app = Flask(__name__) -config_path = os.path.join(abs_path, 'app.cfg') -config_exists = os.path.exists(config_path) -if config_exists or __name__.startswith('uwsgi'): - app.config.from_pyfile(config_path) -cache_path = os.path.join(abs_path, 'cache') -json_path = os.path.join(cache_path, 'audiobooks.json') +CONFIG_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'app.cfg') +if os.path.exists(CONFIG_PATH) or __name__.startswith('uwsgi'): + app.config.from_pyfile(CONFIG_PATH) +CACHE_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'cache') +JSON_PATH = os.path.join(CACHE_PATH, 'audiobooks.json') @app.route('/') def list_books(): ''' Book listing and audiobook RSS/file download - - :a: audiobook hash; if provided without :f: (file) return RSS - :f: file hash; requires associated audiobook (:a:) to download - - Listing of audiobooks returned if no params provided ''' - books = read_cache(json_path) + books = read_cache(JSON_PATH) - a = request.args.get('a') # audiobook hash - f = request.args.get('f') # file hash + book = request.args.get('a') # audiobook hash + track = request.args.get('f') # file hash # audiobook and file parameters provided: serve up file - if a and f: - if not books.get(a) or not books[a]['files'].get(f): + if book and track: + if not books.get(book) or not books[book]['files'].get(track): return 'book or file not found', 404 - f_path = books[a]['files'][f]['path'] - return send_file(f_path, conditional=True) + track_path = books[book]['files'][track]['path'] + return send_file(track_path, conditional=True) # serve up audiobook RSS feed; only audiobook hash provided - elif a: - if not books.get(a): + if book: + if not books.get(book): return 'book not found', 404 - rss = generate_rss(request.base_url, a, books) + rss = generate_rss(request.base_url, book, books) return Response(rss, mimetype='text/xml') - else: - auth = request.authorization - if not auth or not check_auth(app, auth.username, auth.password): - form = {'WWW-Authenticate': 'Basic realm="o/"'} - return Response('unauthorized', 401, form) + # return index if authenticated + auth = request.authorization + if not auth or not check_auth(app, auth.username, auth.password): + form = {'WWW-Authenticate': 'Basic realm="o/"'} + return Response('unauthorized', 401, form) - return render_template('index.html', books=books, + return render_template('index.html', books=books, show_path=app.config.get('SHOW_PATH', True)) def generate(static_path, base_url, audiobook_dirs): + ''' + Static generation of index pages and RSS feeds + ''' static_index_path = os.path.join(static_path, 'index.html') books = Books() books.scan_books(audiobook_dirs) books.write_cache() - books = read_cache(json_path) + books = read_cache(JSON_PATH) # A bit of a hack, but push to the app context stack so we can render a # template outside of a Flask request - _app_ctx_stack.push(app.app_context()) - index = render_template('index.html', books=books, static=True) - _app_ctx_stack.pop() + with app.app_context(): + app_ctx.push() + index = render_template('index.html', books=books, static=True) + app_ctx.pop() os.makedirs(static_path, exist_ok=True) @@ -114,13 +111,12 @@ if __name__ == '__main__': config = objectview(json.loads(args.config)) # override app.cfg app.config.from_object(config) - elif not config_exists: + elif not os.path.exists(CONFIG_PATH): raise Exception(f"Config file '{config_path}' doesn't exist") root_path = os.path.expanduser(app.config['ROOT_PATH']) - if args.scan: - books = Books() + books = Books(CACHE_PATH) books.scan_books(root_path) books.write_cache() elif args.static_path: |