From 325a0da62140d45fdb1d7e610f7830210fb6df69 Mon Sep 17 00:00:00 2001 From: Matthew Jones Date: Sat, 13 Jun 2009 18:33:16 -0400 Subject: [PATCH 1/6] Clean up tweetbox to init properly. Move application code into a dedicated main --- noaaRSS.py | 2 -- stormtweet.conf | 4 ++++ tweetBot.py | 45 ++++++++++++++++++++++++++++++++++++--------- 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/noaaRSS.py b/noaaRSS.py index 9f05163..98eec4e 100644 --- a/noaaRSS.py +++ b/noaaRSS.py @@ -52,8 +52,6 @@ def parse(self, nfeed): self.displayEntry(e) storm=self.recordEntry(e) self.alertOnStorm(storm) -## n = NOAA2Message() -## n.convert(self.shelf["thunderstorm"][newid]) self.shelf["thunderstorm"][newid] # Clean up the existing entries diff --git a/stormtweet.conf b/stormtweet.conf index 1739af4..c73426c 100644 --- a/stormtweet.conf +++ b/stormtweet.conf @@ -2,3 +2,7 @@ checkinterval = 240 noaafeed = http://www.weather.gov/alerts-beta/us.php?x=1 shelfFile = stormtweet + +[tweetbox] +user = stormwarn +password = xxx \ No newline at end of file diff --git a/tweetBot.py b/tweetBot.py index ec57828..b3b573c 100644 --- a/tweetBot.py +++ b/tweetBot.py @@ -2,14 +2,16 @@ import time from model import * from sqlalchemy import func +from ConfigParser import RawConfigParser +import optparse ## setup the model setup_all() class TweetBot: - def __init__(self): - self.tbox=Twitter('stormwarn','st0rmp22wd') - + def __init__(self, tUser, tPassword): + self.tbox = Twitter(tUser, tPassword) + def recordMessage(self, message): ### Need to parse the really lame timestamp twitter uses in 'created_at' dm=DirectMessages(messageID=message['id']) @@ -17,7 +19,7 @@ def recordMessage(self, message): action = message['text'].split()[0].lower() state = message['text'].split()[1].upper() sender = message['sender_screen_name'] - + follower=Follower.get_by(userid=sender) if not follower: follower=Follower(userid=sender) @@ -47,9 +49,9 @@ def recordMessage(self, message): self.tbox.direct_messages.new(user=sender, text='FOR SHAME! You are no longer following storms in %s. Enable updates with follow %s' % (state,state)) else: self.tbox.direct_messages.new(user=sender, text='I am sorry %s , I can not do that. Please try follow|silence|stop XX' % sender) - + session.commit() - + def getMessages(self): lastMessageID=DirectMessages.query().max(DirectMessages.messageID) @@ -71,14 +73,39 @@ def doit(self): self.makeFriends() self.getMessages() +def getConfig(path): # move into common module with stormtweet.getConfig + rc = RawConfigParser() + if path not in rc.read(path): + print("Invalid Configuration File") + sys.exit(1) + return rc +def getOptions(): + op = optparse.OptionParser() + op.add_option("-c", "--config", dest="config", + help="configuration file") + op.add_option("-i", "--initialize", dest="initialize", + help="Initialize database, don't actually post entries") + op.add_option("-v", "--verbose", dest="verbose", + help="Display extra information") + (options, args) = op.parse_args() -if __name__=='__main__': - spam=TweetBot() + if options.config is None: + print("Configuration file required") + sys.exit(1) + + return (options, args) + +def main(): + options, args = getOptions() + config = getConfig(options.config) + spam=TweetBot(config.get("tweetbox", "user"), + config.get("tweetbox", "password")) while 1: spam.doit() print 'sleeping ....' time.sleep(1800) - +if __name__=='__main__': + main() From e5a8bf0316533c657f0e87595646e61119b28fd1 Mon Sep 17 00:00:00 2001 From: Matthew Jones Date: Sat, 13 Jun 2009 18:36:49 -0400 Subject: [PATCH 2/6] Clean up style to match pep8 guidlines --- noaaRSS.py | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/noaaRSS.py b/noaaRSS.py index 98eec4e..8961e75 100644 --- a/noaaRSS.py +++ b/noaaRSS.py @@ -65,13 +65,18 @@ def summarize(self): pass def recordEntry(self, e): - stormType=StormType.get_by(value='Thunderstorm') - stormState=StormStates.get_by(value='New') - uState=UnitedStates.get_by(abbreviation=e['id'][0:2]) - newStorm=Storm(stormID=e['id'],stormType=stormType,effective=e['cap_effective'], - expires=e['cap_expires'],sevLevel=e['cap_severity'], - urgency=e['cap_urgency'],summary=e['summary'], - uState=uState,sState=sState) + stormType=StormType.get_by(value ='Thunderstorm') + stormState=StormStates.get_by(value = 'New') + uState=UnitedStates.get_by(abbreviation = e['id'][0:2]) + newStorm=Storm(stormID = e['id'], + stormType = stormType, + effective = e['cap_effective'], + expires = e['cap_expires'], + sevLevel = e['cap_severity'], + urgency = e['cap_urgency'], + summary = e['summary'], + uState = uState, + sState = sState) newStorm.update_or_save() return newStorm @@ -79,21 +84,15 @@ def alertOnStorm(self,storm): # this whole function may need to run as a separate thread # instead of inline with the rss parsing - ## change the state to proccessing until we finish sending the alerts - stormState=StormStates.get_by(value='Processing') + # change the state to proccessing until we finish sending the alerts + stormState=StormStates.get_by(value = 'Processing') storm.sState=stormState storm.update_or_save() - ##find all active users following the uState affected by the storm + #find all active users following the uState affected by the storm - ## for each active user - #######send a direct message to each user + # for each active user + # send a direct message to each user + #record the tweet sent to each user - #######record the tweet sent to each user - - ##after alerting all users, change the stormstate to dispatched so we know we told everyone - - - - - - + #after alerting all users, change the stormstate to dispatched so we + #know we told everyone From 60b1fe6844df3b507937e8208f7ba20fcef4318d Mon Sep 17 00:00:00 2001 From: Matthew Jones Date: Sat, 13 Jun 2009 18:45:01 -0400 Subject: [PATCH 3/6] Renaming to msgFilter --- msgGen.py => msgFilter.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename msgGen.py => msgFilter.py (100%) diff --git a/msgGen.py b/msgFilter.py similarity index 100% rename from msgGen.py rename to msgFilter.py From e371c3328a1c9446abfac96895abd62a8a85e08c Mon Sep 17 00:00:00 2001 From: Matthew Jones Date: Sat, 13 Jun 2009 18:58:50 -0400 Subject: [PATCH 4/6] Move text into constants, clean up style to conform to pep8, fix comment --- model.py | 8 +++--- tweetBot.py | 79 +++++++++++++++++++++++++++++++++++++---------------- 2 files changed, 59 insertions(+), 28 deletions(-) diff --git a/model.py b/model.py index ecd8fa6..3dca9fb 100644 --- a/model.py +++ b/model.py @@ -1,7 +1,7 @@ from elixir import * import mx.DateTime -## Set the data base backend (maybe pgsql later?) +# Set the data base backend metadata.bind = "postgres://stormtweet:270rm@localhost/stormtweet" ## Debug SQL for now metadata.bind.echo = True @@ -71,7 +71,7 @@ def createDataBase(): TweetStates(value='New') TweetStates(value='Successful') TweetStates(value='Error') - + FollowerStates(value='Active') FollowerStates(value='Silenced') FollowerStates(value='Inactive') @@ -125,6 +125,6 @@ def createDataBase(): UnitedStates(value='WA', name='Washington') UnitedStates(value='WV', name='West Virginia') UnitedStates(value='WI', name='Wisconsin') - UnitedStates(value='WY', name='Wyoming') - + UnitedStates(value='WY', name='Wyoming') + session.commit() diff --git a/tweetBot.py b/tweetBot.py index b3b573c..6d192e9 100644 --- a/tweetBot.py +++ b/tweetBot.py @@ -5,7 +5,27 @@ from ConfigParser import RawConfigParser import optparse -## setup the model +SORRY_MSG = """ +I am sorry %s , I can not do that. Please try follow|silence|stop XX +""" +FOLLOW_SUCCESS_MSG = """ +Congrats! You are now following storms in %s. Silence updates with: silence %s +""" +SILENCE_SUCCESS_MSG = """ +SHHHHHH. You have silenced updates for storms in %s. Enable + updates with: follow %s +""" +STOP_SUCCESS_MSG = """ +FOR SHAME! You are no longer following storms in %s. Enable updates with: + follow %s +""" +UNKNOWN_MSG = """ +I am sorry %s , I can not do that. Please try follow|silence|stop XX +""" +FOLLOWING_MSG = """ +Thanks for following StormWarn! To get storm warnings for a state with postal abbreviation XX send me:\nfollow XX to get updates +""" +# setup the model setup_all() class TweetBot: @@ -13,49 +33,58 @@ def __init__(self, tUser, tPassword): self.tbox = Twitter(tUser, tPassword) def recordMessage(self, message): - ### Need to parse the really lame timestamp twitter uses in 'created_at' - dm=DirectMessages(messageID=message['id']) - ## Expect the message to be 'follow XX' where XX is a state abbreveation + # Need to parse the really lame timestamp twitter uses in 'created_at' + dm = DirectMessages(messageID = message['id']) + # Expect the message to be 'follow XX' where XX is a state + # abbreveiation action = message['text'].split()[0].lower() state = message['text'].split()[1].upper() sender = message['sender_screen_name'] - follower=Follower.get_by(userid=sender) + follower=Follower.get_by(userid = sender) if not follower: - follower=Follower(userid=sender) + follower=Follower(userid = sender) session.commit() ustate = UnitedStates.get_by(value=state) if not ustate: - self.tbox.direct_messages.new(user=sender, text='I am sorry %s , I can not do that. Please try follow|silence|stop XX' % sender) + self.tbox.direct_messages.new(user = sender + text=SORRY_MSG % sender) if action == 'follow': - fstate=FollowerStates.get_by(value='Active') - follower.fState=fstate - follower.uState=ustate + fstate = FollowerStates.get_by(value='Active') + follower.fState = fstate + follower.uState = ustate follower.update() - self.tbox.direct_messages.new(user=sender, text='Congrats! You are now following storms in %s. Silence updates with silence %s' % (state,state)) + self.tbox.direct_messages.new(user = sender, + text = FOLLOW_SUCCESS_MSG % + (state,state)) elif action == 'silence': - fstate=FollowerStates.get_by(value='Slienced') - follower.fState=fstate - follower.uState=ustate + fstate = FollowerStates.get_by(value='Slienced') + follower.fState = fstate + follower.uState = ustate follower.update() - self.tbox.direct_messages.new(user=sender, text='SHHHHHH. You have silenced updates for storms in %s. Enable updates with follow %s' % (state,state)) + self.tbox.direct_messages.new(user = sender, + text = SILENCE_SUCCESS_MSG % + (state,state)) elif action == 'stop': - fstate=FollowerStates.get_by(value='Inactive') - follower.fState=fstate - follower.uState=ustate + fstate = FollowerStates.get_by(value='Inactive') + follower.fState = fstate + follower.uState = ustate follower.update() - self.tbox.direct_messages.new(user=sender, text='FOR SHAME! You are no longer following storms in %s. Enable updates with follow %s' % (state,state)) + self.tbox.direct_messages.new(user = sender, + text = STOP_SUCCESS_MSG % + (state,state)) else: - self.tbox.direct_messages.new(user=sender, text='I am sorry %s , I can not do that. Please try follow|silence|stop XX' % sender) + self.tbox.direct_messages.new(user = sender, + text = UNKNOWN_MSG % sender) session.commit() def getMessages(self): - lastMessageID=DirectMessages.query().max(DirectMessages.messageID) - messages=self.tbox.direct_messages(since_id=lastMessageID) + lastMessageID = DirectMessages.query().max(DirectMessages.messageID) + messages = self.tbox.direct_messages(since_id = lastMessageID) for aMessage in messages: self.recordMessage(aMessage) @@ -66,9 +95,11 @@ def makeFriends(self): newFriends=followers - currentFriends for aFriend in newFriends: self.tbox.friendships.create(id=aFriend) - screen_name=self.tbox.users.show(id=aFriend)['screen_name'] - self.tbox.direct_messages.new(user=screen_name, text='''Thanks for following StormWarn! To get storm warnings for a state with postal abbreviation XX send me:\nfollow XX to get updates''') + screen_name=self.tbox.users.show(id = aFriend)['screen_name'] + self.tbox.direct_messages.new(user = screen_name, + text = FOLLOWING_MSG) session.commit() + def doit(self): self.makeFriends() self.getMessages() From 267c01e532f53897aab263d648dd7fae5e30a383 Mon Sep 17 00:00:00 2001 From: Matthew Jones Date: Sat, 13 Jun 2009 18:59:24 -0400 Subject: [PATCH 5/6] Fix long line --- tweetBot.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tweetBot.py b/tweetBot.py index 6d192e9..e86fbfc 100644 --- a/tweetBot.py +++ b/tweetBot.py @@ -23,7 +23,8 @@ I am sorry %s , I can not do that. Please try follow|silence|stop XX """ FOLLOWING_MSG = """ -Thanks for following StormWarn! To get storm warnings for a state with postal abbreviation XX send me:\nfollow XX to get updates +Thanks for following StormWarn! To get storm warnings for a state with postal + abbreviation XX send me:\nfollow XX to get updates """ # setup the model setup_all() From f500994008e88613f6e1fac976a39fafb049c1b8 Mon Sep 17 00:00:00 2001 From: User Date: Sun, 14 Jun 2009 00:30:31 +0000 Subject: [PATCH 6/6] Fix a comma --- tweetBot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tweetBot.py b/tweetBot.py index e86fbfc..dd07d70 100644 --- a/tweetBot.py +++ b/tweetBot.py @@ -49,7 +49,7 @@ def recordMessage(self, message): ustate = UnitedStates.get_by(value=state) if not ustate: - self.tbox.direct_messages.new(user = sender + self.tbox.direct_messages.new(user = sender, text=SORRY_MSG % sender) if action == 'follow':