-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.py
160 lines (144 loc) · 4.89 KB
/
app.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
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,
solved int
);
''')
cursor.execute('''
CREATE TABLE IF NOT EXISTS problems(
name varchar(100) PRIMARY KEY,
link varchar(1000),
solved int,
level varchar(2)
);
''')
@app.route("/new_here")
def new_here():
return render_template('add_handle.html')
@app.route("/add_handle",methods = ['GET','POST'])
def add():
if request.method == 'POST':
url = "https://codeforces.com/api/user.status?handle=%s" %(request.form['handle'])
req = requests.get(url)
jobj = json.loads(req.text)
error = False
duplicate = False
if jobj['status'] != "OK":
error = True
else:
qry = "select * from handles where handle = '%s'" %(request.form['handle'])
tmp = cursor.execute(qry)
if tmp.fetchone() == None:
cursor.execute('''insert into handles values (?,?)''',(request.form['handle'],0))
else:
duplicate = True
cursor.commit()
return render_template('add_handle.html',error=error,duplicate=duplicate)
return home()
def update():
print("updating")
pset = []
tmp3 = cursor.execute('''Select * from problems''')
while True:
pname = tmp3.fetchone()
if pname == None:
break
pset.append(pname[0])
#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[0]))
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")
# return render_template('home.html')
def test():
print("hi")
@app.route("/")
def home():
ranks = []
tmp = cursor.execute('''select * from handles order by solved desc''')
while True:
handle = tmp.fetchone()
if handle==None:
break
ranks.append((handle[0],handle[1]))
return render_template('home.html',ranklist=ranks)
@app.route("/problems")
def problems():
psolves = []
tmp = cursor.execute('''select * from problems order by solved desc''')
while True:
problem = tmp.fetchone()
if problem == None:
break
psolves.append((problem))
return render_template('problems.html',pdata = psolves)
#add dynamic search
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__":
p = multiprocessing.Process(target=run_loop)
p.start()
app.run(port=os.environ.get("PORT",5000), host='0.0.0.0')