diff --git a/resources/recipes/azstarnet.recipe b/resources/recipes/azstarnet.recipe index 6113f84d40..45339ae208 100644 --- a/resources/recipes/azstarnet.recipe +++ b/resources/recipes/azstarnet.recipe @@ -41,7 +41,7 @@ class Azstarnet(BasicNewsRecipe): }) br.open('http://azstarnet.com/app/registration/proxy.php',data) return br - + remove_tags = [dict(name=['object','link','iframe','base','img'])] diff --git a/src/calibre/customize/ui.py b/src/calibre/customize/ui.py index c544419b06..0f072d1f70 100644 --- a/src/calibre/customize/ui.py +++ b/src/calibre/customize/ui.py @@ -235,6 +235,8 @@ def _run_filetype_plugins(path_to_file, ft=None, occasion='preprocess'): with plugin: try: nfp = plugin.run(path_to_file) + if not nfp: + nfp = path_to_file except: print 'Running file type plugin %s failed with traceback:'%plugin.name traceback.print_exc() diff --git a/src/calibre/ebooks/__init__.py b/src/calibre/ebooks/__init__.py index 38a8ef3662..0b3d6b901a 100644 --- a/src/calibre/ebooks/__init__.py +++ b/src/calibre/ebooks/__init__.py @@ -129,3 +129,11 @@ def render_html(path_to_html, width=590, height=750): del loop return renderer +def check_ebook_format(stream, current_guess): + ans = current_guess + if current_guess.lower() in ('prc', 'mobi', 'azw', 'azw1'): + stream.seek(0) + if stream.read(3) == 'TPZ': + ans = 'tpz' + return ans + diff --git a/src/calibre/ebooks/mobi/reader.py b/src/calibre/ebooks/mobi/reader.py index ed61dbd719..146fdf983c 100644 --- a/src/calibre/ebooks/mobi/reader.py +++ b/src/calibre/ebooks/mobi/reader.py @@ -798,44 +798,40 @@ def get_metadata(stream): from calibre.utils.logging import Log log = Log() mi = MetaInformation(os.path.basename(stream.name), [_('Unknown')]) - try: - mh = MetadataHeader(stream, log) - if mh.title and mh.title != _('Unknown'): - mi.title = mh.title + mh = MetadataHeader(stream, log) + if mh.title and mh.title != _('Unknown'): + mi.title = mh.title - if mh.exth is not None: - if mh.exth.mi is not None: - mi = mh.exth.mi - else: - size = sys.maxint - if hasattr(stream, 'seek') and hasattr(stream, 'tell'): - pos = stream.tell() - stream.seek(0, 2) - size = stream.tell() - stream.seek(pos) - if size < 4*1024*1024: - with TemporaryDirectory('_mobi_meta_reader') as tdir: - with CurrentDir(tdir): - mr = MobiReader(stream, log) - parse_cache = {} - mr.extract_content(tdir, parse_cache) - if mr.embedded_mi is not None: - mi = mr.embedded_mi - if hasattr(mh.exth, 'cover_offset'): - cover_index = mh.first_image_index + mh.exth.cover_offset - data = mh.section_data(int(cover_index)) - else: - data = mh.section_data(mh.first_image_index) - buf = cStringIO.StringIO(data) - try: - im = PILImage.open(buf) - except: - log.exception('Failed to read MOBI cover') - else: - obuf = cStringIO.StringIO() - im.convert('RGB').save(obuf, format='JPEG') - mi.cover_data = ('jpg', obuf.getvalue()) + if mh.exth is not None: + if mh.exth.mi is not None: + mi = mh.exth.mi + else: + size = sys.maxint + if hasattr(stream, 'seek') and hasattr(stream, 'tell'): + pos = stream.tell() + stream.seek(0, 2) + size = stream.tell() + stream.seek(pos) + if size < 4*1024*1024: + with TemporaryDirectory('_mobi_meta_reader') as tdir: + with CurrentDir(tdir): + mr = MobiReader(stream, log) + parse_cache = {} + mr.extract_content(tdir, parse_cache) + if mr.embedded_mi is not None: + mi = mr.embedded_mi + if hasattr(mh.exth, 'cover_offset'): + cover_index = mh.first_image_index + mh.exth.cover_offset + data = mh.section_data(int(cover_index)) + else: + data = mh.section_data(mh.first_image_index) + buf = cStringIO.StringIO(data) + try: + im = PILImage.open(buf) except: - log.filter_level = Log.DEBUG - log.exception('Failed to read MOBI metadata') + log.exception('Failed to read MOBI cover') + else: + obuf = cStringIO.StringIO() + im.convert('RGB').save(obuf, format='JPEG') + mi.cover_data = ('jpg', obuf.getvalue()) return mi diff --git a/src/calibre/library/database2.py b/src/calibre/library/database2.py index 6ccb7363ec..6938628759 100644 --- a/src/calibre/library/database2.py +++ b/src/calibre/library/database2.py @@ -34,7 +34,7 @@ from calibre.customize.ui import run_plugins_on_import from calibre.utils.filenames import ascii_filename from calibre.utils.date import utcnow, now as nowf, utcfromtimestamp -from calibre.ebooks import BOOK_EXTENSIONS +from calibre.ebooks import BOOK_EXTENSIONS, check_ebook_format if iswindows: import calibre.utils.winshell as winshell @@ -993,7 +993,9 @@ class LibraryDatabase2(LibraryDatabase): path=None, notify=True): npath = self.run_import_plugins(fpath, format) format = os.path.splitext(npath)[-1].lower().replace('.', '').upper() - return self.add_format(index, format, open(npath, 'rb'), + stream = open(npath, 'rb') + format = check_ebook_format(stream, format) + return self.add_format(index, format, stream, index_is_id=index_is_id, path=path, notify=notify) def add_format(self, index, format, stream, index_is_id=False, path=None, notify=True): @@ -1596,6 +1598,7 @@ class LibraryDatabase2(LibraryDatabase): npath = self.run_import_plugins(path, format) format = os.path.splitext(npath)[-1].lower().replace('.', '').upper() stream = open(npath, 'rb') + format = check_ebook_format(stream, format) self.add_format(id, format, stream, index_is_id=True) stream.close() self.conn.commit()