This repository has been archived by the owner on Aug 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
oc_windows.py
244 lines (234 loc) · 12.5 KB
/
oc_windows.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
# this file is specifically made for windows
import os.path
import time
from msedge.selenium_tools import Edge, EdgeOptions
from selenium.webdriver.common.by import By
from selenium.common.exceptions import WebDriverException, StaleElementReferenceException
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.common.action_chains import ActionChains
options = EdgeOptions()
options.use_chromium = True
options.add_argument("-inprivate")
options.add_argument("-headless")
options.add_argument("-disable-gpu")
abort_variable = False
def abort():
global abort_variable
abort_variable = True
def initialize():
print("Initializing windows specific")
with open("config.txt", 'r') as f:
config_file = f.read()
config_file = config_file.split("\n"[-1])
options.binary_location = config_file[1][14:config_file[1].find(r'"', 14)]
print("Checking msedge driver")
if not os.path.isfile(os.getcwd() + r"\msedgedriver.exe"):
print("msedge driver missing")
return "Missing"
else:
print("success")
browser = Edge(executable_path="msedgedriver.exe", options=options)
# checking if browser works
try:
browser.get("https://google.com")
print("Initialization complete, program is working correctly")
except WebDriverException:
print("Please restart the program. If the problem persists, update microsoft edge and your system")
os.remove(os.getcwd() + "msedgedriver.exe")
# reading accounts_file
with open("accounts.txt", 'r') as f:
accounts_file = f.read()
accounts_file = accounts_file[127:]
accounts_file = accounts_file.split("\n"[-1])
def upvote(vote_count, comment_link):
print("mode: upvote")
print(vote_count)
browser = Edge(executable_path="msedgedriver.exe", options=options)
# read accounts.txt
with open("accounts.txt", 'r') as f:
accounts_file = f.read()
accounts_file = accounts_file[125:]
accounts_file = accounts_file.split("\n"[-1])
empty_lines = True
while empty_lines:
if accounts_file[len(accounts_file) - 1].find(":") == -1:
del accounts_file[len(accounts_file) - 1]
else:
empty_lines = False
global abort_variable
abort_variable = False
browser.get("https://reddit.com")
# start loop with vote_count amount
for used_accounts_count in range(vote_count):
print(used_accounts_count)
print(vote_count)
if abort_variable:
print("aborting")
browser.quit()
break
else:
print("while in upvote")
# extract username and password
print("used" + str(used_accounts_count))
username = accounts_file[used_accounts_count][:accounts_file[used_accounts_count].find(r':')]
print(username)
password = accounts_file[used_accounts_count][accounts_file[used_accounts_count].find(r':') + 1:]
print(password)
# login
browser.get("https://www.reddit.com/login/")
browser.find_element(By.ID, "loginUsername").send_keys(username)
browser.find_element(By.ID, "loginPassword").send_keys(password)
# press login
browser.find_element(By.XPATH, "/html/body/div/main/div[1]/div/div[2]/form/fieldset[5]/button").click()
# check if reddit throws login error
if browser.find_element(By.XPATH,
"/html/body/div/main/div[1]/div/div[2]/form/fieldset[1]/div").text == "Incorrect username or password":
print("Incorrect username or password, skipping")
browser.get("https://www.reddit.com/")
used_accounts_count += 1
continue
# elif browser.find_element(By.XPATH, "xpath here").text == "error message here":
# pass
# do timeout
else:
# wait for login redirect
WebDriverWait(browser, 10).until(ec.url_to_be("https://www.reddit.com/"))
print("getting link")
browser.get(comment_link)
# wait until upvote button loads
print("upvoting")
WebDriverWait(browser, 10).until(ec.element_to_be_clickable(
(By.XPATH,
"/html/body/div[1]/div/div[2]/div[2]/div/div/div/div[2]/div[3]/div[1]/div[2]/div[5]/div/div/div/div[1]/div/div/div/div[2]/div[3]/div[3]/div[1]/button[1]")))
# check if already upvoted
if browser.find_element(By.XPATH,
"/html/body/div[1]/div/div[2]/div[2]/div/div/div/div[2]/div[3]/div[1]/div[2]/div[5]/div/div/div/div/div/div/div/div[2]/div[3]/div[3]/div[1]/button[1]") \
.get_attribute("aria-pressed") == "true":
print("already upvoted")
else:
browser.execute_script("arguments[0].click();", browser.find_element(By.XPATH,
"/html/body/div[1]/div/div[2]/div[2]/div/div/div/div[2]/div[3]/div[1]/div[2]/div[5]/div/div/div/div[1]/div/div/div/div[2]/div[3]/div[3]/div[1]/button[1]"))
print("upvoted successfully")
# logout
try:
browser.execute_script("arguments[0].click();", browser.find_element(By.XPATH,
"/html/body/div[1]/div/div[2]/div[1]/header/div/div[2]/div[2]/div/div[2]/button/span[1]/i"))
browser.execute_script("arguments[0].click();", WebDriverWait(browser, 10).until(
ec.element_to_be_clickable((By.XPATH, "/html/body/div[5]/div/a[11]"))))
except StaleElementReferenceException:
browser.refresh()
browser.find_element(By.XPATH,
"/html/body/div[1]/div/div[2]/div[1]/header/div/div[2]/div[2]/div/div[2]").click()
browser.execute_script("arguments[0].click();", WebDriverWait(browser, 10).until(
ec.element_to_be_clickable((By.XPATH, "/html/body/div[5]/div/a[11]"))))
# wait for logout
for time_out in range(1000):
login_xpath = "/html/body/div[1]/div/div[2]/div[1]/header/div/div[2]/div/div[1]/a[1]"
try:
if not browser.find_element(By.XPATH, login_xpath).get_attribute("role") == "button":
time_out += 1
time.sleep(0.01)
continue
else:
break
except StaleElementReferenceException:
continue
print("waited")
used_accounts_count += 1
browser.quit()
def downvote(vote_count, comment_link):
print("mode: downvote")
print(vote_count)
browser = Edge(executable_path="msedgedriver.exe", options=options)
# read accounts.txt
with open("accounts.txt", 'r') as f:
accounts_file = f.read()
accounts_file = accounts_file[125:]
accounts_file = accounts_file.split("\n"[-1])
empty_lines = True
while empty_lines:
if accounts_file[len(accounts_file) - 1].find(":") == -1:
del accounts_file[len(accounts_file) - 1]
else:
empty_lines = False
used_accounts_count = 1
global abort_variable
abort_variable = False
browser.get("https://reddit.com")
# start loop with vote_count amount
for used_accounts_count in range(vote_count):
if abort_variable:
print("aborting")
browser.quit()
break
else:
print("Looping downvoting")
# extract username and password
# print("used" + str(used_accounts_count))
username = accounts_file[used_accounts_count][:accounts_file[used_accounts_count].find(r':')]
# print(username)
password = accounts_file[used_accounts_count][accounts_file[used_accounts_count].find(r':') + 1:]
# print(password)
# login
browser.get("https://www.reddit.com/login/")
browser.find_element(By.ID, "loginUsername").send_keys(username)
browser.find_element(By.ID, "loginPassword").send_keys(password)
# press login
browser.find_element(By.XPATH, "/html/body/div/main/div[1]/div/div[2]/form/fieldset[5]/button").click()
# check if reddit throws login error
if browser.find_element(By.XPATH,
"/html/body/div/main/div[1]/div/div[2]/form/fieldset[1]/div").text == "Incorrect username or password":
print("Incorrect username or password, skipping")
browser.get("https://www.reddit.com/")
used_accounts_count += 1
continue
# elif browser.find_element(By.XPATH, "xpath here").text == "error message here":
# pass
# do timeout
else:
# wait for login redirect
WebDriverWait(browser, 10).until(ec.url_to_be("https://www.reddit.com/"))
print("getting link")
browser.get(comment_link)
# wait until downvote button loads
print("downvoting")
WebDriverWait(browser, 10).until(ec.element_to_be_clickable(
(By.XPATH,
"/html/body/div[1]/div/div[2]/div[2]/div/div/div/div[2]/div[3]/div[1]/div[2]/div[5]/div/div/div/div/div/div/div/div[2]/div[3]/div[3]/div[1]/button[2]")))
# check if already downvoted
if browser.find_element(By.XPATH,
"/html/body/div[1]/div/div[2]/div[2]/div/div/div/div[2]/div[3]/div[1]/div[2]/div[5]/div/div/div/div/div/div/div/div[2]/div[3]/div[3]/div[1]/button[2]") \
.get_attribute("aria-pressed") == "true":
print("already downvoted")
else:
browser.execute_script("arguments[0].click();", browser.find_element(By.XPATH,
"/html/body/div[1]/div/div[2]/div[2]/div/div/div/div[2]/div[3]/div[1]/div[2]/div[5]/div/div/div/div/div/div/div/div[2]/div[3]/div[3]/div[1]/button[2]/span/i"))
print("downvoted successfully")
# logout
try:
browser.execute_script("arguments[0].click();", browser.find_element(By.XPATH,
"/html/body/div[1]/div/div[2]/div[1]/header/div/div[2]/div[2]/div/div[2]/button/span[1]/i"))
browser.execute_script("arguments[0].click();", WebDriverWait(browser, 10).until(
ec.element_to_be_clickable((By.XPATH, "/html/body/div[5]/div/a[11]"))))
except StaleElementReferenceException:
browser.refresh()
browser.find_element(By.XPATH,
"/html/body/div[1]/div/div[2]/div[1]/header/div/div[2]/div[2]/div/div[2]").click()
browser.execute_script("arguments[0].click();", WebDriverWait(browser, 10).until(
ec.element_to_be_clickable((By.XPATH, "/html/body/div[5]/div/a[11]"))))
# wait for logout
for time_out in range(1000):
login_xpath = "/html/body/div[1]/div/div[2]/div[1]/header/div/div[2]/div/div[1]/a[1]"
try:
if not browser.find_element(By.XPATH, login_xpath).get_attribute("role") == "button":
time_out += 1
time.sleep(0.01)
continue
else:
break
except StaleElementReferenceException:
continue
print("waited")
used_accounts_count += 1
browser.quit()