forked from signalab/ProtonPack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProtonpack.py
229 lines (204 loc) · 6.56 KB
/
Protonpack.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
""" BotOrNot implementation to catch ghosts in Twitter Universe"""
import tweepy
import botornot
import json
import ast
from pprint import pprint
import time
import sys
__author__ = "Luis Natera"
__credits__ = "BotOrNot" "Adrián Toscano"
__license__ = "GPL"
__version__ = "0.5"
__maintainer__ = "Luis Natera"
__email__ = "nateraluis@gmail.com"
__status__ = "Prototype"
# Twitter keys, place them in config folder
KEYFILE = "config/mykey.json"
accounts_file = open("config/accounts.txt", "r")
accounts = accounts_file.read().split(',')
def menu ():
print('Please select an option:\n')
print('1.- Search an account.\n')
print('2.- Search an account and her followers.\n')
print('3.- Search the list of accounts in the "accounts.txt" file.\n')
print('4.- I have no idea what I\'m doing, take me out\n')
op = input()
print ('\n')
if op == '1':
botAccount()
elif op == '2':
botFollowers()
elif op == '3':
accountList(bon)
elif op == '4':
exit()
# Option 1
def botAccount():
accountCheck = input('Please type the username to check: @')
result = bon.check_account(accountCheck)
try:
jsonStr = json.dumps(result, sort_keys=False, indent=1)
resultJson = json.loads(jsonStr)
score = str(resultJson['score'])
print ('\n'
+ '------'
+ '\n'
+ '@' + accountCheck + " has a bot probability of: " + str(resultJson['score'])
+ '\n'
+ '------'
+ '\n')
# print ("Score: " + str(resultJson['score']) + '\n')
except Exception as e:
print ('An exception ocurred:')
print (e) # Error, not printing the returned error
# Option 2
def botFollowers():
twitterLogin()
accountCheck = input ('Please type the username to check: @')
#Create file to store the followers network
listFollowers = open (accountCheck + '_Followers.csv', 'w')
listFollowers.write('Source,Target\n')
# Create a file to store the scores of the followers
followersScore = open(accountCheck + '_Followers_BotScore.csv', 'w')
followersScore.write('Id,Score\n')
# Check the account score
result = bon.check_account(accountCheck)
jsonStr = json.dumps(result, sort_keys=False, indent=1)
resultJson = json.loads(jsonStr)
score = str(resultJson['score'])
followersScore.write (accountCheck +','+ score + '\n')
print ('\n'
+ '------'
+ '\n'
+ '@' + accountCheck + " has a bot probability of: " + str(resultJson['score'])
+ '\n'
+ '------'
+ '\n')
users = tweepy.Cursor(api.followers, screen_name = accountCheck).items()
user_followers = api.get_user(screen_name=accountCheck)
no_followers = user_followers.followers_count
print ("Now we would check the "
+ str(no_followers)
+ ' followers of @'
+ accountCheck)
count = 0
startTime = time.time()
while True:
try:
user = next(users)
listFollowers.write(user.screen_name + "," + accountCheck + '\n')
count += 1
print ("Account to check: " + user.screen_name)
result = bon.check_account(user.screen_name)
jsonStr = json.dumps(result, sort_keys=False, indent=1)
resultJson = json.loads(jsonStr)
score = str(resultJson['score'])
followersScore.write (user.screen_name +','+ score + '\n')
print ('Score: ' + score)
print ('Accounts done: ' + str(count) + "/" + str(no_followers))
print ("Elapsed Time: " + str((time.time() - startTime)/60) + " min")
print ("Aproximate time to go: "+ str((((time.time() - startTime)/count) * (no_followers - count) / 60)) + " min")
print ("------" + '\n')
if count == no_followers:
sys.exit()
except Exception as e:
listFollowers.write(user.screen_name + "," + accountCheck + '\n')
print ("An exception occurred:")
print (e)
listFollowers.write(user.screen_name +"," + "Null" + '\n')
count += 1
print ('Accounts done: ' + str(count) + "/" + str(no_followers))
print ("Elapsed Time: " + str((time.time() - startTime)/60) + " min")
print ("Aproximate time to go: "+ str((((time.time() - startTime)/count) * (no_followers - count) / 60)) + " min")
print ("------" + '\n')
if count == no_followers:
sys.exit()
except StopIteration:
sys.exit()
#break
# Option 3
def accountList(bon):
countAccounts = len(accounts)
with open ("Accounts_Bots" + ".csv", 'w') as file:
file.write("Id,Score" + '\n')
global accountsDone
accountsDone = 0
startTime = time.time()
for account in accounts:
try:
print ("Account to check: " + account)
result = bon.check_account(account)
jsonStr = json.dumps(result, sort_keys=False, indent=1)
resultJson = json.loads(jsonStr)
score = str(resultJson['score'])
file.write(account + "," + score + '\n')
accountsDone = accountsDone + 1
print ("Score: " + str(resultJson['score']))
print ("Accounts done: " + str(accountsDone) + "/" + str(countAccounts))
print ("Elapsed Time: " + str((time.time() - startTime) / 60) + " min")
print ("Aproximate time to go: "+ str((((time.time() - startTime)/accountsDone) * (countAccounts - accountsDone) / 60)) + " min")
print ("------" + '\n')
except Exception as e:
print ("An exception occurred:")
print (e)
file.write(account +"," + "Null" + '\n')
accountsDone = accountsDone + 1
print ("Accounts done: " + str(accountsDone) + "/" + str(countAccounts))
print ("Elapsed Time: " + str((time.time() - startTime) / 60) + " min")
print ("Aproximate time to go: "+ str((((time.time() - startTime)/accountsDone) * (countAccounts - accountsDone) / 60)) + " min")
print ("------" + '\n')
pass
# Option 4
def exit():
print('Have a nive day!\n')
sys.exit()
# Authenticate with twitter and BotorNot
def authenticate():
key = get_key(KEYFILE)
global bon
bon = botornot.BotOrNot(**key)
return bon
# Load the twitter keys
def get_key(keyfile):
try:
with open(keyfile) as fin:
key = json.load(fin)
except FileNotFoundError as e:
print("Key not found")
print(e)
sys.exit(1)
return key
def get_keyTweepy(keyfile):
try:
with open(keyfile) as fin:
keyTweepy = json.load(fin)
except FileNotFoundError as e:
print ("Key not found")
sys.exit(1)
return keyTweepy
# Authenticate in twitter
def authenticateTweepy(keyTweepy):
global api
api = authenticate(keyTweepy)
return auth
def twitterLogin():
key = get_keyTweepy(KEYFILE)
auth = tweepy.OAuthHandler(key['consumer_key'], key['consumer_secret'])
auth.set_access_token(key['access_token'], key['access_token_secret'])
global api
api = tweepy.API(auth)
return api
def main():
print ('\n'
+ "------------------"
+ '\n'
+ "Welcome to Proton Pack. Ready to catch some bots?"
+ '\n'
+ "------------------"
+ '\n')
bon = authenticate()
menu()
# Execute code
if __name__ == '__main__':
main()