Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
179 lines (151 loc) · 5.12 KB
/
Copy pathmain.py
File metadata and controls
179 lines (151 loc) · 5.12 KB
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
from flask import *
from flask_cors import CORS
from urllib.parse import quote, unquote
import file, ErrorHook
import json, requests, time, random, math, hashlib, os, operator, base64, uuid, threading
app = Flask(__name__)
CORS(app)
browser = "C:\Program Files\Google\Chrome\Application\chrome.exe"
dataPath="\\data_"
# File Start
@app.route("/layui/<path:filename>")
def layuiJs(filename):
return send_from_directory("layui", filename)
@app.route("/layui/<path:pathname>/<path:filename>")
def layui(pathname, filename):
return send_from_directory("layui\\" + pathname, filename)
# File End
@app.route("/")
def _():
return redirect("/index")
@app.route("/index")
def _index():
hide = False
for i, j in seted.items():
hide = hide or j
return render_template(
"index.html",
luogu=seted["luogu"],
hide=("none" if hide else ""),
unhide=("" if hide else "none"),
)
@app.route("/record")
def record():
return render_template("record.html", oj_type=request.args.get("oj", 'luogu'),
hide=request.args.get('hide','no'),height=request.args.get('height','75%'))
@app.route("/luoguProblem")
def luoguRecord():
data=luogu_problem()
pre=data['currentData']['record']['sourceCode']
subtasks=data['currentData']['record']['detail']['judgeResult']['subtasks']
message=data['currentData']['record']['detail']['compileResult']['message']
# print(data)
return render_template("luoguProblem.html",
pre=pre,
subtasks=subtasks,
message=message.replace('\\','\\\\').replace('\n','\\n') if message!=None else "",
)
@app.route("/newOJ", methods=["GET", "POST"])
def newOJ():
global user,cookie
if request.method == "GET":
return render_template("newOJ.html")
else:
oj_type = request.form.get("oj_type")
user[oj_type] = request.form.get("uid")
cookie[oj_type] = request.form.get("cookie")
return "ok"
@app.route("/checkOJ")
def checkOJ():
global user,cookie,seted
try:
oj_type = request.args.get("oj_type")
parse[oj_type](1, True)
except:
return "Error"
seted[oj_type] = True
File.save(dataPath+"\\cookie.json", cookie)
File.save(dataPath+"\\user.json", user)
File.save(dataPath+"\\seted.json", seted)
return "ok"
debug = True
File = file.File("json")
FileText = file.File("plain")
cookie = File.read(dataPath+"\\cookie.json")
user = File.read(dataPath+"\\user.json")
seted = File.read(dataPath+"\\seted.json")
lastupd = FileText.read(dataPath+"\\lastupd.file")
def luogu_parse(page, check=False):
global lastupd
# return jsonify({'code':429,'msg':'429 Too Many Requests<br>请求过快'})
if not check:
if time.time() - float(lastupd) > 0.8:
lastupd = time.time()
FileText.save(dataPath+"\\lastupd.file", lastupd)
else:
return jsonify(
{
"code": 429,
"msg": '429 Too Many Requests<br>请求过快<br><button onclick="window.location.reload()" class="layui-btn">刷新</button>',
}
)
req = requests.get(
"https://www.luogu.com.cn/record/list?user={}&page={}&_contentOnly=1".format(
user["luogu"], page
),
headers={
"Host": "www.luogu.com.cn",
"Cookie": cookie["luogu"],
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br",
"User-Agent": "Apifox/1.0.0 (https://apifox.com)",
},
)
# ans=req.text.split('"));')[0].split('JSON.parse(decodeURIComponent("')[-1]
ans = req.text
# Cookie update
for key, item in req.cookies.items():
cookie[key] = item
data = json.loads(unquote(ans))
# print(data)
data_ = {"code": 0, "count": data["currentData"]["records"]["count"], "data": []}
for i in data["currentData"]["records"]["result"]:
i["oj"] = "https://www.luogu.com.cn/problem/pid"
data_["data"].append(i)
if check:
if data["code"] != 200:
raise
return "ok"
return jsonify(data_)
# @app.route('/getLuoguProblem')
def luogu_problem():
id=request.args.get('id')
req=requests.get('https://www.luogu.com.cn/record/{}?_contentOnly=1'.format(id),
headers={
"Host": "www.luogu.com.cn",
"Cookie": cookie["luogu"],
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br",
"User-Agent": "Apifox/1.0.0 (https://apifox.com)",
},
)
# print(req.text)
return json.loads(req.text)
parse = {"luogu": luogu_parse}
@app.route("/getLuogu")
def getLuogu():
if seted["luogu"]:
page = request.args.get("page", 1, type=int)
limit = request.args.get("limit", 20, type=int)
return luogu_parse(page)
else:
return {"code": 0}
@app.route("/favicon.ico")
def favicon():
return send_from_directory("file", "favicon.ico")
if __name__ == "__main__":
if not debug:
threading.Thread(
target=lambda: os.system('"' + browser + '" --app=http://127.0.0.1:8888')
).start()
app.run(port=8888, debug=debug)