def main(): logger.info("Starting interventions retweeting (pid %s)" % os.getpid()) twitter_stream = TwitterStream(domain="stream.twitter.com", app=1) db.init_db() while 1: for tag_date, tag in TAGS: retweets = 0 while tag_date == datetime.datetime.now().date(): try: for status in twitter_stream.stream.statuses.filter(track=tag): sleep(5) tweet_id = status.get("id_str") retweet_ids = [tweet.tweet_id for tweet in db.session.query(ReTweet).all()] if tweet_id in retweet_ids: continue screen_name = status.get("user").get("screen_name") # Retweet 10 first if retweets < 5: reTweet(tweet_id) logger.info("Retweeted: %s" % tweet_id) retweets += 1 # Retweet 50 if not BOT elif retweets <= 16 and screen_name not in BOTS: reTweet(tweet_id) logger.info("Retweeted: %s" % tweet_id) retweets += 1 # Favorite tweet favorite(tweet_id) if tag_date != datetime.datetime.now().date(): break except: print "Stream connection timeout, restarting..."
def main(): try: first_sleep = randrange(INITIAL_SLEEP_MAX) logger.info('Starting retweet script, but first sleeping %d minutes' % first_sleep) sleep(60*first_sleep) # Get tweets tweets = getTweets(config.TWEET_LIKES, result_type='mixed') # Analyze tweets analyzed = [] db.init_db() retweets = [tweet.tweet_id for tweet in db.session.query(ReTweet).all()] for tweet in tweets: if tweet.get('id_str') not in retweets: analyzed.append((analysis.sentiment(tweet.get('text')), tweet.get('id_str') ) ) analyzed = sorted(analyzed, key=itemgetter(0), reverse=True) # Retweet the most positive tweets retweet_count = randrange(RETWEET_COUNT_MAX) logger.info('Retweeting %d tweets' % retweet_count) for i in range(retweet_count): tweet = analyzed[i] reTweet(tweet[1]) new_retweet = ReTweet() new_retweet.tweet_id = tweet[1] new_retweet.sentiment = tweet[0] new_retweet.retweet_date = datetime.datetime.now() db.session.add(new_retweet) db.session.commit() sleep_time = randrange(TWEET_SLEEP_MAX) logger.info( 'Retweeting %d/%d tweet_id: %s, with sentiment %s and sleeping' ' for %d minutes' % (i+1, retweet_count, analyzed[0][1], analyzed[0][0], sleep_time)) sleep(60*sleep_time) except Exception, err: logger.error('%s: %s' % (Exception, err))