home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_958 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  5.5 KB  |  228 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 re
  7. from calibre.ebooks.metadata import MetaInformation
  8. from calibre.ebooks.chardet import xml_to_unicode
  9. from calibre import entity_to_unicode
  10. from calibre.utils.date import parse_date
  11.  
  12. def get_metadata(stream):
  13.     src = stream.read()
  14.     return get_metadata_(src)
  15.  
  16.  
  17. def get_meta_regexp_(name):
  18.     return re.compile('<meta name=[\'"]' + name + '[\'"] content=[\'"](.+?)[\'"]\\s*/?>', re.IGNORECASE)
  19.  
  20.  
  21. def get_metadata_(src, encoding = None):
  22.     if not isinstance(src, unicode):
  23.         if not encoding:
  24.             src = xml_to_unicode(src)[0]
  25.         else:
  26.             src = src.decode(encoding, 'replace')
  27.     
  28.     title = None
  29.     pat = re.compile('<!--.*?TITLE=(?P<q>[\\\'"])(.+?)(?P=q).*?-->', re.DOTALL)
  30.     match = pat.search(src)
  31.     if match:
  32.         title = match.group(2)
  33.     else:
  34.         pat = re.compile('<title>([^<>]+?)</title>', re.IGNORECASE)
  35.         match = pat.search(src)
  36.         if match:
  37.             title = match.group(1)
  38.         
  39.     if not title:
  40.         for x in ('Title', 'DC.title', 'DCTERMS.title'):
  41.             pat = get_meta_regexp_(x)
  42.             match = pat.search(src)
  43.             if match:
  44.                 title = match.group(1)
  45.                 break
  46.                 continue
  47.         
  48.     
  49.     author = None
  50.     pat = re.compile('<!--.*?AUTHOR=(?P<q>[\\\'"])(.+?)(?P=q).*?-->', re.DOTALL)
  51.     match = pat.search(src)
  52.     if match:
  53.         author = match.group(2).replace(',', ';')
  54.     else:
  55.         for x in ('Author', 'DC.creator.aut', 'DCTERMS.creator.aut'):
  56.             pat = get_meta_regexp_(x)
  57.             match = pat.search(src)
  58.             if match:
  59.                 author = match.group(1)
  60.                 break
  61.                 continue
  62.         
  63.     ent_pat = re.compile('&(\\S+)?;')
  64.     if title:
  65.         title = ent_pat.sub(entity_to_unicode, title)
  66.     
  67.     if author:
  68.         author = ent_pat.sub(entity_to_unicode, author)
  69.     
  70.     mi = None(MetaInformation, title if author else None)
  71.     publisher = None
  72.     pat = re.compile('<!--.*?PUBLISHER=(?P<q>[\\\'"])(.+?)(?P=q).*?-->', re.DOTALL)
  73.     match = pat.search(src)
  74.     if match:
  75.         publisher = match.group(2)
  76.     else:
  77.         for x in ('Publisher', 'DC.publisher', 'DCTERMS.publisher'):
  78.             pat = get_meta_regexp_(x)
  79.             match = pat.search(src)
  80.             if match:
  81.                 publisher = match.group(1)
  82.                 break
  83.                 continue
  84.         
  85.     if publisher:
  86.         mi.publisher = ent_pat.sub(entity_to_unicode, publisher)
  87.     
  88.     isbn = None
  89.     pat = re.compile('<!--.*?ISBN=[\\\'"]([^"\\\']+)[\\\'"].*?-->', re.DOTALL)
  90.     match = pat.search(src)
  91.     if match:
  92.         isbn = match.group(1)
  93.     else:
  94.         for x in ('ISBN', 'DC.identifier.ISBN', 'DCTERMS.identifier.ISBN'):
  95.             pat = get_meta_regexp_(x)
  96.             match = pat.search(src)
  97.             if match:
  98.                 isbn = match.group(1)
  99.                 break
  100.                 continue
  101.         
  102.     if isbn:
  103.         mi.isbn = re.sub('[^0-9xX]', '', isbn)
  104.     
  105.     language = None
  106.     pat = re.compile('<!--.*?LANGUAGE=[\\\'"]([^"\\\']+)[\\\'"].*?-->', re.DOTALL)
  107.     match = pat.search(src)
  108.     if match:
  109.         language = match.group(1)
  110.     else:
  111.         for x in ('DC.language', 'DCTERMS.language'):
  112.             pat = get_meta_regexp_(x)
  113.             match = pat.search(src)
  114.             if match:
  115.                 language = match.group(1)
  116.                 break
  117.                 continue
  118.         
  119.     if language:
  120.         mi.language = language
  121.     
  122.     pubdate = None
  123.     pat = re.compile('<!--.*?PUBDATE=[\\\'"]([^"\\\']+)[\\\'"].*?-->', re.DOTALL)
  124.     match = pat.search(src)
  125.     if match:
  126.         pubdate = match.group(1)
  127.     else:
  128.         for x in ('Pubdate', 'Date of publication', 'DC.date.published', 'DC.date.publication', 'DC.date.issued', 'DCTERMS.issued'):
  129.             pat = get_meta_regexp_(x)
  130.             match = pat.search(src)
  131.             if match:
  132.                 pubdate = match.group(1)
  133.                 break
  134.                 continue
  135.         
  136.     if pubdate:
  137.         
  138.         try:
  139.             mi.pubdate = parse_date(pubdate)
  140.  
  141.     
  142.     timestamp = None
  143.     pat = re.compile('<!--.*?TIMESTAMP=[\\\'"]([^"\\\']+)[\\\'"].*?-->', re.DOTALL)
  144.     match = pat.search(src)
  145.     if match:
  146.         timestamp = match.group(1)
  147.     else:
  148.         for x in ('Timestamp', 'Date of creation', 'DC.date.created', 'DC.date.creation', 'DCTERMS.created'):
  149.             pat = get_meta_regexp_(x)
  150.             match = pat.search(src)
  151.             if match:
  152.                 timestamp = match.group(1)
  153.                 break
  154.                 continue
  155.         
  156.     if timestamp:
  157.         
  158.         try:
  159.             mi.timestamp = parse_date(timestamp)
  160.  
  161.     
  162.     series = None
  163.     pat = re.compile('<!--.*?SERIES=[\\\'"]([^"\\\']+)[\\\'"].*?-->', re.DOTALL)
  164.     match = pat.search(src)
  165.     if match:
  166.         series = match.group(1)
  167.     else:
  168.         pat = get_meta_regexp_('Series')
  169.         match = pat.search(src)
  170.         if match:
  171.             series = match.group(1)
  172.         
  173.     if series:
  174.         mi.series = ent_pat.sub(entity_to_unicode, series)
  175.     
  176.     rating = None
  177.     pat = re.compile('<!--.*?RATING=[\\\'"]([^"\\\']+)[\\\'"].*?-->', re.DOTALL)
  178.     match = pat.search(src)
  179.     if match:
  180.         rating = match.group(1)
  181.     else:
  182.         pat = get_meta_regexp_('Rating')
  183.         match = pat.search(src)
  184.         if match:
  185.             rating = match.group(1)
  186.         
  187.     if rating:
  188.         
  189.         try:
  190.             mi.rating = float(rating)
  191.             if mi.rating < 0:
  192.                 mi.rating = 0
  193.             
  194.             if mi.rating > 5:
  195.                 mi.rating /= 2
  196.             
  197.             if mi.rating > 5:
  198.                 mi.rating = 0
  199.  
  200.     
  201.     comments = None
  202.     pat = re.compile('<!--.*?COMMENTS=[\\\'"]([^"\\\']+)[\\\'"].*?-->', re.DOTALL)
  203.     match = pat.search(src)
  204.     if match:
  205.         comments = match.group(1)
  206.     else:
  207.         pat = get_meta_regexp_('Comments')
  208.         match = pat.search(src)
  209.         if match:
  210.             comments = match.group(1)
  211.         
  212.     if comments:
  213.         mi.comments = ent_pat.sub(entity_to_unicode, comments)
  214.     
  215.     tags = None
  216.     pat = re.compile('<!--.*?TAGS=[\\\'"]([^"\\\']+)[\\\'"].*?-->', re.DOTALL)
  217.     match = pat.search(src)
  218.     if match:
  219.         tags = match.group(1)
  220.     else:
  221.         pat = get_meta_regexp_('Tags')
  222.         match = pat.search(src)
  223.         if match:
  224.             tags = match.group(1)
  225.         
  226.     return mi
  227.  
  228.