def follower_counter(self, current_follower): db = Database() db.connect_db(self.db_name) db.select_col('follower_counter') follower_status = db.find_last_object() previous_follower = follower_status['previous_follower'] diff_follower = current_follower - previous_follower db.insert_object({ 'previous_follower': current_follower, 'current_follower': current_follower + diff_follower, 'diff_follower': diff_follower })
def main(ck, cs, at, ats): bot = Twitter(ck, cs, at, ats) db = Database() db.connect_db(db_name) db.select_col('tweet') minute_wait = 5 while True: l = db.find_last_object() last_id = l['tweet_last_id'] since_id = bot.get_mention_tweet(last_id) me = bot.api.me() my_current_followers = me.followers_count bot.follower_counter(my_current_followers) t = time.localtime() current_time = time.strftime("%H:%M:%S %D", t) print( "\n" + u"\u250C" + "------------------------------------------------", "\n| current time: ", current_time, "\n| newest tweet: ", since_id, "\n| oldest tweet: ", last_id, "\n| current followers: ", me.followers_count, "\n| total tweets: ", me.statuses_count, "\n" + u"\u2514" + "------------------------------------------------") if (last_id != since_id): db.insert_object({'tweet_last_id': since_id}) else: print('no new mention') for sec in range(minute_wait * 60, 0, -1): sys.stdout.write("\r") sys.stdout.write("{:2d} second to check mention.\r".format(sec)) sys.stdout.flush() time.sleep(1)
def get_mention_tweet(self, since_id): new_since_id = since_id db = Database() db.connect_db(self.db_name) db.select_col('tweet') list_tweet = [] for tweet in tweepy.Cursor(self.api.mentions_timeline, since_id=since_id, tweet_mode="extended").items(limit=10): new_since_id = max(tweet.id, new_since_id) words = tweet.full_text.lower().split() if self.is_mentioned(tweet) == self.me.screen_name: criteria = self.get_criteria(tweet) if criteria[0]: if since_id != tweet.id: for tw in self.triggering_words: if tw in words: list_tweet.append(tweet) print('tweet id: {} added to next process'.format( tweet.id)) else: print('tweet id: {} skipped, reason: {}'.format( tweet.id, criteria[1])) elif self.is_mentioned(tweet) != self.me.screen_name: print("skipped: not mentioned") continue self.process_mention(list_tweet) return new_since_id
def process_mention(self, list_tweet): db = Database() db.connect_db(self.db_name) db.select_col('tweet') for tweet in reversed(list_tweet): self.show_status(tweet) words = tweet.full_text.lower().split() if int(self.important_ids["developer_id"] ) == tweet.in_reply_to_user_id: for tw in self.triggering_words: if tw in words: self.tweeted_and_show(self.tweet_text["dont_mock"][0], tweet) elif int(self.important_ids["bot_id"] ) == tweet.in_reply_to_user_id or int( self.important_ids["bot_test_id"] ) == tweet.in_reply_to_user_id: print("skipped: mocked itself") continue else: for tw in self.triggering_words: try: if tw is "pliisi" in words: self.mock_in_pliisi(tweet, db) elif tw is "please" in words: self.mock_in_please(tweet, db) elif tw is "pleasek" in words: self.mock_in_emoji_pattern(tweet, 'k', db) elif tw is "pleaseb" in words: self.mock_in_emoji_pattern(tweet, 'b', db) elif tw is "pleasej" in words: self.mock_in_emoji_pattern(tweet, 'j', db) elif tw == "please😂" in words: self.mock_in_emoji(tweet, "laugh", db) elif tw == "please👏" in words: self.mock_in_emoji(tweet, "clap", db) elif tw == "please🤮" in words: self.mock_in_emoji(tweet, "vomit", db) elif tw == "please🤢" in words: self.mock_in_emoji(tweet, "sick", db) elif tw == "please💩" in words: self.mock_in_emoji(tweet, "poop", db) elif tw == "pleasealay" in words: self.mock_in_alay(tweet, db) elif tw == "pleaseud" in words: self.mock_in_upsidedown(tweet, db) except tweepy.TweepError as e: error = e.api_code error = str(error) error_text = e.response db.insert_object({'tweet_last_id': tweet.id}) if error == self.error_code['private_account'][0]: tweet_err = self.error_code['private_account'][1] self.api.update_status( status=tweet_err, in_reply_to_status_id=tweet.id, auto_populate_reply_metadata=True) self.show_what_tweeted(tweet_err) elif error == self.error_code['blocked_account'][0]: tweet_err = self.error_code['blocked_account'][1] target = self.api.get_user( id=tweet.in_reply_to_user_id) tweet_err = tweet_err.format(target.screen_name) self.api.update_status( status=tweet_err, in_reply_to_status_id=tweet.id, auto_populate_reply_metadata=True) self.show_what_tweeted(tweet_err) elif error == self.error_code['tweet_target_deleted'][ 0]: tweet_err = self.error_code[ 'tweet_target_deleted'][1] self.api.update_status( status=tweet_err, in_reply_to_status_id=tweet.id, auto_populate_reply_metadata=True) self.show_what_tweeted(tweet_err) elif error == self.error_code['tweet_target_to_long'][ 0]: tweet_err = self.error_code[ 'tweet_target_to_long'][1] self.api.update_status( status=tweet_err, in_reply_to_status_id=tweet.id, auto_populate_reply_metadata=True) self.show_what_tweeted(tweet_err) elif error == self.error_code['twitter_over_capacity'][ 0]: tweet_err = self.error_code[ 'twitter_over_capacity'][1] self.api.update_status( status=tweet_err, in_reply_to_status_id=tweet.id, auto_populate_reply_metadata=True) self.show_what_tweeted(tweet_err) elif error == self.error_code['suspended_account'][0]: tweet_err = self.error_code['suspended_account'][1] self.api.update_status( status=tweet_err, in_reply_to_status_id=tweet.id, auto_populate_reply_metadata=True) self.show_what_tweeted(tweet_err) elif error == self.error_code['duplicate_tweet'][0]: tweet_err = self.error_code['duplicate_tweet'][1] self.show_what_tweeted(tweet_err) continue elif error == self.error_code[ 'tweet_deleted_or_not_visible'][0]: tweet_err = self.error_code[ 'tweet_deleted_or_not_visible'][1] self.show_what_tweeted(tweet_err) elif error == self.error_code['page_does_not_exist'][ 0]: tweet_err = self.error_code['page_does_not_exist'][ 1] self.show_what_tweeted(tweet_err) else: t = time.localtime() current_time = time.strftime("%H:%M:%S %D", t) db.select_col('tweet_error') db.insert_object({ 'error_code': str(error), 'timestamp': current_time, 'tweet_id': tweet.id, 'username': tweet.user.screen_name, 'tweet_text': tweet.full_text, 'error_text': error_text }) time.sleep(self.time_interval) continue
import sys import time import os from bot import Twitter from db_mongo import Database db_name = os.environ.get("DB_MOCKTHISTWEET") db = Database() db.connect_db(db_name) db.select_col('environment') consumer_key = db.find_object('consumer_key') consumer_secret = db.find_object('consumer_secret') access_token = db.find_object('access_token') access_token_secret = db.find_object('access_token_secret') def main(ck, cs, at, ats): bot = Twitter(ck, cs, at, ats) db = Database() db.connect_db(db_name) db.select_col('tweet') minute_wait = 5 while True: l = db.find_last_object() last_id = l['tweet_last_id'] since_id = bot.get_mention_tweet(last_id)