Created
August 25, 2010 19:11
-
-
Save fffabs/550084 to your computer and use it in GitHub Desktop.
A basic Python wrapper for the new Forrst API
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
import urllib2 | |
import simplejson | |
API_URL = 'http://api.forrst.com/api/' | |
VERSION = 'v1' | |
# Compose the base URL - maybe at __init__ ? | |
URL = API_URL + VERSION | |
class Forrst: | |
def __init__(self, username, id = '', since = ''): | |
self.username = username | |
self.id = id | |
self.since = since | |
# Retrieve information via the username | |
def getInfoByUsername(self): | |
return str(URL + '/users/info?username=' + self.username) | |
# Retrieve information via the user's ID | |
def getInfoByID(self): | |
return str(URL + '/users/info?id=' + self.id) | |
# Retrieve posts from username since | |
def getPosts(self): | |
return str(URL + '/users/posts?username=' + self.username + '&since=' + self.since) | |
# Forrst takes a username, an ID and an optional 'since' number | |
forrst = Forrst('Toxinide', '1213', '') # e.g. 'Toxinide', '1213', '18900' | |
# posts = forrst.getInfoByUsername() | |
# posts = forrst.getInfoByID() | |
posts = forrst.getPosts() | |
# Check server response for errors | |
# otherwise just print the returning JSON data | |
req = urllib2.Request(posts) | |
try: | |
opener = urllib2.build_opener() | |
f = opener.open(req) | |
except urllib2.URLError, e: | |
if hasattr(e, 'reason'): # Check to see if there is a reason | |
print "Failed to reach the server." | |
print "Reason ", e.reason | |
elif hasattr(e, 'code'): # Check for error codes | |
print "The server couldn\'t fulfill the request." | |
print 'Error code:', e.code | |
else: # Nothing has happened | |
print simplejson.load(f) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment