From 45919220fae0ade2f8e8afe162c34ba576ed744f Mon Sep 17 00:00:00 2001 From: Tyler Darby Date: Sun, 13 Sep 2015 14:29:17 -0400 Subject: [PATCH 1/6] Added support for file-based lists for 'keep following', 'keep muted', and 'keep unmuted'. --- TwitterFollowBot/__init__.py | 80 +++++++++++++++++++++--------------- config.txt | 6 +-- 2 files changed, 49 insertions(+), 37 deletions(-) diff --git a/TwitterFollowBot/__init__.py b/TwitterFollowBot/__init__.py index 2134708..c25c6ec 100644 --- a/TwitterFollowBot/__init__.py +++ b/TwitterFollowBot/__init__.py @@ -81,12 +81,7 @@ def bot_setup(self, config_file="config.txt"): parameter = line[0].strip() value = line[1].strip() - if parameter in ["USERS_KEEP_FOLLOWING", "USERS_KEEP_UNMUTED", "USERS_KEEP_MUTED"]: - if value != "": - self.BOT_CONFIG[parameter] = set([int(x) for x in value.split(",")]) - else: - self.BOT_CONFIG[parameter] = set() - elif parameter in ["FOLLOW_BACKOFF_MIN_SECONDS", "FOLLOW_BACKOFF_MAX_SECONDS"]: + if parameter in ["FOLLOW_BACKOFF_MIN_SECONDS", "FOLLOW_BACKOFF_MAX_SECONDS"]: self.BOT_CONFIG[parameter] = int(value) else: self.BOT_CONFIG[parameter] = value @@ -218,6 +213,42 @@ def get_follows_list(self): return set(follows_list) + def get_keep_following_list(self): + """ + Returns the set of users that you do not want to unfollow. + """ + + keep_following_list = [] + with open(self.BOT_CONFIG['USERS_KEEP_FOLLOWING_FILE'], "r") as in_file: + for line in in_file: + keep_following_list.append(int(line)) + + return set(keep_following_list) + + def get_keep_muted_list(self): + """ + Returns the set of users that you do not want to unmute. + """ + + keep_muted_list = [] + with open(self.BOT_CONFIG['USERS_KEEP_MUTED_FILE'], "r") as in_file: + for line in in_file: + keep_MUTED_list.append(int(line)) + + return set(keep_muted_list) + + def get_keep_unmuted_list(self): + """ + Returns the set of users that you do not want to mute. + """ + + keep_unmuted_list = [] + with open(self.BOT_CONFIG['USERS_KEEP_UNMUTED_FILE'], "r") as in_file: + for line in in_file: + keep_following_list.append(int(line)) + + return set(keep_unmuted_list) + def search_tweets(self, phrase, count=100, result_type="recent"): """ Returns a list of tweets matching a phrase (hashtag, word, etc.). @@ -380,8 +411,9 @@ def auto_unfollow_nonfollowers(self,count=None): following = self.get_follows_list() followers = self.get_followers_list() + keep_following = self.get_keep_following_list() - not_following_back = following - followers + not_following_back = following - followers - keep_following not_following_back = list(not_following_back)[:count] # update the "already followed" file with users who didn't follow back already_followed = set(not_following_back) @@ -397,12 +429,10 @@ def auto_unfollow_nonfollowers(self,count=None): out_file.write(str(val) + "\n") for user_id in not_following_back: - if user_id not in self.BOT_CONFIG["USERS_KEEP_FOLLOWING"]: - - self.wait_on_action() + self.wait_on_action() - self.TWITTER_CONNECTION.friendships.destroy(user_id=user_id) - print("Unfollowed %d" % (user_id), file=sys.stdout) + self.TWITTER_CONNECTION.friendships.destroy(user_id=user_id) + print("Unfollowed %d" % (user_id), file=sys.stdout) def auto_unfollow_all_followers(self,count=None): """ @@ -411,7 +441,7 @@ def auto_unfollow_all_followers(self,count=None): following = self.get_follows_list() for user_id in following: - if user_id not in self.BOT_CONFIG["USERS_KEEP_FOLLOWING"]: + if user_id not in self.get_keep_following_list(): self.wait_on_action() @@ -424,12 +454,13 @@ def auto_mute_following(self): """ following = self.get_follows_list() + muted = set(self.TWITTER_CONNECTION.mutes.users.ids(screen_name=self.BOT_CONFIG["TWITTER_HANDLE"])["ids"]) not_muted = following - muted for user_id in not_muted: - if user_id not in self.BOT_CONFIG["USERS_KEEP_UNMUTED"]: + if user_id not in self.get_keep_unmuted_list(): self.TWITTER_CONNECTION.mutes.users.create(user_id=user_id) print("Muted %d" % (user_id), file=sys.stdout) @@ -441,7 +472,7 @@ def auto_unmute(self): muted = set(self.TWITTER_CONNECTION.mutes.users.ids(screen_name=self.BOT_CONFIG["TWITTER_HANDLE"])["ids"]) for user_id in muted: - if user_id not in self.BOT_CONFIG["USERS_KEEP_MUTED"]: + if user_id not in self.get_keep_muted_list(): self.TWITTER_CONNECTION.mutes.users.destroy(user_id=user_id) print("Unmuted %d" % (user_id), file=sys.stdout) @@ -451,22 +482,3 @@ def send_tweet(self, message): """ return self.TWITTER_CONNECTION.statuses.update(status=message) - - def auto_add_to_list(self, phrase, list_slug, count=100, result_type="recent"): - """ - Add users to list slug that are tweeting phrase. - """ - - result = self.search_tweets(phrase, count, result_type) - - for tweet in result["statuses"]: - try: - if tweet["user"]["screen_name"] == self.BOT_CONFIG["TWITTER_HANDLE"]: - continue - - result = self.TWITTER_CONNECTION.lists.members.create(owner_screen_name=self.BOT_CONFIG["TWITTER_HANDLE"], - slug=list_slug, - screen_name=tweet["user"]["screen_name"]) - print("User %s added to the list %s" % (tweet["user"]["screen_name"], list_slug), file=sys.stdout) - except TwitterHTTPError as api_error: - print(api_error) diff --git a/config.txt b/config.txt index 36c4aa5..fa3f35e 100644 --- a/config.txt +++ b/config.txt @@ -6,8 +6,8 @@ TWITTER_HANDLE: ALREADY_FOLLOWED_FILE:already-followed.txt FOLLOWERS_FILE:followers.txt FOLLOWS_FILE:following.txt -USERS_KEEP_FOLLOWING: -USERS_KEEP_UNMUTED: -USERS_KEEP_MUTED: +USERS_KEEP_FOLLOWING_FILE: +USERS_KEEP_UNMUTED_FILE: +USERS_KEEP_MUTED_FILE: FOLLOW_BACKOFF_MIN_SECONDS:10 FOLLOW_BACKOFF_MAX_SECONDS:60 \ No newline at end of file From 727bb93f90266f42597c4e617562d8abf7c118c3 Mon Sep 17 00:00:00 2001 From: Tyler Darby Date: Sun, 13 Sep 2015 14:33:28 -0400 Subject: [PATCH 2/6] Fixed accidental function deletion. --- TwitterFollowBot/__init__.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/TwitterFollowBot/__init__.py b/TwitterFollowBot/__init__.py index c25c6ec..36f62ff 100644 --- a/TwitterFollowBot/__init__.py +++ b/TwitterFollowBot/__init__.py @@ -482,3 +482,22 @@ def send_tweet(self, message): """ return self.TWITTER_CONNECTION.statuses.update(status=message) + + def auto_add_to_list(self, phrase, list_slug, count=100, result_type="recent"): + """ + Add users to list slug that are tweeting phrase. + """ + + result = self.search_tweets(phrase, count, result_type) + + for tweet in result["statuses"]: + try: + if tweet["user"]["screen_name"] == self.BOT_CONFIG["TWITTER_HANDLE"]: + continue + + result = self.TWITTER_CONNECTION.lists.members.create(owner_screen_name=self.BOT_CONFIG["TWITTER_HANDLE"], + slug=list_slug, + screen_name=tweet["user"]["screen_name"]) + print("User %s added to the list %s" % (tweet["user"]["screen_name"], list_slug), file=sys.stdout) + except TwitterHTTPError as api_error: + print(api_error) From 38b73141f21fd0c172c00b3432fccfcd1ba4dd79 Mon Sep 17 00:00:00 2001 From: Tyler Darby Date: Sun, 13 Sep 2015 14:41:23 -0400 Subject: [PATCH 3/6] Added the actual files to the config, forgot about that. --- config.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/config.txt b/config.txt index fa3f35e..eb60599 100644 --- a/config.txt +++ b/config.txt @@ -6,8 +6,8 @@ TWITTER_HANDLE: ALREADY_FOLLOWED_FILE:already-followed.txt FOLLOWERS_FILE:followers.txt FOLLOWS_FILE:following.txt -USERS_KEEP_FOLLOWING_FILE: -USERS_KEEP_UNMUTED_FILE: -USERS_KEEP_MUTED_FILE: +USERS_KEEP_FOLLOWING_FILE:keep-following.txt +USERS_KEEP_UNMUTED_FILE:keep-unmuted.txt +USERS_KEEP_MUTED_FILE:keep-muted.txt FOLLOW_BACKOFF_MIN_SECONDS:10 FOLLOW_BACKOFF_MAX_SECONDS:60 \ No newline at end of file From 531b98fc32ce7e140ad7e97c51d740e1a0a8b93f Mon Sep 17 00:00:00 2001 From: Tyler Darby Date: Sun, 13 Sep 2015 21:48:39 -0400 Subject: [PATCH 4/6] Added a counter for the auto following feature. --- TwitterFollowBot/__init__.py | 22 +++------------------- 1 file changed, 3 insertions(+), 19 deletions(-) diff --git a/TwitterFollowBot/__init__.py b/TwitterFollowBot/__init__.py index 36f62ff..56b4bfb 100644 --- a/TwitterFollowBot/__init__.py +++ b/TwitterFollowBot/__init__.py @@ -317,6 +317,7 @@ def auto_follow(self, phrase, count=100, result_type="recent"): result = self.search_tweets(phrase, count, result_type) following = self.get_follows_list() do_not_follow = self.get_do_not_follow_list() + followed_num = 1 for tweet in result["statuses"]: try: @@ -329,8 +330,10 @@ def auto_follow(self, phrase, count=100, result_type="recent"): self.TWITTER_CONNECTION.friendships.create(user_id=tweet["user"]["id"], follow=False) following.update(set([tweet["user"]["id"]])) + followed_num = followed_num + 1 print("Followed %s" % (tweet["user"]["screen_name"]), file=sys.stdout) + print("Now on number " + str(followed_num) + " of " + str(count)) except TwitterHTTPError as api_error: # quit on rate limit errors @@ -482,22 +485,3 @@ def send_tweet(self, message): """ return self.TWITTER_CONNECTION.statuses.update(status=message) - - def auto_add_to_list(self, phrase, list_slug, count=100, result_type="recent"): - """ - Add users to list slug that are tweeting phrase. - """ - - result = self.search_tweets(phrase, count, result_type) - - for tweet in result["statuses"]: - try: - if tweet["user"]["screen_name"] == self.BOT_CONFIG["TWITTER_HANDLE"]: - continue - - result = self.TWITTER_CONNECTION.lists.members.create(owner_screen_name=self.BOT_CONFIG["TWITTER_HANDLE"], - slug=list_slug, - screen_name=tweet["user"]["screen_name"]) - print("User %s added to the list %s" % (tweet["user"]["screen_name"], list_slug), file=sys.stdout) - except TwitterHTTPError as api_error: - print(api_error) From 1e9b7c09e7085398d207757f60fe1cc229db6a86 Mon Sep 17 00:00:00 2001 From: Tyler Darby Date: Sun, 13 Sep 2015 21:50:46 -0400 Subject: [PATCH 5/6] Added that function back again. I'm dumb. --- TwitterFollowBot/__init__.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/TwitterFollowBot/__init__.py b/TwitterFollowBot/__init__.py index 56b4bfb..8986991 100644 --- a/TwitterFollowBot/__init__.py +++ b/TwitterFollowBot/__init__.py @@ -485,3 +485,23 @@ def send_tweet(self, message): """ return self.TWITTER_CONNECTION.statuses.update(status=message) + + def auto_add_to_list(self, phrase, list_slug, count=100, result_type="recent"): + """ + Add users to list slug that are tweeting phrase. + """ + + result = self.search_tweets(phrase, count, result_type) + + for tweet in result["statuses"]: + try: + if tweet["user"]["screen_name"] == self.BOT_CONFIG["TWITTER_HANDLE"]: + continue + + result = self.TWITTER_CONNECTION.lists.members.create(owner_screen_name=self.BOT_CONFIG["TWITTER_HANDLE"], + slug=list_slug, + screen_name=tweet["user"]["screen_name"]) + print("User %s added to the list %s" % (tweet["user"]["screen_name"], list_slug), file=sys.stdout) + except TwitterHTTPError as api_error: + print(api_error) + From 90f6ae3aa221b3d5a4b3a0dd84b0e05cd673c4aa Mon Sep 17 00:00:00 2001 From: Tyler Darby Date: Sun, 13 Sep 2015 22:05:54 -0400 Subject: [PATCH 6/6] Fixed typo. --- TwitterFollowBot/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TwitterFollowBot/__init__.py b/TwitterFollowBot/__init__.py index 8986991..b83dd9b 100644 --- a/TwitterFollowBot/__init__.py +++ b/TwitterFollowBot/__init__.py @@ -245,7 +245,7 @@ def get_keep_unmuted_list(self): keep_unmuted_list = [] with open(self.BOT_CONFIG['USERS_KEEP_UNMUTED_FILE'], "r") as in_file: for line in in_file: - keep_following_list.append(int(line)) + keep_unmuted_list.append(int(line)) return set(keep_unmuted_list)