def get_mentions(): """ Get all mentions from the timeline and store them in the db. """ t = Twitter(auth=OAuth(Config.access_token, Config.access_token_secret, Config.api_key, Config.api_secret)) mentions = t.statuses.mentions_timeline() for mention in mentions: user_id = UserDao.add_user(mention["user"]["screen_name"], mention["user"]["id"]) if UserTweetDao.is_new_user_tweet(mention["id"]): UserTweetDao.create_user_tweet(user_id, mention["id"], mention["text"], mention)
def read_stream(): """ Listens to the user-stream and reacts to mention events with a recommendation. """ twitter_user_stream = TwitterStream(auth=OAuth(Config.access_token, Config.access_token_secret, Config.api_key, Config.api_secret), domain='userstream.twitter.com') for msg in twitter_user_stream.user(): logging.info(msg) recommend = False # check if the the bot was mentioned in the status update if "entities" in msg: for mention in msg["entities"]["user_mentions"]: if mention["screen_name"] == Config.name.replace("@", ""): recommend = True if recommend: user_id = UserDao.add_user(msg["user"]["screen_name"], msg["user"]["id"]) UserTweetDao.create_user_tweet(user_id, msg["id"], msg["text"], msg) Recommender.get_recommendation() distribute_recommendations()
def do_recommendation(tweet, keyword_list="", delete_fails=False): # TODO only persist if there is a recommendation? user = UserDao.add_user(tweet["user"]["screen_name"], tweet["user"]["id"]) nr_distributed = 0 if not UserTweetDao.is_existing_user_tweet(tweet["id"]): if len(keyword_list) > 0: tweet_text = keyword_list else: tweet_text = tweet["text"] UserTweetDao.create_user_tweet(user.id, tweet["id"], tweet_text, tweet) Recommender.get_recommendation() nr_distributed = distribute_recommendations() # TODO delete failed # if nr_distributed == 0 and delete_fails: # # user.delete() # pass return nr_distributed