Created
November 30, 2008 23:05
-
-
Save kastner/30548 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 django.utils import simplejson as json | |
| from google.appengine.api import urlfetch | |
| class MyFlickr(object): | |
| """A simple class for dealing with flickr via the API""" | |
| def __init__(self, api_key): | |
| super(MyFlickr, self).__init__() | |
| self.api_key = api_key | |
| self.api_host = "http://api.flickr.com/services/rest/" | |
| def default_args(self, method): | |
| return { | |
| "method": method, | |
| "api_key": self.api_key, | |
| "format": "json", | |
| "nojsoncallback": 1, | |
| } | |
| def url_for_method(self, method, args={}): | |
| qs = "" | |
| url_args = self.default_args(method) | |
| url_args.update(args) | |
| for key in url_args: | |
| qs += key + "=" + str(url_args[key]) + "&" | |
| return self.api_host + "?" + qs | |
| def call(self, method, args={}): | |
| result = urlfetch.fetch(self.url_for_method(method, args)) | |
| return json.loads(result.content) | |
| def get_photos(self, resp): | |
| if resp["stat"] == "fail": | |
| return "Error: " + resp["message"] | |
| else: | |
| return resp["photos"]["photo"] | |
| def interesting(self, page=1): | |
| photos = self.call("flickr.interestingness.getList", {"page": page}) | |
| return self.get_photos(photos) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment