-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
71 lines (53 loc) · 1.85 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
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import csv
import multiprocessing as mp
from multiprocessing import Manager
CONTEST_CODE = "START160A"
PROCESS_LIMIT=6
# create a new Chrome session
driver = webdriver.Chrome()
# make the browser full screen
driver.maximize_window()
def checkStanding(handle, data):
print(f"Opening {handle}")
driver.get(f"https://www.codechef.com/rankings/{CONTEST_CODE}?itemsPerPage=100&order=asc&page=1&search={handle}")
WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.CLASS_NAME, "_scored-problems__header_t8ea1_196")))
try:
row = driver.find_element(By.ID, "MUIDataTableBodyRow-0")
except:
print("Not found!")
return
elements = row.find_elements(By.XPATH, ".//td/div[2]/*")
elements_list = [element.text for element in elements]
elements_list[1] = handle
data[handle] = elements_list
def main():
manager = Manager()
data = manager.dict()
pool = mp.Pool(PROCESS_LIMIT)
jobs = []
with open('handles.txt') as f:
handles = f.read().splitlines()
for handle in handles:
jobs.append(pool.apply_async(checkStanding, (handle, data,)))
for job in jobs:
job.get()
pool.close()
pool.join()
print(data)
values = list(data.values())
values.sort(key=lambda x: int(x[0]))
# close the browser window
driver.quit()
with open(f"{CONTEST_CODE}.csv", "w", newline="") as f:
writer = csv.writer(f)
writer.writerows(values)
if __name__ == '__main__':
main()