· 7 years ago · Jun 20, 2018, 02:14 PM
1from config import AWS_KEY, SECRET_KEY
2from amazonproduct import API
3
4import os
5from StringIO import StringIO
6
7try: # make it python2.4 compatible!
8 from hashlib import md5
9except ImportError: # pragma: no cover
10 from md5 import new as md5
11
12class ResponseCachingAPI (API):
13
14 CACHEDIR = 'cache'
15
16 def _fetch(self, url):
17 """
18 Monkey patch for the _fetch method of amazon API.
19 Preserve url of the last call and cache response into the file
20 (no cache invalidation yet)
21 """
22 self._last_url = url
23
24 if self.enable_cache:
25 path = os.path.join(self.CACHEDIR, self._cachename)
26 if os.path.isfile(path):
27 print 'Reading response from %s...' % path
28 f = open(path, 'r')
29 return f
30
31 print 'Fetching response from %s...' % url
32 resp = StringIO(API._fetch(self, url).read())
33
34 if self.enable_cache:
35 print 'Storing response in %s...' % path
36 f = open(path, 'w+')
37 f.write(resp.read())
38 resp.seek(0)
39 f.close()
40
41 return resp
42
43 def __init__(self, access_key_id, secret_access_key, locale,
44 associate_tag=None, processor=None, enable_cache=True):
45 """
46 Monkey patch for the __init__ method of amazon API
47 Takes one more param, enable_cache and store it and tries to create
48 cache dir if needed
49 """
50 API.__init__(self, access_key_id, secret_access_key, locale,
51 associate_tag, processor)
52
53 self.enable_cache = enable_cache
54 if self.enable_cache and not os.path.isdir(self.CACHEDIR):
55 os.mkdir(self.CACHEDIR)
56
57 def _build_url(self, **qargs):
58 """
59 Saving hash of meaningful part of the url to use it as cache key
60 """
61 url = API._build_url(self, **qargs)
62 cachename = "&".join([chunk for chunk in url.split('&') \
63 if chunk.find('Timestamp') != 0 and chunk.find('Signature') != 0])
64
65 m = md5()
66 m.update(cachename)
67 self._cachename = m.hexdigest()
68
69 return url
70
71if __name__ == '__main__':
72 api = ResponseCachingAPI(AWS_KEY, SECRET_KEY, 'de')
73 api.item_search('Books', Author='Terry Pratchett')
74 api.item_search('Books', Author='Terry Pratchett')