home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_945 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  4.0 KB  |  80 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 etree
  10. from calibre import browser
  11. from calibre.utils.date import parse_date, utcnow
  12. from calibre.ebooks.metadata import MetaInformation, string_to_authors
  13. AWS_NS = 'http://webservices.amazon.com/AWSECommerceService/2005-10-05'
  14.  
  15. def AWS(tag):
  16.     return '{%s}%s' % (AWS_NS, tag)
  17.  
  18.  
  19. class ISBNNotFound(ValueError):
  20.     pass
  21.  
  22.  
  23. def check_for_errors(root, isbn):
  24.     err = root.find('.//' + AWS('Error'))
  25.     if err is not None:
  26.         text = etree.tostring(err, method = 'text', pretty_print = True, encoding = unicode)
  27.         if 'AWS.InvalidParameterValue' + isbn in text:
  28.             raise ISBNNotFound(isbn)
  29.         'AWS.InvalidParameterValue' + isbn in text
  30.         raise Exception('Failed to get metadata with error: ' + text)
  31.     err is not None
  32.  
  33.  
  34. def get_social_metadata(title, authors, publisher, isbn):
  35.     mi = MetaInformation(title, authors)
  36.     if isbn:
  37.         br = browser()
  38.         response_xml = br.open('http://status.calibre-ebook.com/aws/metadata/' + isbn).read()
  39.         root = etree.fromstring(response_xml)
  40.         
  41.         try:
  42.             check_for_errors(root, isbn)
  43.         except ISBNNotFound:
  44.             return mi
  45.  
  46.         mi.title = root.findtext('.//' + AWS('Title'))
  47.         authors = [ x.text for x in root.findall('.//' + AWS('Author')) ]
  48.         mi.publisher = root.findtext('.//' + AWS('Publisher'))
  49.         
  50.         try:
  51.             d = root.findtext('.//' + AWS('PublicationDate'))
  52.             if d:
  53.                 default = utcnow().replace(day = 15)
  54.                 d = parse_date(d[0].text, assume_utc = True, default = default)
  55.                 mi.pubdate = d
  56.         except:
  57.             None if authors else []
  58.  
  59.         
  60.         try:
  61.             rating = float(root.findtext('.//' + AWS('AverageRating')))
  62.             num_of_reviews = int(root.findtext('.//' + AWS('TotalReviews')))
  63.             if num_of_reviews > 4 and rating > 0 and rating < 5:
  64.                 mi.rating = rating
  65.         except:
  66.             None if authors else []
  67.  
  68.         tags = [ x.text for x in root.findall('.//%s/%s' % (AWS('Subjects'), AWS('Subject'))) ]
  69.         comments = root.find('.//%s/%s' % (AWS('EditorialReview'), AWS('Content')))
  70.         return mi
  71.  
  72.  
  73. def main(args = sys.argv):
  74.     print get_social_metadata(None, None, None, '9781416551720')
  75.     return 0
  76.  
  77. if __name__ == '__main__':
  78.     sys.exit(main())
  79.  
  80.