def save_to_database(data): try: user = session.query(User).filter_by(id=str(data['user']['id'])).one() except NoResultFound: user = create_user_helper(data['user']) session.add(user) hashtag_results = [] hashtags = data['entities']['hashtags'] for hashtag in hashtags: hashtag = hashtag['text'].lower() try: hashtag_obj = session.query(Hashtag).filter_by(text=hashtag).one() except NoResultFound: hashtag_obj = Hashtag(text=hashtag) session.add(hashtag_obj) hashtag_results.append(hashtag_obj) tweet = create_tweet_helper(data, user) for hashtag in hashtag_results: tweet.hashtags.append(hashtag) session.add(tweet) session.commit()
def save_to_database(data): try: user = session.query(User).filter_by(id=str(data['user']['id'])).one() except NoResultFound: user = create_user_helper(data['user']) session.add(user) hashtag_results = [] hashtags = data['entities']['hashtags'] for hastag in hashtags: hashtag = hashtag['text'].lower() try: hashtag_obj = session.query(Hashtag).filter_by(text=hashtag).one() except NoResultFound: hashtag_obj = Hashtag(text=hashtag) session.add(hashtag_obj) hashtag_results.append(hashtag_obj) tweet = create_tweet_helper(data, user) for hastag in hashtag_results: tweet.hashtags.append(hashtag) session.add(tweet) session.commit()
def _update_sets(api, session, tweet_set, start_num): """ Broke out a helper method so we didn't have to repeat the code for our last set. This helper method does the heavy lifting for us """ # Grab out just the tweet ids using a list comprehension tweet_ids = [tweet.tid for tweet in tweet_set] # Using the tweepy api, grab the updated tweets # `trim_user` drops user data updated_set = api.statuses_lookup(tweet_ids, trim_user=True) # iterate through update set for updated_tweet in updated_set: # the values we want to update fav_count = updated_tweet.favorite_count retweet_count = updated_tweet.retweet_count # Get the old tweet using it's twitter id (tid for short) tid = updated_tweet.id database_tweet = session.query(Tweet).filter_by(tid=tid).one() # update the tweet information in our database database_tweet.favorite_count = fav_count database_tweet.retweet_count = retweet_count # User feedback print('index: {}'.format(database_tweet.id)) print('favs: {} \t retweets: {}'.format(fav_count, retweet_count)) # save our changes to the database session.commit()