-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.py
251 lines (185 loc) · 6.24 KB
/
app.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
import sys
import os
from selenium import webdriver
from time import sleep
import time
import getpass
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
#for chrome
#driver = webdriver.Chrome()
#chrome end
#for firefox
#driver = webdriver.Firefox()
#firefox end
#Default run
'''
driver = webdriver.Chrome
'''
while(1):
print('select your browser: ')
print('1. Chrome')
print('2. Firefox')
x = int(input())
if(x==1):
driver = webdriver.Chrome()
break
elif(x==2):
driver = webdriver.Firefox()
break
else:
print('Invalid Input, try again...')
driver.get("https://www.hackerrank.com/login")
window_before = driver.window_handles[0]
'''
# for a faster experience, set this for a default account.
# Comment out the 2 lines of code below after this.
Username = ''
Password = ''
'''
Username = str(input('Enter Username: '))
Password = getpass.getpass('Enter the Password: ')
user = driver.find_element_by_xpath('//*[@id="input-1"]')
user.send_keys(Username)
passw = driver.find_element_by_xpath('//*[@id="input-2"]')
passw.send_keys(Password)
time.sleep(1)
buttons = driver.find_elements_by_tag_name('button')
for button in buttons:
if(button.text == 'Log In'):
button.click()
time.sleep(3)
check_login = driver.find_elements_by_xpath("//*[contains(text(), 'Invalid login or password. Please try again.')]")
if(check_login):
print('Sorry,Invalid login!, Launch Again')
time.sleep(2)
driver.quit()
sys.exit()
url = 'https://www.hackerrank.com/domains/algorithms?badge_type=problem-solving'
driver.execute_script("window.open('%s')" % url)
#get the window opened in the new tab
window_after = driver.window_handles[1]
prob_name = str(input("Enter the problem name: "))
#attr = data[str(prob_name)]
#switch on to new child window
driver.switch_to.window(window_after)
body = driver.find_element_by_css_selector('body')
# try:
# #WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@data-attr1='%s']" % attr))).click()
# WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, '%s' % prob_name))).click()
# except:
# driver.quit()
# sys.exit("Sorry, can't seem to find the solution!")
i=0
print('Searching...')
try:
while(1):
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
try:
element = driver.find_element_by_partial_link_text(prob_name)
if(element):
element.click()
break
except:
pass
i+=1
time.sleep(2)
if(i>=45):
driver.quit()
sys.exit("Sorry, can't seem to find the solution!")
time.sleep(5)
print('Done!')
except:
print('..Search terminated..')
sys.exit("Exiting..Can't seem to find the solution")
#get the window handle that has opened in the new tab
board = driver.window_handles[1]
#switch on to new child window
driver.switch_to.window(board)
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//a[@data-attr1='topscorers']"))).click()
time.sleep(2)
#but = driver.find_element_by_xpath("//span[@class = 'Select-arrow-zone']")
button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//span[@class = 'Select-arrow-zone']")))
button.click()
time.sleep(3)
sorter = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[@aria-label = '100']")))
print('Listing the Top 100 Submissions')
sorter.click()
time.sleep(3)
lang = '' # Do not not leave emmpty, else default language(cpp) is assumed
if(lang == ''):
print("Default Language is selected")
else:
print("Selected language is %s" %(lang))
'''
Available Options( Case Sensitive ) for lang are, just copy and paste any one according to your language preference:
cpp
python3
c
python
java
go
java8
csharp
javascript
php
ruby
haskell
bash
scala
'''
flag = 1
check = 0
reveal = driver.find_elements_by_tag_name('button')
for button in reveal:
if(button.text == 'Reveal solutions'):
check = 1
print('reveal button located')
time.sleep(5)
if(check == 0):
try:
flag = 1
print('Looking for a %s submission' % lang)
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//a[@data-attr2='%s']" % lang))).click()
except:
flag = 0
print("Couldn't find a submission in %s , looking for it in cpp" % lang)
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//a[@data-attr2='cpp']"))).click()
elif(check == 1):
print("Revealing Submissions")
for button in reveal:
if(button.text == 'Reveal solutions'):
button.click()
conf2 = driver.find_elements_by_tag_name('button')
for button in conf2:
if(button.text == 'Yes'):
button.click()
time.sleep(5)
try:
flag = 1
print('Looking for a %s submission' % lang)
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//a[@data-attr2='%s']" % lang))).click()
except:
flag = 0
print("Couldn't find a submission in %s , looking for it in cpp" % lang)
e = WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.XPATH, "//a[@data-attr2='cpp']")))
driver.execute_script("arguments[0].click();", e)
time.sleep(2)
code = driver.window_handles[2]
driver.switch_to.window(code)
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "/html/body/pre")))
element = driver.find_element_by_css_selector('body')
time.sleep(3)
element.send_keys(Keys.CONTROL + 'a')
if(flag==1):
filename = "code/%s_%s.txt" % (prob_name , lang)
else:
filename = "code/%s_cpp.txt" % (prob_name)
os.makedirs(os.path.dirname(filename), exist_ok=True)
with open(filename, "w") as f:
f.write(element.text)
f.close()
sys.exit("Borat: Great Success!!")