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_filtered_stream(bwl_list): query = "" for i in range(number_query_items): query += random.choice(bwl_list) + "," query = query[:-1] twitter_stream = TwitterStream(auth=OAuth(Config.access_token, Config.access_token_secret, Config.api_key, Config.api_secret), domain='userstream.twitter.com') iterator = twitter_stream.statuses.filter(track=query) i = 0 rest_calls = 0 for tweet in iterator: if "lang" in tweet and "retweeted_status" not in tweet: if "en" == tweet["lang"]: if UserDao.is_new_user(tweet["user"]["id"]): if rest_calls < 5: rest_calls += 1 # check if user has multiple tweets with keywords count = get_timeline_by_id(tweet["user"]["id"], bwl_list) if count > threshold_domain_expert: key_list = [] for key in bwl_list: key = key.lower() if key in tweet["text"].lower(): key_list.append(key) recs = do_recommendation(tweet, " ".join(key_list), True) if recs > 0: i += 1 print("rec found") else: print("rest calls exhausted") break if i == rotate_after_hits: break
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