epub parser: take larges image from manifest, strip html tags from description
This commit is contained in:
parent
d3c56670be
commit
78c9c5443f
1 changed files with 27 additions and 18 deletions
|
@ -11,6 +11,7 @@ from urllib.parse import unquote
|
||||||
|
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
import stdnum.isbn
|
import stdnum.isbn
|
||||||
|
from ox import strip_tags, decode_html
|
||||||
|
|
||||||
from utils import normalize_isbn, find_isbns, get_language
|
from utils import normalize_isbn, find_isbns, get_language
|
||||||
|
|
||||||
|
@ -36,14 +37,20 @@ def cover(path):
|
||||||
if opf:
|
if opf:
|
||||||
info = ET.fromstring(z.read(opf[0]))
|
info = ET.fromstring(z.read(opf[0]))
|
||||||
manifest = info.findall('{http://www.idpf.org/2007/opf}manifest')[0]
|
manifest = info.findall('{http://www.idpf.org/2007/opf}manifest')[0]
|
||||||
for e in manifest.getchildren():
|
images = [e for e in manifest.getchildren() if 'image' in e.attrib['media-type']]
|
||||||
if 'image' in e.attrib['media-type']:
|
if images:
|
||||||
|
image_data = []
|
||||||
|
for e in images:
|
||||||
filename = unquote(e.attrib['href'])
|
filename = unquote(e.attrib['href'])
|
||||||
filename = os.path.normpath(os.path.join(os.path.dirname(opf[0]), filename))
|
filename = os.path.normpath(os.path.join(os.path.dirname(opf[0]), filename))
|
||||||
if filename in files:
|
if filename in files:
|
||||||
data = z.read(filename)
|
image_data.append((filename, z.read(filename)))
|
||||||
break
|
if image_data:
|
||||||
elif 'html' in e.attrib['media-type']:
|
image_data.sort(key=lambda i: len(i[1]))
|
||||||
|
data = image_data[-1][1]
|
||||||
|
if not data:
|
||||||
|
for e in manifest.getchildren():
|
||||||
|
if 'html' in e.attrib['media-type']:
|
||||||
filename = unquote(e.attrib['href'])
|
filename = unquote(e.attrib['href'])
|
||||||
filename = os.path.normpath(os.path.join(os.path.dirname(opf[0]), filename))
|
filename = os.path.normpath(os.path.join(os.path.dirname(opf[0]), filename))
|
||||||
html = z.read(filename).decode('utf-8', 'ignore')
|
html = z.read(filename).decode('utf-8', 'ignore')
|
||||||
|
@ -91,6 +98,8 @@ def info(epub):
|
||||||
data[key] = value.split(', ')
|
data[key] = value.split(', ')
|
||||||
else:
|
else:
|
||||||
data[key] = value
|
data[key] = value
|
||||||
|
if 'description' in data:
|
||||||
|
data['description'] = strip_tags(decode_html(data['description']))
|
||||||
text = extract_text(epub)
|
text = extract_text(epub)
|
||||||
data['textsize'] = len(text)
|
data['textsize'] = len(text)
|
||||||
if not 'isbn' in data:
|
if not 'isbn' in data:
|
||||||
|
|
Loading…
Add table
Reference in a new issue