Abstracted out parameter passing so that it's nothing but **kwargs. Function kwargs are passed to a url-builder function and builds based off of that; should make life easier if Twitter decides to change their API parameters, because users can then change what they're passing without modifying the library.

This commit is contained in:
Ryan McGrath 2009-05-12 01:52:44 -04:00
parent 79f14afba2
commit dda51e2f54

View file

@ -8,7 +8,7 @@ import simplejson, urllib, urllib2, base64
# Need to support URL shortening # Need to support URL shortening
class tango: class setup:
def __init__(self, authtype = None, username = None, password = None): def __init__(self, authtype = None, username = None, password = None):
self.authtype = authtype self.authtype = authtype
self.username = username self.username = username
@ -21,17 +21,20 @@ class tango:
else: else:
pass pass
def getUserTimeline(self, count = None, since_id = None, max_id = None, page = None): def constructApiURL(self, base_url, params):
# Fairly close to full API support, need a few other methods. queryURL = base_url
userTimelineURL = "http://twitter.com/statuses/user_timeline/" + self.username + ".json" questionMarkUsed = False
if count is not None: for param in params:
userTimelineURL += "?count=" + count if params[param] is not None:
if since_id is not None: queryURL += (("&" if questionMarkUsed is True else "?") + param + "=" + params[param])
userTimelineURL += "?since_id=" + since_id questionMarkUsed = True
if max_id is not None: return queryURL
userTimelineURL += "?max_id=" + max_id
if page is not None: def getUserTimeline(self, **kwargs):
userTimelineURL += "?page=" + page # 99% API compliant, I think - need to figure out Gzip compression and auto-getting based on authentication
# By doing this with kwargs and constructing a url outside, we can stay somewhat agnostic of API changes - it's all
# based on what the user decides to pass. We just handle the heavy lifting! :D
userTimelineURL = self.constructApiURL("http://twitter.com/statuses/user_timeline/" + self.username + ".json", kwargs)
userTimeline = simplejson.load(urllib2.urlopen(userTimelineURL)) userTimeline = simplejson.load(urllib2.urlopen(userTimelineURL))
formattedTimeline = [] formattedTimeline = []
for tweet in userTimeline: for tweet in userTimeline: