mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-08 10:44:09 -04:00
Forgot to commit these in my last commit
This commit is contained in:
parent
cde906b936
commit
fca3f5d24b
26
src/calibre/ebooks/metadata/opf.py
Normal file
26
src/calibre/ebooks/metadata/opf.py
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
#!/usr/bin/env python2
|
||||||
|
# vim:fileencoding=utf-8
|
||||||
|
# License: GPLv3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
|
||||||
|
|
||||||
|
from __future__ import (unicode_literals, division, absolute_import,
|
||||||
|
print_function)
|
||||||
|
|
||||||
|
from calibre.ebooks.metadata import parse_opf_version
|
||||||
|
from calibre.ebooks.metadata.opf2 import OPF
|
||||||
|
from calibre.ebooks.metadata.utils import parse_opf
|
||||||
|
|
||||||
|
class DummyFile(object):
|
||||||
|
|
||||||
|
def __init__(self, raw):
|
||||||
|
self.raw = raw
|
||||||
|
|
||||||
|
def read(self):
|
||||||
|
return self.raw
|
||||||
|
|
||||||
|
def get_metadata(stream):
|
||||||
|
if isinstance(stream, bytes):
|
||||||
|
stream = DummyFile(stream)
|
||||||
|
root = parse_opf(stream)
|
||||||
|
ver = parse_opf_version(root.get('version'))
|
||||||
|
opf = OPF(None, preparsed_opf=root, read_toc=False)
|
||||||
|
return opf.to_book_metadata(), ver, opf.raster_cover, opf.first_spine_item()
|
27
src/calibre/ebooks/metadata/utils.py
Normal file
27
src/calibre/ebooks/metadata/utils.py
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
#!/usr/bin/env python2
|
||||||
|
# vim:fileencoding=utf-8
|
||||||
|
# License: GPLv3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
|
||||||
|
|
||||||
|
from __future__ import (unicode_literals, division, absolute_import,
|
||||||
|
print_function)
|
||||||
|
|
||||||
|
from calibre.ebooks.chardet import xml_to_unicode
|
||||||
|
from lxml import etree
|
||||||
|
|
||||||
|
PARSER = etree.XMLParser(recover=True, no_network=True)
|
||||||
|
|
||||||
|
def parse_opf(stream_or_path):
|
||||||
|
stream = stream_or_path
|
||||||
|
if not hasattr(stream, 'read'):
|
||||||
|
stream = open(stream, 'rb')
|
||||||
|
raw = stream.read()
|
||||||
|
if not raw:
|
||||||
|
raise ValueError('Empty file: '+getattr(stream, 'name', 'stream'))
|
||||||
|
raw, encoding = xml_to_unicode(raw, strip_encoding_pats=True, resolve_entities=True, assume_utf8=True)
|
||||||
|
raw = raw[raw.find('<'):]
|
||||||
|
root = etree.fromstring(raw, PARSER)
|
||||||
|
if root is None:
|
||||||
|
raise ValueError('Not an OPF file')
|
||||||
|
return root
|
||||||
|
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user