forked from rhiever/TwitterFollowBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitter_follow_bot.py
183 lines (134 loc) · 5.83 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
"""
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 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"])
# 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
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=True)
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():
"""
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=True)
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))