-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathboj.py
123 lines (96 loc) · 3.78 KB
/
boj.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
import re
import requests
import json
from bs4 import BeautifulSoup
BOJ_URL = 'https://www.acmicpc.net'
STATUS_URL = '%s/status' % (BOJ_URL)
DOWNLOAD_URL = '%s/source/download' % (BOJ_URL)
SOURCE_URL = '%s/source' % (BOJ_URL)
class BOJ:
def __init__(self):
self.user_id = None
self.cookie = None
def login_boj(self, user_id, user_password):
url = '%s/%s' % (BOJ_URL, 'signin')
data = {'login_user_id': user_id, 'login_password': user_password}
res = requests.post(url=url, data=data, allow_redirects=False)
if res.status_code != 302:
return True, str(res.status_code)
if not 'Set-Cookie' in res.headers:
return True, 'Cookie not found'
if not 'Location' in res.headers:
return True, 'Redirecting error'
if 'error' in res.headers['Location']:
return True, 'Login failed for user'
cookie = ''
flag_cfduid = False
flag_oj = False
for element in re.split(',| ', res.headers['Set-Cookie']):
if '__cfduid' in element:
cookie += element + ' '
flag_cfduid = True
if 'OnlineJudge' in element:
cookie += element
flag_oj = True
if not (flag_cfduid and flag_oj):
return True, 'Invalid cookie'
self.user_id = user_id
self.cookie = cookie
return False, 'Login succeed'
def get_solved_problems(self):
if self.user_id is None:
return True, 'Login is required'
url = STATUS_URL
params = {'user_id': self.user_id, 'language_id': '-1', 'result_id': '4'}
res = requests.get(url=url, params=params)
if res.status_code != 200:
return True, str(res.status_code)
soup = BeautifulSoup(res.text, 'html.parser')
selector = soup.find(id='status-table').tbody.find_all('tr')
if selector is None:
return True, 'Selector is none'
solved_problems = []
for tr in selector:
td = tr.find_all('td')
problem = {}
problem['submission_id'] = td[0].text
problem['problem_id'] = td[2].a.text
problem['problem_title'] = td[2].a['title']
problem['memory'] = td[4].text
problem['time'] = td[5].text
problem['language'] = td[6].text
problem['length'] = td[7].text
problem['date'] = td[8].a['title']
solved_problems.append(problem)
return False, solved_problems
def download_source(self, submission_id):
if self.cookie is None:
return True, 'Login is required'
url = '%s/%s' % (DOWNLOAD_URL, submission_id)
headers = {'Cookie': self.cookie}
res = requests.get(url=url, headers=headers)
if res.status_code != 200:
return True, str(res.status_code)
if res.text is None:
return True, 'Source is none'
source = res.text
return False, source
def get_private(self, submission_id):
if self.cookie is None:
return True, 'Login is required'
url = '%s/%s' % (SOURCE_URL, submission_id)
headers = {'Cookie': self.cookie}
res = requests.get(url=url, headers=headers)
if res.status_code != 200:
return True, str(res.status_code)
soup = BeautifulSoup(res.text, 'html.parser')
selector = soup.find_all('input', {'name': 'code_open'})
if selector is None:
return True, 'Selector is none'
if selector[1].has_attr('checked'):
return False, False
return False, True
def get_user_id(self):
if self.user_id is None:
return True, 'Login is required'
return False, self.user_id