aboutsummaryrefslogtreecommitdiff
path: root/roka.py
diff options
context:
space:
mode:
authorJordan <me@jordan.im>2020-09-23 21:43:11 -0700
committerJordan <me@jordan.im>2020-09-23 21:43:11 -0700
commitf7055594aac17b2d5fd4e936a5924ece3e68cd63 (patch)
treea654130d7e51306abcffe7f8b4fa85001bf57578 /roka.py
parent50eae99c1991295a4e32facea456d17e3c6a2c6c (diff)
downloadroka-f7055594aac17b2d5fd4e936a5924ece3e68cd63.tar.gz
roka-f7055594aac17b2d5fd4e936a5924ece3e68cd63.zip
update name to roka, support --scan, cleanup
Diffstat (limited to 'roka.py')
-rwxr-xr-xroka.py67
1 files changed, 67 insertions, 0 deletions
diff --git a/roka.py b/roka.py
new file mode 100755
index 0000000..4c4da57
--- /dev/null
+++ b/roka.py
@@ -0,0 +1,67 @@
+#!/usr/bin/env python3
+
+import argparse
+import os
+from flask import Flask, request, Response, render_template, send_file
+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__)
+app.config.from_pyfile(os.path.join(abs_path, 'app.cfg'))
+cache_path = os.path.join(abs_path, '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)
+
+ a = request.args.get('a') # audiobook hash
+ f = 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):
+ return 'book or file not found', 404
+
+ f_path = books[a]['files'][f]['path']
+ return send_file(f_path, conditional=True)
+
+ # serve up audiobook RSS feed; only audiobook hash provided
+ elif a:
+ if not books.get(a):
+ return 'book not found', 404
+
+ rss = generate_rss(request, 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 render_template('index.html', books=books)
+
+if __name__ == '__main__':
+ desc = 'roka: listen to audiobooks with podcast apps via RSS'
+ parser = argparse.ArgumentParser(description=desc)
+ parser.add_argument('--scan', dest='scan', action='store_true',
+ help='scan audiobooks directory for new books',
+ required=False)
+ args = parser.parse_args()
+
+ if args.scan:
+ books = Books()
+ books.scan_books()
+ books.write_cache()
+ else:
+ app.run(host='127.0.0.1', port='8085', threaded=True)