Shortened constructApiURL() down to one line, and it's hopefully more efficient. searchTwitter() and some others now raise TangoError Exceptions when they hit a snag; entire library can hopefully be converted by the end of the week. Need to swap out string concatenation methods, as the method used in here is proven to be slower... could be a mess in larger applications.

This commit is contained in:
Ryan McGrath 2009-07-07 08:27:33 -04:00
parent 4a910f3b80
commit b4f62e9420

View file

@ -52,7 +52,7 @@ class setup:
except HTTPError, e: except HTTPError, e:
if self.debug is True: if self.debug is True:
print e.headers print e.headers
print "Huh, authentication failed with your provided credentials. Try again? (" + str(e.code) + " failure)" raise TangoError("Authentication failed with your provided credentials. Try again? (" + str(e.code) + " failure)")
# OAuth functions; shortcuts for verifying the credentials. # OAuth functions; shortcuts for verifying the credentials.
def fetch_response_oauth(self, oauth_request): def fetch_response_oauth(self, oauth_request):
@ -76,16 +76,8 @@ class setup:
print e.headers print e.headers
print "shortenURL() failed with a " + str(e.code) + " error code." print "shortenURL() failed with a " + str(e.code) + " error code."
def constructApiURL(self, base_url, params, **kwargs): def constructApiURL(self, base_url, params):
queryURL = base_url return base_url + "?" + "&".join(["%s=%s" %(key, value) for (key, value) in params.iteritems()])
questionMarkUsed = False
if kwargs.has_key("questionMarkUsed") is True:
questionMarkUsed = True
for param in params:
if params[param] is not None:
queryURL += (("&" if questionMarkUsed is True else "?") + param + "=" + params[param])
questionMarkUsed = True
return queryURL
def getRateLimitStatus(self, rate_for = "requestingIP"): def getRateLimitStatus(self, rate_for = "requestingIP"):
try: try:
@ -169,7 +161,6 @@ class setup:
if self.debug is True: if self.debug is True:
print e.headers print e.headers
raise TangoError("updateStatus() failed with a " + str(e.code) + "error code.") raise TangoError("updateStatus() failed with a " + str(e.code) + "error code.")
#print "updateStatus() failed with a " + str(e.code) + " error code."
else: else:
raise TangoError("updateStatus() requires you to be authenticated.") raise TangoError("updateStatus() requires you to be authenticated.")
@ -384,7 +375,6 @@ class setup:
print e.headers print e.headers
print "updateProfile() failed with a " + e.code + " error code." print "updateProfile() failed with a " + e.code + " error code."
else: else:
# If they're not authenticated
print "updateProfile() requires you to be authenticated." print "updateProfile() requires you to be authenticated."
def getFavorites(self, page = "1"): def getFavorites(self, page = "1"):
@ -543,17 +533,16 @@ class setup:
print e.headers print e.headers
print "getBlockedIDs() failed with a " + str(e.code) + " error code." print "getBlockedIDs() failed with a " + str(e.code) + " error code."
else: else:
print "getBlockedIDs() requires you to be authenticated." raise TangoError("getBlockedIDs() requires you to be authenticated.")
def searchTwitter(self, search_query, **kwargs): def searchTwitter(self, search_query, **kwargs):
baseURL = "http://search.twitter.com/search.json?" + urllib.urlencode({"q": search_query}) searchURL = self.constructApiURL("http://search.twitter.com/search.json", kwargs) + "&" + urllib.urlencode({"q": search_query})
searchURL = self.constructApiURL(baseURL, kwargs, questionMarkUsed=True)
try: try:
return simplejson.load(urllib2.urlopen(searchURL)) return simplejson.load(urllib2.urlopen(searchURL))
except HTTPError, e: except HTTPError, e:
if self.debug is True: if self.debug is True:
print e.headers print e.headers
print "getSearchTimeline() failed with a " + str(e.code) + " error code." raise TangoError("getSearchTimeline() failed with a %s error code." % `e.code`)
def getCurrentTrends(self, excludeHashTags = False): def getCurrentTrends(self, excludeHashTags = False):
apiURL = "http://search.twitter.com/trends/current.json" apiURL = "http://search.twitter.com/trends/current.json"