-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathpornbothunter.py
304 lines (230 loc) · 6.83 KB
/
pornbothunter.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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/usr/bin/env python3
"""
Twitter Porn bot hunter
"""
import datetime
import os
import pycurl
import random
import re
import time
from io import BytesIO
from urllib.error import URLError
from urllib.parse import urlencode
from urllib.request import urlopen
import requests
import tweepy
from bs4 import BeautifulSoup
from googlesearch.googlesearch import GoogleSearch
from secrets import *
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tweepy.API(auth)
pseudos = []
patterns = [
'site:twitter.com "Twerk dancer/Fitness lover"',
'site:twitter.com "Cosplay master \\ Travel lover"',
'site:twitter.com "Cosplay master/Travel lover"',
'site:twitter.com "Cosplay master \\\\ Travel lover"',
'site:twitter.com "Cosplay master // MARVEL fan"',
'site:twitter.com "Cosplay fan // MARVEL fan"',
'site:twitter.com "Sweet lady. Dancing \\\\ DC fan"',
'site:twitter.com "Sweet lady. Twerk dancer. Cats lover"',
'site:twitter.com "Pretty girl. Dancing. Fitness"',
'site:twitter.com "Actress \\\\ Travel"',
'site:twitter.com "Costume designer // Dogs lover"',
'site:twitter.com "Gamer / Dogs lover"',
'site:twitter.com "Cute girl. Cosplay fan/MARVEL fan"',
'site:twitter.com "Humble girl. Cosplayer/DC fan"',
'site:twitter.com "Simple girl. Gamer \ Traveler"',
'site:twitter.com "Voice actress. Dogs lover'
]
TEMP_FILE = 'temp.jpg'
DELAY_BETWEEN_PUBLICATION = 3600
def paste(content, title, key):
"""
Create a paste on https://pastebin.com
Parameters
----------
content: String
Body of the paste
title: String
Title of the paste
key: String
The API dev key from https://pastebin.com
Returns
-------
Optional[String]
The URL of the paste if successful, None otherwise
"""
options = {
'api_dev_key': key,
'api_option': 'paste',
'api_paste_name': title,
'api_paste_code': content,
'api_paste_private': 0
}
try:
response = urlopen('http://pastebin.com/api/api_post.php', urlencode(options).encode('utf-8'))
return response.read()
except (URLError, Exception):
return None
def parse_google_web_search(search_result):
"""
Parse a Google web search.
Parameters
----------
search_result : String[]
Google web search result
"""
for result_item in search_result.results:
handle = result_item.url.split("/")[3]
if handle not in pseudos:
pseudos.insert(0, handle)
print("pseudo: {}, length: {}".format(handle, len(pseudos)))
def publish_tweet(handle):
"""
Publish a tweet.
Parameters
----------
handle : String
pseudo of the bot
Returns
-------
Boolean
True if the tweet had been published, otherwise False
"""
profile_url = get_profile_picture_url("https://twitter.com/{}".format(
handle))
if profile_url and download_image(profile_url):
user = api.get_user(handle)
message = ("Pseudo: {}"
"\nFollowers: {}"
"\nFollowing: {}"
"\nCreated at: {}").format(handle, user.followers_count,
user.friends_count,
user.created_at)
description_link = get_link_description(user.description)
if description_link:
message = message + "\nBio link: " + description_link
api.update_with_media(TEMP_FILE, status=message)
os.remove(TEMP_FILE)
return True
else:
message = "Pseudo: " + handle + "\nStatus: suspended"
api.update_status(message)
def download_image(profile_picture_url):
"""
Download image.
Parameters
----------
profile_picture_url : String
URL of the Twitter profile picture
Returns
-------
Boolean
True if the image had been downloaded, otherwise False
"""
request = requests.get(profile_picture_url, stream=True)
if request.status_code == 200:
with open(TEMP_FILE, 'wb') as image:
for chunk in request:
image.write(chunk)
return True
else:
print("Unable to download image")
return False
def get_profile_picture_url(profile_url):
"""
Get the profile picture URL.
Parameters
----------
profile_url : String
URL of the Twitter profile
Returns
-------
Optional[String]
Profile picture URL otherwise None
"""
url_re = re.compile(r"https://pbs.twimg.com/profile_images/.*?jpg")
page = urlopen(profile_url)
if page:
html = page.read().decode('utf-8')
if html:
profile_picture_url = url_re.findall(html)
if profile_picture_url:
return profile_picture_url[0]
def get_link_description(description):
"""
Get the link in the description.
Parameters
----------
description : String
Twitter description of the bot
Returns
-------
Optional[String]
HTTP link in the description otherwise None
"""
url_re = re.compile("http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*(),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+")
link_description = url_re.findall(description)
if link_description:
return link_description[0]
def google_image_search(image_url):
"""
Get the link in the description.
Parameters
----------
image_url : String
Url of the profile picture of the Twitter bot
"""
search_url = 'https://www.google.com/searchbyimage?&image_url='
buffer = BytesIO()
full_url = search_url + image_url + '&q=site:twitter.com&intitle:"(@"'
conn = pycurl.Curl()
conn.setopt(conn.URL, str(full_url))
conn.setopt(conn.FOLLOWLOCATION, 1)
conn.setopt(conn.USERAGENT,
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11'
' (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11')
conn.setopt(conn.WRITEFUNCTION, buffer.write)
conn.perform()
conn.close()
soup = BeautifulSoup(buffer.getvalue().decode('utf-8'), 'html.parser')
for div in soup.findAll('div', attrs={'class': 'rc'}):
s_link = div.find('a')
if 'status' not in s_link:
handle = s_link['href'].split("/")[3]
if handle not in pseudos:
pseudos.append(handle)
print("pseudo: {}, length: {}".format(handle, len(pseudos)))
def publish_summary_tweet():
"""
Publish the summary tweet
"""
# Create the pastebin content
paste_content = "Detected Twitter porn bots by @PornBotHunter:"
for name in pseudos:
paste_content += "\n@{}".format(name)
now = datetime.datetime.now()
title = "Detected bots by @PornBotHunter {}".format(
now.strftime("%d/%m/%Y"))
pbin_url = paste(paste_content, title, pastebin_dev_key)
if pbin_url:
message = ("Currently, @PornBotHunter detected {} #Twitter #porn #bots."
"Detailed list is available here: {}").format(len(pseudos),
pbin_url)
api.update_status(message)
if __name__ == '__main__':
while True:
result = GoogleSearch().search(random.choice(patterns), num_results=100)
parse_google_web_search(result)
publish_summary_tweet()
time.sleep(DELAY_BETWEEN_PUBLICATION)
for pseudo in pseudos:
url = get_profile_picture_url("https://twitter.com/{}".format(
pseudo))
if url:
google_image_search(url)
publish_tweet(pseudo)
time.sleep(DELAY_BETWEEN_PUBLICATION)