def unfollow(twitter, handle): user = User.all().filter('screen_name =', handle).get() if user is None: logging.warning('User %s sent a stop DM but is not on the datastore' % (handle, )) return user.active = False user.put() send_stop_dm(twitter, handle)
def dm(self, bodies): text_body = [body.decode() for type, body in bodies if type == 'text/plain'][0] sender = re.search('twitter.com/([^ \n\r\t]+)', text_body).groups()[0] text_lines = text_body.split('\n') dm_lines = [] for line in text_lines: if line.find(sender) == -1: dm_lines.append(line) else: break dm_body = ''.join(dm_lines).strip() \ .replace('\n', ' ') \ .replace('\t', ' ') \ .replace('\r', ' ') twitter = login_twitter_bot() logging.debug(dm_body) if dm_body == STOP_KEYWORD: unfollow(twitter, sender) elif dm_body == RESTART_KEYWORD: refollow(twitter, sender) elif dm_body == HELP_KEYWORD: send_help_dm(twitter, sender) elif dm_body == WHERE_KEYWORD: user = User.all().filter('screen_name =', sender).get() if user is None: logging.warning('An unknown user sent a DM (%s)' % (sender, )) else: send_where_dm(twitter, user) elif dm_body == ABOUT_KEYWORD: send_about_dm(twitter, sender) elif dm_body == FORECAST_KEYWORD: send_forecast_dm(twitter, sender) else: user = User.all().filter('screen_name =', sender).get() if user is None: logging.warning('An unknown user sent a DM (%s)' % (sender, )) else: relocate(twitter, dm_body, user) user.put()
def send_success_dm(twitter, handle): location = None try: location = User.all().filter('screen_name =', handle).get().location except: logging.error("Error getting %s's data."%(handle, )) return text = "Te avisaremos cuando llueva por tus pagos, en %s! Cancela el servicio mandando '%s'." %(location.name, STOP_KEYWORD) twitter.send_direct_message(screen_name=handle, text=text) logging.debug("Successfully found %s" % (handle, ))
def send_forecast_dm(twitter, handle): location = None try: location = User.all().filter('screen_name =', handle).get().location except: logging.error('User or location not found!') return date = location.next_rain_datetime + datetime.timedelta(hours=location.timezone) twitter.send_direct_message(screen_name=handle, text='Pronosticado lluvia para el '+str(date.day)+' a las '+str(date.hour)) logging.debug('User %s asked for forecast. Sent.'%(handle, ))
def follow(twitter, handle): user_data = follow_user_and_get(twitter, handle) if user_data is None: return user = User.all().filter('screen_name =', handle).get() if user is None: user = User(screen_name=handle) else: user.active = True if user.location is not None: user.put() send_success_dm(twitter, handle) return relocate(twitter, user_data.location, user) user.put()
def get(self): all_places = list(Location.all()) rainy_places = self._get_locations(all_places) twitter = login_twitter_bot() logging.debug("The rainy places are: "+str([location.name for location in rainy_places])) for place in all_places: if place in rainy_places: users = list(User.all(keys_only=True).filter('location =', place).filter('active =', True)) message = self._format_message(place) user_len = len(users) for i in range(0, user_len / RainNotification.DM_PER_REQUEST + 1): slice = users[i*RainNotification.DM_PER_REQUEST:(i + 1)*RainNotification.DM_PER_REQUEST] if len(slice) > 0: QueueHandler.queue_notify(message, slice, i*10) else: place.last_broadcast_rain = False db.put(all_places)