Fixed Library: Added signing to AWS requests.
[mspang/pyceo.git] / ceo / pymazon.py
1 #!/usr/bin/python
2
3 from xml.dom import minidom, Node
4 import urllib
5 import time
6 import datetime
7 import hashlib
8 import base64
9 import hmac
10
11 class PyMazonError(Exception):
12     """Holds information about an error that occured during a pymazon request"""
13     def __init__(self, messages):
14         self.__message = '\n'.join(messages)
15
16     def __get_message(self):
17         return self.__message
18
19     def __str__(self):
20         return repr(self.__message)
21
22     message = property(fget=__get_message)
23
24
25 class PyMazonBook:
26     """Stores information about a book retrieved via PyMazon."""
27     def __init__(self, title, authors, publisher, year, isbn10, isbn13, edition):
28         self.__title = title
29         self.__authors = authors
30         self.__publisher = publisher
31         self.__year = year 
32         self.__isbn10 = isbn10
33         self.__isbn13 = isbn13
34         self.__edition = edition
35
36     def __str__(self):
37         return 'Title:     ' + self.title + '\n' + \
38                'Author(s): ' + ', '.join(self.authors) + '\n' \
39                  'Publisher: ' + self.publisher + '\n' + \
40                  'Year:      ' + self.year + '\n' + \
41                  'ISBN-10:   ' + self.isbn10 + '\n' + \
42                  'ISBN-13:   ' + self.isbn13 + '\n' + \
43                  'Edition:   ' + self.edition
44
45     def __get_title(self):
46         return self.__title
47
48     def __get_authors(self):
49         return self.__authors
50     
51     def __get_publisher(self):
52         return self.__publisher
53
54     def __get_year(self):
55         return self.__year
56
57     def __get_isbn10(self):
58         return self.__isbn10
59
60     def __get_isbn13(self):
61         return self.__isbn13
62
63     def __get_edition(self):
64         return self.__edition
65
66     title = property(fget=__get_title)
67     authors = property(fget=__get_authors)
68     publisher = property(fget=__get_publisher)
69     year = property(fget=__get_year)
70     isbn10 = property(fget=__get_isbn10)
71     isbn13 = property(fget=__get_isbn13)
72     edition = property(fget=__get_edition)
73
74
75 class PyMazon:
76     """A method of looking up book information on Amazon."""
77     def __init__(self, accesskey, secretkey):
78         self.__key = accesskey
79         self.__secret = secretkey
80         self.__last_query_time = 0
81
82     def __form_request(self, isbn):
83         content = {}
84         dstamp = datetime.datetime.utcfromtimestamp(time.time())
85         content['Timestamp'] = dstamp.strftime('%Y-%m-%dT%H:%M:%S.000Z')
86         content['Service'] = 'AWSECommerceService'
87         content['Version'] = '2008-08-19'
88         content['Operation'] = 'ItemLookup'
89         content['ResponseGroup'] = 'ItemAttributes'
90         content['IdType'] = 'ISBN'
91         content['SearchIndex'] = 'Books'
92         content['ItemId'] = isbn
93         content['AWSAccessKeyId'] = self.__key
94
95         URI_String = []
96
97         for key, value in sorted(content.items()):
98             URI_String.append('%s=%s' % (key, urllib.quote(value)))
99
100         req = '&'.join(URI_String)
101         to_sign_req = 'GET\necs.amazonaws.com\n/onca/xml\n' + req
102
103         h = hmac.new(self.__secret, to_sign_req, hashlib.sha256)
104         sig = base64.b64encode(h.digest())
105         req += '&Signature=%s' % urllib.quote(sig)
106
107         return 'http://ecs.amazonaws.com/onca/xml?' + req
108
109     def __elements_text(self, element, name):
110         result = []
111         matching = element.getElementsByTagName(name)
112         for match in matching:
113             if len(match.childNodes) != 1:
114                 continue
115             child = match.firstChild
116             if child.nodeType != Node.TEXT_NODE:
117                 continue
118             result.append(child.nodeValue.strip())
119         return result
120
121     def __format_errors(self, errors):
122         error_list = []
123         for error in errors:
124             error_list.extend(self.__elements_text(error, 'Message'))
125         return error_list
126
127     def __extract_single(self, element, name):
128         matches = self.__elements_text(element, name)
129         if len(matches) == 0:
130             return ''
131         return matches[0]
132
133     def lookup(self, isbn):
134         file = urllib.urlretrieve(self.__form_request(isbn))[0]
135         xmldoc = minidom.parse(file)
136
137         cur_time = time.time()
138         while cur_time - self.__last_query_time < 1.0:
139             sleep(cur_time - self.__last_query_time)
140             cur_time = time.time()
141         self.__last_query_time = cur_time
142
143         errors = xmldoc.getElementsByTagName('Errors')
144         if len(errors) != 0:
145             raise PyMazonError, self.__format_errors(errors)
146
147         title = self.__extract_single(xmldoc, 'Title')
148         authors = self.__elements_text(xmldoc, 'Author')
149         publisher = self.__extract_single(xmldoc, 'Publisher')
150         year = self.__extract_single(xmldoc, 'PublicationDate')[0:4]
151         isbn10 = self.__extract_single(xmldoc, 'ISBN')
152         isbn13 = self.__extract_single(xmldoc, 'EAN')
153         edition = self.__extract_single(xmldoc, 'Edition')
154         
155         return PyMazonBook(title, authors, publisher, year, isbn10, isbn13, edition)