def main(self): '''Main of the Validate class''' try: # test if it was retweeted enough to be retweeted by me if self.tweet.retweet_count >= self.cfgvalues['retweets']: # send the tweet if all checks are ok if not self.notretweethashes() and self.retweetonlyifhashtags( ) and self.retweetonlyifolderthan( ) and self.retweetonlyifoyoungerthan( ) and self.retweetonlyifmatchingregex(): self.storeit = True if self.args.dryrun: print("tweet {} sent!".format(self.tweet.id)) else: # at last retweet the tweet self.api.retweet(self.tweet.id) if self.cfgvalues['like']: self.api.create_favorite(self.tweet.id) else: self.storeit = False except (tweepy.error.TweepError) as err: print("{}".format(err)) print( "the tweet is probably retweeted already. Twitter does not allow to retweet 2 times" ) finally: # now store the tweet if not self.twp.wasposted(self.tweet.id) and self.storeit: if not self.args.dryrun: self.twp.storetweet(self.tweet.id) WaitAMoment(self.cfgvalues['waitminsecs'], self.cfgvalues['waitmaxsecs'])
def test_waitamoment(self): '''Test WaitAMoment class''' before = datetime.datetime.now() wam = WaitAMoment(1, 2) after = datetime.datetime.now() res = after - before self.assertTrue(res.seconds >= 1)
def __init__(self, cfgvalues, api, tweet): '''send the tweet''' self.cfgvalues = cfgvalues self.api = api hasnotretweethasthags = False hasonlyifhashtags = False self.twp = TweetWasPosted(self.cfgvalues) try: # test if it was retweeted enough to be retweeted by me if len(self.api.retweets(tweet)) >= self.cfgvalues['retweets']: # test if the tweet has a hashtag for not retweeting it if not self.notretweethashes(tweet): # if the tweet does not have one of the stop hashtags, send it hasnotretweethasthags = True if self.retweetonlyifhashtags(tweet): # if the tweet has one of the tags which allows retweeting it, send it hasonlyifhashtags = True # send the tweet if all checks are ok if hasnotretweethasthags and hasonlyifhashtags: #self.api.retweet(tweet) print("tweet {} sent!".format(tweet)) except (tweepy.error.TweepError) as err: print("{}".format(err)) print( "the tweet is probably retweeted already. Twitter does not allow to retweet 2 times" ) finally: # now store the tweet if not self.twp.wasposted(tweet): self.twp.storetweet(tweet) WaitAMoment(self.cfgvalues['waitminsecs'], self.cfgvalues['waitmaxsecs'])
def main(self): '''Main of the Validate class''' try: # test if it was retweeted enough to be retweeted by me if self.tweet.retweet_count >= self.cfgvalues['retweets']: print("IHT:", self.has_invalid_hashtags(), "VHT:", self.has_valid_hashtags(),"OLD:", self.is_old_enough(), "YNG:",self.is_young_enough(), "BL:", self.is_blacklisted()) # send the tweet if all checks are ok invalid = self.tweet.retweeted or self.tweet.in_reply_to_status_id or \ self.has_invalid_hashtags() or not self.has_valid_hashtags() \ or not self.is_old_enough() or not self.is_young_enough() \ or self.is_blacklisted() self.postit = not invalid if self.postit: if not self.args.dryrun: # at last retweet the tweet self.api.retweet(self.tweet.id) if self.cfgvalues['like']: self.api.create_favorite(self.tweet.id) print("tweet {} sent!".format(self.tweet.id)) except (tweepy.error.TweepError) as err: print("{}".format(err)) print("the tweet is probably retweeted already. Twitter does not allow to retweet 2 times") finally: # now set the tweet as processed if not self.args.dryrun: self.twp.process_tweet(self.tweet.id, self.postit) if self.postit: WaitAMoment(self.cfgvalues['waitminsecs'], self.cfgvalues['waitmaxsecs'])
def update_cache_table(self, max_tweets=300): last_id = self.twp.get_last_cached_id() if not last_id: print("No cache, first lookup") # empty cache, look for a big amount of results lasttweets = self.api.search(self.cfgvalues['search_query'], count=100) count = len(lasttweets) self.twp.cache_tweets(lasttweets) while len(lasttweets) == 100 and count < max_tweets: WaitAMoment(1, 1) #look for older than max_id max_id = lasttweets[len(lasttweets) - 1].id - 1 lasttweets = self.api.search(self.cfgvalues['search_query'], count=100, max_id=max_id) count += len(lasttweets) self.twp.cache_tweets(lasttweets) else: print("Updating table from ID %d", last_id) #look for newer than last_id lasttweets = self.api.search(self.cfgvalues['search_query'], count=100, since_id=last_id) count = len(lasttweets) self.twp.cache_tweets(lasttweets) while len(lasttweets) == 100 and count < max_tweets: WaitAMoment(1, 1) #look for newer than last_id AND older than max_id max_id = lasttweets[len(lasttweets) - 1].id - 1 lasttweets = self.api.search(self.cfgvalues['search_query'], count=100, max_id=max_id, since_id=last_id) count += len(lasttweets) self.twp.cache_tweets(lasttweets) print("Added %d entries to cache table" % count)