store metadata per user. remove primaryid. only store isbn13
This commit is contained in:
parent
90648f9e65
commit
02e040d9f5
16 changed files with 245 additions and 192 deletions
|
|
@ -5,14 +5,13 @@
|
|||
import os
|
||||
import xml.etree.ElementTree as ET
|
||||
import zipfile
|
||||
from io import BytesIO
|
||||
import re
|
||||
from urllib.parse import unquote
|
||||
import lxml.html
|
||||
|
||||
import stdnum.isbn
|
||||
from ox import strip_tags, decode_html
|
||||
|
||||
from utils import normalize_isbn, find_isbns, get_language
|
||||
from utils import find_isbns, get_language, to_isbn13
|
||||
|
||||
import logging
|
||||
logger = logging.getLogger(__name__)
|
||||
|
|
@ -104,13 +103,24 @@ def info(epub):
|
|||
}.get(key, key)
|
||||
value = e.text.strip()
|
||||
if key == 'identifier':
|
||||
value = normalize_isbn(value)
|
||||
if stdnum.isbn.is_valid(value):
|
||||
data['isbn'] = [value]
|
||||
value = to_isbn13(value)
|
||||
if value:
|
||||
data['isbn'] = value
|
||||
elif key == 'author':
|
||||
data[key] = value.split(', ')
|
||||
else:
|
||||
data[key] = value
|
||||
guide = info.findall('{http://www.idpf.org/2007/opf}guide')
|
||||
if guide:
|
||||
for ref in guide[0].findall('{http://www.idpf.org/2007/opf}reference'):
|
||||
if ref.attrib.get('type') == 'toc':
|
||||
filename = unquote(ref.attrib['href'])
|
||||
filename = os.path.normpath(os.path.join(os.path.dirname(opf[0]), filename))
|
||||
toc = z.read(filename)
|
||||
if toc:
|
||||
doc = lxml.html.document_fromstring(toc)
|
||||
data['tableofcontents'] = '\n'.join([a.text_content() for a in doc.xpath('//a')])
|
||||
|
||||
if 'description' in data:
|
||||
data['description'] = strip_tags(decode_html(data['description']))
|
||||
text = extract_text(epub)
|
||||
|
|
@ -118,7 +128,7 @@ def info(epub):
|
|||
if not 'isbn' in data:
|
||||
isbn = extract_isbn(text)
|
||||
if isbn:
|
||||
data['isbn'] = [isbn]
|
||||
data['isbn'] = isbn
|
||||
if 'date' in data and 'T' in data['date']:
|
||||
data['date'] = data['date'].split('T')[0]
|
||||
if 'language' in data and isinstance(data['language'], str):
|
||||
|
|
@ -139,4 +149,3 @@ def extract_isbn(data):
|
|||
isbns = find_isbns(data)
|
||||
if isbns:
|
||||
return isbns[0]
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue