-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain_1.py
306 lines (277 loc) · 9.23 KB
/
main_1.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
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
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('ranklist.db',check_same_thread=False)
cursor2 = sqlite3.connect('ranklist.db',check_same_thread=False)
cursor.execute('''
CREATE TABLE IF NOT EXISTS handles(
handle varchar(100) PRIMARY KEY,
rating int,
solved int
);
''')
cursor.execute('''
CREATE TABLE IF NOT EXISTS problems(
ProblemID varchar(10) PRIMARY KEY,
name varchar(255),
rating int,
points int,
link varchar(255),
solved int
);
''')
cursor.execute('''
CREATE TABLE IF NOT EXISTS tags(
ProblemID varchar(10),
tags varchar(255)
);
''')
cursor1 = sqlite3.connect('all_problems.db', check_same_thread=False)
cursor1.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)
);
''')
cursor1.execute('''
CREATE TABLE IF NOT EXISTS tags(
ProblemID varchar(10),
tags varchar(255)
);
''')
@app.route("/all_problems", methods = ['GET','POST'])
def all_problems():
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 = []
print(len(problems))
url1 = "https://codeforces.com/problemset/problem/"
for data in problems:
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)
pdata.append(block)
return render_template('problem.html', pdata = pdata)
@app.route("/new_here")
def new_here():
print("new here")
return render_template('add_handle.html')
@app.route("/add_handle", methods = ['GET','POST'])
def add():
print("adding")
if request.method == 'POST':
handle = (request.form['handle'])
url = "https://codeforces.com/api/user.info?handles=%s" %handle
print(url)
temp = requests.get(url)
obj = json.loads(temp.text)
error = "OK"
# print(obj)
if obj['status'] != "OK":
error = "Error : Account not found"
else:
handle = handle.replace("'", "/'")
qry = "select * from handles where handle = '%s'" %handle
tmp = cursor.execute(qry)
if tmp.fetchone() != None:
error = "Account is already registered"
else:
data = obj['result'][0]
print(data)
if 'country' not in data:
error = "Add Country = India"
elif data['country'] != "India":
error = "Set Country = India in codeforces Profile"
elif 'organization' not in data:
error = "Add Organization = IIT Indore"
elif data['organization'] != "IIT Indore":
error = "Set Organization = IIT Indore in Codeforces Profile"
else:
qry = "insert into handles values ('%s',%d,0)" %(handle, data['rating'])
print(qry)
cursor.execute(qry)
print(1)
cursor.commit()
print(error)
return render_template('add_handle.html', error=error)
return render_template('home.html')
@app.route("/home")
@app.route("/")
@app.route("/ranklist", methods = ['GET', 'POST'])
def show_handles():
tmp = cursor.execute('''select * from handles''')
ranklist = []
while True:
data = tmp.fetchone()
if data == None:
break
print(data)
block = []
block.append(data[0])
block.append(data[1])
block.append(data[2])
link = "https://codeforces.com/profile/" + data[0]
block.append(link)
ranklist.append(block)
return render_template('home.html', ranklist = ranklist)
@app.route("/add_problem")
def new_problem():
print("new here")
return render_template('add_problems.html')
@app.route("/add_problem", methods = ['GET','POST'])
def add_problem():
print("adding")
if request.method == 'POST':
name = (request.form['problemName'])
error = "OK"
name = name.replace("'", "/'")
qry = "select * from problems where name = '%s'" %name
tmp = cursor1.execute(qry)
data = tmp.fetchone()
if data == None:
error = "Question Not Found"
else:
qry = "insert into problems values ('%s','%s',%d,%d,'%s',0)" %(data[0], data[2], data[3], data[4], data[5])
print(qry)
cursor.execute(qry)
qry1 = "select * from tags where problemID = '%s'"%(data[0])
tmp = cursor1.execute(qry1)
while True:
data = tmp.fetchone()
if data == None:
break
qry2 = "insert into tags values ('%s','%s')"%(data[0], data[1])
print(qry2)
cursor.execute(qry2)
print(1)
cursor.commit()
print(error)
return render_template('add_handle.html', error=error)
return render_template('home.html')
@app.route("/problems", methods = ['GET', 'POST'])
def show_problems():
tmp = cursor.execute('''select * from problems''')
pdata = []
while True:
data = tmp.fetchone()
block = []
if data == None:
break
print(data)
block.append(data[1])
block.append(data[2])
block.append(data[3])
tags = []
qry = "select tags from tags where ProblemID = '%s'" %data[0]
tmp1 = cursor.execute(qry)
while True:
tag = tmp1.fetchone()
if tag == None:
break
tags.append(tag[0])
block.append(tags)
block.append(data[4])
block.append(data[5])
pdata.append(block)
return render_template('problems.html', pdata = pdata)
def update():
print("updating")
pset = []
tmp3 = cursor.execute('''Select * from problems''')
while True:
pname = tmp3.fetchone()
if pname == None:
break
pset.append(pname[1])
#print(pset)
tmp = cursor.execute('''SELECT * from handles''')
while True:
handle = tmp.fetchone()
if handle == None:
break
#print("updating %s" %(handle[0]))
cursor2.execute('''update handles set solved = 0 where handle = '%s' '''%(handle[0]))
tmp = cursor.execute('''Select * from problems''')
while True:
problem = tmp.fetchone()
if problem == None:
break
cursor2.execute('''update problems set solved = 0 where name = '%s'; ''' %(problem[1]))
tmp = cursor.execute('''SELECT handle from handles''')
while True:
handle = tmp.fetchone()
probstatus = dict()
if handle == None:
break
try:
url = "https://codeforces.com/api/user.status?handle=%s" %(handle[0])
sub1 = requests.get(url)
sub = json.loads(sub1.text)
if sub['status'] != "OK":
continue
for stat in sub['result']:
if stat['verdict'] == "OK":
probstatus[stat['problem']['name']] = True
for x in probstatus:
if probstatus[x] == True and x in pset:
#print(x)
qry = '''select * from problems where name = "%s" ''' %(x)
tmp2 = cursor2.execute(qry)
if tmp2.fetchone() != None:
try:
qry = '''update handles set solved = solved+1 where handle = "%s" ''' %(handle[0])
cursor2.execute(qry)
except:
print("not possible here")
try:
qry = '''update problems set solved = solved+1 where name = "%s" ''' %(x)
cursor2.execute(qry)
except:
print("not possible here")
print("done %s" %(handle))
except:
print("skipping %s" %(handle))
cursor.commit()
cursor2.commit()
print("done update")
def work():
try:
update()
except:
print("update not possible")
schedule.every(10).minutes.do(work)
def run_loop():
while True:
schedule.run_pending()
time.sleep(1)
if __name__ == "__main__":
app.secret_key = 'sec key'
app.config['SESSION_TYPE'] = 'filesystem'
app.run(debug=True)