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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __license__ = 'GPL v3'
  5. __copyright__ = '2008, Kovid Goyal kovid@kovidgoyal.net'
  6. __docformat__ = 'restructuredtext en'
  7. import sys
  8. import re
  9. from lxml import html
  10. from lxml.html import soupparser
  11. from calibre import browser
  12. from calibre.ebooks.metadata import check_isbn
  13. from calibre.ebooks.metadata.book.base import Metadata
  14. from calibre.ebooks.chardet import xml_to_unicode
  15. from calibre.library.comments import sanitize_comments_html
  16.  
  17. def find_asin(br, isbn):
  18.     q = 'http://www.amazon.com/s?field-keywords=' + isbn
  19.     raw = br.open_novisit(q).read()
  20.     raw = xml_to_unicode(raw, strip_encoding_pats = True, resolve_entities = True)[0]
  21.     root = html.fromstring(raw)
  22.     revs = root.xpath('//*[@class="asinReviewsSummary" and @name]')
  23.     revs = [ x.get('name') for x in revs ]
  24.     if revs:
  25.         return revs[0]
  26.  
  27.  
  28. def to_asin(br, isbn):
  29.     if len(isbn) == 13:
  30.         
  31.         try:
  32.             asin = find_asin(br, isbn)
  33.         import traceback
  34.         traceback.print_exc()
  35.         asin = None
  36.  
  37.     else:
  38.         asin = isbn
  39.     return asin
  40.  
  41.  
  42. def get_social_metadata(title, authors, publisher, isbn):
  43.     mi = Metadata(title, authors)
  44.     if not isbn:
  45.         return mi
  46.     isbn = check_isbn(isbn)
  47.     if not isbn:
  48.         return mi
  49.     br = browser()
  50.     asin = to_asin(br, isbn)
  51.     xisbn = xisbn
  52.     import calibre.ebooks.metadata.xisbn
  53.     for i in xisbn.get_associated_isbns(isbn):
  54.         asin = to_asin(br, i)
  55.         if get_metadata(br, asin, mi):
  56.             return mi
  57.     
  58.     return mi
  59.  
  60.  
  61. def get_metadata(br, asin, mi):
  62.     q = 'http://amzn.com/' + asin
  63.     
  64.     try:
  65.         raw = br.open_novisit(q).read()
  66.     except Exception:
  67.         e = None
  68.         if callable(getattr(e, 'getcode', None)) and e.getcode() == 404:
  69.             return False
  70.         raise 
  71.     except:
  72.         e.getcode() == 404
  73.  
  74.     if '<title>404 - ' in raw:
  75.         return False
  76.     raw = xml_to_unicode(raw, strip_encoding_pats = True, resolve_entities = True)[0]
  77.     root = soupparser.fromstring(raw)
  78.     ratings = root.xpath('//form[@id="handleBuy"]/descendant::*[@class="asinReviewsSummary"]')
  79.     if ratings:
  80.         pat = re.compile('([0-9.]+) out of (\\d+) stars')
  81.         r = ratings[0]
  82.         for elem in r.xpath('descendant::*[@title]'):
  83.             t = elem.get('title')
  84.             m = pat.match(t)
  85.             if m is not None:
  86.                 
  87.                 try:
  88.                     mi.rating = (float(m.group(1)) / float(m.group(2))) * 5
  89.                 e.getcode() == 404
  90.  
  91.                 continue
  92.         
  93.     
  94.     desc = root.xpath('//div[@id="productDescription"]/*[@class="content"]')
  95.     if desc:
  96.         desc = desc[0]
  97.         for c in desc.xpath('descendant::*[@class="seeAll" or @class="emptyClear" or @href]'):
  98.             c.getparent().remove(c)
  99.         
  100.         desc = html.tostring(desc, method = 'html', encoding = unicode).strip()
  101.         desc = re.sub('<([a-zA-Z0-9]+)\\s[^>]+>', '<\\1>', desc)
  102.         desc = re.sub('(?s)<em>--This text ref.*?</em>', '', desc)
  103.         desc = re.sub('(?s)<!--.*?-->', '', desc)
  104.         mi.comments = sanitize_comments_html(desc)
  105.     
  106.     return True
  107.  
  108.  
  109. def main(args = sys.argv):
  110.     print get_social_metadata('Angels & Demons', None, None, '9781416580829')
  111.     print 
  112.     return None
  113.     print get_social_metadata('Star Trek: Destiny: Mere Mortals', None, None, '9781416551720')
  114.     print 
  115.     print get_social_metadata('The Great Gatsby', None, None, '0743273567')
  116.     return 0
  117.  
  118. if __name__ == '__main__':
  119.     sys.exit(main())
  120.  
  121.