-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfollower.py
More file actions
224 lines (198 loc) · 6.25 KB
/
follower.py
File metadata and controls
224 lines (198 loc) · 6.25 KB
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
# -*- coding: utf-8 -*-
import tweepy
import sys
import json
from tweepy import OAuthHandler
import time
import re
global uid
global ids
global totaluid
global numu
consumer_key = '****'
consumer_secret = '****'
access_token = '****-****'
access_secret = '****'
auth = OAuthHandler(consumer_key,consumer_secret)
auth.set_access_token(access_token,access_secret)
api = tweepy.API(auth)
def limit_handled(cursor):
numm = 0
while True:
try:
yield cursor.next()
except tweepy.RateLimitError:
numm += 1
print("pull rest")
print("pull timmer counter:"+str(numm))
time.sleep(60)
print("pull resume")
except tweepy.error.TweepError:
yield []
return
def cashe(uid):
txtname = ("ids"+str(uid)+".txt")
try:
file_object2 = open(txtname,'r')
b = []
lines = file_object2.readlines()
for a in lines:
line = re.sub("[\n''' ]", '', a)
b.append(line)
file_object2.close()
return b
except:
return False
def follower_page(uid):
ids = cashe(uid)
if ids != False:
print("cashe")
return ids
elif ids == False:
print("noncashe")
ids = []
for page in limit_handled(tweepy.Cursor(api.followers_ids,user_id=uid).pages()):
ids.extend(page)
txtname = str("ids"+str(uid)+".txt")
with open(txtname,"a") as f:
for i in ids:
k=' '.join([str(i)])
f.write(k+"\n")
return ids
def reduce(function, iterable, initializer=None):
it = iter(iterable)
if initializer is None:
try:
initializer = next(it)
except StopIteration:
raise TypeError('reduce() of empty sequence with no initial value')
accum_value = initializer
for x in it:
accum_value = function(accum_value, x)
return accum_value
print("Begin reading standard base")
uid = "498084894"
numu = 0
totaluid = []
file_object2 = open("total.txt",'r')
b = []
lines = file_object2.readlines()
for a in lines:
line = re.sub("[\n''' ]", '', a)
b.append(line)
file_object2.close()
totaluid = b
print("Finish reading base, the number of base now is "+str(len(totaluid)))
def isfurry(uid,totaluid):
numif = 0
isf = cashe(uid)
if isf != False:
print("Cashed")
elif isf == False:
print("Noncashed")
isf = []
for page in limit_handled(tweepy.Cursor(api.friends_ids,user_id=uid).pages()):
isf.extend(page)
print("Following "+str(len(isf)))
for i in isf:
if i in totaluid:
numif +=1
print(str(numif)+" in common for following")
print("Percentage of total following"+str(numif/(len(isf)+0.00000001)))
a = numif/(len(isf)+0.00001)
i = str(str(uid)+"\n"+str(len(isf))+"\n"+str(a)+"\n")
with open("record2.txt","a") as f:
f.write(i)
if numif == 0 or len(isf) == 0:
print("BiJiao following False and 0")
return False
elif numif/(len(isf)+0.00000001) <= 0.3:
print("BiJiao following False")
return False
else:
print("BiJiao True")
return True
def totaluser(ids,numu,totaluid):
for i in ids:
totaluid.append(i)
totaluid = quchong(totaluid)
return totaluid,totaluid[numu]
def totaluserfalse(numu,totaluid):
return totaluid[numu]
def quchong(totaluid):
func = lambda x,y:x if y in x else x + [y]
totaluid = reduce(func, [[], ] + totaluid)
return totaluid
def bijiao(uid,ids,totaluid):
numc = 0
for i in ids:
if i in totaluid:
numc +=1
print(str(numc)+" in common.")
print("Percentage of common is "+str(numc/(len(ids)+0.00001)))
i = str(uid)+"\n"+str(numc)+"\n"+str(numc/(len(ids)+0.00001))+"\n"
with open("record.txt","a") as f:
f.write(i)
if numc == 0 or len(ids) == 0:
print("BiJiao False and 0 numbers found")
return False
elif numc/(len(ids)+0.00001) <= 0.01 or numc <= 5:
print("BiJiao False")
return False
else:
print("BiJiao True")
return True
def main(uid,numu,totaluid):
numu = 0
while numu < len(totaluid):
print("This is the "+str(numu+1)+" time(s) running.")
ids = follower_page(uid)
print("The currently number of followers of "+ str(uid) + " is " +str(len(ids)) )
totaluid = quchong(totaluid)
print("The current base amount is "+str(len(totaluid)))
result = bijiao(uid,ids,totaluid)
print("Result is "+str(result))
if result == True:
totaluid,uid = totaluser(ids,numu,totaluid)
print("The new uid is "+str(uid))
with open("total.txt","w") as f:
if len(ids) > 0:
for i in totaluid:
k=' '.join(str(i))
f.write(k+"\n")
print("Finish")
numu +=1
result = True
if result == False:
result = isfurry(uid,totaluid)
if result == True:
print(str(uid)+'is furry.')
totaluid,uid = totaluser(ids,numu,totaluid)
print("The new uid is "+str(uid))
print("len of ids is "+str(len(ids)))
if len(ids) > 0:
with open("total.txt","w") as f:
for i in totaluid:
k=' '.join(str(i))
f.write(k+"\n")
print("Finish")
numu +=1
result = True
else:
print(str(uid)+"is not a furry.")
totaluid.remove(uid)
uid = totaluserfalse(numu,totaluid)
with open("total.txt","w") as f:
for i in totaluid:
k=' '.join(str(i))
f.write(k+"\n")
print("Finish")
numu +=1
result = True
print("finish")
with open("totallen.txt","w") as f:
i = len(totaluid)
k=' '.join(str(i))
f.write(k+"\n")
print("finish")
main(uid,numu,totaluid)