├── README.md ├── auto.py └── main.py /README.md: -------------------------------------------------------------------------------- 1 | #Twitter Follow Bot 2 | 3 | A Python bot that can automatically follow users and favorite tweets associated with a specific search query on Twitter. Also has the ability to unfollow all users not currently following you back. 4 | 5 | ##Algorithm 6 | It only follows user who have follower count > 2000 & following count > 2000 and the ratio of followers to following is greater than 0.6 7 | 8 | I have found these modifications very helpful. 9 | 10 | ##Dependencies 11 | 12 | You will need to install Python's `twitter` library first: 13 | 14 | easy_install twitter 15 | 16 | You will also need to create an app account on https://dev.twitter.com/apps 17 | 18 | 1. Sign in with your Twitter account 19 | 2. Create a new app account 20 | 3. Modify the settings for that app account to allow read & write 21 | 4. Generate a new OAuth token with those permissions 22 | 5. Manually edit this script and put those tokens in the script 23 | 24 | ##Usage 25 | 26 | Currently, the bot has three functions: 27 | 28 | ####Automatically follow any users that tweet something with a specific phrase 29 | 30 | from twitter_follow_bot import auto_follow 31 | 32 | auto_follow("phrase") 33 | 34 | You can also search based on hashtags. 35 | 36 | By default, the bot looks up the 100 most recent tweets. You can change this number with the `count` parameter: 37 | 38 | from twitter_follow_bot import auto_follow 39 | 40 | auto_follow("phrase", count=1000) 41 | 42 | ####Automatically follow any users that have followed you 43 | 44 | from twitter_follow_bot import auto_follow_followers 45 | 46 | auto_follow_followers() 47 | 48 | ####Automatically favorite any tweets that have a specific phrase 49 | 50 | from twitter_follow_bot import auto_fav 51 | 52 | auto_fav("phrase", count=1000) 53 | 54 | ####Automatically unfollow any users that have not followed you back (with exceptions that you can set) 55 | 56 | from twitter_follow_bot import auto_unfollow_nonfollowers 57 | 58 | auto_unfollow_nonfollowers() 59 | 60 | 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. 61 | 62 | Inspired by https://github.com/rhiever/twitter-follow-bot 63 | -------------------------------------------------------------------------------- /auto.py: -------------------------------------------------------------------------------- 1 | from twitter import Twitter, OAuth, TwitterHTTPError 2 | 3 | # put your twitter tokens, keys, secrets, and twitter handle in the following variables 4 | OAUTH_TOKEN = "" 5 | OAUTH_SECRET = "" 6 | CONSUMER_KEY = "" 7 | CONSUMER_SECRET = "" 8 | TWITTER_HANDLE = "" 9 | 10 | t = Twitter(auth=OAuth(OAUTH_TOKEN, OAUTH_SECRET, 11 | CONSUMER_KEY, CONSUMER_SECRET)) 12 | 13 | 14 | def search_tweets(q, count=10, result_type="recent"): 15 | """ 16 | Returns a list of tweets matching a certain phrase (hashtag, word, etc.) 17 | """ 18 | 19 | return t.search.tweets(q=q, result_type=result_type, count=count) 20 | 21 | 22 | def auto_fav(q, count=1, result_type="recent"): 23 | """ 24 | Favorites tweets that match a certain phrase (hashtag, word, etc.) 25 | """ 26 | 27 | result = search_tweets(q, count, result_type) 28 | 29 | for tweet in result['statuses']: 30 | try: 31 | # don't favorite your own tweets 32 | if tweet['user']['screen_name'] == TWITTER_HANDLE: 33 | continue 34 | 35 | result = t.favorites.create(_id=tweet['id']) 36 | print "favorited: %s" % (result['text']).encode('utf-8') 37 | 38 | # when you have already favorited a tweet this error is thrown 39 | except TwitterHTTPError as e: 40 | print "error: ", e 41 | 42 | 43 | def auto_follow(q, count=10, result_type="recent"): 44 | """ 45 | Follows anyone who tweets about a specific phrase (hashtag, word, etc.) 46 | """ 47 | 48 | result = search_tweets(q, count, result_type) 49 | following = set(t.friends.ids(screen_name=TWITTER_HANDLE)["ids"]) 50 | 51 | for tweet in result['statuses']: 52 | try: 53 | if tweet['user']['screen_name'] != TWITTER_HANDLE and tweet['user']['id'] not in following: 54 | if tweet['user']['followers_count'] > 2000 and tweet['user']['friends_count'] > 2000 : 55 | ratio = tweet['user']['friends_count']/tweet['user']['followers_count'] 56 | if ratio > 0.6: 57 | t.friendships.create(user_id=tweet['user']['id'], follow=True) 58 | following.update(set([tweet['user']['id']])) 59 | print "followed " + tweet['user']['screen_name'] 60 | 61 | except TwitterHTTPError as e: 62 | print "error: ", e 63 | 64 | # quit on error unless it's because someone blocked me 65 | if "blocked" not in str(e).lower(): 66 | quit() 67 | 68 | 69 | def auto_follow_followers(): 70 | """ 71 | Follows back everyone who's followed you 72 | """ 73 | 74 | following = set(t.friends.ids(screen_name=TWITTER_HANDLE)["ids"]) 75 | followers = set(t.followers.ids(screen_name=TWITTER_HANDLE)["ids"]) 76 | 77 | not_following_back = followers - following 78 | 79 | for user_id in not_following_back: 80 | try: 81 | t.friendships.create(user_id=user_id, follow=True) 82 | except Exception as e: 83 | print e 84 | 85 | 86 | def auto_unfollow_nonfollowers(): 87 | """ 88 | Unfollows everyone who hasn't followed you back 89 | """ 90 | 91 | following = set(t.friends.ids(screen_name=TWITTER_HANDLE)["ids"]) 92 | followers = set(t.followers.ids(screen_name=TWITTER_HANDLE)["ids"]) 93 | 94 | # put user IDs here that you want to keep following even if they don't 95 | # follow you back 96 | users_keep_following = set([]) 97 | 98 | not_following_back = following - followers 99 | 100 | for userid in not_following_back: 101 | if userid not in users_keep_following: 102 | t.friendships.destroy(user_id=userid) 103 | 104 | 105 | 106 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | from auto import auto_follow 2 | from auto import auto_fav 3 | 4 | #this is a sample program to use the auto.py script 5 | auto_fav("ubuntu", count=1) 6 | auto_fav("teamfollow", count=1) 7 | auto_fav("teamfollowback", count=0) 8 | 9 | #auto follow 10 users who come when we search for keyword "followback" 10 | auto_follow("followback", count=10) 11 | auto_follow("teamfollow", count=5) 12 | 13 | --------------------------------------------------------------------------------