parse epubs without manifest

This commit is contained in:
j 2016-01-06 18:40:23 +05:30
parent c86ba8ac99
commit d866b4de91
1 changed files with 61 additions and 53 deletions

View File

@ -39,43 +39,49 @@ def cover(path):
if opf: if opf:
#logger.debug('opf: %s', z.read(opf[0]).decode()) #logger.debug('opf: %s', z.read(opf[0]).decode())
info = ET.fromstring(z.read(opf[0])) info = ET.fromstring(z.read(opf[0]))
metadata = info.findall('{http://www.idpf.org/2007/opf}metadata')[0] metadata = info.findall('{http://www.idpf.org/2007/opf}metadata')
manifest = info.findall('{http://www.idpf.org/2007/opf}manifest')[0] if metadata:
for e in metadata.getchildren(): metadata = metadata[0]
if e.tag == '{http://www.idpf.org/2007/opf}meta' and e.attrib.get('name') == 'cover': manifest = info.findall('{http://www.idpf.org/2007/opf}manifest')
cover_id = e.attrib['content'] if manifest:
for e in manifest.getchildren(): manifest = manifest[0]
if e.attrib['id'] == cover_id: if metadata and manifest:
filename = unquote(e.attrib['href']) for e in metadata.getchildren():
filename = os.path.normpath(os.path.join(os.path.dirname(opf[0]), filename)) if e.tag == '{http://www.idpf.org/2007/opf}meta' and e.attrib.get('name') == 'cover':
if filename in files: cover_id = e.attrib['content']
return use(filename) for e in manifest.getchildren():
images = [e for e in manifest.getchildren() if 'image' in e.attrib['media-type']] if e.attrib['id'] == cover_id:
if images: filename = unquote(e.attrib['href'])
image_data = [] filename = os.path.normpath(os.path.join(os.path.dirname(opf[0]), filename))
for e in images: if filename in files:
filename = unquote(e.attrib['href']) return use(filename)
filename = os.path.normpath(os.path.join(os.path.dirname(opf[0]), filename)) if manifest:
if filename in files: images = [e for e in manifest.getchildren() if 'image' in e.attrib['media-type']]
image_data.append((filename, z.read(filename))) if images:
if image_data: image_data = []
image_data.sort(key=lambda i: len(i[1])) for e in images:
data = image_data[-1][1] filename = unquote(e.attrib['href'])
logger.debug('using %s', image_data[-1][0]) filename = os.path.normpath(os.path.join(os.path.dirname(opf[0]), filename))
return data if filename in files:
for e in manifest.getchildren(): image_data.append((filename, z.read(filename)))
if 'html' in e.attrib['media-type']: if image_data:
filename = unquote(e.attrib['href']) image_data.sort(key=lambda i: len(i[1]))
filename = os.path.normpath(os.path.join(os.path.dirname(opf[0]), filename)) data = image_data[-1][1]
html = z.read(filename).decode('utf-8', 'ignore') logger.debug('using %s', image_data[-1][0])
img = re.compile('<img.*?src="(.*?)"').findall(html) return data
#svg image for e in manifest.getchildren():
img += re.compile('<image.*?href="(.*?)"').findall(html) if 'html' in e.attrib['media-type']:
if img: filename = unquote(e.attrib['href'])
img = unquote(img[0]) filename = os.path.normpath(os.path.join(os.path.dirname(opf[0]), filename))
img = os.path.normpath(os.path.join(os.path.dirname(filename), img)) html = z.read(filename).decode('utf-8', 'ignore')
if img in files: img = re.compile('<img.*?src="(.*?)"').findall(html)
return use(img) #svg image
img += re.compile('<image.*?href="(.*?)"').findall(html)
if img:
img = unquote(img[0])
img = os.path.normpath(os.path.join(os.path.dirname(filename), img))
if img in files:
return use(img)
# fallback return black cover # fallback return black cover
img = Image.new('RGB', (80, 128)) img = Image.new('RGB', (80, 128))
o = BytesIO() o = BytesIO()
@ -94,22 +100,24 @@ def info(epub):
opf = [f.filename for f in z.filelist if f.filename.endswith('opf')] opf = [f.filename for f in z.filelist if f.filename.endswith('opf')]
if opf: if opf:
info = ET.fromstring(z.read(opf[0])) info = ET.fromstring(z.read(opf[0]))
metadata = info.findall('{http://www.idpf.org/2007/opf}metadata')[0] metadata = info.findall('{http://www.idpf.org/2007/opf}metadata')
for e in metadata.getchildren(): if metadata:
if e.text and e.text.strip() and e.text not in ('unknown', 'none'): metadata = metadata[0]
key = e.tag.split('}')[-1] for e in metadata.getchildren():
key = { if e.text and e.text.strip() and e.text not in ('unknown', 'none'):
'creator': 'author', key = e.tag.split('}')[-1]
}.get(key, key) key = {
value = e.text.strip() 'creator': 'author',
if key == 'identifier': }.get(key, key)
value = normalize_isbn(value) value = e.text.strip()
if stdnum.isbn.is_valid(value): if key == 'identifier':
data['isbn'] = [value] value = normalize_isbn(value)
elif key == 'author': if stdnum.isbn.is_valid(value):
data[key] = value.split(', ') data['isbn'] = [value]
else: elif key == 'author':
data[key] = value data[key] = value.split(', ')
else:
data[key] = value
if 'description' in data: if 'description' in data:
data['description'] = strip_tags(decode_html(data['description'])) data['description'] = strip_tags(decode_html(data['description']))
text = extract_text(epub) text = extract_text(epub)