Exemple #1
0
class DirectMessageHandler(object):
    ''' Handles direct messages '''

    def __init__(self, username, password):
        self.api = Api(username=username, password=password)
        self.r = RedisConnector()
        self.get_messages()
        self.respond()

    def log(self, level, user, message):
        ''' Creates a log entry everytime a dm is sent. '''
        now = datetime.now()
        self.r.lpush('logs.booneweather:dm:sent', '%s | %s' % (now, user))
        self.r.lpush('logs:booneweather:dm:%s' % (user,),
                     '%s | %s | %s' % (now, level, message))

    def get_messages(self):
        ''' Gets a list of the direct messages. '''
        try:
            self.messages = self.api.GetDirectMessages()
        except HTTPError:
            pass
        except URLError:
            pass

    def respond(self):
        ''' Responds to each direct message and deletes it. '''
        for m in self.messages:
            command = m.text.split(' ')
            if len(command) > 2:
                message = self.command_help()
            if ['current', 'temp'] == command:
                message = self.command_current_temp() 
            elif ['current', 'precip'] == command:
                message = self.command_current_precip()
            else:
                message = self.command_help()
            user = m.sender_screen_name
            self.api.PostDirectMessage(user, message)
            self.log('debug', user, 'Sent DM: %s' % (message,))
            self.api.DestroyDirectMessage(m.id)

    def command_help(self):
        ''' Returns the help info to the user. '''
        return 'oh noes! commands: current temp, help'

    def command_current_temp(self):
        ''' Returns the current conditions to the user.'''
        temp = self.r.get('weather:current:temp')
        return 'heyo! the temp is %s F.' % (temp,)

    def command_current_precip(self):
        """Returns the current precipitation in boone!"""
        cond = self.r.get('weather:current:cond')
        return 'yo there! it\'s currently %s in boone' % (cond,)
 def update(self):
     r = RedisConnector()
     temp = r.get("booneweather:current:temp")
     cond = r.get("booneweather:current:cond")
     today_high = r.get("booneweather:today:high")
     today_low = r.get("booneweather:today:low")
     tom_high = r.get("booneweather:tomorrow:high")
     tom_low = r.get("booneweather:tomorrow:low")
     if temp and cond and tom_high and tom_low:
         tweet = "It's %sF and %s in %s. Today's high %sF, low: %sF Tomorrow's high %sF, low: %sF #boone #wncwx"
         name = choice("boonetana,booneville,boonetopia,booneberg".split(","))
         self.tweet = tweet % (temp, cond, name, today_high, today_low, tom_high, tom_low)
         try:
             self.api.update_status(self.tweet)
         except HTTPError:
             # fail whale ftw
             pass
         except tweepy.error.TweepError:
             # duplicate tweet.
             pass
     else:
         raise NoDataInRedis
Exemple #3
0
 def update(self):
     r = RedisConnector()
     temp = r.get(self.bot_name + ':current:temp')
     cond = r.get(self.bot_name + ':current:cond')
     today_high = r.get(self.bot_name + ':today:high')
     today_low = r.get(self.bot_name + ':today:low')
     tom_high = r.get(self.bot_name + ':tomorrow:high')
     tom_low = r.get(self.bot_name + ':tomorrow:low')
     if (temp and cond and tom_high and tom_low):
         tweet = "It's %sF and %s in %s. High today: %sF, low: %sF High tomorrow:  %sF, low: %sF %s"
         name = choice(self.city_names.split(','))
         self.tweet = tweet % (temp, cond, name, today_high, today_low, tom_high, tom_low, self.hash_tags)
         try:
             self.api.update_status(self.tweet)
         except HTTPError:
             # fail whale ftw
             pass
         except tweepy.error.TweepError:
             # duplicate tweet.
             pass
     else:
         raise NoDataInRedis
Exemple #4
0
 def __init__(self, username, password):
     self.api = Api(username=username, password=password)
     self.r = RedisConnector()
     self.get_messages()
     self.respond()