openmedialibrary/oml/media/epub.py

117 lines
3.9 KiB
Python
Raw Normal View History

2014-05-04 17:26:43 +00:00
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
2014-09-02 22:32:44 +00:00
2014-05-04 17:26:43 +00:00
2014-05-19 18:12:02 +00:00
import os
2014-05-04 17:26:43 +00:00
import xml.etree.ElementTree as ET
import zipfile
2014-10-31 18:49:36 +00:00
from io import BytesIO
2014-05-19 18:12:02 +00:00
import re
from urllib.parse import unquote
2014-05-04 17:26:43 +00:00
2014-09-01 10:38:14 +00:00
from PIL import Image
2014-05-04 17:26:43 +00:00
import stdnum.isbn
from utils import normalize_isbn, find_isbns
2014-05-19 18:12:02 +00:00
import logging
logger = logging.getLogger('oml.media.epub')
2014-05-04 17:26:43 +00:00
def cover(path):
2014-05-19 18:12:02 +00:00
logger.debug('cover %s', path)
data = None
try:
z = zipfile.ZipFile(path)
except zipfile.BadZipFile:
logger.debug('invalid epub file %s', path)
return data
2014-05-19 18:12:02 +00:00
for f in z.filelist:
2014-05-21 00:02:21 +00:00
if 'cover' in f.filename.lower() and f.filename.split('.')[-1] in ('jpg', 'jpeg', 'png'):
2014-05-19 18:12:02 +00:00
logger.debug('using %s', f.filename)
data = z.read(f.filename)
break
if not data:
files = [f.filename for f in z.filelist]
opf = [f for f in files if f.endswith('opf')]
2014-05-19 18:12:02 +00:00
if opf:
info = ET.fromstring(z.read(opf[0]))
manifest = info.findall('{http://www.idpf.org/2007/opf}manifest')[0]
for e in manifest.getchildren():
2014-05-21 00:02:21 +00:00
if 'image' in e.attrib['media-type']:
filename = unquote(e.attrib['href'])
2014-05-21 00:02:21 +00:00
filename = os.path.normpath(os.path.join(os.path.dirname(opf[0]), filename))
if filename in files:
data = z.read(filename)
break
2014-05-21 00:02:21 +00:00
elif 'html' in e.attrib['media-type']:
filename = unquote(e.attrib['href'])
2014-05-19 18:12:02 +00:00
filename = os.path.normpath(os.path.join(os.path.dirname(opf[0]), filename))
2014-10-31 08:58:52 +00:00
html = z.read(filename).decode('utf-8')
2014-05-19 18:12:02 +00:00
img = re.compile('<img.*?src="(.*?)"').findall(html)
2015-11-17 18:23:07 +00:00
#svg image
img += re.compile('<image.*?href="(.*?)"').findall(html)
2014-05-19 18:12:02 +00:00
if img:
img = unquote(img[0])
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
2014-05-19 18:12:02 +00:00
if not data:
img = Image.new('RGB', (80, 128))
2014-10-31 18:49:36 +00:00
o = BytesIO()
2014-05-19 18:12:02 +00:00
img.save(o, format='jpeg')
data = o.getvalue()
o.close()
2014-05-04 17:26:43 +00:00
return data
def info(epub):
data = {}
try:
z = zipfile.ZipFile(epub)
except zipfile.BadZipFile:
logger.debug('invalid epub file %s', epub)
return data
2014-05-04 17:26:43 +00:00
opf = [f.filename for f in z.filelist if f.filename.endswith('opf')]
if opf:
info = ET.fromstring(z.read(opf[0]))
metadata = info.findall('{http://www.idpf.org/2007/opf}metadata')[0]
for e in metadata.getchildren():
2015-11-16 15:52:36 +00:00
if e.text and e.text not in ('unknown', 'none'):
2014-05-04 17:26:43 +00:00
key = e.tag.split('}')[-1]
key = {
'creator': 'author',
}.get(key, key)
value = e.text
if key == 'identifier':
value = normalize_isbn(value)
if stdnum.isbn.is_valid(value):
2014-05-21 00:02:21 +00:00
data['isbn'] = [value]
2014-05-04 17:26:43 +00:00
else:
data[key] = e.text
text = extract_text(epub)
data['textsize'] = len(text)
if not 'isbn' in data:
isbn = extract_isbn(text)
if isbn:
2014-05-21 00:02:21 +00:00
data['isbn'] = [isbn]
2014-05-16 14:30:16 +00:00
if 'date' in data and 'T' in data['date']:
data['date'] = data['date'].split('T')[0]
2014-05-04 17:26:43 +00:00
return data
def extract_text(path):
2014-11-15 01:05:33 +00:00
data = ''
2014-05-04 17:26:43 +00:00
z = zipfile.ZipFile(path)
for f in z.filelist:
2014-11-15 01:05:33 +00:00
if '/._' in f.filename or f.filename.startswith('._'):
continue
2014-05-04 17:26:43 +00:00
if f.filename.endswith('html'):
2014-11-15 01:05:33 +00:00
data += z.read(f.filename).decode()
2014-05-04 17:26:43 +00:00
return data
def extract_isbn(data):
isbns = find_isbns(data)
if isbns:
return isbns[0]