When adding PRC/MOBI files that are actually Topaz files, change detected file type to Topaz

This commit is contained in:
Kovid Goyal 2010-02-26 14:54:15 -07:00
parent 37db157f85
commit 8a930b53af
5 changed files with 50 additions and 41 deletions

View File

@ -41,7 +41,7 @@ class Azstarnet(BasicNewsRecipe):
}) })
br.open('http://azstarnet.com/app/registration/proxy.php',data) br.open('http://azstarnet.com/app/registration/proxy.php',data)
return br return br
remove_tags = [dict(name=['object','link','iframe','base','img'])] remove_tags = [dict(name=['object','link','iframe','base','img'])]

View File

@ -235,6 +235,8 @@ def _run_filetype_plugins(path_to_file, ft=None, occasion='preprocess'):
with plugin: with plugin:
try: try:
nfp = plugin.run(path_to_file) nfp = plugin.run(path_to_file)
if not nfp:
nfp = path_to_file
except: except:
print 'Running file type plugin %s failed with traceback:'%plugin.name print 'Running file type plugin %s failed with traceback:'%plugin.name
traceback.print_exc() traceback.print_exc()

View File

@ -129,3 +129,11 @@ def render_html(path_to_html, width=590, height=750):
del loop del loop
return renderer 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

View File

@ -798,44 +798,40 @@ def get_metadata(stream):
from calibre.utils.logging import Log from calibre.utils.logging import Log
log = Log() log = Log()
mi = MetaInformation(os.path.basename(stream.name), [_('Unknown')]) mi = MetaInformation(os.path.basename(stream.name), [_('Unknown')])
try: mh = MetadataHeader(stream, log)
mh = MetadataHeader(stream, log) if mh.title and mh.title != _('Unknown'):
if mh.title and mh.title != _('Unknown'): mi.title = mh.title
mi.title = mh.title
if mh.exth is not None: if mh.exth is not None:
if mh.exth.mi is not None: if mh.exth.mi is not None:
mi = mh.exth.mi mi = mh.exth.mi
else: else:
size = sys.maxint size = sys.maxint
if hasattr(stream, 'seek') and hasattr(stream, 'tell'): if hasattr(stream, 'seek') and hasattr(stream, 'tell'):
pos = stream.tell() pos = stream.tell()
stream.seek(0, 2) stream.seek(0, 2)
size = stream.tell() size = stream.tell()
stream.seek(pos) stream.seek(pos)
if size < 4*1024*1024: if size < 4*1024*1024:
with TemporaryDirectory('_mobi_meta_reader') as tdir: with TemporaryDirectory('_mobi_meta_reader') as tdir:
with CurrentDir(tdir): with CurrentDir(tdir):
mr = MobiReader(stream, log) mr = MobiReader(stream, log)
parse_cache = {} parse_cache = {}
mr.extract_content(tdir, parse_cache) mr.extract_content(tdir, parse_cache)
if mr.embedded_mi is not None: if mr.embedded_mi is not None:
mi = mr.embedded_mi mi = mr.embedded_mi
if hasattr(mh.exth, 'cover_offset'): if hasattr(mh.exth, 'cover_offset'):
cover_index = mh.first_image_index + mh.exth.cover_offset cover_index = mh.first_image_index + mh.exth.cover_offset
data = mh.section_data(int(cover_index)) data = mh.section_data(int(cover_index))
else: else:
data = mh.section_data(mh.first_image_index) data = mh.section_data(mh.first_image_index)
buf = cStringIO.StringIO(data) buf = cStringIO.StringIO(data)
try: try:
im = PILImage.open(buf) 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())
except: except:
log.filter_level = Log.DEBUG log.exception('Failed to read MOBI cover')
log.exception('Failed to read MOBI metadata') else:
obuf = cStringIO.StringIO()
im.convert('RGB').save(obuf, format='JPEG')
mi.cover_data = ('jpg', obuf.getvalue())
return mi return mi

View File

@ -34,7 +34,7 @@ from calibre.customize.ui import run_plugins_on_import
from calibre.utils.filenames import ascii_filename from calibre.utils.filenames import ascii_filename
from calibre.utils.date import utcnow, now as nowf, utcfromtimestamp 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: if iswindows:
import calibre.utils.winshell as winshell import calibre.utils.winshell as winshell
@ -993,7 +993,9 @@ class LibraryDatabase2(LibraryDatabase):
path=None, notify=True): path=None, notify=True):
npath = self.run_import_plugins(fpath, format) npath = self.run_import_plugins(fpath, format)
format = os.path.splitext(npath)[-1].lower().replace('.', '').upper() 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) 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): 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) npath = self.run_import_plugins(path, format)
format = os.path.splitext(npath)[-1].lower().replace('.', '').upper() format = os.path.splitext(npath)[-1].lower().replace('.', '').upper()
stream = open(npath, 'rb') stream = open(npath, 'rb')
format = check_ebook_format(stream, format)
self.add_format(id, format, stream, index_is_id=True) self.add_format(id, format, stream, index_is_id=True)
stream.close() stream.close()
self.conn.commit() self.conn.commit()