Commit 224906db authored by Dmitry Shelepnev's avatar Dmitry Shelepnev
Browse files

Creating exctract_cover_memory for EPUB

parent 5c5e746d
Loading
Loading
Loading
Loading
+7 −11
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@ from book_tools.format.mimetype import Mimetype
from book_tools.format.util import list_zip_file_infos
from book_tools.format.epub import EPub
from book_tools.format.fb2 import FB2, FB2Zip
from book_tools.format.other import Dummy
#from fbreader.format.pdf import PDF
#from fbreader.format.msword import MSWord
from book_tools.format.mobi import Mobipocket
@@ -22,6 +23,8 @@ class __detector:
    @staticmethod
    def file(filename):
        (n, e) = os.path.splitext(filename)
        if e.lower() == '.xml':
            return Mimetype.XML
        if e.lower() == '.fb2':
            return Mimetype.FB2
        elif e.lower()=='.epub' or e.lower()=='.zip':
@@ -62,7 +65,8 @@ def detect_mime(file):
                        pass
        elif mime == Mimetype.OCTET_STREAM:
            mobiflag =  file.read(68)
            if mobiflag.decode()[60:] == 'BOOKMOBI':
            mobiflag = mobiflag[60:]
            if mobiflag.decode() == 'BOOKMOBI':
                return Mimetype.MOBI
    except:
        pass
@@ -82,16 +86,8 @@ def create_bookfile(file, original_filename):
        return FB2Zip(file, original_filename)
    elif mimetype == Mimetype.MOBI:
        return Mobipocket(file, original_filename)
#    elif mimetype == Mimetype.PDF:
#        return PDF(path, original_filename)
#    elif mimetype == Mimetype.MSWORD:
#        return MSWord(path, original_filename)
#    elif mimetype == Mimetype.RTF:
#        return RTF(path, original_filename)
#    elif mimetype == Mimetype.DJVU:
#        return DjVu(path, original_filename)
#    elif mimetype in [Mimetype.TEXT]:
#        return Dummy(path, original_filename, mimetype)
    elif mimetype in [Mimetype.TEXT, Mimetype.PDF, Mimetype.MSWORD, Mimetype.RTF, Mimetype.DJVU]:
       return Dummy(file, original_filename, mimetype)
    else:
        raise Exception('File type \'%s\' is not supported, sorry' % mimetype)

+3 −0
Original line number Diff line number Diff line
@@ -34,6 +34,9 @@ class BookFile(object):
    def extract_cover_internal(self, working_dir):
        return (None, False)

    def extract_cover_memory(self):
        return None

    @staticmethod
    def __is_text(text):
        return isinstance(text, str)
+19 −10
Original line number Diff line number Diff line
@@ -145,7 +145,7 @@ class EPub(BookFile):
            return xpath('/opf:package/opf:manifest/opf:item[@href="%s"]' % ref)

        def image_infos(node):
            path = os.path.normpath(prefix + node.get('href'))
            path = os.path.normpath(prefix + node.get('href')).replace('\\','/')
            try:
                fileinfo = self.__zip_file.getinfo(path)
            except:
@@ -172,43 +172,45 @@ class EPub(BookFile):
                raise Exception('unknown mimetype %s' % mime)

        try:
            return image_infos(xpath('/opf:package/opf:manifest/opf:item[@properties="cover-image"]'))
        except:
            node = xpath('/opf:package/opf:manifest/opf:item[@properties="cover-image"]')
            return image_infos(node)
        except Exception as err:
            pass

        try:
            node = xpath('/opf:package/opf:metadata/opf:meta[@name="cover"]')
            return image_infos(xpath('/opf:package/opf:manifest/opf:item[@id="%s"]' % node.get('content')))
        except:
        except Exception as err:
            pass

        try:
            node = xpath('/opf:package/opf:metadata/meta[@name="cover"]')
            return image_infos(xpath('/opf:package/opf:manifest/opf:item[@id="%s"]' % node.get('content')))
        except:
        except Exception as err:
            pass

        try:
            node = xpath('/package/metadata/meta[@name="cover"]')
            return image_infos(xpath('/package/manifest/item[@id="%s"]' % node.get('content')))
        except:
        except Exception as err:
            pass

        try:
            node = xpath('/opf:package/opf:guide/opf:reference[@type="other.ms-coverimage-standard"][@title="Cover"]')
            return image_infos(item_for_href(node.get('href')))
        except:
        except Exception as err:
            pass

        try:
            node = xpath('/opf:package/opf:guide/opf:reference[@type="other.ms-coverimage-standard"]')
            return image_infos(item_for_href(node.get('href')))
        except:
        except Exception as err:
            pass

        try:
            return image_infos(xpath('/opf:package/opf:manifest/opf:item[@id="cover"]'))
        except:
            node = xpath('/opf:package/opf:manifest/opf:item[@id="cover"]')
            return image_infos(node)
        except Exception as err:
            pass

        return []
@@ -402,3 +404,10 @@ class EPub(BookFile):
            shutil.move(os.path.join(working_dir, name), os.path.join(working_dir, split[-1]))
            shutil.rmtree(os.path.join(working_dir, split[0]))
        return (split[-1] if len(split) > 0 else None, False)

    def extract_cover_memory(self):
        if len(self.cover_fileinfos) == 0:
            return None
        name = self.cover_fileinfos[-1]['filename']
        content = self.__zip_file.open(name).read()
        return content
+13 −0
Original line number Diff line number Diff line
@@ -52,6 +52,18 @@ class FB2Base(BookFile):
        except:
            return (None, False)

    def extract_cover_memory(self):
        try:
            tree = self.__create_tree__()
            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:]
            res = tree.xpath('//fb:binary[@id="%s"]' % cover_id, namespaces=self.__namespaces)
            content = base64.b64decode(res[0].text)
            return content
        except Exception as err:
            print(err)
            return None

    def __detect_title(self, tree):
        res = tree.xpath('/fb:FictionBook/fb:description/fb:title-info/fb:book-title', namespaces=self.__namespaces)
        if len(res) == 0:
@@ -131,6 +143,7 @@ class FB2(FB2Base):

    def __create_tree__(self):
        try:
            self.file.seek(0, 0)
            return etree.parse(self.file)
        except:
            raise FB2StructureException('the file is not a valid XML')
+8 −0
Original line number Diff line number Diff line
from book_tools.format.bookfile import BookFile

class Dummy(BookFile):
    def __init__(self, file, original_filename, mimetype):
        BookFile.__init__(self, file, original_filename, mimetype)

    def __exit__(self, kind, value, traceback):
        pass
 No newline at end of file
Loading