home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_958 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  1.7 KB  |  52 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __license__ = 'GPL v3'
  5. __copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
  6. import cStringIO
  7. import os
  8. from calibre.ebooks.metadata.opf2 import OPF
  9.  
  10. def get_metadata(stream):
  11.     LitContainer = LitContainer
  12.     import calibre.ebooks.lit.reader
  13.     Log = Log
  14.     import calibre.utils.logging
  15.     litfile = LitContainer(stream, Log())
  16.     src = litfile.get_metadata().encode('utf-8')
  17.     litfile = litfile._litfile
  18.     opf = OPF(cStringIO.StringIO(src), os.getcwd())
  19.     mi = opf.to_book_metadata()
  20.     covers = []
  21.     for item in opf.iterguide():
  22.         if 'cover' not in item.get('type', '').lower():
  23.             continue
  24.         
  25.         ctype = item.get('type')
  26.         href = item.get('href', '')
  27.         candidates = [
  28.             href,
  29.             href.replace('&', '%26')]
  30.         for item in litfile.manifest.values():
  31.             if item.path in candidates:
  32.                 
  33.                 try:
  34.                     covers.append((litfile.get_file('/data/' + item.internal), ctype))
  35.                 except:
  36.                     pass
  37.  
  38.                 break
  39.                 continue
  40.         
  41.     
  42.     covers.sort(cmp = (lambda x, y: cmp(len(x[0]), len(y[0]))), reverse = True)
  43.     idx = 0
  44.     if len(covers) > 1:
  45.         if covers[1][1] == covers[0][1] + '-standard':
  46.             idx = 1
  47.         
  48.     
  49.     mi.cover_data = ('jpg', covers[idx][0])
  50.     return mi
  51.  
  52.