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
|
2015-11-16 15:02:45 +00:00
|
|
|
from urllib.parse import unquote
|
2014-05-04 17:26:43 +00:00
|
|
|
|
|
|
|
import stdnum.isbn
|
2016-01-05 09:11:42 +00:00
|
|
|
from ox import strip_tags, decode_html
|
2014-05-04 17:26:43 +00:00
|
|
|
|
2015-12-25 14:10:49 +00:00
|
|
|
from utils import normalize_isbn, find_isbns, get_language
|
2014-05-04 17:26:43 +00:00
|
|
|
|
2014-05-19 18:12:02 +00:00
|
|
|
import logging
|
2015-11-29 14:56:38 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2014-05-19 18:12:02 +00:00
|
|
|
|
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
|
2015-11-16 15:02:45 +00:00
|
|
|
try:
|
|
|
|
z = zipfile.ZipFile(path)
|
|
|
|
except zipfile.BadZipFile:
|
|
|
|
logger.debug('invalid epub file %s', path)
|
|
|
|
return data
|
2016-01-05 09:50:47 +00:00
|
|
|
|
|
|
|
def use(filename):
|
|
|
|
logger.debug('using %s', filename)
|
|
|
|
return z.read(filename)
|
|
|
|
|
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'):
|
2016-01-05 09:50:47 +00:00
|
|
|
return use(f.filename)
|
|
|
|
files = [f.filename for f in z.filelist]
|
|
|
|
opf = [f for f in files if f.endswith('opf')]
|
|
|
|
if opf:
|
|
|
|
#logger.debug('opf: %s', z.read(opf[0]).decode())
|
|
|
|
info = ET.fromstring(z.read(opf[0]))
|
2016-01-06 13:10:23 +00:00
|
|
|
metadata = info.findall('{http://www.idpf.org/2007/opf}metadata')
|
|
|
|
if metadata:
|
|
|
|
metadata = metadata[0]
|
|
|
|
manifest = info.findall('{http://www.idpf.org/2007/opf}manifest')
|
|
|
|
if manifest:
|
|
|
|
manifest = manifest[0]
|
|
|
|
if metadata and manifest:
|
|
|
|
for e in metadata.getchildren():
|
|
|
|
if e.tag == '{http://www.idpf.org/2007/opf}meta' and e.attrib.get('name') == 'cover':
|
|
|
|
cover_id = e.attrib['content']
|
|
|
|
for e in manifest.getchildren():
|
|
|
|
if e.attrib['id'] == cover_id:
|
|
|
|
filename = unquote(e.attrib['href'])
|
|
|
|
filename = os.path.normpath(os.path.join(os.path.dirname(opf[0]), filename))
|
|
|
|
if filename in files:
|
|
|
|
return use(filename)
|
|
|
|
if manifest:
|
|
|
|
images = [e for e in manifest.getchildren() if 'image' in e.attrib['media-type']]
|
|
|
|
if images:
|
|
|
|
image_data = []
|
|
|
|
for e in images:
|
|
|
|
filename = unquote(e.attrib['href'])
|
|
|
|
filename = os.path.normpath(os.path.join(os.path.dirname(opf[0]), filename))
|
|
|
|
if filename in files:
|
|
|
|
image_data.append((filename, z.read(filename)))
|
|
|
|
if image_data:
|
|
|
|
image_data.sort(key=lambda i: len(i[1]))
|
|
|
|
data = image_data[-1][1]
|
|
|
|
logger.debug('using %s', image_data[-1][0])
|
|
|
|
return data
|
|
|
|
for e in manifest.getchildren():
|
|
|
|
if 'html' in e.attrib['media-type']:
|
|
|
|
filename = unquote(e.attrib['href'])
|
|
|
|
filename = os.path.normpath(os.path.join(os.path.dirname(opf[0]), filename))
|
|
|
|
html = z.read(filename).decode('utf-8', 'ignore')
|
|
|
|
img = re.compile('<img.*?src="(.*?)"').findall(html)
|
|
|
|
#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)
|
2014-05-04 17:26:43 +00:00
|
|
|
return data
|
|
|
|
|
|
|
|
def info(epub):
|
|
|
|
data = {}
|
2015-11-16 15:02:45 +00:00
|
|
|
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]))
|
2016-01-06 13:10:23 +00:00
|
|
|
metadata = info.findall('{http://www.idpf.org/2007/opf}metadata')
|
|
|
|
if metadata:
|
|
|
|
metadata = metadata[0]
|
|
|
|
for e in metadata.getchildren():
|
|
|
|
if e.text and e.text.strip() and e.text not in ('unknown', 'none'):
|
|
|
|
key = e.tag.split('}')[-1]
|
|
|
|
key = {
|
|
|
|
'creator': 'author',
|
|
|
|
}.get(key, key)
|
|
|
|
value = e.text.strip()
|
|
|
|
if key == 'identifier':
|
|
|
|
value = normalize_isbn(value)
|
|
|
|
if stdnum.isbn.is_valid(value):
|
|
|
|
data['isbn'] = [value]
|
|
|
|
elif key == 'author':
|
|
|
|
data[key] = value.split(', ')
|
|
|
|
else:
|
|
|
|
data[key] = value
|
2016-01-05 09:11:42 +00:00
|
|
|
if 'description' in data:
|
|
|
|
data['description'] = strip_tags(decode_html(data['description']))
|
2014-05-04 17:26:43 +00:00
|
|
|
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]
|
2015-12-25 14:10:49 +00:00
|
|
|
if 'language' in data and isinstance(data['language'], str):
|
|
|
|
data['language'] = get_language(data['language'])
|
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'):
|
2016-01-03 15:30:30 +00:00
|
|
|
data += z.read(f.filename).decode('utf-8', 'ignore')
|
2014-05-04 17:26:43 +00:00
|
|
|
return data
|
|
|
|
|
|
|
|
def extract_isbn(data):
|
|
|
|
isbns = find_isbns(data)
|
|
|
|
if isbns:
|
|
|
|
return isbns[0]
|
|
|
|
|