From a5ee5afc7edfbe82dee489f7f40ceae31012998f Mon Sep 17 00:00:00 2001 From: MoYoez Date: Sat, 26 Aug 2023 21:55:55 +0800 Subject: [PATCH] add songinfo args --- Readme.md | 6 ++-- app.py | 89 ++++++++++++++++++++++++------------------------------- method.py | 51 +++++++++++++++++++++++++++++-- 3 files changed, 91 insertions(+), 55 deletions(-) diff --git a/Readme.md b/Readme.md index a2b36dd..4f1cf95 100644 --- a/Readme.md +++ b/Readme.md @@ -10,7 +10,7 @@ Just Like Phigros Unlimited API, due to it's unstable,so I write this. **Get Users Best, support Overflow songs, you can delete this limit.** -> args: Session | overflow (max:20) +> args: Session | overflow (max:20) | songinfo Optional(bool) (Default:False) ``` { @@ -63,7 +63,7 @@ Just Like Phigros Unlimited API, due to it's unstable,so I write this. **Check User's best Songs.** -> args: songid | Session | diff: Optional(Default "IN") +> args: songid | Session | diff: Optional(Default "IN") | songinfo Optional(bool) (Default:False) - tips: songid just like "DESTRUCTION321.Normal1zervsBrokenNerdz" || diff just like "EZ" "AT" @@ -108,7 +108,7 @@ Just Like Phigros Unlimited API, due to it's unstable,so I write this. - [x] /api/phi/rand -> no args +> songinfo Optional(bool) (Default:False) ``` { diff --git a/app.py b/app.py index f5fef76..412fe84 100644 --- a/app.py +++ b/app.py @@ -7,11 +7,7 @@ info, info_by, levels, - info_illustrator, - info_hd_designer, - info_at_designer, - info_ez_desinger, - info_in_desingner, + song_info_handler_main, ) import random as rand @@ -34,12 +30,17 @@ def index(): 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} @@ -47,6 +48,21 @@ def get_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) @@ -61,12 +77,17 @@ 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: @@ -75,10 +96,10 @@ def get_songs(): get_formatData = BestsRender.get_formatData(session) Contents = {"record": Contents} Content = {**Contents, **get_formatData} - data = { - "status": True, - "content": Content, - } + 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" @@ -89,6 +110,11 @@ def get_songs(): @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] @@ -105,6 +131,9 @@ def get_rand(): "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, @@ -139,47 +168,7 @@ def get_song_info(): if songid is None: return jsonify({"message": "songid is required."}) try: - result_info_name = info[songid] - result_info_by = info_by[songid] - result_get_diff_list: list = difficulty[songid] # list - result_get_ins_by = info_illustrator[songid] - result_get_ez_designer = info_ez_desinger[songid] - result_get_hd_desinger = info_hd_designer[songid] - result_get_in_desinger = info_in_desingner[songid] - result_get_at_desinger = info_at_designer[songid] - showEZDetailed = { - "EZ": {"rating": result_get_diff_list[0], "charter": result_get_ez_designer} - } - showHDDetailed = { - "HD": {"rating": result_get_diff_list[1], "charter": result_get_hd_desinger} - } - showInDetailed = { - "IN": {"rating": result_get_diff_list[2], "charter": result_get_in_desinger} - } - if len(result_get_diff_list) >= 4: - showAtDetailed = { - "AT": { - "rating": result_get_diff_list[3], - "charter": result_get_at_desinger, - } - } - else: - showAtDetailed = {} - getChartDetailedInfo = { - **showEZDetailed, - **showHDDetailed, - **showInDetailed, - **showAtDetailed, - "level_list": result_get_diff_list, - } - infos = { - "songname": result_info_name, - "composer": result_info_by, - "illustrator": result_get_ins_by, - "chartDetail": getChartDetailedInfo, - } - content = {"songid": songid, "info": infos} - data = {"status": True, "content": content} + 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") diff --git a/method.py b/method.py index 65474eb..ab9ce2f 100644 --- a/method.py +++ b/method.py @@ -112,7 +112,7 @@ def parse_render_bests(gameRecord, overflow: int): if overflow > 21: overflow = 21 reader = ByteReader(gameRecord) - for i in range(reader.readVarShort()): + for _ in range(reader.readVarShort()): songId = reader.readString()[:-2] record = reader.readRecord(songId) records.extend(record) @@ -137,7 +137,7 @@ def get_songs_stat_main(gameRecord, songid, diff): getdiff = diff # Diff should be EZ,HD,IN,AT if getdiff != "EZ" and getdiff != "HD" and getdiff != "IN" and getdiff != "AT": getdiff = "IN" - for i in range(reader.readVarShort()): + for _ in range(reader.readVarShort()): songId = reader.readString()[:-2] record = reader.readRecord(songId) if songId == songid: @@ -264,3 +264,50 @@ def get_songs_stats(session, songid, diff): gameRecord = DataPackage.GameReader(result) gameRecord = decrypt_gameRecord(gameRecord) return get_songs_stat_main(gameRecord, songid, diff) + + +def song_info_handler_main(songid: str): + try: + result_info_name = info[songid] + result_info_by = info_by[songid] + result_get_diff_list: list = difficulty[songid] # list + result_get_ins_by = info_illustrator[songid] + result_get_ez_designer = info_ez_desinger[songid] + result_get_hd_desinger = info_hd_designer[songid] + result_get_in_desinger = info_in_desingner[songid] + result_get_at_desinger = info_at_designer[songid] + showEZDetailed = { + "EZ": {"rating": result_get_diff_list[0], "charter": result_get_ez_designer} + } + showHDDetailed = { + "HD": {"rating": result_get_diff_list[1], "charter": result_get_hd_desinger} + } + showInDetailed = { + "IN": {"rating": result_get_diff_list[2], "charter": result_get_in_desinger} + } + if len(result_get_diff_list) >= 4: + showAtDetailed = { + "AT": { + "rating": result_get_diff_list[3], + "charter": result_get_at_desinger, + } + } + else: + showAtDetailed = {} + getChartDetailedInfo = { + **showEZDetailed, + **showHDDetailed, + **showInDetailed, + **showAtDetailed, + "level_list": result_get_diff_list, + } + infos = { + "songname": result_info_name, + "composer": result_info_by, + "illustrator": result_get_ins_by, + "chartDetail": getChartDetailedInfo, + } + content = {"songid": songid, "info": infos} + return content + except Exception as e: + return {}