-
Notifications
You must be signed in to change notification settings - Fork 445
/
__init__.py
476 lines (365 loc) · 19.3 KB
/
__init__.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
# -*- coding: utf-8 -*-
"""
Copyright 2016 Randal S. Olson
This file is part of the Twitter Bot library.
The Twitter 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 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 Bot library. If not, see http://www.gnu.org/licenses/.
"""
from __future__ import print_function
from twitter import Twitter, OAuth, TwitterHTTPError
import os
import sys
import time
import random
class TwitterBot:
"""
Bot that automates several actions on Twitter, such as following users
and favoriting tweets.
"""
def __init__(self, config_file="config.txt"):
# this variable contains the configuration for the bot
self.BOT_CONFIG = {}
# this variable contains the authorized connection to the Twitter API
self.TWITTER_CONNECTION = None
self.bot_setup(config_file)
# Used for random timers
random.seed()
def wait_on_action(self):
min_time = 0
max_time = 0
if "FOLLOW_BACKOFF_MIN_SECONDS" in self.BOT_CONFIG:
min_time = int(self.BOT_CONFIG["FOLLOW_BACKOFF_MIN_SECONDS"])
if "FOLLOW_BACKOFF_MAX_SECONDS" in self.BOT_CONFIG:
max_time = int(self.BOT_CONFIG["FOLLOW_BACKOFF_MAX_SECONDS"])
if min_time > max_time:
temp = min_time
min_time = max_time
max_time = temp
wait_time = random.randint(min_time, max_time)
if wait_time > 0:
print("Choosing time between %d and %d - waiting %d seconds before action" % (min_time, max_time, wait_time))
time.sleep(wait_time)
return wait_time
def bot_setup(self, config_file="config.txt"):
"""
Reads in the bot configuration file and sets up the bot.
Defaults to config.txt if no configuration file is specified.
If you want to modify the bot configuration, edit your config.txt.
"""
with open(config_file, "r") as in_file:
for line in in_file:
line = line.split(":")
parameter = line[0].strip()
value = line[1].strip()
if parameter in ["USERS_KEEP_FOLLOWING", "USERS_KEEP_UNMUTED", "USERS_KEEP_MUTED"]:
if value != "":
self.BOT_CONFIG[parameter] = set([int(x) for x in value.split(",")])
else:
self.BOT_CONFIG[parameter] = set()
elif parameter in ["FOLLOW_BACKOFF_MIN_SECONDS", "FOLLOW_BACKOFF_MAX_SECONDS"]:
self.BOT_CONFIG[parameter] = int(value)
else:
self.BOT_CONFIG[parameter] = value
# make sure that the config file specifies all required parameters
required_parameters = ["OAUTH_TOKEN", "OAUTH_SECRET", "CONSUMER_KEY",
"CONSUMER_SECRET", "TWITTER_HANDLE",
"ALREADY_FOLLOWED_FILE",
"FOLLOWERS_FILE", "FOLLOWS_FILE"]
missing_parameters = []
for required_parameter in required_parameters:
if (required_parameter not in self.BOT_CONFIG or
self.BOT_CONFIG[required_parameter] == ""):
missing_parameters.append(required_parameter)
if len(missing_parameters) > 0:
self.BOT_CONFIG = {}
raise Exception("Please edit %s to include the following parameters: %s.\n\n"
"The bot cannot run unless these parameters are specified."
% (config_file, ", ".join(missing_parameters)))
# make sure all of the sync files exist locally
for sync_file in [self.BOT_CONFIG["ALREADY_FOLLOWED_FILE"],
self.BOT_CONFIG["FOLLOWS_FILE"],
self.BOT_CONFIG["FOLLOWERS_FILE"]]:
if not os.path.isfile(sync_file):
with open(sync_file, "w") as out_file:
out_file.write("")
# check how old the follower sync files are and recommend updating them
# if they are old
if (time.time() - os.path.getmtime(self.BOT_CONFIG["FOLLOWS_FILE"]) > 86400 or
time.time() - os.path.getmtime(self.BOT_CONFIG["FOLLOWERS_FILE"]) > 86400):
print("Warning: Your Twitter follower sync files are more than a day old. "
"It is highly recommended that you sync them by calling sync_follows() "
"before continuing.", file=sys.stderr)
# create an authorized connection to the Twitter API
self.TWITTER_CONNECTION = Twitter(auth=OAuth(self.BOT_CONFIG["OAUTH_TOKEN"],
self.BOT_CONFIG["OAUTH_SECRET"],
self.BOT_CONFIG["CONSUMER_KEY"],
self.BOT_CONFIG["CONSUMER_SECRET"]))
def sync_follows(self):
"""
Syncs the user's followers and follows locally so it isn't necessary
to repeatedly look them up via the Twitter API.
It is important to run this method at least daily so the bot is working
with a relatively up-to-date version of the user's follows.
Do not run this method too often, however, or it will quickly cause your
bot to get rate limited by the Twitter API.
"""
# sync the user's followers (accounts following the user)
followers_status = self.TWITTER_CONNECTION.followers.ids(screen_name=self.BOT_CONFIG["TWITTER_HANDLE"])
followers = set(followers_status["ids"])
next_cursor = followers_status["next_cursor"]
with open(self.BOT_CONFIG["FOLLOWERS_FILE"], "w") as out_file:
for follower in followers:
out_file.write("%s\n" % (follower))
while next_cursor != 0:
followers_status = self.TWITTER_CONNECTION.followers.ids(screen_name=self.BOT_CONFIG["TWITTER_HANDLE"],
cursor=next_cursor)
followers = set(followers_status["ids"])
next_cursor = followers_status["next_cursor"]
with open(self.BOT_CONFIG["FOLLOWERS_FILE"], "a") as out_file:
for follower in followers:
out_file.write("%s\n" % (follower))
# sync the user's follows (accounts the user is following)
following_status = self.TWITTER_CONNECTION.friends.ids(screen_name=self.BOT_CONFIG["TWITTER_HANDLE"])
following = set(following_status["ids"])
next_cursor = following_status["next_cursor"]
with open(self.BOT_CONFIG["FOLLOWS_FILE"], "w") as out_file:
for follow in following:
out_file.write("%s\n" % (follow))
while next_cursor != 0:
following_status = self.TWITTER_CONNECTION.friends.ids(screen_name=self.BOT_CONFIG["TWITTER_HANDLE"],
cursor=next_cursor)
following = set(following_status["ids"])
next_cursor = following_status["next_cursor"]
with open(self.BOT_CONFIG["FOLLOWS_FILE"], "a") as out_file:
for follow in following:
out_file.write("%s\n" % (follow))
def get_do_not_follow_list(self):
"""
Returns the set of users the bot has already followed in the past.
"""
dnf_list = []
with open(self.BOT_CONFIG["ALREADY_FOLLOWED_FILE"], "r") as in_file:
for line in in_file:
dnf_list.append(int(line))
return set(dnf_list)
def get_followers_list(self):
"""
Returns the set of users that are currently following the user.
"""
followers_list = []
with open(self.BOT_CONFIG["FOLLOWERS_FILE"], "r") as in_file:
for line in in_file:
followers_list.append(int(line))
return set(followers_list)
def get_follows_list(self):
"""
Returns the set of users that the user is currently following.
"""
follows_list = []
with open(self.BOT_CONFIG["FOLLOWS_FILE"], "r") as in_file:
for line in in_file:
follows_list.append(int(line))
return set(follows_list)
def search_tweets(self, phrase, count=100, result_type="recent"):
"""
Returns a list of tweets matching a phrase (hashtag, word, etc.).
"""
return self.TWITTER_CONNECTION.search.tweets(q=phrase, result_type=result_type, count=count)
def auto_fav(self, phrase, count=100, result_type="recent"):
"""
Favorites tweets that match a phrase (hashtag, word, etc.).
"""
result = self.search_tweets(phrase, count, result_type)
for tweet in result["statuses"]:
try:
# 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)
# when you have already favorited a tweet, this error is thrown
except TwitterHTTPError as api_error:
# quit on rate limit errors
if "rate limit" in str(api_error).lower():
print("You have been rate limited. "
"Wait a while before running the bot again.", file=sys.stderr)
return
if "you have already favorited this status" not in str(api_error).lower():
print("Error: %s" % (str(api_error)), file=sys.stderr)
def auto_rt(self, phrase, count=100, result_type="recent"):
"""
Retweets tweets that match a phrase (hashtag, word, etc.).
"""
result = self.search_tweets(phrase, count, result_type)
for tweet in result["statuses"]:
try:
# 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)
# when you have already retweeted a tweet, this error is thrown
except TwitterHTTPError as api_error:
# quit on rate limit errors
if "rate limit" in str(api_error).lower():
print("You have been rate limited. "
"Wait a while before running the bot again.", file=sys.stderr)
return
print("Error: %s" % (str(api_error)), file=sys.stderr)
def auto_follow(self, phrase, count=100, result_type="recent"):
"""
Follows anyone who tweets about a phrase (hashtag, word, etc.).
"""
result = self.search_tweets(phrase, count, result_type)
following = self.get_follows_list()
do_not_follow = self.get_do_not_follow_list()
for tweet in result["statuses"]:
try:
if (tweet["user"]["screen_name"] != self.BOT_CONFIG["TWITTER_HANDLE"] and
tweet["user"]["id"] not in following and
tweet["user"]["id"] not in do_not_follow):
self.wait_on_action()
self.TWITTER_CONNECTION.friendships.create(user_id=tweet["user"]["id"], follow=False)
following.update(set([tweet["user"]["id"]]))
print("Followed %s" %
(tweet["user"]["screen_name"]), 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_follow_followers(self,count=None):
"""
Follows back everyone who's followed you.
"""
following = self.get_follows_list()
followers = self.get_followers_list()
not_following_back = followers - following
not_following_back = list(not_following_back)[:count]
for user_id in not_following_back:
try:
self.wait_on_action()
self.TWITTER_CONNECTION.friendships.create(user_id=user_id, follow=False)
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_follow_followers_of_user(self, user_twitter_handle, count=100):
"""
Follows the followers of a specified user.
"""
following = self.get_follows_list()
followers_of_user = set(self.TWITTER_CONNECTION.followers.ids(screen_name=user_twitter_handle)["ids"][:count])
do_not_follow = self.get_do_not_follow_list()
for user_id in followers_of_user:
try:
if (user_id not in following and
user_id not in do_not_follow):
self.wait_on_action()
self.TWITTER_CONNECTION.friendships.create(user_id=user_id, follow=False)
print("Followed %s" % user_id, 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.
"""
following = self.get_follows_list()
followers = self.get_followers_list()
not_following_back = following - followers
not_following_back = list(not_following_back)[:count]
# update the "already followed" file with users who didn't follow back
already_followed = set(not_following_back)
already_followed_list = []
with open(self.BOT_CONFIG["ALREADY_FOLLOWED_FILE"], "r") as in_file:
for line in in_file:
already_followed_list.append(int(line))
already_followed.update(set(already_followed_list))
with open(self.BOT_CONFIG["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 self.BOT_CONFIG["USERS_KEEP_FOLLOWING"]:
self.wait_on_action()
self.TWITTER_CONNECTION.friendships.destroy(user_id=user_id)
print("Unfollowed %d" % (user_id), file=sys.stdout)
def auto_unfollow_all_followers(self,count=None):
"""
Unfollows everyone that you are following(except those who you have specified not to)
"""
following = self.get_follows_list()
for user_id in following:
if user_id not in self.BOT_CONFIG["USERS_KEEP_FOLLOWING"]:
self.wait_on_action()
self.TWITTER_CONNECTION.friendships.destroy(user_id=user_id)
print("Unfollowed %d" % (user_id), file=sys.stdout)
def auto_mute_following(self):
"""
Mutes everyone that you are following.
"""
following = self.get_follows_list()
muted = set(self.TWITTER_CONNECTION.mutes.users.ids(screen_name=self.BOT_CONFIG["TWITTER_HANDLE"])["ids"])
not_muted = following - muted
for user_id in not_muted:
if user_id not in self.BOT_CONFIG["USERS_KEEP_UNMUTED"]:
self.TWITTER_CONNECTION.mutes.users.create(user_id=user_id)
print("Muted %d" % (user_id), file=sys.stdout)
def auto_unmute(self):
"""
Unmutes everyone that you have muted.
"""
muted = set(self.TWITTER_CONNECTION.mutes.users.ids(screen_name=self.BOT_CONFIG["TWITTER_HANDLE"])["ids"])
for user_id in muted:
if user_id not in self.BOT_CONFIG["USERS_KEEP_MUTED"]:
self.TWITTER_CONNECTION.mutes.users.destroy(user_id=user_id)
print("Unmuted %d" % (user_id), file=sys.stdout)
def send_tweet(self, message):
"""
Posts a tweet.
"""
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"])
print("User %s added to the list %s" % (tweet["user"]["screen_name"], list_slug), file=sys.stdout)
except TwitterHTTPError as api_error:
print(api_error)