Skip to content
Snippets Groups Projects
Commit 5398b5bf authored by Dmitry Shelepnev's avatar Dmitry Shelepnev
Browse files

Fix fb2 cover extracting

parent a3d85998
No related branches found
No related tags found
No related merge requests found
...@@ -20,7 +20,7 @@ class Namespace(object): ...@@ -20,7 +20,7 @@ class Namespace(object):
class FB2Base(BookFile): class FB2Base(BookFile):
def __init__(self, file, original_filename, mimetype): def __init__(self, file, original_filename, mimetype):
BookFile.__init__(self, file, original_filename, mimetype) BookFile.__init__(self, file, original_filename, mimetype)
self.__namespaces = {'xlink': Namespace.XLINK} self.__namespaces = {'fb':Namespace.FICTION_BOOK20,'xlink': Namespace.XLINK}
try: try:
tree = self.__create_tree__() tree = self.__create_tree__()
self.__detect_namespaces(tree) self.__detect_namespaces(tree)
...@@ -47,7 +47,7 @@ class FB2Base(BookFile): ...@@ -47,7 +47,7 @@ class FB2Base(BookFile):
tree = self.__create_tree__() tree = self.__create_tree__()
res = tree.xpath('/fb:FictionBook/fb:description/fb:title-info/fb:coverpage/fb:image', namespaces=self.__namespaces) res = tree.xpath('/fb:FictionBook/fb:description/fb:title-info/fb:coverpage/fb:image', namespaces=self.__namespaces)
cover_id = res[0].get('{' + Namespace.XLINK + '}href')[1:] cover_id = res[0].get('{' + Namespace.XLINK + '}href')[1:]
res = tree.xpath('/fb:binary[@id="%s"]' % cover_id, namespaces=self.__namespaces) res = tree.xpath('/fb:FictionBook/fb:binary[@id="%s"]' % cover_id, namespaces=self.__namespaces)
content = base64.b64decode(res[0].text) content = base64.b64decode(res[0].text)
with open(os.path.join(working_dir, 'cover.jpeg'), 'wb') as cover_file: with open(os.path.join(working_dir, 'cover.jpeg'), 'wb') as cover_file:
cover_file.write(content) cover_file.write(content)
...@@ -60,15 +60,17 @@ class FB2Base(BookFile): ...@@ -60,15 +60,17 @@ class FB2Base(BookFile):
tree = self.__create_tree__() tree = self.__create_tree__()
res = tree.xpath('/fb:FictionBook/fb:description/fb:title-info/fb:coverpage/fb:image', namespaces=self.__namespaces) res = tree.xpath('/fb:FictionBook/fb:description/fb:title-info/fb:coverpage/fb:image', namespaces=self.__namespaces)
cover_id = res[0].get('{' + Namespace.XLINK + '}href')[1:] cover_id = res[0].get('{' + Namespace.XLINK + '}href')[1:]
res = tree.xpath('/fb:binary[@id="%s"]' % cover_id, namespaces=self.__namespaces) print(cover_id)
res = tree.xpath('/fb:FictionBook/fb:binary[@id="%s"]' % cover_id, namespaces=self.__namespaces)
content = base64.b64decode(res[0].text) content = base64.b64decode(res[0].text)
return content return content
except Exception as err: except Exception as err:
print("exception Extract %s"%err)
return None return None
def __detect_namespaces(self, tree): def __detect_namespaces(self, tree):
tag = tree.getroot().tag if tree.getroot().tag.find(Namespace.FICTION_BOOK21) > 0:
self.__namespaces['fb'] = Namespace.FICTION_BOOK20 if tag.find(Namespace.FICTION_BOOK20)>0 else Namespace.FICTION_BOOK21 self.__namespaces['fb'] = Namespace.FICTION_BOOK21
return None return None
def __detect_title(self, tree): def __detect_title(self, tree):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment