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()
Exemple #3
0
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
def get_recommendation():
    new_tweets = UserTweetDao.get_new_user_tweets()

    for tweet in new_tweets:
        UserTweetDao.update_status(tweet, Enums.UserTweetStatus.requested)
        rec_input_list = get_rec_input_from_tweet(tweet)

        rec = None

        if len(rec_input_list) > 0:
            rec = recommend(rec_input_list)

        if rec is not None:
            text = get_rec_text_for_tweet(tweet, rec)
            RecommendationDao.create_recommendation(tweet.id, rec.fullRec, text)
            UserTweetDao.update_status(tweet, Enums.UserTweetStatus.done)
        else:
            UserTweetDao.update_status(tweet, Enums.UserTweetStatus.no_recommendation)
Exemple #5
0
def get_recommendation():
    new_tweets = UserTweetDao.get_new_user_tweets()

    for tweet in new_tweets:
        UserTweetDao.update_status(tweet, Enums.UserTweetStatus.requested)
        rec_input_list = get_rec_input_from_tweet(tweet)

        rec = None

        if len(rec_input_list) > 0:
            rec = recommend(rec_input_list)

        if rec is not None:
            text = get_rec_text_for_tweet(tweet, rec)
            RecommendationDao.create_recommendation(tweet.id, rec.fullRec,
                                                    text)
            UserTweetDao.update_status(tweet, Enums.UserTweetStatus.done)
        else:
            UserTweetDao.update_status(tweet,
                                       Enums.UserTweetStatus.no_recommendation)