-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathclients.py
More file actions
360 lines (292 loc) · 11.3 KB
/
Copy pathclients.py
File metadata and controls
360 lines (292 loc) · 11.3 KB
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
from __future__ import annotations
import json
import re
import shutil
import string
import subprocess
import time
from typing import Any, Set, cast
import requests
import undetected_chromedriver as uc
from bs4 import BeautifulSoup
from config import (
CODEFORCES_API_BASE,
CODEFORCES_LOGIN_URL,
CODEFORCES_SUBMISSION_URL,
DELAY_AFTER_PAGE_FETCH,
SPOJ_BASE_URL,
)
from exceptions import APIError
from models import Submission
from ui import Console
def _get_chrome_version() -> int | None:
chrome_paths = [
'google-chrome',
'google-chrome-stable',
'chromium',
'chromium-browser',
'/usr/bin/google-chrome',
]
for path in chrome_paths:
if shutil.which(path) or path.startswith('/'):
try:
result = subprocess.run(
[path, '--version'],
capture_output=True,
text=True,
timeout=5,
)
if result.returncode == 0:
match = re.search(r'(\d+)\.', result.stdout)
if match:
return int(match.group(1))
except (subprocess.SubprocessError, FileNotFoundError):
continue
return None
class BrowserResponse:
def __init__(self, text: str, url: str, status_code: int = 200) -> None:
self.text = text
self.url = url
self.status_code = status_code
def json(self) -> dict[str, Any]:
return cast(dict[str, Any], json.loads(self.text))
class BrowserSession:
def __init__(self, ui: Console) -> None:
self._driver: uc.Chrome | None = None
self._cookies_valid = False
self._ui = ui
def _create_driver(self, headless: bool) -> uc.Chrome:
chrome_version = _get_chrome_version()
options = uc.ChromeOptions()
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.page_load_strategy = 'eager'
if headless:
options.add_argument('--headless=new')
return uc.Chrome(options=options, version_main=chrome_version)
def _ensure_driver(self) -> uc.Chrome:
if self._driver is None:
self._driver = self._create_driver(headless=False)
return self._driver
def _is_cloudflare_challenge(self) -> bool:
try:
if not self._driver:
return False
page_source = self._driver.page_source
title = self._driver.title or ''
challenge_titles = [
'Just a moment',
'Attention Required',
'Verification',
]
for ct in challenge_titles:
if ct in title:
return True
if len(title) > 5 and 'Verification' not in title:
return False
if len(page_source) < 500:
return True
return False
except Exception:
return True
def _after_captcha_solved(self) -> None:
self._ui.print_info('CAPTCHA solved!')
time.sleep(3)
if self._driver:
self._driver.refresh()
time.sleep(2)
self._cookies_valid = True
def _wait_for_cloudflare(self, url: str, timeout: int = 120) -> None:
if not self._is_cloudflare_challenge():
self._cookies_valid = True
return
self._ui.print_info('Please solve the CAPTCHA in the browser window...')
start = time.time()
while time.time() - start < timeout:
if not self._is_cloudflare_challenge():
time.sleep(2)
self._after_captcha_solved()
return
time.sleep(1)
self._ui.print_error('Warning: Cloudflare wait timeout')
def get(self, url: str, **kwargs: Any) -> BrowserResponse:
driver = self._ensure_driver()
try:
from selenium.webdriver.common.keys import Keys
driver.find_element('tag name', 'body').send_keys(Keys.TAB)
except Exception:
pass
driver.get(url)
self._wait_for_cloudflare(url, timeout=120)
time.sleep(1)
return BrowserResponse(driver.page_source, driver.current_url)
def post(
self, url: str, data: dict[str, Any] | None = None, **kwargs: Any
) -> BrowserResponse:
driver = self._ensure_driver()
if url not in (driver.current_url or ''):
driver.get(url)
self._wait_for_cloudflare(url, timeout=120)
if data:
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
for name, value in data.items():
try:
field = driver.find_element(By.NAME, name)
field.clear()
field.send_keys(str(value))
except Exception:
driver.execute_script(
"var el = document.querySelector('[name=\"' + arguments[0] + '\"]'); if (el) el.value = arguments[1];",
name,
str(value),
)
try:
password_field = driver.find_element(
By.CSS_SELECTOR,
"input[type='password']",
)
password_field.send_keys(Keys.RETURN)
except Exception:
driver.execute_script(
"var form = document.querySelector('form'); if (form) form.submit();"
)
time.sleep(3)
self._wait_for_cloudflare(url, timeout=120)
return BrowserResponse(driver.page_source, driver.current_url)
def minimize(self) -> None:
if self._driver:
try:
self._driver.minimize_window()
except Exception:
pass
def close(self) -> None:
if self._driver:
self._driver.quit()
self._driver = None
def __enter__(self) -> 'BrowserSession':
return self
def __exit__(self, *args: Any) -> None:
self.close()
class CodeforcesClient:
def __init__(
self,
session: BrowserSession,
delay_after_fetch: float = DELAY_AFTER_PAGE_FETCH,
) -> None:
self.session = session
self.delay_after_fetch = delay_after_fetch
self._gym_contests: Set[int] = set()
self._regular_contests: Set[int] = set()
def login(self, handle: str, password: str) -> bool:
page = self.session.get(CODEFORCES_LOGIN_URL)
soup = BeautifulSoup(page.text, 'html.parser')
time.sleep(self.delay_after_fetch)
csrf_token = soup.find('input', {'name': 'csrf_token'})
if csrf_token is None:
return False
data = {
'handleOrEmail': handle,
'password': password,
'csrf_token': csrf_token['value'],
'action': 'enter',
}
response = self.session.post(CODEFORCES_LOGIN_URL, data=data)
soup = BeautifulSoup(response.text, 'html.parser')
return soup.find('input', {'name': 'handleOrEmail'}) is None
def load_contest_info(self) -> None:
for gym_status in ('false', 'true'):
try:
response = requests.get(
f'{CODEFORCES_API_BASE}/contest.list?gym={gym_status}'
).json()
except (requests.RequestException, ValueError) as exc:
print(str(exc))
raise APIError('Error getting contests info.') from exc
if response['status'] != 'OK':
print(f'status: {response["status"]}')
raise APIError('Error getting contests info.')
for contest in response['result']:
if gym_status == 'true':
self._gym_contests.add(contest['id'])
else:
self._regular_contests.add(contest['id'])
def is_gym_contest(self, contest_id: int) -> bool:
return contest_id in self._gym_contests
def get_submissions_metadata(self, handle: str) -> list[dict[str, Any]]:
try:
response = requests.get(
f'{CODEFORCES_API_BASE}/user.status?handle={handle}'
).json()
except (requests.RequestException, ValueError) as exc:
raise APIError('Error getting submission info.') from exc
if response['status'] != 'OK':
raise APIError('Error getting submission info.')
return cast(list[dict[str, Any]], response['result'])
def get_source_code(self, submission: Submission) -> str | None:
contest_type = 'gym' if submission.is_gym() else 'contest'
url = CODEFORCES_SUBMISSION_URL.format(
contest_type=contest_type,
contest_id=submission.contest_id,
submission_id=submission.submission_id,
)
page = self.session.get(url)
time.sleep(self.delay_after_fetch)
soup = BeautifulSoup(page.text, 'html.parser')
source_element = soup.find(id='program-source-text')
if source_element is None:
return None
lines = source_element.find_all('li')
if lines:
source_lines = []
for li in lines:
line_text = li.get_text()
line_text = line_text.replace('\xa0', ' ')
source_lines.append(line_text)
source = '\n'.join(source_lines)
else:
for br in source_element.find_all('br'):
br.replace_with('\n')
source = source_element.get_text()
source = source.replace('\xa0', ' ')
printable = set(string.printable)
return ''.join(c for c in source if c in printable).rstrip()
@property
def gym_contests(self) -> Set[int]:
return self._gym_contests
@property
def regular_contests(self) -> Set[int]:
return self._regular_contests
class SpojClient:
def __init__(self, session: BrowserSession) -> None:
self.session = session
def login(self, handle: str, password: str) -> bool:
login_url = f'{SPOJ_BASE_URL}/login/'
data = {
'next_raw': '/',
'login_user': handle,
'password': password,
}
self.session.post(login_url, data=data)
time.sleep(2)
self.session.get(f'{SPOJ_BASE_URL}/')
page = self.session.get(f'{SPOJ_BASE_URL}/myaccount')
soup = BeautifulSoup(page.text, 'html.parser')
return soup.find('a', {'href': '/login'}) is None
def get_my_account_page(self) -> BeautifulSoup:
page = self.session.get(f'{SPOJ_BASE_URL}/myaccount')
return BeautifulSoup(page.text, 'html.parser')
def get_submission_page(self, link: str) -> BeautifulSoup:
page = self.session.get(f'{SPOJ_BASE_URL}{link}')
return BeautifulSoup(page.text, 'html.parser')
def get_edit_page(self, edit_link: str) -> str:
page = self.session.get(f'{SPOJ_BASE_URL}{edit_link}')
return cast(str, page.text)
def get_source_code(self, submission: Submission) -> str | None:
if submission.source is None:
return None
soup = BeautifulSoup(submission.source, 'html.parser')
textarea = soup.find('textarea')
if textarea:
return textarea.get_text()
return None