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,27 +37,33 @@ 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]))
|
||||||
filename = unquote(e.attrib['href'])
|
data = image_data[-1][1]
|
||||||
filename = os.path.normpath(os.path.join(os.path.dirname(opf[0]), filename))
|
if not data:
|
||||||
html = z.read(filename).decode('utf-8', 'ignore')
|
for e in manifest.getchildren():
|
||||||
img = re.compile('<img.*?src="(.*?)"').findall(html)
|
if 'html' in e.attrib['media-type']:
|
||||||
#svg image
|
filename = unquote(e.attrib['href'])
|
||||||
img += re.compile('<image.*?href="(.*?)"').findall(html)
|
filename = os.path.normpath(os.path.join(os.path.dirname(opf[0]), filename))
|
||||||
if img:
|
html = z.read(filename).decode('utf-8', 'ignore')
|
||||||
img = unquote(img[0])
|
img = re.compile('<img.*?src="(.*?)"').findall(html)
|
||||||
img = os.path.normpath(os.path.join(os.path.dirname(filename), img))
|
#svg image
|
||||||
if img in files:
|
img += re.compile('<image.*?href="(.*?)"').findall(html)
|
||||||
logger.debug('using %s', img)
|
if img:
|
||||||
data = z.read(img)
|
img = unquote(img[0])
|
||||||
break
|
img = os.path.normpath(os.path.join(os.path.dirname(filename), img))
|
||||||
|
if img in files:
|
||||||
|
logger.debug('using %s', img)
|
||||||
|
data = z.read(img)
|
||||||
|
break
|
||||||
if not data:
|
if not data:
|
||||||
img = Image.new('RGB', (80, 128))
|
img = Image.new('RGB', (80, 128))
|
||||||
o = BytesIO()
|
o = BytesIO()
|
||||||
|
@ -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…
Reference in a new issue