mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-08 18:54:09 -04:00
Autodetect if a zip/rar file is actually a comic. Fixes #4880 (RAR/ZIP file Autodetection to treat them as CBR/CBZ)
This commit is contained in:
parent
a3052cf127
commit
a7b5f60f6f
@ -6,17 +6,21 @@ __license__ = 'GPL v3'
|
|||||||
__copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
|
__copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
|
||||||
__docformat__ = 'restructuredtext en'
|
__docformat__ = 'restructuredtext en'
|
||||||
|
|
||||||
import textwrap, os
|
import os
|
||||||
from contextlib import closing
|
from contextlib import closing
|
||||||
|
|
||||||
from calibre.customize import FileTypePlugin
|
from calibre.customize import FileTypePlugin
|
||||||
|
|
||||||
|
def is_comic(list_of_names):
|
||||||
|
extensions = set([x.rpartition('.')[-1].lower() for x in list_of_names])
|
||||||
|
return len(extensions) == 1 and iter(extensions).next() in ('jpg', 'jpeg', 'png')
|
||||||
|
|
||||||
class ArchiveExtract(FileTypePlugin):
|
class ArchiveExtract(FileTypePlugin):
|
||||||
name = 'Archive Extract'
|
name = 'Archive Extract'
|
||||||
author = 'Kovid Goyal'
|
author = 'Kovid Goyal'
|
||||||
description = textwrap.dedent(_('''\
|
description = _('Extract common e-book formats from archives '
|
||||||
Extract common e-book formats from archives (zip/rar) files.
|
'(zip/rar) files. Also try to autodetect if they are actually '
|
||||||
'''))
|
'cbz/cbr files.')
|
||||||
file_types = set(['zip', 'rar'])
|
file_types = set(['zip', 'rar'])
|
||||||
supported_platforms = ['windows', 'osx', 'linux']
|
supported_platforms = ['windows', 'osx', 'linux']
|
||||||
on_import = True
|
on_import = True
|
||||||
@ -35,6 +39,13 @@ class ArchiveExtract(FileTypePlugin):
|
|||||||
fnames = zf.namelist()
|
fnames = zf.namelist()
|
||||||
|
|
||||||
fnames = [x for x in fnames if '.' in x]
|
fnames = [x for x in fnames if '.' in x]
|
||||||
|
if is_comic(fnames):
|
||||||
|
ext = '.cbr' if is_rar else '.cbz'
|
||||||
|
of = self.temporary_file('_archive_extract'+ext)
|
||||||
|
with open(archive, 'rb') as f:
|
||||||
|
of.write(f.read())
|
||||||
|
of.close()
|
||||||
|
return of.name
|
||||||
if len(fnames) > 1 or not fnames:
|
if len(fnames) > 1 or not fnames:
|
||||||
return archive
|
return archive
|
||||||
fname = fnames[0]
|
fname = fnames[0]
|
||||||
|
@ -13,6 +13,9 @@ from calibre.ptempfile import PersistentTemporaryFile
|
|||||||
from calibre.libunrar import extract_member, names
|
from calibre.libunrar import extract_member, names
|
||||||
|
|
||||||
def get_metadata(stream):
|
def get_metadata(stream):
|
||||||
|
from calibre.ebooks.metadata.archive import is_comic
|
||||||
|
from calibre.ebooks.metadata.meta import get_metadata
|
||||||
|
|
||||||
path = getattr(stream, 'name', False)
|
path = getattr(stream, 'name', False)
|
||||||
if not path:
|
if not path:
|
||||||
pt = PersistentTemporaryFile('_rar-meta.rar')
|
pt = PersistentTemporaryFile('_rar-meta.rar')
|
||||||
@ -21,6 +24,8 @@ def get_metadata(stream):
|
|||||||
path = pt.name
|
path = pt.name
|
||||||
path = os.path.abspath(path)
|
path = os.path.abspath(path)
|
||||||
file_names = list(names(path))
|
file_names = list(names(path))
|
||||||
|
if is_comic(file_names):
|
||||||
|
return get_metadata(stream, 'cbr')
|
||||||
for f in file_names:
|
for f in file_names:
|
||||||
stream_type = os.path.splitext(f)[1].lower()
|
stream_type = os.path.splitext(f)[1].lower()
|
||||||
if stream_type:
|
if stream_type:
|
||||||
@ -29,7 +34,6 @@ def get_metadata(stream):
|
|||||||
'rb', 'imp', 'pdf', 'lrf'):
|
'rb', 'imp', 'pdf', 'lrf'):
|
||||||
data = extract_member(path, match=None, name=f)[1]
|
data = extract_member(path, match=None, name=f)[1]
|
||||||
stream = StringIO(data)
|
stream = StringIO(data)
|
||||||
from calibre.ebooks.metadata.meta import get_metadata
|
|
||||||
return get_metadata(stream, stream_type)
|
return get_metadata(stream, stream_type)
|
||||||
raise ValueError('No ebook found in RAR archive')
|
raise ValueError('No ebook found in RAR archive')
|
||||||
|
|
||||||
|
@ -8,15 +8,21 @@ from cStringIO import StringIO
|
|||||||
|
|
||||||
|
|
||||||
def get_metadata(stream):
|
def get_metadata(stream):
|
||||||
|
from calibre.ebooks.metadata.meta import get_metadata
|
||||||
|
from calibre.ebooks.metadata.archive import is_comic
|
||||||
stream_type = None
|
stream_type = None
|
||||||
zf = ZipFile(stream, 'r')
|
zf = ZipFile(stream, 'r')
|
||||||
for f in zf.namelist():
|
names = zf.namelist()
|
||||||
|
if is_comic(names):
|
||||||
|
# Is probably a comic
|
||||||
|
return get_metadata(stream, 'cbz')
|
||||||
|
|
||||||
|
for f in names:
|
||||||
stream_type = os.path.splitext(f)[1].lower()
|
stream_type = os.path.splitext(f)[1].lower()
|
||||||
if stream_type:
|
if stream_type:
|
||||||
stream_type = stream_type[1:]
|
stream_type = stream_type[1:]
|
||||||
if stream_type in ('lit', 'opf', 'prc', 'mobi', 'fb2', 'epub',
|
if stream_type in ('lit', 'opf', 'prc', 'mobi', 'fb2', 'epub',
|
||||||
'rb', 'imp', 'pdf', 'lrf'):
|
'rb', 'imp', 'pdf', 'lrf'):
|
||||||
from calibre.ebooks.metadata.meta import get_metadata
|
|
||||||
stream = StringIO(zf.read(f))
|
stream = StringIO(zf.read(f))
|
||||||
return get_metadata(stream, stream_type)
|
return get_metadata(stream, stream_type)
|
||||||
raise ValueError('No ebook found in ZIP archive')
|
raise ValueError('No ebook found in ZIP archive')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user