def hydrate_twitter_text_and_followers(profile_id, altmetric_twitter_posts):

    logger.info(u"in hydrate_twitter_text_and_followers for profile {profile_id}".format(
        profile_id=profile_id))

    tweets_to_hydrate_from_twitter = []
    # get them all at once into the session so gets below go faster
    tweets = Tweet.query.filter(Tweet.profile_id==profile_id)
    tweet_dict = dict([((tweet.tweet_id, tweet.tiid), tweet) for tweet in tweets])

    for tiid, post_list in altmetric_twitter_posts.iteritems():
        for post in post_list:
            #### store tweet and tweeter stuff from altmetric
            tweet_id = post["tweet_id"]
            screen_name = post["author"]["id_on_source"]

            if (tweet_id, tiid) in tweet_dict.keys():
                tweet = tweet_dict[(tweet_id, tiid)]
                if not tweet.tweet_text and not tweet.is_deleted:
                    tweets_to_hydrate_from_twitter.append(tweet)
            else:
                if not Tweet.query.get((tweet_id, tiid)):
                    tweet = Tweet(tweet_id=tweet_id, tiid=tiid)
                    tweet.set_attributes_from_altmetric_post(post)
                    tweet.profile_id = profile_id
                    tweets_to_hydrate_from_twitter.append(tweet)
                    db.session.add(tweet)
                if not tweet.tweeter:
                    tweeter = Tweeter.query.get(screen_name)
                    if not tweeter:
                        tweeter = Tweeter(screen_name=screen_name)
                        db.session.add(tweeter)
                    tweeter.set_attributes_from_altmetric_post(post)
                commit(db)

    logger.info(u"before tweets_to_hydrate_from_twitter for {profile_id}".format(
        profile_id=profile_id))
    if tweets_to_hydrate_from_twitter:
        # save the altmetric stuff first
        commit(db)

        tweet_ids = [tweet.tweet_id for tweet in tweets_to_hydrate_from_twitter]
        logger.info(u"calling get_and_save_tweet_text_and_tweeter_followers for profile {profile_id}".format(
            profile_id=profile_id))

        get_and_save_tweet_text_and_tweeter_followers(tweets_to_hydrate_from_twitter)
        commit(db)

    else:
        logger.info(u"no tweets to hydrate for profile {profile_id}".format(
            profile_id=profile_id))

    return


# see http://docs.sqlalchemy.org/en/rel_0_9/orm/relationships.html#non-relational-comparisons-materialized-path
dataset/ETHPy150Open Impactstory/total-impact-webapp/totalimpactwebapp/tweet.py/hydrate_twitter_text_and_followers/original