diff --git a/oml/commands.py b/oml/commands.py index a1dce28..272e1b9 100644 --- a/oml/commands.py +++ b/oml/commands.py @@ -242,20 +242,11 @@ def command_dump_json(*args): if not args: print('usage: ./ctl json_dump dump.json') sys.exit(1) - import json - from ox.django.shortcuts import _to_json - import item.models import db + import state with db.session(): - items = [] - for i in item.models.Item.query: - j = i.json() - for f in i.files: - j['path'] = f.fullpath() - break - items.append(j) - with open(args[0], 'w') as f: - json.dump(items, f, indent=1, default=_to_json, ensure_ascii=False, sort_keys=True) + library = state.user().library + library.export_json(args[0]) def main(): actions = globals() diff --git a/oml/downloads.py b/oml/downloads.py index cee6396..8bd6cc4 100644 --- a/oml/downloads.py +++ b/oml/downloads.py @@ -28,6 +28,7 @@ class Downloads(Thread): if now > settings.server.get('last_update_check', 0) + 24*60*60: settings.server['last_update_check'] = now update.download() + state.user().library.export_json() def download_next(self): import item.models diff --git a/oml/user/models.py b/oml/user/models.py index c6d0d3e..2e32c99 100644 --- a/oml/user/models.py +++ b/oml/user/models.py @@ -337,6 +337,24 @@ class List(db.Model): def create_symlinks(self): pass + def export_json(self, path=None): + if not path: + if self.name: + name = os.path.join('Lists', self.name) + else: + name = 'Books' + path = os.path.join(os.path.expanduser(settings.preferences['libraryPath']), name, 'library.json') + from utils import _to_json + items = [] + for i in self.get_items(): + j = i.json() + for f in i.files: + j['path'] = f.path + break + items.append(j) + with open(path, 'w') as f: + json.dump(items, f, indent=1, default=_to_json, ensure_ascii=False, sort_keys=True) + class Metadata(db.Model): __tablename__ = 'user_metadata' @@ -475,6 +493,7 @@ def export_list(data): for f in list(existing_files - new_files): os.unlink(f) utils.remove_empty_folders(prefix) + self.export_json(os.path.join(prefix, 'library.json')) trigger_event('activity', { 'activity': 'export', 'progress': [count, count], diff --git a/oml/utils.py b/oml/utils.py index 6f3809a..f669146 100644 --- a/oml/utils.py +++ b/oml/utils.py @@ -383,3 +383,11 @@ def can_connect_dns(host="8.8.8.8", port=53): except: pass return False + +def _to_json(python_object): + if isinstance(python_object, datetime): + if python_object.year < 1900: + tt = python_object.timetuple() + return '%d-%02d-%02dT%02d:%02d%02dZ' % tuple(list(tt)[:6]) + return python_object.strftime('%Y-%m-%dT%H:%M:%SZ') + raise TypeError(u'%s %s is not JSON serializable' % (repr(python_object), type(python_object)))