From 95085bde8cb4f3ae660beadd81e9a3c909442cc1 Mon Sep 17 00:00:00 2001 From: j Date: Tue, 27 May 2014 16:08:14 +0200 Subject: [PATCH] use metadata.opf or metadata if available --- oml/item/scan.py | 8 ++++---- oml/media/__init__.py | 19 ++++++++++++++---- oml/media/opf.py | 45 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 8 deletions(-) create mode 100644 oml/media/opf.py diff --git a/oml/item/scan.py b/oml/item/scan.py index f1d85b5..8b96175 100644 --- a/oml/item/scan.py +++ b/oml/item/scan.py @@ -40,10 +40,10 @@ def remove_missing(): if dirty: db.session.commit() -def add_file(id, f, prefix): +def add_file(id, f, prefix, from_=None): user = state.user() path = f[len(prefix):] - data = media.metadata(f) + data = media.metadata(f, from_) file = File.get_or_create(id, data, path) item = file.item if 'primaryid' in file.info: @@ -88,7 +88,7 @@ def run_scan(): id = media.get_id(f) file = File.get(id) if not file: - file = add_file(id, f, prefix) + file = add_file(id, f, prefix, f) added += 1 trigger_event('change', {}) @@ -168,7 +168,7 @@ def run_import(options=None): shutil.move(f_import, f) else: shutil.copy(f_import, f) - file = add_file(id, f, prefix_books) + file = add_file(id, f, prefix_books, f_import) file.move() item = file.item if listname: diff --git a/oml/media/__init__.py b/oml/media/__init__.py index 8b0a7b3..5b8fbb1 100644 --- a/oml/media/__init__.py +++ b/oml/media/__init__.py @@ -11,6 +11,7 @@ import ox import pdf import epub import txt +import opf def get_id(f=None, data=None): if data: @@ -19,11 +20,12 @@ def get_id(f=None, data=None): return base64.b32encode(ox.sha1sum(f, cached=True).decode('hex')) -def metadata(f): +def metadata(f, from_=None): ext = f.split('.')[-1] data = {} data['extension'] = ext data['size'] = os.stat(f).st_size + if ext == 'pdf': info = pdf.info(f) elif ext == 'epub': @@ -31,9 +33,15 @@ def metadata(f): elif ext == 'txt': info = txt.info(f) + opf_info = {} + metadata_opf = os.path.join(os.path.dirname(from_ or f), 'metadata.opf') + if os.path.exists(metadata_opf): + opf_info = opf.info(metadata_opf) + for key in ( - 'title', 'author', 'date', 'publisher', 'isbn', - 'textsize', 'pages' + 'title', 'author', 'date', 'publisher', + 'language', 'textsize', 'pages', + 'isbn', 'asin' ): if key in info: value = info[key] @@ -44,9 +52,12 @@ def metadata(f): value = None if value: data[key] = info[key] - + if key in opf_info: + data[key] = opf_info[key] if 'isbn' in data: data['primaryid'] = ['isbn', data['isbn'][0]] + elif 'asin' in data: + data['primaryid'] = ['asin', data['asin'][0]] if 'author' in data: if isinstance(data['author'], basestring): data['author'] = data['author'].split('; ') diff --git a/oml/media/opf.py b/oml/media/opf.py new file mode 100644 index 0000000..dc55ef3 --- /dev/null +++ b/oml/media/opf.py @@ -0,0 +1,45 @@ +# -*- coding: utf-8 -*- +# vi:si:et:sw=4:sts=4:ts=4 +from __future__ import division + +import xml.etree.ElementTree as ET + +import stdnum.isbn + +from utils import normalize_isbn +from ox import strip_tags +import ox.iso + +def info(opf): + data = {} + with open(opf) as fd: + opf = ET.fromstring(fd.read()) + ns = '{http://www.idpf.org/2007/opf}' + metadata = opf.findall(ns + 'metadata')[0] + for e in metadata.getchildren(): + if e.text: + key = e.tag.split('}')[-1] + key = { + 'creator': 'author', + }.get(key, key) + value = e.text + if key == 'identifier': + isbn = normalize_isbn(value) + if stdnum.isbn.is_valid(isbn): + if not 'isbn' in data: + data['isbn'] = [isbn] + else: + data['isbn'].append(isbn) + if e.attrib.get(ns + 'scheme') == 'AMAZON': + if not 'asin' in data: + data['asin'] = [value] + else: + data['asin'].append(value) + else: + data[key] = strip_tags(e.text) + #YYY-MM-DD + if 'date' in data and len(data['date']) > 10: + data['date'] =data['date'][:10] + if 'language' in data: + data['language'] = ox.iso.codeToLang(data['language']) + return data