diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/books.py | 44 | ||||
-rw-r--r-- | lib/tinytag/tinytag.py | 2 | ||||
-rw-r--r-- | lib/util.py | 7 |
3 files changed, 17 insertions, 36 deletions
diff --git a/lib/books.py b/lib/books.py index 1c4b4a5..3244d13 100644 --- a/lib/books.py +++ b/lib/books.py @@ -9,20 +9,11 @@ from datetime import timedelta 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') -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')) - class Books: - def __init__(self): - ''' - Book-related handlers (r/w cache) and track discovery - ''' - if os.path.exists(JSON_PATH): + def __init__(self, cache_path): + self.cache_path = cache_path + self.json_path = os.path.join(cache_path, 'audiobooks.json') + if os.path.exists(self.json_path): self._cache = self._read_cache() else: self._cache = {} @@ -56,24 +47,21 @@ class Books: ''' Dump contents of :books: to :json_path: ''' - if not os.path.exists(CACHE_PATH): - os.mkdir(CACHE_PATH) - with open(JSON_PATH, 'w') as cache: + if not os.path.exists(self.cache_path): + os.mkdir(self.cache_path) + with open(self.json_path, 'w') as cache: json.dump(self.books, cache, indent=4) def _read_cache(self): ''' Return dict of existing cache ''' - with open(JSON_PATH, 'r') as cache: + with open(self.json_path, 'r') as cache: data = json.load(cache) return data def _validate(self, v, b): - ''' - Returns :v: if :v: and v.isspace(), otherwise :b: - ''' if v and not v.isspace(): return v @@ -86,15 +74,12 @@ 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 - - :cache: existing JSON cache, used to determine which content is new - (existing content is not re-hashed) + Discover audiobooks under :root_path: and populate books ''' ex = self._get_path_hash_dict() - dirs = self._get_dirs(APP.config['ROOT_PATH']) + dirs = self._get_dirs(audiobook_path) books = dict() for path in dirs: @@ -110,8 +95,7 @@ class Books: def _check_dir(self, path): ''' - Determine if :path: contains (supported) audio files; return populated - book dict or None + Determine if :path: contains (supported) audio files ''' ext = ['mp3'] # m4b seems to be unsupported by Apple is_book = False @@ -145,7 +129,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() @@ -207,5 +191,3 @@ class Books: book['duration_str'] = duration_str.split('.')[0] return (folder_hash, book) - - return None diff --git a/lib/tinytag/tinytag.py b/lib/tinytag/tinytag.py index 2ba79be..40e7a59 100644 --- a/lib/tinytag/tinytag.py +++ b/lib/tinytag/tinytag.py @@ -34,7 +34,7 @@ from __future__ import print_function import re -from collections import MutableMapping +from collections.abc import MutableMapping import codecs from functools import reduce import struct diff --git a/lib/util.py b/lib/util.py index 1eb6af6..4693ef1 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' } |