From dda51e2f54cef225ea3402be61c62c0365104fb6 Mon Sep 17 00:00:00 2001 From: Ryan McGrath Date: Tue, 12 May 2009 01:52:44 -0400 Subject: [PATCH] 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. --- tango.py | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/tango.py b/tango.py index 84f5ec0..153e44f 100644 --- a/tango.py +++ b/tango.py @@ -8,7 +8,7 @@ import simplejson, urllib, urllib2, base64 # Need to support URL shortening -class tango: +class setup: def __init__(self, authtype = None, username = None, password = None): self.authtype = authtype self.username = username @@ -21,17 +21,20 @@ class tango: else: pass - def getUserTimeline(self, count = None, since_id = None, max_id = None, page = None): - # Fairly close to full API support, need a few other methods. - userTimelineURL = "http://twitter.com/statuses/user_timeline/" + self.username + ".json" - if count is not None: - userTimelineURL += "?count=" + count - if since_id is not None: - userTimelineURL += "?since_id=" + since_id - if max_id is not None: - userTimelineURL += "?max_id=" + max_id - if page is not None: - userTimelineURL += "?page=" + page + def constructApiURL(self, base_url, params): + queryURL = base_url + questionMarkUsed = False + for param in params: + if params[param] is not None: + queryURL += (("&" if questionMarkUsed is True else "?") + param + "=" + params[param]) + questionMarkUsed = True + return queryURL + + def getUserTimeline(self, **kwargs): + # 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)) formattedTimeline = [] for tweet in userTimeline: