class TwitterGetter(object): """ Download tweets from Twitter. """ def __init__(self, twitter): self.twitter_api = twitter # Twython object self.db_model = TwitterDbModel() self.exec_error = False def get_all_tweets(self, sleep_seconds_n=16): """Get all possible tweets for all companies.""" # Browse all companies for company in self.db_model.get_companies(): print "=====%d: %s=====" % (company['id'], company['tw_search_name']) try: # If company has no Twitter name, use only search name field. if not company['tw_name']: self.__get_search_tweets('search_name', company) continue # Skip other tweets types. # Get tweets containing @companyUsername: @CocaCola self.__get_search_tweets('mention', company) # Get tweets containg "company name" but not containing @companyUsername: coco cola -@CocaCola self.__get_search_tweets('search_name', company) # Get tweets which are replies to company tweets: to:CocaCola self.__get_search_tweets('reply', company) # Get tweets from company timeline self.__get_timeline_tweets(company) except Exception, e: self.exec_error = True print "serious error: %s" % repr(e) self.__send_serious_error(e) break # end loop # Wait some time to obey Twitter API rate limits. time.sleep(sleep_seconds_n) # Log execution self.db_model.add_log_exec(3, self.exec_error)
def __init__(self, twitter): self.twitter_api = twitter # Twython object self.db_model = TwitterDbModel() self.exec_error = False