-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
203 lines (183 loc) · 6.7 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
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
import json
from flask import Flask, jsonify, request, make_response
from method import (
BestsRender,
difficulty,
info,
info_by,
levels,
song_info_handler_main,
)
from fuzzy import *
import random as rand
app = Flask(__name__)
# init
BestsRender.read_difficulty("difficulty.csv")
BestsRender.read_playerInfo("info.csv")
listDiff = list(difficulty.items())
getLength = len(difficulty)
@app.route("/", methods=["GET"])
def index():
return jsonify({"message": "Hello World! Phigros API SERVICE IS ALIVE!"})
@app.route("/api/phi/bests", methods=["GET"])
def get_bests():
session = request.args.get("session")
overflow = request.args.get("overflow")
songinfo = request.args.get("songinfo")
if session is None:
return jsonify({"message": "session is required."})
if overflow is None:
overflow = 0
else:
overflow = int(overflow)
if songinfo is None or songinfo == "false":
songinfo = False
else:
songinfo = True
try:
bests, isphi = BestsRender.get_bests(session, overflow)
is_phi = {"phi": isphi}
best_list_args = {"bests": bests}
best_list = {**is_phi, **best_list_args}
get_formatData = BestsRender.get_formatData(session)
content = {**best_list, **get_formatData}
if songinfo:
# generated a songslist id.
i = 0
songinfoList = []
for _ in bests:
listChatNum = bests[i]
getSortSongID = listChatNum["songId"]
songinfoList.append(song_info_handler_main(songid=getSortSongID))
i = i + 1
full_reply = {"songinfo": songinfoList}
content = {
**best_list,
**full_reply,
**get_formatData,
}
data = {"status": True, "content": content}
data = json.dumps(data, ensure_ascii=False).encode("utf-8")
data = make_response(data)
data.headers["Content-Type"] = "application/json; charset=utf-8"
except Exception as e:
return jsonify({"status": False, "message": str(e)})
return data
@app.route("/api/phi/best", methods=["GET"])
def get_songs():
session = request.args.get("session")
songs = request.args.get("songid")
diff = request.args.get("diff")
songinfo = request.args.get("songinfo")
if session is None:
return jsonify({"message": "session is required."})
if songs is None:
return jsonify({"message": "songid is required."})
if diff is None:
diff = "IN"
if songinfo is None or songinfo == "false":
songinfo = False
else:
songinfo = True
try:
Contents, msg = BestsRender.get_songs_stats(session, songs, diff)
if Contents is None:
data = {"status": False, "message": msg}
else:
get_formatData = BestsRender.get_formatData(session)
Contents = {"record": Contents}
Content = {**Contents, **get_formatData}
if songinfo:
dict_songinfo = {"song_info": song_info_handler_main(songid=songs)}
Content = {**Content, **dict_songinfo}
data = {"status": True, "content": Content}
data = json.dumps(data, ensure_ascii=False).encode("utf-8")
data = make_response(data)
data.headers["Content-Type"] = "application/json; charset=utf-8"
except Exception as e:
return jsonify({"status": False, "message": str(e)})
return data
@app.route("/api/phi/rand", methods=["GET"])
def get_rand():
songinfo = request.args.get("songinfo")
if songinfo is None or songinfo == "false":
songinfo = False
else:
songinfo = True
try:
getRandSongIDNum = rand.randrange(0, getLength)
result = listDiff[getRandSongIDNum - 1]
result_info_name = info[result[0]]
result_info_by = info_by[result[0]]
result_get_diff_list = result[1]
getLength_result_range = rand.randrange(0, len(result_get_diff_list))
getLevel = levels[getLength_result_range]
getRating = result_get_diff_list[getLength_result_range]
Content = {
"songid": result[0],
"songname": result_info_name,
"composer": result_info_by,
"level": getLevel,
"rating": getRating,
}
if songinfo:
dict_songinfo = {"song_info": song_info_handler_main(songid=result[0])}
Content = {**Content, **dict_songinfo}
data = {
"status": True,
"content": Content,
}
data = json.dumps(data, ensure_ascii=False).encode("utf-8")
data = make_response(data)
data.headers["Content-Type"] = "application/json; charset=utf-8"
except Exception as e:
return jsonify({"status": False, "message": str(e)})
return data
@app.route("/api/phi/info", methods=["GET"])
def get_info():
session = request.args.get("session")
if session is None:
return jsonify({"message": "session is required."})
try:
get_formatData = BestsRender.get_formatData(session)
except Exception as e:
return jsonify({"status": False, "message": str(e)})
data = {"status": True, "Content": get_formatData}
data = json.dumps(data, ensure_ascii=False).encode("utf-8")
data = make_response(data)
data.headers["Content-Type"] = "application/json; charset=utf-8"
return data
@app.route("/api/phi/song", methods=["GET"])
def get_song_info():
songid = request.args.get("songid")
if songid is None:
return jsonify({"message": "songid is required."})
try:
data = {"status": True, "content": song_info_handler_main(songid=songid)}
except Exception as e:
return jsonify({"status": False, "message": str(e)})
data = json.dumps(data, ensure_ascii=False).encode("utf-8")
data = make_response(data)
data.headers["Content-Type"] = "application/json; charset=utf-8"
return data
@app.route("/api/phi/search", methods=["GET"])
def get_song_id():
params = request.args.get("params")
if params is None:
return jsonify({"message": "No Params, please input params."})
try:
result_song, possibility, song_id = search_song(parmas=params)
result = {
"song_name": result_song,
"song_ratio": possibility,
"song_id": song_id,
}
data = {"status": True, "content": result}
except Exception as e:
return jsonify({"status": False, "message": str(e)})
data = json.dumps(data, ensure_ascii=False).encode("utf-8")
data = make_response(data)
data.headers["Content-Type"] = "application/json; charset=utf-8"
return data
if __name__ == "__main__":
app.run(debug=True)