diff --git a/src/calibre/customize/builtins.py b/src/calibre/customize/builtins.py index 28be488dce..9ca7ae590d 100644 --- a/src/calibre/customize/builtins.py +++ b/src/calibre/customize/builtins.py @@ -25,6 +25,17 @@ every time you add an HTML file to the library.\ html2oeb(htmlfile, of) return of.name +class OPFMetadataReader(MetadataReaderPlugin): + + name = 'Read OPF metadata' + file_types = set(['opf']) + description = _('Read metadata from %s files')%'OPF' + + def get_metadata(self, stream, ftype): + from calibre.ebooks.metadata.opf2 import OPF + from calibre.ebooks.metadata import MetaInformation + return MetaInformation(OPF(stream, os.getcwd())) + class RTFMetadataReader(MetadataReaderPlugin): name = 'Read RTF metadata' @@ -167,6 +178,16 @@ class ComicMetadataReader(MetadataReaderPlugin): ext = os.path.splitext(path)[1][1:] mi.cover_data = (ext.lower(), data) return mi + +class ZipMetadataReader(MetadataReaderPlugin): + + name = 'Read ZIP metadata' + file_types = set(['zip', 'oebzip']) + description = _('Read metadata from ebooks in ZIP archives') + + def get_metadata(self, stream, ftype): + from calibre.ebooks.metadata.zip import get_metadata + return get_metadata(stream) class EPUBMetadataWriter(MetadataWriterPlugin): diff --git a/src/calibre/ebooks/metadata/zip.py b/src/calibre/ebooks/metadata/zip.py new file mode 100644 index 0000000000..441aa7e3da --- /dev/null +++ b/src/calibre/ebooks/metadata/zip.py @@ -0,0 +1,24 @@ +from __future__ import with_statement +__license__ = 'GPL v3' +__copyright__ = '2008, Kovid Goyal ' + +import os +from zipfile import ZipFile +from cStringIO import StringIO + + +def get_metadata(stream): + stream_type = None + zf = ZipFile(stream, 'r') + for f in zf.namelist(): + stream_type = os.path.splitext(f)[1].lower() + if stream_type: + stream_type = stream_type[1:] + if stream_type in ('lit', 'opf', 'prc', 'mobi', 'fb2', 'epub', + 'rb', 'imp', 'pdf', 'lrf'): + from calibre.ebooks.metadata.meta import get_metadata + stream = StringIO(zf.read(f)) + return get_metadata(stream, stream_type) + raise ValueError('No ebook found in ZIP archive') + + \ No newline at end of file