-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathtr6.py
More file actions
260 lines (208 loc) · 7.39 KB
/
Copy pathtr6.py
File metadata and controls
260 lines (208 loc) · 7.39 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
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
#!/usr/bin/env python
#-*- coding: utf-8 -*-
#############################################################
#NAME: TR6 // TWITTER AUTOREPORTER 6.0 REBUILD #
#############################################################
from splinter import Browser
import sys, getopt, re, shutil,os, codecs
from splinter.request_handler.status_code import HttpResponseError
from splinter.element_list import ElementList
import time, datetime
# uncomment if you want to use privoxy + tor
proxyIP = '127.0.0.1'
proxyPort = 8118
proxy_settings = {'network.proxy.type': 1,
'network.proxy.http': proxyIP,
'network.proxy.http_port': proxyPort,
'network.proxy.ssl': proxyIP,
'network.proxy.ssl_port':proxyPort,
'network.proxy.socks': proxyIP,
'network.proxy.socks_port':proxyPort,
'network.proxy.ftp': proxyIP,
'network.proxy.ftp_port':proxyPort
}
LOGFILENAME="twitter_report.log"
def logintw(browser, aloginb, username, password ):
time.sleep(5)
browser.execute_script('document.getElementsByName("session[username_or_email]")[1].value = "'+username+'"')
browser.execute_script('document.getElementsByName("session[password]")[1].value = "'+password+'"')
aloginb.click()
def help():
print 'tr6.py -u <Twitter username> -i <file> -f <y|n> -w <y|n> -r <y|n>'
print " -f -> harvest followers "
print " -w -> harvest following "
print " -t -> harvest twitters "
print " -r -> report user "
sys.exit(2)
def report(browser):
try:
browser.find_by_css('.user-dropdown').click()
browser.find_by_css('li.report-text button[type="button"]').click()
except:
pass
time.sleep(2)
browser.choose('input[type="radio"][value="spam"]').click(1)
def getTwitterData(browser):
contain=[]
browser.execute_script("$(document).scrollTop($(document).scrollTop()+$(document).height());")
time.sleep(0.5)
tweets=browser.find_by_css('.TweetTextSize.TweetTextSize--16px.js-tweet-text.tweet-text')
tweets+=browser.find_by_css('.TweetTextSize.TweetTextSize--26px.js-tweet-text.tweet-text')
for tweet in tweets:
contain.append(tweet.value)
return contain
def goGetTwitters(id, browser):
browser.visit("https://twitter.com/%s" % (id))
time.sleep(1)
last=""
contents=getTwitterData(browser)
if len(contents)==0:
return False
while last!=contents[-1][0]:
last=contents[-1][0]
contents=getTwitterData(browser)
file=codecs.open("tweets_%s.txt" % id, "wb+", "utf-8")
for value in contents:
file.write('"%s"\n' % (value) )
file.close()
return True
def getProfileData(browser, ldups):
names=browser.find_by_css('.ProfileNameTruncated-link.u-textInheritColor.js-nav.js-action-profile-name')
descs=browser.find_by_css('.ProfileCard-bio.u-dir')
alinks=browser.find_by_css('.ProfileCard-screennameLink.u-linkComplex.js-nav')
profiles=[]
for x, y, z in zip(alinks, names, descs):
if x["href"] not in ldups:
profiles.append([x["href"], y.value, z.value])
return profiles
def goharvest(id, browser, op):
browser.visit("https://twitter.com/%s/%s" % (id, op))
harvestfname="%s_%s.txt" % (op, id)
fpath= "/".join(os.path.dirname(os.path.abspath(__file__)).split("/")[:-1])
ldups=open("allurls.txt", "rb").readlines()
browser.execute_script("$(document).scrollTop($(document).scrollTop()+$(document).height());")
ldups=map(lambda ldup: ldup.strip(), ldups)
lastfollower=" "
links=[]
try:
time.sleep(2.8)
links+=getProfileData(browser, ldups+map(lambda x: x[0], links))
if len(links)==0:
return False
except:
return False
else:
while lastfollower!=links[-1][0]:
lastfollower=links[-1][0]
browser.execute_script("$(document).scrollTop($(document).scrollTop()+$(document).height());")
time.sleep(1.4)
try:
links+=getProfileData(browser, ldups+map(lambda x: x[0], links))
except:
break
links+=getProfileData(browser, ldups+map(lambda x: x[0], links))
harvest = codecs.open(harvestfname, "wb+", "utf-8")
for link, name, desc in links:
harvest.write('"%s","%s","%s"\n' % (link, name, desc))
harvest.close()
#os.system("cat %s/followers/%s | unique %s/analisys/%s " % (fpath, harvestfname, fpath, harvestfname))
return True
def getTimestamp():
return datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
def logger(msg):
open(LOGFILENAME, "a+").write("+[%s] %s\n" % ( getTimestamp() , msg))
print msg
def main(argv):
if len(argv)==0:
help()
report=hvf=hvw=tuits=False
try:
opts, args = getopt.getopt(argv,"hi:u:i:f:w:t:r",["file=","user=","followers=","following=", "tuits=","report="])
except getopt.GetoptError:
help()
for opt, arg in opts:
if opt == '-h':
help()
elif opt in ("-i", "--file"):
txt = arg
elif opt in ("-u", "--user"):
username = arg
elif opt in ("-f", "--followers") and arg=="y":
hvf=True
elif opt in ("-w", "--following") and arg=="y":
hvw=True
elif opt in ("-t", "--tuits") and arg=="y":
tuits=True
elif opt in ("-r", "--report") and arg=="y":
report=True
try:
ufile = open(txt, 'rb').readlines()
except:
print "Cant open %s" % txt
sys.exit(0)
password = raw_input("Enter your twitter password : ")
browser = Browser( 'firefox' , profile_preferences=proxy_settings )
browser.visit("https://twitter.com/login/")
if browser.is_element_present_by_value("Log in", wait_time=8):
aloginb=browser.find_by_xpath('.//button[@type="submit"]')[0]
logintw(browser, aloginb, username, password)
else:
print "timeout loading page"
sys.exit(5)
for line in ufile:
try:
if re.search("intent", line):
url = re.match(r"https?://(www\.)?twitter\.com/intent/(#!/)?@?([^/\s]*)",line.strip())
url = url.group()
urltypeid=True
else:
url = line.strip()
urltypeid=False
browser.visit(url)
time.sleep(1)
if not re.search('suspended', browser.url):
if urltypeid:
browser.find_by_css('a.fn.url.alternate-context').click()
else:
msg = line.strip() + ' - Suspended'
logger(msg)
continue
# report user
msg=" "
if report:
report(browser)
msg="RP"
id = browser.find_by_css('a.ProfileHeaderCard-screennameLink.u-linkComplex.js-nav')["href"][1:]
id = id.split("/")[-1]
followers = browser.find_by_css('a[data-nav="followers"] .ProfileNav-value')
following = browser.find_by_css('a[data-nav="following"] .ProfileNav-value')
try:
msg = "%s %s %s %s" % (followers.value, following.value, url.strip(), msg)
except:
msg = " %s %s " % (url.strip(), msg)
# harvest twitters
if tuits:
if goGetTwitters(id, browser):
msg+=" TW"
# harvest followers
if hvf:
if goharvest(id, browser, "followers"):
msg+=" FO"
# harvest following
if hvw:
if goharvest(id, browser, "following"):
msg+=" FI"
except KeyboardInterrupt:
break
except HttpResponseError:
msg = line.strip()+' - HttpResponseError'
except:
if line:
msg = line.strip()+' - CatchAllError'
else:
logger(msg)
if __name__ == "__main__":
try:
main(sys.argv[1:])
except KeyboardInterrupt:
sys.stdout.write('\n Program stopped!')