Skip to content

Instantly share code, notes, and snippets.

@fffabs
Created August 25, 2010 19:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fffabs/550084 to your computer and use it in GitHub Desktop.
Save fffabs/550084 to your computer and use it in GitHub Desktop.
A basic Python wrapper for the new Forrst API
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