Twitter: more cleanup, clarify the auth stuff a bit

This commit is contained in:
Brian S. Stephan 2012-07-14 08:41:40 -05:00
parent 82765c7404
commit 2dd27dde4b
1 changed files with 45 additions and 17 deletions

View File

@ -104,17 +104,23 @@ class Twitter(Module):
"""Attempt to do twitter things."""
if self.getstatusre.search(what):
return self.reply(connection, event, self.twitter_getstatus(connection, event, nick, userhost, what, admin_unlocked))
return self.reply(connection, event, self.twitter_getstatus(connection, event, nick,
userhost, what, admin_unlocked))
elif self.getuserstatusre.search(what):
return self.reply(connection, event, self.twitter_getuserstatus(connection, event, nick, userhost, what, admin_unlocked))
return self.reply(connection, event, self.twitter_getuserstatus(connection, event, nick,
userhost, what, admin_unlocked))
elif self.tweetre.search(what):
return self.reply(connection, event, self.twitter_tweet(connection, event, nick, userhost, what, admin_unlocked))
return self.reply(connection, event, self.twitter_tweet(connection, event, nick,
userhost, what, admin_unlocked))
elif self.replytore.search(what):
return self.reply(connection, event, self.twitter_replyto(connection, event, nick, userhost, what, admin_unlocked))
return self.reply(connection, event, self.twitter_replyto(connection, event, nick,
userhost, what, admin_unlocked))
elif self.gettokenre.search(what):
return self.reply(connection, event, self.twitter_gettoken(connection, event, nick, userhost, what, admin_unlocked))
return self.reply(connection, event, self.twitter_gettoken(connection, event, nick,
userhost, what, admin_unlocked))
elif self.authre.search(what):
return self.reply(connection, event, self.twitter_auth(connection, event, nick, userhost, what, admin_unlocked))
return self.reply(connection, event, self.twitter_auth(connection, event, nick,
userhost, what, admin_unlocked))
def twitter_getstatus(self, connection, event, nick, userhost, what, admin_unlocked):
"""Get a status by tweet ID."""
@ -165,7 +171,7 @@ class Twitter(Module):
match = self.tweetre.search(what)
if match:
tweet = match.group(1)
if self.twit.VerifyCredentials() is None:
if self._verify_credentials() is None:
return "You must be authenticated to tweet."
if admin_unlocked is False:
return "Only admins can tweet."
@ -186,7 +192,7 @@ class Twitter(Module):
status_id = match.group(1)
tweet = match.group(2)
if self.twit.VerifyCredentials() is None:
if self._verify_credentials() is None:
return "You must be authenticated to tweet."
if admin_unlocked is False:
return "Only admins can tweet."
@ -222,6 +228,9 @@ class Twitter(Module):
match = self.authre.search(what)
if match:
if self._verify_credentials() is not None:
return "The bot is already logged in!"
authtoken = match.group(1)
oauth_verifier = authtoken
@ -231,18 +240,37 @@ class Twitter(Module):
client = oauth.Client(self.consumer, token)
resp, content = client.request(self.access_token_url, "POST")
self.access_token = dict(urlparse.parse_qsl(content))
access_token = dict(urlparse.parse_qsl(content))
self._log_in_to_twitter(access_token['oauth_token'], access_token['oauth_token_secret'])
# finally, create the twitter API object
self.twit = twitter.Api(self.consumer_key, self.consumer_secret, self.access_token['oauth_token'], self.access_token['oauth_token_secret'])
if self._verify_credentials() is not None:
# ugly, but the best place to track it. store the connection for later use
self.connection = connection
# ugly, but the best place to track it. store the connection for later use
self.connection = connection
# print timeline stuff. this will set up the appropriate timer
self._check_self_timeline()
# print timeline stuff. this will set up the appropriate timer
self._check_self_timeline()
return "The bot is now logged in."
else:
return "Could not log in with supplied credentials."
return "The bot is now logged in."
def _verify_credentials(self):
"""Wrap the exceptions in the twitter client VerifyExceptions()."""
try:
return self.twit.VerifyCredentials()
except Exception as e:
print("Exception in checking login status: " + str(e))
return None
def _log_in_to_twitter(self, oauth_token, oauth_token_secret):
"""Do the actual authentication against Twitter."""
# create the twitter API object
self.twit = twitter.Api(self.consumer_key, self.consumer_secret, oauth_token, oauth_token_secret)
#if self._verify_credentials() is not None:
# # save the auth token for later reuse
def thread_do(self):
"""Check the timeline."""
@ -257,7 +285,7 @@ class Twitter(Module):
if self.next_timeline_check < time.time():
self.next_timeline_check = time.time() + 300
if self.twit.VerifyCredentials() is not None:
if self._verify_credentials() is not None:
# get the id of the last check we made
since_id = self._get_last_since_id()
output_channel = self._get_output_channel()