Twitter: cleanup/organize the code a bit

This commit is contained in:
Brian S. Stephan 2011-01-19 19:44:04 -06:00
parent 89905ac771
commit e88a934569
1 changed files with 48 additions and 14 deletions

View File

@ -26,18 +26,28 @@ from extlib import twitter
from Module import Module from Module import Module
class Twitter(Module): class Twitter(Module):
"""Access Twitter via the bot as an authenticated client.""" """Access Twitter via the bot as an authenticated client."""
def __init__(self, irc, config, server): def __init__(self, irc, config, server):
"""Prepare for oauth stuff (but don't execute it yet)."""
"""Prompt the user for oauth stuff when starting up.
TODO: make this optional, and have API calls log if they need auth.
"""
Module.__init__(self, irc, config, server) Module.__init__(self, irc, config, server)
# begin oauth magic # setup regexes
getstatuspattern = '^!twitter\s+getstatus\s+(\S+)$'
getuserstatuspattern = '^!twitter\s+getuserstatus\s+(\S+)(\s+.*|$)'
tweetpattern = '^!twitter\s+tweet\s+(.*)'
gettokenpattern = '^!twitter\s+gettoken$'
authpattern = '^!twitter\s+auth\s+(\S+)$'
self.getstatusre = re.compile(getstatuspattern)
self.getuserstatusre = re.compile(getuserstatuspattern)
self.tweetre = re.compile(tweetpattern)
self.gettokenre = re.compile(gettokenpattern)
self.authre = re.compile(authpattern)
# prep oauth magic
self.consumer_key = 'N2aSGxBP8t3cCgWyF1B2Aw' self.consumer_key = 'N2aSGxBP8t3cCgWyF1B2Aw'
self.consumer_secret = '0aQPEV4K3MMpicfi2lDtCP5pvjsKaqIpfuWtsPzx8' self.consumer_secret = '0aQPEV4K3MMpicfi2lDtCP5pvjsKaqIpfuWtsPzx8'
@ -53,11 +63,23 @@ class Twitter(Module):
self.authed = False self.authed = False
def do(self, connection, event, nick, userhost, what, admin_unlocked): def do(self, connection, event, nick, userhost, what, admin_unlocked):
""" """Attempt to do twitter things."""
Attempt to do twitter things.
"""
match = re.search('^!twitter\s+getstatus\s+(\S+)$', what) if self.getstatusre.search(what):
return self.twitter_getstatus(connection, event, nick, userhost, what, admin_unlocked)
elif self.getuserstatusre.search(what):
return self.twitter_getuserstatus(connection, event, nick, userhost, what, admin_unlocked)
elif self.tweetre.search(what):
return self.twitter_tweet(connection, event, nick, userhost, what, admin_unlocked)
elif self.gettokenre.search(what):
return self.twitter_gettoken(connection, event, nick, userhost, what, admin_unlocked)
elif self.authre.search(what):
return 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."""
match = self.getstatusre.search(what)
if match: if match:
status = match.group(1) status = match.group(1)
try: try:
@ -66,7 +88,10 @@ class Twitter(Module):
except twitter.TwitterError as e: except twitter.TwitterError as e:
return 'Couldn\'t obtain status: ' + str(e) return 'Couldn\'t obtain status: ' + str(e)
match = re.search('^!twitter\s+getuserstatus\s+(\S+)(\s+.*|$)', what) def twitter_getuserstatus(self, connection, event, nick, userhost, what, admin_unlocked):
"""Get a status for a user. Allows for getting one other than the most recent."""
match = self.getuserstatusre.search(what)
if match: if match:
user = match.group(1) user = match.group(1)
index = match.group(2) index = match.group(2)
@ -87,7 +112,10 @@ class Twitter(Module):
except twitter.TwitterError as e: except twitter.TwitterError as e:
return 'Couldn\'t obtain status: ' + str(e) return 'Couldn\'t obtain status: ' + str(e)
match = re.search('^!twitter\s+tweet\s+(.*)', what) def twitter_tweet(self, connection, event, nick, userhost, what, admin_unlocked):
"""Tweet. Needs authentication."""
match = self.tweetre.search(what)
if match: if match:
tweet = match.group(1) tweet = match.group(1)
if self.authed is False: if self.authed is False:
@ -103,7 +131,10 @@ class Twitter(Module):
except twitter.TwitterError as e: except twitter.TwitterError as e:
return 'Couldn\'t tweet: ' + str(e) return 'Couldn\'t tweet: ' + str(e)
match = re.search('^!twitter\s+gettoken$', what) def twitter_gettoken(self, connection, event, nick, userhost, what, admin_unlocked):
"""Get an oauth token, so that the user may authenticate the bot."""
match = self.gettokenre.search(what)
if match: if match:
# get request token # get request token
resp, content = self.client.request(self.request_token_url, "GET") resp, content = self.client.request(self.request_token_url, "GET")
@ -115,7 +146,10 @@ class Twitter(Module):
# have the user auth # have the user auth
return 'Go to the following link in your browser: %s?oauth_token=%s and then send me the pin.' % (self.authorize_url, self.request_token['oauth_token']) return 'Go to the following link in your browser: %s?oauth_token=%s and then send me the pin.' % (self.authorize_url, self.request_token['oauth_token'])
match = re.search('^!twitter\s+auth\s+(\S+)$', what) def twitter_auth(self, connection, event, nick, userhost, what, admin_unlocked):
"""Authenticate, given a PIN (following gettoken)."""
match = self.authre.search(what)
if match: if match:
authtoken = match.group(1) authtoken = match.group(1)
oauth_verifier = authtoken oauth_verifier = authtoken