-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathall_problem.py
89 lines (77 loc) · 2.49 KB
/
all_problem.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
import requests,schedule,sqlite3,json
from operator import itemgetter
from flask import Flask,render_template,session,request,redirect,url_for,flash
import os
import multiprocessing
import time
app = Flask(__name__)
cursor = sqlite3.connect('all_problems.db',check_same_thread=False)
cursor.execute('''
CREATE TABLE IF NOT EXISTS problems(
ProblemID varchar(10) PRIMARY KEY,
problemsetName varchar(255),
name varchar(255),
rating int,
points int,
link varchar(255)
);
''')
cursor.execute('''
CREATE TABLE IF NOT EXISTS tags(
ProblemID varchar(10),
tags varchar(255)
);
''')
@app.route("/all_problems", methods = ['GET','POST'])
def home():
print(request.method)
print(1)
url = "https://codeforces.com/api/problemset.problems?"
print(url)
temp = requests.get(url)
obj = json.loads(temp.text)
problems = []
for problem in obj['result']['problems']:
problems.append(problem)
pdata = []
url1 = "https://codeforces.com/problemset/problem/"
for data in problems:
qry1 = "insert into problems values ("
qry2 = "insert into tags values ("
block = []
block.append(data['name'])
if 'rating' not in data:
block.append(0)
else:
block.append(data['rating'])
if 'points' not in data:
block.append(0)
else:
block.append(data['points'])
block.append(data['tags'])
code = ""
if 'contestId' in data and 'index' in data:
link = url1 + str(data['contestId']) + "/" + str(data['index'])
block.append(link)
code = str(data['contestId']) + str(data['index'])
qry1 = qry1 + "'" + code + "'"
qry2 = qry2 + "'" + code + "'"
pdata.append(block)
name = block[0]
name = name.replace("'", "''")
qry1 = qry1 + ",'" + "main" + "','"+ name + "'," + str(block[1]) + "," + str(block[2]) + ",'" + block[4] + "')"
cursor.execute(qry1)
for tag in block[3]:
qry3 = qry2 + ",'" + tag +"')"
cursor.execute(qry3)
qry = "select * from problems"
tmp = cursor.execute(qry)
for i in range(0, 10):
problem = tmp.fetchone()
print(problem)
cursor.commit()
return render_template('problem.html', pdata = pdata)
if __name__ == "__main__":
app.secret_key = 'sec key'
app.config['SESSION_TYPE'] = 'filesystem'
app.run(debug=True)