Created
October 31, 2010 21:28
-
-
Save dchaplinsky/657174 to your computer and use it in GitHub Desktop.
Patch for python implementation of amazonproduct API to enable caching and preserve some debug info.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from amazonproduct import API | |
import os | |
from hashlib import md5 | |
old_fetch = API._fetch | |
old_build_url = API._build_url | |
old_init = API.__init__ | |
def new_fetch(self, url): | |
""" | |
Monkey patch for the _fetch method of amazon API. | |
Preserve url of the last call and cache response into the file | |
(no cache invalidation yet) | |
""" | |
self._last_url = url | |
if self.enable_cache: | |
path = os.path.join(self.CACHEDIR, self._cachename) | |
if os.path.isfile(path): | |
f = open(path, 'r') | |
return f | |
resp = old_fetch(self, url) | |
if self.enable_cache: | |
f = open(path, 'w+') | |
f.write(resp.read()) | |
resp.seek(0) | |
f.close() | |
return resp | |
def new_init(self, access_key_id, secret_access_key, locale, | |
associate_tag=None, processor=None, enable_cache=True): | |
""" | |
Monkey patch for the __init__ method of amazon API | |
Takes one more param, enable_cache and store it and tries to create | |
cache dir if needed | |
""" | |
self.enable_cache = enable_cache | |
if self.enable_cache and not os.path.isdir(self.CACHEDIR): | |
os.mkdir(self.CACHEDIR) | |
return old_init(self, access_key_id, secret_access_key, locale, | |
associate_tag, processor) | |
def new_build_url(self, **qargs): | |
""" | |
Saving hash of meaningful part of the url to use it as cache key | |
""" | |
url = old_build_url(self, **qargs) | |
cachename = "&".join([chunk for chunk in url.split('&') \ | |
if chunk.find('Timestamp') != 0 and chunk.find('Signature') != 0]) | |
m = md5() | |
m.update(cachename) | |
self._cachename = m.hexdigest() | |
return url | |
API.CACHEDIR = 'cache' | |
API._fetch = new_fetch | |
API.__init__ = new_init | |
API._build_url = new_build_url |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment