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