Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to follow users from a list with usernames #124

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 46 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 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.

Add comma-separated Twitter user IDs to the `USERS_KEEP` entries to:
Expand All @@ -79,29 +82,29 @@ 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

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")

Expand All @@ -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
Expand All @@ -125,76 +128,83 @@ 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 a username 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.

#### 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()

Expand All @@ -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.**
56 changes: 48 additions & 8 deletions TwitterFollowBot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.).
Expand All @@ -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)

Expand All @@ -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)

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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"])
Expand Down