From f178e9065adfcb5649946ba5830f3b03602a4df6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Guimar=C3=A3es?= Date: Wed, 25 Jan 2017 18:19:58 -0200 Subject: [PATCH 1/2] Allow to follow users from a list with usernames --- README.md | 82 ++++++++++++++++++++---------------- TwitterFollowBot/__init__.py | 56 ++++++++++++++++++++---- 2 files changed, 94 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index 06ba3c2..299d7f3 100644 --- a/README.md +++ b/README.md @@ -54,14 +54,17 @@ Before running the bot, you must first set it up so it can connect to the Twitte ALREADY_FOLLOWED_FILE:already-followed.txt FOLLOWERS_FILE:followers.txt FOLLOWS_FILE:following.txt + USERS_TO_FOLLOW_FILE:users-to-follow.txt USERS_KEEP_FOLLOWING: USERS_KEEP_UNMUTED: USERS_KEEP_MUTED: FOLLOW_BACKOFF_MIN_SECONDS:10 FOLLOW_BACKOFF_MAX_SECONDS:60 - + `OAUTH_TOKEN`, `OAUTH_SECRET`, `CONSUMER_KEY`, `CONSUMER_SECRET` are your API keys that you received from creating your app account. `TWITTER_HANDLE` is your Twitter name, case-sensitive. +`USERS_TO_FOLLOW_FILE` is a file with one username per line of users thant you want to follow. + You can change the `FILE` entries if you want to store that information in a specific location on your computer. By default, the files will be created in your current directory. Add comma-separated Twitter user IDs to the `USERS_KEEP` entries to: @@ -79,7 +82,7 @@ For example: USERS_KEEP_FOLLOWING:1234,1235,1236 USERS_KEEP_UNMUTED: ... - + You can look up a users' Twitter ID [here](http://tweeterid.com/). ### Create an instance of the bot @@ -87,21 +90,21 @@ You can look up a users' Twitter ID [here](http://tweeterid.com/). To create an instance of the bot: from TwitterFollowBot import TwitterBot - + my_bot = TwitterBot() - + By default, the bot will look for a configuration file called `config.txt` in your current directory. - + If you want to use a different configuration file, pass the configuration file to the bot as follows: from TwitterFollowBot import TwitterBot - + my_bot = TwitterBot("my-other-bot-config.txt") - + Note that this allows you to run multiple instances of the bot with different configurations, for example if you run multiple Twitter accounts: from TwitterFollowBot import TwitterBot - + my_bot = TwitterBot() my_other_bot = TwitterBot("my-other-bot-config.txt") @@ -110,12 +113,12 @@ Note that this allows you to run multiple instances of the bot with different co Due to Twitter API rate limiting, the bot must maintain a local cache of all of your followers so it doesn't use all of your API time looking up your followers. It is highly recommended to sync the bot's local cache daily: from TwitterFollowBot import TwitterBot - + my_bot = TwitterBot() my_bot.sync_follows() - + The bot will create cache files where you specified in the configuration file. - + **DO NOT** delete the cache files ("followers.txt", "follows.txt", and "already-followed.txt" by default) unless you want to start the bot over with a fresh cache. ### Automating Twitter actions with the bot @@ -125,59 +128,66 @@ This bot has several functions for programmatically interacting with Twitter: #### Automatically follow any users that tweet something with a specific phrase from TwitterFollowBot import TwitterBot - + my_bot = TwitterBot() my_bot.auto_follow("phrase") - + You can also search based on hashtags: from TwitterFollowBot import TwitterBot - + my_bot = TwitterBot() my_bot.auto_follow("#hashtag") - + By default, the bot looks up the 100 most recent tweets. You can change this number with the `count` parameter: from TwitterFollowBot import TwitterBot - + my_bot = TwitterBot() my_bot.auto_follow("phrase", count=1000) - + #### Automatically follow any users that have followed you from TwitterFollowBot import TwitterBot - + my_bot = TwitterBot() my_bot.auto_follow_followers() #### Automatically follow any users that follow a user - + from TwitterFollowBot import TwitterBot - - my_bot = TwitterBot() + + my_bot = TwitterBot() my_bot.auto_follow_followers_of_user("jack", count=1000) +#### Automatically follow users from the user to follow list + + from TwitterFollowBot import TwitterBot + + my_bot = TwitterBot() + my_bot.follow_from_list() + #### Automatically favorite any tweets that have a specific phrase from TwitterFollowBot import TwitterBot - + my_bot = TwitterBot() my_bot.auto_fav("phrase", count=1000) - + #### Automatically retweet any tweets that have a specific phrase from TwitterFollowBot import TwitterBot - + my_bot = TwitterBot() my_bot.auto_rt("phrase", count=1000) #### Automatically unfollow any users that have not followed you back from TwitterFollowBot import TwitterBot - + my_bot = TwitterBot() my_bot.auto_unfollow_nonfollowers() - + If there are certain users that you would like to not unfollow, add their user id to the USERS_KEEP_FOLLOWING list. You will need to manually edit the code if you want to add special users that you will keep following even if they don't follow you back. @@ -185,16 +195,16 @@ You will need to manually edit the code if you want to add special users that yo #### Automatically unfollow all users. from TwitterFollowBot import TwitterBot - + my_bot = TwitterBot() my_bot.auto_unfollow_all_followers() - + #### Automatically mute all users that you have followed from TwitterFollowBot import TwitterBot - + my_bot = TwitterBot() my_bot.auto_mute_following() @@ -203,30 +213,30 @@ You will need to manually edit the code if you want to add special users that yo #### Automatically unmute everyone you have muted from TwitterFollowBot import TwitterBot - + my_bot = TwitterBot() my_bot.auto_unmute() - -You will need to manually edit the code if you want to add special users that will remain muted. + +You will need to manually edit the code if you want to add special users that will remain muted. #### Post a tweet on twitter from TwitterFollowBot import TwitterBot - + my_bot = TwitterBot() my_bot.send_tweet("Hello world!") #### Automatically add users tweeting about something to one of your list from TwitterFollowBot import TwitterBot - + my_bot = TwitterBot() my_bot.auto_add_to_list("#TwitterBot", "twitterbot-list", count=10) - + In the example above, the bot will try to add 10 users to the twitterbot-list that are tweeting #TwitterBot. Remember that the max number of users in a list is 5000. - + ## Have questions? Need help with the bot? If you're having issues with or have questions about the bot, [file an issue](https://github.com/rhiever/TwitterFollowBot/issues) in this repository so one of the project managers can get back to you. **Please [check the existing (and closed) issues](https://github.com/rhiever/TwitterFollowBot/issues?q=is%3Aissue) to make sure your issue hasn't already been addressed.** diff --git a/TwitterFollowBot/__init__.py b/TwitterFollowBot/__init__.py index 4f53f46..dcfa10d 100644 --- a/TwitterFollowBot/__init__.py +++ b/TwitterFollowBot/__init__.py @@ -218,6 +218,18 @@ def get_follows_list(self): return set(follows_list) + def get_users_to_follow_list(self): + """ + Return a list of users to follow. + """ + + users_to_follow_list = [] + with open(self.BOT_CONFIG["USERS_TO_FOLLOW_FILE"], "r") as in_file: + for line in in_file: + users_to_follow_list.append(line.strip()) + + return set(users_to_follow_list) + def search_tweets(self, phrase, count=100, result_type="recent"): """ Returns a list of tweets matching a phrase (hashtag, word, etc.). @@ -237,9 +249,9 @@ def auto_fav(self, phrase, count=100, result_type="recent"): # don't favorite your own tweets if tweet["user"]["screen_name"] == self.BOT_CONFIG["TWITTER_HANDLE"]: continue - + self.wait_on_action() - + result = self.TWITTER_CONNECTION.favorites.create(_id=tweet["id"]) print("Favorited: %s" % (result["text"].encode("utf-8")), file=sys.stdout) @@ -266,9 +278,9 @@ def auto_rt(self, phrase, count=100, result_type="recent"): # don't retweet your own tweets if tweet["user"]["screen_name"] == self.BOT_CONFIG["TWITTER_HANDLE"]: continue - + self.wait_on_action() - + result = self.TWITTER_CONNECTION.statuses.retweet(id=tweet["id"]) print("Retweeted: %s" % (result["text"].encode("utf-8")), file=sys.stdout) @@ -377,6 +389,34 @@ def auto_follow_followers_of_user(self, user_twitter_handle, count=100): if "already requested to follow" not in str(api_error).lower(): print("Error: %s" % (str(api_error)), file=sys.stderr) + def follow_from_list(self, count=None): + """ + Follow followers from a list. + """ + + users_to_follow = self.get_users_to_follow_list() + + for username in users_to_follow: + try: + self.wait_on_action() + + self.TWITTER_CONNECTION.friendships.create(screen_name=username, follow=False) + print("Followed %s" % username, file=sys.stdout) + + except TwitterHTTPError as api_error: + # quit on rate limit errors + if "unable to follow more people at this time" in str(api_error).lower(): + print("You are unable to follow more people at this time. " + "Wait a while before running the bot again or gain " + "more followers.", file=sys.stderr) + return + + # don't print "already requested to follow" errors - they're + # frequent + if "already requested to follow" not in str(api_error).lower(): + print("Error: %s" % (str(api_error)), file=sys.stderr) + + def auto_unfollow_nonfollowers(self,count=None): """ Unfollows everyone who hasn't followed you back. @@ -455,19 +495,19 @@ 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"]) From 2cf378e921b864564ed4285a713d16a9301174d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Guimar=C3=A3es?= Date: Wed, 25 Jan 2017 18:27:23 -0200 Subject: [PATCH 2/2] Update README --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 299d7f3..6d07d4f 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,7 @@ Before running the bot, you must first set it up so it can connect to the Twitte `OAUTH_TOKEN`, `OAUTH_SECRET`, `CONSUMER_KEY`, `CONSUMER_SECRET` are your API keys that you received from creating your app account. `TWITTER_HANDLE` is your Twitter name, case-sensitive. -`USERS_TO_FOLLOW_FILE` is a file with one username per line of users thant you want to follow. +`USERS_TO_FOLLOW_FILE` is a file with usernames that you want to follow. You can change the `FILE` entries if you want to store that information in a specific location on your computer. By default, the files will be created in your current directory. @@ -160,7 +160,7 @@ By default, the bot looks up the 100 most recent tweets. You can change this num my_bot = TwitterBot() my_bot.auto_follow_followers_of_user("jack", count=1000) -#### Automatically follow users from the user to follow list +#### Automatically follow users from a username list from TwitterFollowBot import TwitterBot