├── already-followed.csv ├── example_twitter_info.py ├── sample_twitter_codes.py └── twitter_follow_bot.py /already-followed.csv: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProgrammingforMarketers/grow-twitter-following/bc62a6fb42056eea9e1e9d08742e8cabfbec1b09/already-followed.csv -------------------------------------------------------------------------------- /example_twitter_info.py: -------------------------------------------------------------------------------- 1 | OAUTH_TOKEN = "token here" 2 | OAUTH_SECRET = "secret here" 3 | CONSUMER_KEY = "key here" 4 | CONSUMER_SECRET = "secret here" 5 | TWITTER_HANDLE = "handle here" -------------------------------------------------------------------------------- /sample_twitter_codes.py: -------------------------------------------------------------------------------- 1 | '''' 2 | In this code we'll have a bunch of examples you can use at your own discretion. 3 | 4 | Simply remove the three ' marks above and below the code you want in order to run it, while 5 | leaving the text within a new set of three ' marks. 6 | 7 | Once that's done, go to your Terminal, navigate to where this code and the twitter_follow_bot 8 | code is (they have to be in the same folder), and just type in "python sample_twitter_codes.py" (without quotes) 9 | 10 | WARNING: Following too many people, favoriting too many things, CAN and WILL get you banned. 11 | 12 | Be smart. And have fun :). 13 | 14 | Justin and Nat 15 | ''' 16 | 17 | ''' 18 | #1 Here you can automatically follow people who tweet about a certain phrase. Just replace the phrase 19 | with something relevant to you! Also you can set the count to whatever makes you most comfortable. 20 | 21 | from twitter_follow_bot import auto_follow 22 | auto_follow("phrase", count=100) 23 | ''' 24 | 25 | ''' 26 | #2 In this code, change "jwmares" to the twitter handle whose followers you want to follow, 27 | and set the count to how many people should be followed. Default is 100. 28 | 29 | from twitter_follow_bot import auto_follow_followers_for_user 30 | auto_follow_followers_for_user("jwmares", count=10) 31 | ''' 32 | 33 | ''' 34 | #3 This code will let you favoite things that are relevant to you. Just replace "phrase" with the phrase 35 | you want to favorite for, and set the count to how many things you want to favorite. 36 | 37 | from twitter_follow_bot import auto_fav 38 | auto_fav("phrase", count=100) 39 | ''' 40 | 41 | ''' 42 | #4 This code will automatically un-follow everyone who hasn't followed you back. 43 | 44 | from twitter_follow_bot import auto_unfollow_nonfollowers 45 | auto_unfollow_nonfollowers() 46 | ''' -------------------------------------------------------------------------------- /twitter_follow_bot.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | """ 4 | Copyright 2014 Randal S. Olson 5 | 6 | This file is part of the Twitter Follow Bot library. 7 | 8 | The Twitter Follow Bot library is free software: you can redistribute it and/or 9 | modify it under the terms of the GNU General Public License as published by the 10 | Free Software Foundation, either version 3 of the License, or (at your option) any 11 | later version. 12 | 13 | The Twitter Follow Bot library is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License along with the Twitter 18 | Follow Bot library. If not, see http://www.gnu.org/licenses/. 19 | 20 | Code only slightly modified by Programming for Marketers to allow for separate variable 21 | storage. 22 | """ 23 | 24 | from twitter import Twitter, OAuth, TwitterHTTPError 25 | import os 26 | from twitter_info import * 27 | 28 | # put the full path and file name of the file you want to store your "already followed" 29 | # list in 30 | ALREADY_FOLLOWED_FILE = "already-followed.csv" 31 | 32 | t = Twitter(auth=OAuth(OAUTH_TOKEN, OAUTH_SECRET, 33 | CONSUMER_KEY, CONSUMER_SECRET)) 34 | 35 | 36 | def search_tweets(q, count=100, result_type="recent"): 37 | """ 38 | Returns a list of tweets matching a certain phrase (hashtag, word, etc.) 39 | """ 40 | 41 | return t.search.tweets(q=q, result_type=result_type, count=count) 42 | 43 | 44 | def auto_fav(q, count=100, result_type="recent"): 45 | """ 46 | Favorites tweets that match a certain phrase (hashtag, word, etc.) 47 | """ 48 | 49 | result = search_tweets(q, count, result_type) 50 | 51 | for tweet in result["statuses"]: 52 | try: 53 | # don't favorite your own tweets 54 | if tweet["user"]["screen_name"] == TWITTER_HANDLE: 55 | continue 56 | 57 | result = t.favorites.create(_id=tweet["id"]) 58 | print("favorited: %s" % (result["text"].encode("utf-8"))) 59 | 60 | # when you have already favorited a tweet, this error is thrown 61 | except TwitterHTTPError as e: 62 | print("error: %s" % (str(e))) 63 | 64 | 65 | def auto_rt(q, count=100, result_type="recent"): 66 | """ 67 | Retweets tweets that match a certain phrase (hashtag, word, etc.) 68 | """ 69 | 70 | result = search_tweets(q, count, result_type) 71 | 72 | for tweet in result["statuses"]: 73 | try: 74 | # don't retweet your own tweets 75 | if tweet["user"]["screen_name"] == TWITTER_HANDLE: 76 | continue 77 | 78 | result = t.statuses.retweet(id=tweet["id"]) 79 | print("retweeted: %s" % (result["text"].encode("utf-8"))) 80 | 81 | # when you have already retweeted a tweet, this error is thrown 82 | except TwitterHTTPError as e: 83 | print("error: %s" % (str(e))) 84 | 85 | 86 | def get_do_not_follow_list(): 87 | """ 88 | Returns list of users the bot has already followed. 89 | """ 90 | 91 | # make sure the "already followed" file exists 92 | if not os.path.isfile(ALREADY_FOLLOWED_FILE): 93 | with open(ALREADY_FOLLOWED_FILE, "w") as out_file: 94 | out_file.write("") 95 | 96 | # read in the list of user IDs that the bot has already followed in the 97 | # past 98 | do_not_follow = set() 99 | dnf_list = [] 100 | with open(ALREADY_FOLLOWED_FILE) as in_file: 101 | for line in in_file: 102 | dnf_list.append(int(line)) 103 | 104 | do_not_follow.update(set(dnf_list)) 105 | del dnf_list 106 | 107 | return do_not_follow 108 | 109 | 110 | def auto_follow(q, count=100, result_type="recent"): 111 | """ 112 | Follows anyone who tweets about a specific phrase (hashtag, word, etc.) 113 | """ 114 | 115 | result = search_tweets(q, count, result_type) 116 | following = set(t.friends.ids(screen_name=TWITTER_HANDLE)["ids"]) 117 | do_not_follow = get_do_not_follow_list() 118 | 119 | for tweet in result["statuses"]: 120 | try: 121 | if (tweet["user"]["screen_name"] != TWITTER_HANDLE and 122 | tweet["user"]["id"] not in following and 123 | tweet["user"]["id"] not in do_not_follow): 124 | 125 | t.friendships.create(user_id=tweet["user"]["id"], follow=False) 126 | following.update(set([tweet["user"]["id"]])) 127 | 128 | print("followed %s" % (tweet["user"]["screen_name"])) 129 | 130 | except TwitterHTTPError as e: 131 | print("error: %s" % (str(e))) 132 | 133 | # quit on error unless it's because someone blocked me 134 | if "blocked" not in str(e).lower(): 135 | quit() 136 | 137 | 138 | def auto_follow_followers_for_user(user_screen_name, count=100): 139 | """ 140 | Follows the followers of a user 141 | """ 142 | following = set(t.friends.ids(screen_name=TWITTER_HANDLE)["ids"]) 143 | followers_for_user = set(t.followers.ids(screen_name=user_screen_name)["ids"][:count]); 144 | do_not_follow = get_do_not_follow_list() 145 | 146 | for user_id in followers_for_user: 147 | try: 148 | if (user_id not in following and 149 | user_id not in do_not_follow): 150 | 151 | t.friendships.create(user_id=user_id, follow=False) 152 | print("followed %s" % user_id) 153 | 154 | except TwitterHTTPError as e: 155 | print("error: %s" % (str(e))) 156 | 157 | def auto_follow_followers(): 158 | """ 159 | Follows back everyone who's followed you 160 | """ 161 | 162 | following = set(t.friends.ids(screen_name=TWITTER_HANDLE)["ids"]) 163 | followers = set(t.followers.ids(screen_name=TWITTER_HANDLE)["ids"]) 164 | 165 | not_following_back = followers - following 166 | 167 | for user_id in not_following_back: 168 | try: 169 | t.friendships.create(user_id=user_id, follow=False) 170 | except Exception as e: 171 | print("error: %s" % (str(e))) 172 | 173 | 174 | def auto_unfollow_nonfollowers(): 175 | """ 176 | Unfollows everyone who hasn't followed you back 177 | """ 178 | 179 | following = set(t.friends.ids(screen_name=TWITTER_HANDLE)["ids"]) 180 | followers = set(t.followers.ids(screen_name=TWITTER_HANDLE)["ids"]) 181 | 182 | # put user IDs here that you want to keep following even if they don't 183 | # follow you back 184 | users_keep_following = set([]) 185 | 186 | not_following_back = following - followers 187 | 188 | # make sure the "already followed" file exists 189 | if not os.path.isfile(ALREADY_FOLLOWED_FILE): 190 | with open(ALREADY_FOLLOWED_FILE, "w") as out_file: 191 | out_file.write("") 192 | 193 | # update the "already followed" file with users who didn't follow back 194 | already_followed = set(not_following_back) 195 | af_list = [] 196 | with open(ALREADY_FOLLOWED_FILE) as in_file: 197 | for line in in_file: 198 | af_list.append(int(line)) 199 | 200 | already_followed.update(set(af_list)) 201 | del af_list 202 | 203 | with open(ALREADY_FOLLOWED_FILE, "w") as out_file: 204 | for val in already_followed: 205 | out_file.write(str(val) + "\n") 206 | 207 | for user_id in not_following_back: 208 | if user_id not in users_keep_following: 209 | t.friendships.destroy(user_id=user_id) 210 | print("unfollowed %d" % (user_id)) 211 | 212 | 213 | def auto_mute_following(): 214 | """ 215 | Mutes everyone that you are following 216 | """ 217 | following = set(t.friends.ids(screen_name=TWITTER_HANDLE)["ids"]) 218 | muted = set(t.mutes.users.ids(screen_name=TWITTER_HANDLE)["ids"]) 219 | 220 | not_muted = following - muted 221 | 222 | # put user IDs of people you do not want to mute here 223 | users_keep_unmuted = set([]) 224 | 225 | # mute all 226 | for user_id in not_muted: 227 | if user_id not in users_keep_unmuted: 228 | t.mutes.users.create(user_id=user_id) 229 | print("muted %d" % (user_id)) 230 | 231 | 232 | def auto_unmute(): 233 | """ 234 | Unmutes everyone that you have muted 235 | """ 236 | muted = set(t.mutes.users.ids(screen_name=TWITTER_HANDLE)["ids"]) 237 | 238 | # put user IDs of people you want to remain muted here 239 | users_keep_muted = set([]) 240 | 241 | # mute all 242 | for user_id in muted: 243 | if user_id not in users_keep_muted: 244 | t.mutes.users.destroy(user_id=user_id) 245 | print("unmuted %d" % (user_id)) 246 | --------------------------------------------------------------------------------