From e92870d42beaa1a01ab48a60cb322efa95e399bc Mon Sep 17 00:00:00 2001 From: Dylan Garrett Date: Sun, 6 Jun 2021 15:12:58 -0700 Subject: Some hacks. But generates some static rss feeds --- lib/util.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'lib') 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' } -- cgit v1.2.3-54-g00ecf From f27620092d16676b77e92ae409c15e71d246f025 Mon Sep 17 00:00:00 2001 From: Dylan Garrett Date: Sun, 6 Jun 2021 15:51:04 -0700 Subject: Allow setting scan dir from terminal --- lib/books.py | 6 +++--- roka.py | 11 +++++++---- 2 files changed, 10 insertions(+), 7 deletions(-) (limited to 'lib') diff --git a/lib/books.py b/lib/books.py index 1c4b4a5..84754f6 100644 --- a/lib/books.py +++ b/lib/books.py @@ -86,7 +86,7 @@ class Books: now = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S") print('%s %s' % (now, msg)) - def scan_books(self): + def scan_books(self, audiobook_path=None): ''' Discover audiobooks under :root_path: and populate books object @@ -94,7 +94,7 @@ class Books: (existing content is not re-hashed) ''' ex = self._get_path_hash_dict() - dirs = self._get_dirs(APP.config['ROOT_PATH']) + dirs = self._get_dirs(audiobook_path or APP.config['ROOT_PATH']) books = dict() for path in dirs: @@ -145,7 +145,7 @@ class Books: # previous conditions met, we've found at least one track is_book = True - self._log(f) + self._log(os.path.join(path, f)) # hash track (used as a key) and update folder hash file_hash = hashlib.md5() diff --git a/roka.py b/roka.py index aafc3a3..809cf3c 100755 --- a/roka.py +++ b/roka.py @@ -72,11 +72,11 @@ def my_render_template( app, ) -def generate(static_path, base_url): +def generate(static_path, base_url, audiobook_dirs): static_index_path = os.path.join(static_path, 'index.html') books = Books() - books.scan_books() + books.scan_books(audiobook_dirs) # TODO avoid writing and reading cache books.write_cache() books = read_cache(json_path) @@ -115,6 +115,9 @@ if __name__ == '__main__': parser.add_argument('--base_url', dest='base_url', type=str, action='store', help='Base URL to use in static files', required=False) + parser.add_argument('--scan_dir', dest='scan_dir', type=str, action='store', + help='Directory to scan for audiobooks', + required=False) args = parser.parse_args() if args.static_path and not args.base_url or args.base_url and not args.static_path: @@ -122,9 +125,9 @@ if __name__ == '__main__': if args.scan: books = Books() - books.scan_books() + books.scan_books(args.scan_dir) books.write_cache() elif args.static_path: - generate(args.static_path, args.base_url) + generate(args.static_path, args.base_url, args.scan_dir) else: app.run(host='127.0.0.1', port='8085', threaded=True) -- cgit v1.2.3-54-g00ecf From 3d850b4d695acb4de3616eca947cfb0b7390154e Mon Sep 17 00:00:00 2001 From: Dylan Garrett Date: Sun, 6 Jun 2021 15:58:51 -0700 Subject: Allow base_url with no app.cfg --- lib/books.py | 3 ++- roka.py | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/books.py b/lib/books.py index 84754f6..6f3510b 100644 --- a/lib/books.py +++ b/lib/books.py @@ -15,7 +15,8 @@ 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')) +if os.path.exists(os.path.join(ABS_PATH, '../', 'app.cfg')): + APP.config.from_pyfile(os.path.join(ABS_PATH, '../', 'app.cfg')) class Books: def __init__(self): diff --git a/roka.py b/roka.py index 4b6a523..8d81641 100755 --- a/roka.py +++ b/roka.py @@ -9,7 +9,8 @@ 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')) +if os.path.exists(os.path.join(abs_path, 'app.cfg')): + 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') -- cgit v1.2.3-54-g00ecf From a262d5da79d4140fa866f885481ffcad2c33bb93 Mon Sep 17 00:00:00 2001 From: Dylan Garrett Date: Mon, 7 Jun 2021 07:58:48 -0700 Subject: Fix sort --- lib/util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/util.py b/lib/util.py index 4a884af..4693ef1 100644 --- a/lib/util.py +++ b/lib/util.py @@ -119,7 +119,7 @@ def generate_rss(base_url, book, books, static=False): track_list.append(track) else: # we have populated and unique track values, use those - key = lambda x: books[book]['files'][x]['track'] + key = lambda x: int(books[book]['files'][x]['track']) # populate XML attribute values required by Apple podcasts for idx, f in enumerate(sorted(books[book]['files'], key=key)): -- cgit v1.2.3-54-g00ecf From 1356749036af0fc07253aac4f534d84c99e8c741 Mon Sep 17 00:00:00 2001 From: Dylan Garrett Date: Wed, 9 Jun 2021 22:08:05 -0700 Subject: Cleanup config and allow passing as json on terminal --- lib/books.py | 7 +------ roka.py | 26 ++++++++++++++++++-------- 2 files changed, 19 insertions(+), 14 deletions(-) (limited to 'lib') diff --git a/lib/books.py b/lib/books.py index 6f3510b..95df374 100644 --- a/lib/books.py +++ b/lib/books.py @@ -13,11 +13,6 @@ ABS_PATH = os.path.dirname(os.path.abspath(__file__)) 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__) -if os.path.exists(os.path.join(ABS_PATH, '../', 'app.cfg')): - APP.config.from_pyfile(os.path.join(ABS_PATH, '../', 'app.cfg')) - class Books: def __init__(self): ''' @@ -95,7 +90,7 @@ class Books: (existing content is not re-hashed) ''' ex = self._get_path_hash_dict() - dirs = self._get_dirs(audiobook_path or APP.config['ROOT_PATH']) + dirs = self._get_dirs(audiobook_path) books = dict() for path in dirs: diff --git a/roka.py b/roka.py index 8d81641..cfcd7e5 100755 --- a/roka.py +++ b/roka.py @@ -3,14 +3,13 @@ import argparse import os import shutil +import json 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 abs_path = os.path.dirname(os.path.abspath(__file__)) app = Flask(__name__) -if os.path.exists(os.path.join(abs_path, 'app.cfg')): - 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') @@ -78,7 +77,6 @@ def generate(static_path, base_url, audiobook_dirs): books = Books() books.scan_books(audiobook_dirs) - # TODO avoid writing and reading cache books.write_cache() books = read_cache(json_path) index = my_render_template(app, 'index.html', books=books, static=True) @@ -102,7 +100,8 @@ def generate(static_path, base_url, audiobook_dirs): for f_key, file in book['files'].items(): f_path = file['path'] copy_path = os.path.join(book_dir, f_key + '.mp3') - shutil.copyfile(f_path, copy_path) + if not os.path.exists(copy_path): + shutil.copyfile(f_path, copy_path) if __name__ == '__main__': desc = 'roka: listen to audiobooks with podcast apps via RSS' @@ -116,19 +115,30 @@ if __name__ == '__main__': parser.add_argument('--base_url', dest='base_url', type=str, action='store', help='Base URL to use in static files', required=False) - parser.add_argument('--scan_dir', dest='scan_dir', type=str, action='store', - help='Directory to scan for audiobooks', + parser.add_argument('--config', dest='config', type=str, action='store', + help='Json configuration instead of app.cfg', required=False) args = parser.parse_args() if args.static_path and not args.base_url or args.base_url and not args.static_path: parser.error('--generate and --base_url must be included together') + if args.config: + class objectview(object): + def __init__(self, d): + self.__dict__ = d + config = objectview(json.loads(args.config)) + app.config.from_object(config) + elif os.path.exists(os.path.join(abs_path, 'app.cfg')): + app.config.from_pyfile(os.path.join(abs_path, 'app.cfg')) + + root_path = os.path.expanduser(app.config['ROOT_PATH']) + if args.scan: books = Books() - books.scan_books(args.scan_dir) + books.scan_books(root_path) books.write_cache() elif args.static_path: - generate(args.static_path, args.base_url, args.scan_dir) + generate(args.static_path, args.base_url, root_path) else: app.run(host='127.0.0.1', port='8085', threaded=True) -- cgit v1.2.3-54-g00ecf