forked from CrapTheCoder/UpCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CodeForcesScraper.py
167 lines (122 loc) · 5.88 KB
/
CodeForcesScraper.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
import requests
import json
from json.decoder import JSONDecodeError
import logging
from time import sleep
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import *
logging.getLogger('WDM').setLevel(logging.NOTSET)
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36',
}
def get_submission_info(username):
submissions = json.loads(requests.get(f'https://codeforces.com/api/user.status?handle={username}').text)['result']
for submission in submissions:
if submission['verdict'] == 'OK':
try:
if len(str(submission["problem"]["contestId"])) <= 4 and len(submission["author"]["members"]) == 1:
yield {
'language': submission['programmingLanguage'],
'problem_code': f'{submission["problem"]["contestId"]}{submission["problem"]["index"]}',
'solution_id': submission['id'],
'problem_name': submission['problem']['name'] if 'name' in submission['problem'] else '',
'problem_link': f'https://codeforces.com/contest/{submission["problem"]["contestId"]}/problem/{submission["problem"]["index"]}',
'link': f'https://codeforces.com/contest/{submission["contestId"]}/submission/{submission["id"]}?f0a28=2',
}
except KeyError:
pass
def get_code(driver):
lines = driver.find_elements(By.CSS_SELECTOR, '#program-source-text > ol > li')
return '\n'.join(line.text for line in lines)
def get_solutions(username, all_info=None):
try:
if all_info is None:
all_info = list(get_submission_info(username))
except JSONDecodeError:
logging.error("CodeForces API is currently unavailable. Please try again later.")
return
sub_id_info = {info['solution_id']: info for info in all_info}
for info in all_info:
sub_id_info[info['solution_id']] = info
options = Options()
options.add_argument('headless')
options.add_argument('window-size=1920x1080')
options.add_argument('disable-gpu')
driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)
driver.get(f'https://codeforces.com/submissions/{username}')
sleep(1)
select = Select(driver.find_element(By.ID, 'verdictName'))
select.select_by_value('OK')
driver.find_element(By.CSS_SELECTOR, '#sidebar > div.roundbox.sidebox.status-filter-box > div:nth-child(5) > form > div:nth-child(4) > input[type=submit]:nth-child(1)').click()
sub_ids = [info['solution_id'] for info in all_info]
try:
pages = int(driver.find_elements(By.CSS_SELECTOR, '#pageContent > div > ul > li > span')[-1].text)
except IndexError:
pages = 1
index = 1
driver.get(f'https://codeforces.com/submissions/{username}/page/{index}')
prev = {}
fail_counter = 0
failed = []
for sub_id in sub_ids:
if index > pages:
break
if fail_counter:
try:
driver.get(sub_id_info[sub_id]['link'])
code = get_code(driver)
sub_id_info[sub_id]['solution'] = code
prev[code] = sub_id_info[sub_id]
yield sub_id_info[sub_id]
fail_counter -= 1
if fail_counter == 0:
driver.get(f'https://codeforces.com/submissions/{username}/page/{index}')
sleep(0.3)
continue
except NoSuchElementException:
failed.append((sub_id, index))
sleep(120)
driver.quit()
driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)
driver.get(f'https://codeforces.com/submissions/{username}/page/{index}')
fail_counter = 0
for _ in range(3):
try:
element = driver.find_element(By.PARTIAL_LINK_TEXT, str(sub_id))
driver.execute_script("arguments[0].click();", element)
sleep(0.3)
counter = 0
cur = []
while not cur or ((cur in prev) and ((
sub_id_info[sub_id]['problem_code'] != prev[cur]['problem_code']) or
(sub_id_info[sub_id]['problem_code'][-1].isdigit() and prev[cur]['problem_code'][-1].isdigit()))
):
sleep(0.3)
cur = '\n'.join(ele.text for ele in driver.find_elements(By.CSS_SELECTOR, '#facebox > div > div > div > pre > code > ol > li'))
counter += 1
if counter % 3 == 0:
driver.refresh()
driver.execute_script("arguments[0].click();", element)
sleep(0.3)
cur = '\n'.join(ele.text for ele in driver.find_elements(By.CSS_SELECTOR, '#facebox > div > div > div > pre > code > ol > li'))
code = cur
sub_id_info[sub_id]['solution'] = code.replace('\u00a0', '\n')
prev[code] = sub_id_info[sub_id]
yield sub_id_info[sub_id]
break
except NoSuchElementException:
if driver.current_url == 'https://codeforces.com/':
sleep(60)
else:
index += 1
driver.get(f'https://codeforces.com/submissions/{username}/page/{index}')
break
except StaleElementReferenceException:
driver.execute_script("location.reload(true);")
sleep(2)
else:
fail_counter = 5