-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.py
223 lines (201 loc) · 10.9 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
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
import re
import secrets.credentials as secret
import urllib.request
from bs4 import BeautifulSoup
import requests
import time
import hashlib
from tinydb import Query
from urllib.request import urlopen, Request
from pyquery import PyQuery as pq
import mariadb
import sys
import random
# Change these numbers accordingly if you are building the database
alreadyProcessedProblems = 1540
numberOfPageToUpdate = 1
##########################################################################################################
# Connect to MariaDB database
##########################################################################################################
try:
conn = mariadb.connect(
user=secret.user,
password=secret.password,
host=secret.host,
port=secret.port,
database=secret.database,
autocommit=True
)
except mariadb.Error as e:
print(f"Error connecting to MariaDB Platform: {e}")
sys.exit(1)
# Get Cursor
db = conn.cursor()
##########################################################################################################
##########################################################################################################
# Enter all problems into external database table 'problems'
##########################################################################################################
url = "https://codeforces.com/api/problemset.problems"
requestPrefix = str(random.randrange(100000, 1000000))
apiSigString = hashlib.sha512(
(requestPrefix + "/problemset.problems?apiKey=" + secret.apiKey + "&time=" + int(time.time()).__str__() +
"#" + secret.apiSecret).encode("utf-8")).hexdigest()
apiSig = requestPrefix + apiSigString
params = {"apiKey": secret.apiKey, "time": int(time.time()), "apiSig": apiSig}
data = requests.get(url, params=params)
problems = data.json()['result']['problems']
april_fool_contests = "(1505, 1331, 1145, 952, 784, 656, 409, 290, 171)"
for problem in problems:
if int(problem['contestId']) <= alreadyProcessedProblems:
continue
try:
print("Inserting into problems: " + str(problem['contestId']) + str(problem['index']))
db.execute(
"INSERT INTO problems (problem_id, contest_id, contest_index, problem_name, problem_type, problem_url) "
"VALUES (?,?,?,?,?,?)",
(str(problem['contestId']) + problem['index'], int(problem['contestId']), problem['index'], problem['name'],
problem['type'],
"https://codeforces.com/problemset/problem/" + str(problem['contestId']) + "/" + problem['index']))
except mariadb.Error as e:
print(f"Error: {e}")
##########################################################################################################
##########################################################################################################
# Find statement lengths of all problems
##########################################################################################################
db.execute(
"SELECT problem_id, problem_url from problems"
)
problemSet = db.fetchall()
for (problem_id, problem_url) in problemSet:
if int(re.findall(r'\d+', problem_id)[0]) <= alreadyProcessedProblems:
continue
try:
print("Updating problem statement length: " + problem_id)
req = Request(problem_url, headers={'User-Agent': 'Mozilla/5.0'})
webpage = urlopen(req).read()
html_code = pq(webpage)
statement = html_code.find('.problem-statement').remove('.header').remove('.input-specification').remove(
'.output-specification').remove('.sample-tests').html()
qq = Query()
if statement is not None:
db.execute(
"UPDATE problems SET problem_statement_length = ? WHERE problem_id = ?",
(len(statement), problem_id))
except mariadb.Error as e:
print(f"Error: {e}")
continue
##########################################################################################################
##########################################################################################################
# Add tags, problem ratings and solved_by
##########################################################################################################
db.execute("SELECT * FROM tags")
tags = db.fetchall()
tagMap = {}
for (tag_id, tag_key) in tags:
tagMap[tag_key] = tag_id
url = "https://codeforces.com/problemset/page/"
for num in range(1, numberOfPageToUpdate):
print("Processing page: " + str(num))
req = urllib.request.urlopen(url + str(num))
data = BeautifulSoup(req, "html.parser")
for row in data.find_all('tr'):
try:
problem_id = row.find('a').text.strip()
if int(re.findall(r'\d+', problem_id)[0]) <= alreadyProcessedProblems:
continue
db.execute("SELECT problem_id FROM problems WHERE problem_id = ?", (problem_id,))
problem = db.fetchall()
if len(problem) == 1:
tags = list(map(lambda tag: tag.text.strip(), row.findAll("a", attrs={"class": "notice"})))
for tag in tags:
try:
db.execute(
"INSERT INTO problems_tags (problem_id, tag_id) VALUES (?,?)", (problem_id, tagMap[tag])
)
except Exception as e:
print(f"Error: {e}")
continue
rating = int(row.find("span", attrs={"class": "ProblemRating"}).text.strip())
solved_by = int(row.find("a", attrs={"title": "Participants solved the problem"}).text[2:])
try:
db.execute(
"UPDATE problems SET difficulty_rating = ?, solved_by = ? where problem_id = ?", (rating,
solved_by,
problem_id)
)
except Exception as e:
print(f"Error: {e}")
continue
print('#%d done' % num)
except Exception as e:
print(f"Error: {e}")
continue
##########################################################################################################
##########################################################################################################
# Print problems by ratings to Markdown table
##########################################################################################################
f = open('problem_table.txt', 'w')
problem_stats_f = open('problem_stats_table.txt', 'w')
stats = {}
for x in range(800, 4000, 100):
db.execute("SELECT problem_id, problem_name, problem_url, problem_statement_length, "
"difficulty_rating, solved_by FROM problems WHERE difficulty_rating = " + str(x) +
" AND problem_statement_length <= 1000 AND contest_id not in " + april_fool_contests +
"ORDER BY problem_statement_length")
problems = db.fetchall()
if len(problems) >= 1:
f.write("<details>\n"
" <summary><span id=" + str(x) + ">" + str(x) + "</span></summary>\n\n"
)
f.write("|# | ID | Problem | Rating |\n"
"|--- | ---| ----- | ---------- |\n")
index = 1
for (problem_id, problem_name, problem_url, problem_statement_length,
difficulty_rating, solved_by) in problems:
try:
f.write(
"|" + str(index) + "|" + problem_id + "|[" + problem_name + "](" + problem_url + ")|"
+ str(difficulty_rating) + "|\n")
except Exception as e:
print(f"Error: {e}")
continue
index = index + 1
f.write("</details>")
f.write("\n\n")
stats[str(x)] = str(index - 1)
problem_stats_f.write("| Title | Links |\n"
"|:---: | :---:|\n"
"| Newbie| [800](#700)(" + stats['800'] + ") [900](#800)(" + stats['900'] + ") [1000](#900)(" +
stats['1000'] + ") [1100](#1000)(" + stats['1100'] + ")|\n"
"| Pupil | [1200](#1100)(" + stats[
'1200'] + ") [1300](#1200)(" + stats['1300'] + ") |\n"
"| Specialist| [1400](#1300)(" + stats[
'1400'] + ") [1500](#1400)(" + stats['1500'] + ") |\n"
"| Expert | [1600](#1500)(" + stats[
'1600'] + ") [1700](#1600)(" + stats['1700'] + ") [1800](#1700)(" + stats['1800'] + ") |\n"
"| Candidate Master| [1900](#1800)(" +
stats['1900'] + ") [2000](#1900)(" + stats['2000'] + ") |\n"
"| Master | [2100](#2000)(" + stats[
'2100'] + ") [2200](#2100)(" + stats['2200'] + ")|\n"
"| International Master| [2300](#2200)(" +
stats['2300'] + ")|\n"
"| Grandmaster| [2400](#2300)(" + stats['2400'] + ") [2500](#2400)(" + stats[
'2500'] + ")|\n"
"| International Grandmaster| [2600](#2500)(" + stats['2600'] + ") [2700](#2600)(" +
stats['2700'] + ") [2800](#2700)("
"" + stats['2800'] + ") ["
"2900](#2800)(" + stats[
'2900'] + ") |\n"
"| <span id=700>Legendary Grandmaster</span>| [3000](#2900)(" + stats['3000'] +
") [3100](#3000)(" +
stats['3100'] + ") [3200](#3100)(" + stats[
'3200'] + ") [3300]("
"#3200)(" + stats['3300'] + ") ["
"3400]("
"#3300)(" + stats['3400'] + ") ["
"3500](#3400)(" + stats[
'3500'] + ") |\n")
##########################################################################################################
##########################################################################################################
# Print index table to Markdown table
##########################################################################################################