-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathtwitter-bot.py
186 lines (159 loc) · 5.9 KB
/
twitter-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
import tweepy
from tweepy import OAuthHandler
import config as config
import argparse
import time, datetime
# Default limit of tweets
def_limit = 5
delay_ = 1
delay_follow = 1
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument('-q', '--query', required=False, action='store', help='Query')
parser.add_argument('-l', '--limit', required=False, action='store', help='Max. number of tweets')
parser.add_argument('-g', '--geocode', required=False, action='store', help='Geocode. Ex: "40.432,-3.708,10km"')
parser.add_argument('-o', '--option', required=True, action='store', help='Option')
parser.add_argument('-i', '--input', required=False, action='store', help='Input list')
my_args = parser.parse_args()
return my_args
def follow_funct(query, geocode, limit, api):
# Cursor
if query is not None and geocode is None:
cursor = tweepy.Cursor(api.search, q=query, count=int(limit)).items(int(limit))
if query is None and geocode is not None:
cursor= tweepy.Cursor(api.search, geocode=geocode, count=int(limit)).items(int(limit))
if query is not None and geocode is not None:
cursor = tweepy.Cursor(api.search, q=query, geocode=geocode, count=int(limit)).items(int(limit))
# Start following people
print ("Following %s people: " % limit)
for tweet in cursor:
screen_name = tweet.user.screen_name
api.create_friendship(screen_name)
print (" - " + screen_name)
time.sleep(delay_follow)
def follow_list(api, userlist):
userlist = open(userlist).read().splitlines()
for screen_name in userlist:
if screen_name != '':
print ("Following %s"%(screen_name))
api.create_friendship(screen_name)
time.sleep(delay_follow)
def unfollow_funct(api):
# Get info about followers/followings and whitelist
followers = api.followers_ids()
followings = api.friends_ids()
whitelist = open("whitelist.txt").read().splitlines()
# Start unfollowing
for i in followings:
screen_name = api.get_user(i).screen_name
if i not in followers and screen_name not in whitelist:
print ("Unfollowing %s" % screen_name)
api.destroy_friendship(screen_name)
def unfollow_list(api, userlist):
userlist = open(userlist).read().splitlines()
for screen_name in userlist:
if screen_name != '':
print ("Unfollowing %s"%(screen_name))
api.destroy_friendship(screen_name)
def ratelimit():
print ("Rate limit. Waiting for 2 minutes nad trying again.")
print ("If it fails again, try to regenerate Twitter Keys and Tokens")
time.sleep(120)
return
def report(api):
followers = api.followers_ids()
followings = api.friends_ids()
followers_names=[]
followings_names=[]
# People following me
print ("\nFOLLOWERS\n")
counter = 0
for i in followers:
try:
time.sleep(delay_)
screen_name = api.get_user(i).screen_name
followers_names.append(screen_name)
counter+=1
print (str(counter)+") "+screen_name)
except:
ratelimit()
# People i follow
print ("\nFOLLOWING\n")
counter = 0
for j in followings:
try:
time.sleep(delay_)
screen_name = api.get_user(j).screen_name
followings_names.append(screen_name)
counter+=1
print (str(counter)+") "+screen_name)
except:
ratelimit()
screen_name = api.get_user(j).screen_name
followings_names.append(screen_name)
counter+=1
print (str(counter)+") "+screen_name)
# People who i follow but they do not follow me
didnt_follow_me = list(set(followings_names)-set(followers_names))
# People who follow me but i do not follow them
didnt_follow_them = list(set(followers_names)-set(followings_names))
counter = 0
print ("\nI FOLLOW BUT THEY DO NOT FOLLOW BACK\n")
for k in didnt_follow_me:
counter+=1
print (str(counter)+") "+k)
counter=0
print ("\nTHEY FOLLOW BUT I DO NOT FOLLOW BACK\n")
for l in didnt_follow_them:
counter+=1
print (str(counter)+") "+l)
print ("\n------------------------- SUMMARY -------------------------")
print ("Total followers: %s"%(len(followers_names)))
print ("Total following: %s"%(len(followings_names)))
print ("Total people i follow but they do not follow me: %s"%(len(didnt_follow_me)))
print ("Total people following me but i do not follow them: %s"%(len(didnt_follow_them)))
print ("\nDate: %s"%(datetime.datetime.now()))
print ("-----------------------------------------------------------")
def main():
# Check config file is ok
if config.consumer_key != "" or config.consumer_secret != "" or config.access_token != "" or config.access_token_secret != "":
consumer_key=config.consumer_key
consumer_secret=config.consumer_secret
access_token=config.access_token
access_token_secret=config.access_token_secret
else:
print ("Fill the config file please!")
return
# Auth process
try:
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
except ValueError:
print("Error authenticating!")
# Different options
args = get_args()
option = args.option
if option == "follow":
query = args.query
geocode = args.geocode
input_ = args.input
if query is not None or geocode is not None:
limit = args.limit
if limit is None:
limit = def_limit
print ("Default number of tweets: %s" % limit)
follow_funct(args.query, args.geocode, limit, api)
elif input_ is not None:
follow_list(api, input_)
elif option == "unfollow-back":
unfollow_funct(api)
elif option == "unfollow":
input_ = args.input
unfollow_list(api, input_)
elif option == "info":
report(api)
else:
print ("Unknown function")
if __name__ == "__main__":
main()