aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDylan Garrett <dylan.garrett@gmail.com>2021-06-06 15:12:58 -0700
committerDylan Garrett <dylan.garrett@gmail.com>2021-06-06 15:12:58 -0700
commite92870d42beaa1a01ab48a60cb322efa95e399bc (patch)
tree8a2a204a411f4077954d3b634039eeea28fc9388
parent53d8bcb0c29f5e7a8492ad715ca25930f997f352 (diff)
downloadroka-e92870d42beaa1a01ab48a60cb322efa95e399bc.tar.gz
roka-e92870d42beaa1a01ab48a60cb322efa95e399bc.zip
Some hacks. But generates some static rss feeds
-rw-r--r--lib/util.py7
-rwxr-xr-xroka.py49
-rw-r--r--templates/index.html2
3 files changed, 51 insertions, 7 deletions
diff --git a/lib/util.py b/lib/util.py
index a2f982d..4a884af 100644
--- a/lib/util.py
+++ b/lib/util.py
@@ -78,9 +78,7 @@ def escape(s):
return s
-def generate_rss(request, books):
- book = request.args.get('a') # audiobook hash
-
+def generate_rss(base_url, book, books, static=False):
# we only make use of the itunes ns, others provided for posterity
namespaces = {
'itunes':'http://www.itunes.com/dtds/podcast-1.0.dtd',
@@ -156,8 +154,9 @@ def generate_rss(request, books):
pub_format = '%a, %d %b %Y %H:%M:%S %z'
pub_date.text = (date(2000, 12, 31) - timedelta(days=idx)).strftime(
pub_format)
+ url_format = '{}{}/{}.mp3' if static else '{}?a={}&f={}'
enc_attr = {
- 'url': '{}?a={}&f={}'.format(request.base_url, book, f),
+ 'url': url_format.format(base_url, book, f),
'length': str(books[book]['files'][f]['size_bytes']),
'type': 'audio/mpeg'
}
diff --git a/roka.py b/roka.py
index 4c4da57..5f0da49 100755
--- a/roka.py
+++ b/roka.py
@@ -2,7 +2,7 @@
import argparse
import os
-from flask import Flask, request, Response, render_template, send_file
+from flask import Flask, request, Response, render_template, send_file, templating
from lib.books import Books
from lib.util import check_auth, escape, generate_rss, read_cache
@@ -11,6 +11,9 @@ 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')
+static_path = os.path.join(abs_path, 'static')
+static_index_path = os.path.join(static_path, 'index.html')
+base_url = "blah.com/"
@app.route('/')
def list_books():
@@ -40,7 +43,7 @@ def list_books():
if not books.get(a):
return 'book not found', 404
- rss = generate_rss(request, books)
+ rss = generate_rss(request.base_url, a, books)
return Response(rss, mimetype='text/xml')
else:
@@ -51,17 +54,59 @@ def list_books():
return render_template('index.html', books=books)
+def my_render_template(
+ app, template_name_or_list, **context
+) -> str:
+ """Renders a template from the template folder with the given
+ context.
+
+ :param template_name_or_list: the name of the template to be
+ rendered, or an iterable with template names
+ the first one existing will be rendered
+ :param context: the variables that should be available in the
+ context of the template.
+ """
+ app.update_template_context(context)
+ return templating._render(
+ app.jinja_env.get_or_select_template(template_name_or_list),
+ context,
+ app,
+ )
+
+def generate():
+ books = Books()
+ books.scan_books()
+ books.write_cache()
+ books = read_cache(json_path)
+ index = my_render_template(app, 'index.html', books=books, static=True)
+
+ indexfile = open(static_index_path, 'w')
+ indexfile.write(index)
+ indexfile.close()
+
+ for key in books.keys():
+ rss = generate_rss(base_url, key, books, static=True)
+ rss_path = os.path.join(static_path, key + '.xml')
+ rssfile = open(rss_path, 'w')
+ rssfile.write(rss.decode('utf-8'))
+ rssfile.close()
+
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)
+ parser.add_argument('--generate', dest='generate', action='store_true',
+ help='',
+ required=False)
args = parser.parse_args()
if args.scan:
books = Books()
books.scan_books()
books.write_cache()
+ elif args.generate:
+ generate()
else:
app.run(host='127.0.0.1', port='8085', threaded=True)
diff --git a/templates/index.html b/templates/index.html
index c05ff8e..4e6998d 100644
--- a/templates/index.html
+++ b/templates/index.html
@@ -35,7 +35,7 @@
</tr>
{% for b, v in books.items() %}
<tr>
- <td><a href="?a={{ b }}">{{ v['title']|escape }}</a></td>
+ <td><a href="{{'?a=' if not static else '/'}}{{ b }}{{'.xml' if static}}">{{ v['title']|escape }}</a></td>
<td>{{ v['path']|escape }}</td>
<td>{{ v['files']|length }}</td>
<td>{{ v['duration_str'] }}</td>