-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
232 lines (201 loc) · 9.52 KB
/
main.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
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import os
from datetime import datetime
def chooseBrowser():
global driver
inp = input("Choose 1 for Firefox\nChoose 2 for Chrome\n\n")
if inp == "1":
print("You chose Firefox, installing driver..")
from selenium.webdriver.firefox.service import Service as FirefoxService
from webdriver_manager.firefox import GeckoDriverManager
driver = webdriver.Firefox(service=FirefoxService(GeckoDriverManager().install()))
print("\n\n\n\n")
askForReportAndBlock()
elif inp == "2":
print("You chose Chrome, installing driver..")
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-logging"])
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()),options=options)
print("\n\n\n\n")
askForReportAndBlock()
else:
print("\n\n\n\nYou must choose between 1 or 2")
return chooseBrowser()
def askForReportAndBlock():
global reportAndBlock
global scanAccounts
inp = input("Choose 1 to Block accounts\nChoose 2 to Block and Report accounts\n\n")
if inp == "1":
reportAndBlock = False
scanAccounts = False
elif inp == "2":
reportAndBlock = True
scanAccounts = False
elif inp == "3":
scanAccounts = True
else:
print("\n\n\n\nYou must choose between 1 or 3")
return askForReportAndBlock()
def monthStringToInt(monthString):
if monthString == "Jan":
return 1
elif monthString == "Feb":
return 2
elif monthString == "Mar":
return 3
elif monthString == "Apr":
return 4
elif monthString == "May":
return 5
elif monthString == "Jun":
return 6
elif monthString == "Jul":
return 7
elif monthString == "Aug":
return 8
elif monthString == "Sep":
return 9
elif monthString == "Oct":
return 10
elif monthString == "Nov":
return 11
elif monthString == "Dec":
return 12
else:
return None
def postTimeToHour(value, unit):
# return the time since post in hour
if unit == "d":
return int(value) * 24
if unit == "h":
return int(value)
if unit == "m":
return int(value) / 60
chooseBrowser()
if scanAccounts:
driver.get("https://9gag.com/fresh")
buffer = []
posters = {}
lastPostTime = None
date = datetime.now()
try:
print("Scanning...You may now scroll to oblivion.\n")
print("Warning: Scanning might produce false positives if you can't see 1h old posts at least.")
previous_stream = None
# Loop to scan continuously while on 9gag.
while EC.presence_of_element_located((By.CSS_SELECTOR, ".list-stream")):
for stream in driver.find_elements(By.CSS_SELECTOR, ".list-stream"):
# Prevent scaning all previous posts each frame.
if stream.id != previous_stream:
if not stream.id in buffer:
buffer.append(stream.id)
previous_stream = stream.id
# Scan posts in curent stream.
post_list = stream.find_elements(By.CSS_SELECTOR, ".ui-post-creator")
for post in post_list:
try:
if not post.id in buffer:
poster = post.find_element(By.CSS_SELECTOR, ".ui-post-creator__author")
time = post.find_element(By.CSS_SELECTOR, ".ui-post-creator__creation")
# print(poster.text, time.text)
lastPostTime = time.text
if not poster.text in posters.keys():
posters[poster.text] = [time.text]
else:
posters[poster.text].append(time.text)
buffer.append(post.id)
except:
# Promoted post or ad.
pass
except:
# Estimate the scrolled time span in fresh (take the oldest post)
if len(lastPostTime) == 9:
postYear = lastPostTime[-2:]
postMonth = monthStringToInt(lastPostTime[3:5])
postDay = lastPostTime[:1]
postDate = datetime.strptime(20+postYear, postMonth, postDay, '%Y %m %d')
scanDate = datetime.strptime(date.tm_year, date.tm_month, date.tm_day, '%Y %m %d')
deltaDate = scanDate - postDate
hoursElapsed = deltaDate.totalseconds() / 3600
elif len(lastPostTime) == 6:
postMonth = monthStringToInt(lastPostTime[3:5])
postDay = lastPostTime[:1]
postDate = datetime.strptime("2023", postMonth, postDay, '%Y %m %d')
scanDate = datetime.strptime("2023", date.tm_month, date.tm_day, '%Y %m %d')
deltaDate = scanDate - postDate
hoursElapsed = deltaDate.totalseconds() / 3600
elif len(lastPostTime) == 3:
hoursElapsed = postTimeToHour(lastPostTime[:1], lastPostTime[-1])
elif len(lastPostTime) == 2:
hoursElapsed = postTimeToHour(lastPostTime[0], lastPostTime[-1])
else:
# error in date format or "Just Now"
hoursElapsed = None
existingBots = []
bots = []
with open(os.path.realpath(os.path.dirname(__file__)) + os.sep + 'bots.txt', 'r') as file:
for bot in file:
existingBots.append(bot.strip())
print("Elapsed time in scroll:", hoursElapsed)
for poster, times in posters.items():
if poster != "9GAGGER":
# Estimate the average amount of posts per hour from this user.
postPerHour = len(times) / hoursElapsed
if postPerHour > 1:
if not poster in existingBots:
bots.append(poster)
newBotsList = existingBots + bots
print(newBotsList)
newBotsList.sort()
print(newBotsList)
print("HERE ARE THE (IM)POSTERS FOUND")
with open(os.path.realpath(os.path.dirname(__file__)) + os.sep + 'bots.txt', 'w') as file:
for bot in newBotsList:
file.write("\n" + bot)
print(bot)
else:
driver.get("https://9gag.com/login")
input("Log into 9gag. Press ENTER after login.")
with open(os.path.realpath(os.path.dirname(__file__)) + os.sep + 'bots.txt', 'r') as file:
for bot in file:
bot = bot.strip()
driver.get('https://9gag.com/u/'+bot)
try:
if(reportAndBlock):
#Report User
#Click Menu-Button
WebDriverWait(driver,5).until(EC.presence_of_element_located((By.CSS_SELECTOR, ".uikit-popup-menu a")))
driver.find_element(By.CSS_SELECTOR, ".uikit-popup-menu a").click()
#Click Report Button
WebDriverWait(driver,5).until(EC.presence_of_element_located((By.CSS_SELECTOR, ".uikit-popup-menu .menu ul li:nth-child(2) a")))
driver.find_element(By.CSS_SELECTOR, ".uikit-popup-menu .menu ul li:nth-child(2) a").click()
#Click Spam Button
WebDriverWait(driver,5).until(EC.presence_of_element_located((By.CSS_SELECTOR, ".report-type__type div:nth-child(2)")))
driver.find_element(By.CSS_SELECTOR, ".report-type__type div:nth-child(2)").click()
#Click Report Button
WebDriverWait(driver,5).until(EC.presence_of_element_located((By.CSS_SELECTOR, ".report-btn")))
driver.find_element(By.CSS_SELECTOR, ".report-btn").click()
#Block User
#Click Menu-Button
WebDriverWait(driver,5).until(EC.presence_of_element_located((By.CSS_SELECTOR, ".uikit-popup-menu a")))
driver.find_element(By.CSS_SELECTOR, ".uikit-popup-menu a").click()
#Find Block-Button
WebDriverWait(driver,5).until(EC.presence_of_element_located((By.CSS_SELECTOR, ".uikit-popup-menu .menu ul li:nth-child(3) a")))
blockButton = driver.find_element(By.CSS_SELECTOR, ".uikit-popup-menu .menu ul li:nth-child(3) a")
print(blockButton.get_attribute('innerHTML'))
#Check if User is already blocked or not
if "Unblock" not in blockButton.get_attribute('innerHTML'):
blockButton.click()
WebDriverWait(driver,5).until(EC.presence_of_element_located((By.CSS_SELECTOR, ".ui-dialog__actions button.btn-color-danger")))
driver.find_element(By.CSS_SELECTOR, ".ui-dialog__actions button.btn-color-danger").click()
print("User "+bot+" successfully blocked")
else:
driver.get("https://9gag.com")
print("User "+bot+" was already blocked. Skipping...")
except:
print("User "+bot+" was already blocked. Skipping...")