def update_averages_and_std_deviation(): for z in following: user = Tweet.query.filter_by(user_id=z).all() retweet_counts = [y.retweet_count for y in user] # average retweet count of user_id if len(retweet_counts) != 0: average = sum(retweet_counts)/len(retweet_counts) calculate = sum([pow((g-average), 2) for g in retweet_counts]) standard_deviation = math.sqrt(calculate/len(retweet_counts)) else: average = 0 calculate = 0 standard_deviation = 0 Tweet.query.filter_by(user_id=z).update(dict(average_rt_count=average, std_deviation=standard_deviation)) try: db.session.commit() except: db.session.rollback() for x in user: if tweet_age_in_hours(x) < 168: if standard_deviation != 0: x.std_dev_sigma = (x.retweet_count - average)/standard_deviation if standard_deviation == 0: x.std_dev_sigma = 0 if len(retweet_counts) < 25 and x.std_dev_sigma > 3: x.std_dev_sigma = 3.0 if len(retweet_counts) < 3: x.std_dev_sigma = .125 tweet_hour_age = tweet_age_in_hours(x) number_of_times_retweeted = times_appears_in_stream(x.link, link_counter) points = (10*(x.std_dev_sigma))*number_of_times_retweeted score_with_time = hacker_news(points, tweet_hour_age) x.score = round(points) x.score_with_time = score_with_time try: db.session.commit() except: db.session.rollback()
def get_tweets_update_db(): get_tweets = tavorite.getHomeTimeline(count=200, include_entities=1, include_retweets=1) for x in get_tweets: tweet = Tweet(x) tweet_in_db = Tweet.query.filter_by(tweet_id=tweet.tweet_id).first() if tweet_in_db: if tweet_age_in_hours(tweet_in_db) < 14: if tweet_in_db.retweet_count < tweet.retweet_count: tweet_in_db.retweet_count = tweet.retweet_count db.session.commit() else: if tweet.url_exists: try: db.session.add(tweet) db.session.commit() except: db.session.rollback() print "update successful"