Skip to content

Instantly share code, notes, and snippets.

@mattspitz
Created October 31, 2012 16:21
Show Gist options
  • Save mattspitz/3988022 to your computer and use it in GitHub Desktop.
Save mattspitz/3988022 to your computer and use it in GitHub Desktop.
Thin-API offline example
import functools
import os
import pickle
import urllib
import urllib2
CACHE_DIR = "/tmp/cached_results"
def cache_on_disk(filename_fn):
""" Takes a function that accepts (self, args, kwargs) and converts it into a reasonable filename representing that function call. """
# returns another decorator because cache_on_disk takes a parameter
def dec(fn):
@functools.wraps(fn)
def wrapped_fn(self, *args, **kwargs):
filename = os.path.join(CACHE_DIR, filename_fn(self, args, kwargs))
if self.offline:
return pickle.load(open(filename, 'r'))
results = fn(self, *args, **kwargs)
if self.cache_locally:
pickle.dump(results, open(filename, 'w'))
return results
return wrapped_fn
return dec
class MyAPIClient(object):
def __init__(self, offline=False, cache_locally=False):
self.offline = offline
self.cache_locally = cache_locally
super(MyAPIClient, self).__init__()
@cache_on_disk(lambda self,(query,),kwargs: "google_search_%s%s" % (query, "|" + kwargs["site"] if "site" in kwargs else ""))
def do_google_search(self, query, site=None):
""" Silly little wrapper to make Google searches. """
if site is not None:
query = query + " site:%s" % site
url = "https://google.com/search?%s" % urllib.urlencode({"q": query})
opener = urllib2.build_opener()
opener.addheaders = [("User-Agent", "Mozilla/5.0")] # Google blocks requests from python's urllib User-Agent. Not surprising.
return opener.open(url).read()
def main():
import time
uncached = MyAPIClient(cache_locally=True)
start = time.time()
uncached.do_google_search("dogs", site="cuteoverload.com")
print "uncached", time.time()-start
start = time.time()
cached = MyAPIClient(offline=True)
print "cached", time.time()-start
if __name__ == "__main__":
main()
@christinac
Copy link

line 38: query = query + " site:%s" % site instead?

@mattspitz
Copy link
Author

Whoops. Just looked at this again (for an API caching example!) and you're totally right. Fixed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment