From 6902377ea7d9b7d5095ff48ca88998ac8f860a67 Mon Sep 17 00:00:00 2001 From: MoYoez Date: Sun, 10 Sep 2023 18:13:53 +0800 Subject: [PATCH] add /phi/search router --- Readme.md | 28 ++++++++++++++++++++++++++++ app.py | 22 ++++++++++++++++++++++ fuzzy.py | 36 ++++++++++++++++++++++++++++++++++++ method.py | 1 + requirements.txt | 3 +-- 5 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 fuzzy.py diff --git a/Readme.md b/Readme.md index 4f1cf95..182cc93 100644 --- a/Readme.md +++ b/Readme.md @@ -160,6 +160,34 @@ Just Like Phigros Unlimited API, due to it's unstable,so I write this. } ``` +- [x] /api/phi/search + +> args: params + +* Use Difflib SequenceMatcher, maybe not work well :( + + ``` +{ + "status": true, + "content": { + "song_name": "DESTRUCTION 3,2,1", + "song_ratio": 0.7857142857142857, + "song_id": "DESTRUCTION321.Normal1zervsBrokenNerdz" + } +} + + ``` + * If cannot found: + + ``` +{ + "message": "None", + "status": false +} + + ``` + + ... diff --git a/app.py b/app.py index 412fe84..b595f4d 100644 --- a/app.py +++ b/app.py @@ -9,6 +9,7 @@ levels, song_info_handler_main, ) +from fuzzy import * import random as rand @@ -177,5 +178,26 @@ def get_song_info(): 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) diff --git a/fuzzy.py b/fuzzy.py new file mode 100644 index 0000000..e68ad61 --- /dev/null +++ b/fuzzy.py @@ -0,0 +1,36 @@ +import difflib +from pathlib import Path + + +load_songname_list = [] +load_dict = {} + +load_data = Path().absolute() / "info.csv" + + +with open(load_data, "r", encoding="utf-8") as f: + reader = f.readlines() + i = 0 + for row in reader: + split_data = row.split("\\") + # Generate a dict + song_id = split_data[0] + song_name = split_data[1] + # load dict + load_dict.update({song_name: song_id}) + load_songname_list.append(split_data[1]) + i + 1 + + +def search_song(parmas: str): + best_match = None + best_ratio = 0 + for i in range(len(load_songname_list)): + get_name = load_songname_list[i] + # use fuzzy + similarity_ratio = difflib.SequenceMatcher(None, parmas, get_name).ratio() + if similarity_ratio > best_ratio: + best_ratio = similarity_ratio + best_match = get_name + best_song_id = load_dict[best_match] + return best_match, best_ratio, best_song_id diff --git a/method.py b/method.py index ab9ce2f..0a544fd 100644 --- a/method.py +++ b/method.py @@ -10,6 +10,7 @@ levels = ["EZ", "HD", "IN", "AT"] difficulty = OrderedDict() +dict_fuzzy = {} info = {} info_by = {} info_illustrator = {} diff --git a/requirements.txt b/requirements.txt index 62d1c43..148554c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,2 @@ Flask==2.3.2 -pycryptodome==3.18.0 -Requests==2.31.0 +Requests==2.31.0 \ No newline at end of file