Skip to content

Commit

Permalink
add /phi/search router
Browse files Browse the repository at this point in the history
  • Loading branch information
MoYoez committed Sep 10, 2023
1 parent 1a15f9a commit 6902377
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 2 deletions.
28 changes: 28 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
```
...
Expand Down
22 changes: 22 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
levels,
song_info_handler_main,
)
from fuzzy import *
import random as rand


Expand Down Expand Up @@ -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)
36 changes: 36 additions & 0 deletions fuzzy.py
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions method.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

levels = ["EZ", "HD", "IN", "AT"]
difficulty = OrderedDict()
dict_fuzzy = {}
info = {}
info_by = {}
info_illustrator = {}
Expand Down
3 changes: 1 addition & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
Flask==2.3.2
pycryptodome==3.18.0
Requests==2.31.0
Requests==2.31.0

0 comments on commit 6902377

Please sign in to comment.