From 4a910f3b803757e86d98cde16ca79437db011f53 Mon Sep 17 00:00:00 2001 From: Ryan McGrath Date: Mon, 6 Jul 2009 03:02:01 -0400 Subject: [PATCH] Added check for authentication in updateStatus(). Not sure why this was never there; ideally, I should replace all these is_autheticated calls at some point with a decorator. This commit also has a new TangoError Exception class, which updateStatus() will now raise. This should be spread throughout the library in the coming week, when I have a chance to hit these two issues. (Note: The Python 3 version of Tango will most likely incur a lag of a day or so this week, in terms of receiving updates. This will hopefully be made easier in the future... --- tango.py | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/tango.py b/tango.py index d19193c..4d3546a 100644 --- a/tango.py +++ b/tango.py @@ -24,6 +24,12 @@ try: except ImportError: print "Tango requires oauth for authentication purposes. http://oauth.googlecode.com/svn/code/python/oauth/oauth.py" +class TangoError(Exception): + def __init__(self, msg): + self.msg = msg + def __str__(self): + return repr(self.msg) + class setup: def __init__(self, authtype = "OAuth", username = None, password = None, oauth_keys = None, debug = False): self.authtype = authtype @@ -154,14 +160,18 @@ class setup: print "Failed with a " + str(e.code) + " error code. Does this user hide/protect their updates? If so, you'll need to authenticate and be their friend to get their timeline." def updateStatus(self, status, in_reply_to_status_id = None): - if len(list(status)) > 140: - print "This status message is over 140 characters, but we're gonna try it anyway. Might wanna watch this!" - try: - return simplejson.load(self.opener.open("http://twitter.com/statuses/update.json?", urllib.urlencode({"status": status, "in_reply_to_status_id": in_reply_to_status_id}))) - except HTTPError, e: - if self.debug is True: - print e.headers - print "updateStatus() failed with a " + str(e.code) + " error code." + if self.authenticated is True: + if len(list(status)) > 140: + print "This status message is over 140 characters, but we're gonna try it anyway. Might wanna watch this!" + try: + return simplejson.load(self.opener.open("http://twitter.com/statuses/update.json?", urllib.urlencode({"status": status, "in_reply_to_status_id": in_reply_to_status_id}))) + except HTTPError, e: + if self.debug is True: + print e.headers + raise TangoError("updateStatus() failed with a " + str(e.code) + "error code.") + #print "updateStatus() failed with a " + str(e.code) + " error code." + else: + raise TangoError("updateStatus() requires you to be authenticated.") def destroyStatus(self, id): if self.authenticated is True: