aboutsummaryrefslogtreecommitdiff
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
parent50eae99c1991295a4e32facea456d17e3c6a2c6c (diff)
downloadroka-f7055594aac17b2d5fd4e936a5924ece3e68cd63.tar.gz
roka-f7055594aac17b2d5fd4e936a5924ece3e68cd63.zip
update name to roka, support --scan, cleanup
-rw-r--r--README11
-rw-r--r--[-rwxr-xr-x]lib/books.py (renamed from rebuild.py)22
-rw-r--r--lib/util.py2
-rwxr-xr-xroka.py (renamed from run.py)16
-rw-r--r--uwsgi.ini.example2
5 files changed, 34 insertions, 19 deletions
diff --git a/README b/README
index cf9254b..fac2e80 100644
--- a/README
+++ b/README
@@ -1,7 +1,7 @@
-audiobook-rss: stream directory of audiobooks to podcasting apps via RSS
+roka: stream directory of audiobooks to podcasting apps via RSS
-demo (no audio): https://demo.jordan.im/audiobook-rss/
-iOS podcast app: https://demo.jordan.im/audiobook-rss/apple-podcasts.png
+demo (no audio): https://demo.jordan.im/roka/
+iOS podcast app: https://demo.jordan.im/roka/apple-podcasts.png
installation
------------
@@ -12,9 +12,10 @@ b) install python dependencies flask and uwsgi
$ pip install --user flask uwsgi
-c) execute rebuild.py to populate audiobook JSON cache
+c) run roka.py with --scan to populate audiobook JSON cache (can be re-run to
+ update cache upon download of new books)
- $ ./rebuild.py
+ $ ./roka.py --scan
d) execute uwsgi.sh to start the server
diff --git a/rebuild.py b/lib/books.py
index d9144ca..86ab4b6 100755..100644
--- a/rebuild.py
+++ b/lib/books.py
@@ -10,12 +10,12 @@ from flask import Flask
from lib.tinytag import TinyTag
ABS_PATH = os.path.dirname(os.path.abspath(__file__))
-CACHE_PATH = os.path.join(ABS_PATH, 'cache')
+CACHE_PATH = os.path.join(ABS_PATH, '../', 'cache')
JSON_PATH = os.path.join(CACHE_PATH, 'audiobooks.json')
# use Flask's config parser, configparser would be hacky
APP = Flask(__name__)
-APP.config.from_pyfile(os.path.join(ABS_PATH, 'app.cfg'))
+APP.config.from_pyfile(os.path.join(ABS_PATH, '../', 'app.cfg'))
class Books:
def __init__(self):
@@ -27,9 +27,6 @@ class Books:
else:
self._cache = {}
- self.books = self._get_books()
- self._write_cache()
-
def _get_dirs(self, path):
'''
Return list of directories recursively discovered in :path:
@@ -38,6 +35,7 @@ class Books:
for root, dirs, _ in os.walk(path):
for d in dirs:
ret.append(os.path.join(root, d))
+
return ret
def _get_path_hash_dict(self):
@@ -52,9 +50,10 @@ class Books:
path = self._cache[k]['path']
if os.path.exists(path):
ret[path] = k
+
return ret
- def _write_cache(self):
+ def write_cache(self):
'''
Dump contents of :books: to :json_path:
'''
@@ -69,6 +68,7 @@ class Books:
'''
with open(JSON_PATH, 'r') as cache:
data = json.load(cache)
+
return data
def _validate(self, v, b):
@@ -77,6 +77,7 @@ class Books:
'''
if v and not v.isspace():
return v
+
return b
def _log(self, msg):
@@ -86,7 +87,7 @@ class Books:
now = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S")
print('%s %s' % (now, msg))
- def _get_books(self):
+ def scan_books(self):
'''
Discover audiobooks under :root_path: and populate books object
@@ -105,7 +106,8 @@ class Books:
book = self._check_dir(path)
if book:
books[book[0]] = book[1]
- return books
+
+ self.books = books
def _check_dir(self, path):
'''
@@ -204,9 +206,7 @@ class Books:
# e.g. 2 days, 5:47:47
duration_str = str(timedelta(seconds=book['duration']))
book['duration_str'] = duration_str.split('.')[0]
+
return (folder_hash, book)
return None
-
-if __name__ == '__main__':
- books = Books()
diff --git a/lib/util.py b/lib/util.py
index fbddb2e..a2f982d 100644
--- a/lib/util.py
+++ b/lib/util.py
@@ -23,7 +23,7 @@ def read_cache(json_path):
except Exception:
raise ValueError('error loading JSON cache')
else:
- raise ValueError('cache not found, run rebuild.py')
+ raise ValueError('cache not found, run ./roka.py --scan')
return books
diff --git a/run.py b/roka.py
index e48f19f..4c4da57 100755
--- a/run.py
+++ b/roka.py
@@ -1,7 +1,9 @@
#!/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__))
@@ -50,4 +52,16 @@ def list_books():
return render_template('index.html', books=books)
if __name__ == '__main__':
- app.run(host='127.0.0.1', port='8085', threaded=True)
+ 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)
diff --git a/uwsgi.ini.example b/uwsgi.ini.example
index 49dee9c..2a2093f 100644
--- a/uwsgi.ini.example
+++ b/uwsgi.ini.example
@@ -2,6 +2,6 @@
http = 127.0.0.1:8085
processes = 2
threads = 4
-wsgi-file = run.py
+wsgi-file = roka.py
callable = app
master = true