新增本地音乐模糊搜索

This commit is contained in:
涵曦 2024-04-30 12:47:57 +00:00
parent eb35da595f
commit f962fcaa99
6 changed files with 44 additions and 1 deletions

1
.gitignore vendored
View File

@ -163,3 +163,4 @@ cython_debug/
ffmpeg
music
test.sh

View File

@ -28,6 +28,10 @@ def getvolume():
"volume": xiaomusic.get_volume(),
}
@app.route("/searchmusic")
def searchmusic():
name = request.args.get('name')
return xiaomusic.searchmusic(name)
@app.route("/", methods=["GET"])
def redirect_to_index():

View File

@ -64,4 +64,26 @@ $(function(){
}
});
}
// 监听输入框的输入事件
$("#music-name").on('input', function() {
var inputValue = $(this).val();
// 发送Ajax请求
$.ajax({
url: "searchmusic", // 服务器端处理脚本
type: "GET",
dataType: "json",
data: {
name: inputValue
},
success: function(data) {
// 清空datalist
$("#autocomplete-list").empty();
// 添加新的option元素
$.each(data, function(i, item) {
$('<option>').val(item).appendTo("#autocomplete-list");
});
}
});
});
});

View File

@ -44,7 +44,8 @@
</div>
<hr>
<div>
<input id="music-name" type="text" placeholder="请输入搜索关键词(如:MV高清版 周杰伦 七里香)"></input>
<datalist id="autocomplete-list"></datalist>
<input id="music-name" type="text" placeholder="请输入搜索关键词(如:MV高清版 周杰伦 七里香)" list="autocomplete-list"></input>
<input id="music-filename" type="text" placeholder="请输入保存为的文件名称(如:周杰伦七里香)"></input>
<button id="play">播放</button>
</div>

View File

@ -7,6 +7,7 @@ import socket
from http.cookies import SimpleCookie
from typing import AsyncIterator
from urllib.parse import urlparse
import difflib
from requests.utils import cookiejar_from_dict
@ -61,3 +62,6 @@ def validate_proxy(proxy_str: str) -> bool:
return True
# 模糊搜索
def fuzzyfinder(user_input, collection):
return difflib.get_close_matches(user_input, collection, 10, cutoff=0.1)

View File

@ -30,6 +30,7 @@ from xiaomusic.config import (
from xiaomusic.utils import (
calculate_tts_elapse,
parse_cookie_string,
fuzzyfinder,
)
EOF = object()
@ -330,6 +331,7 @@ class XiaoMusic:
# 本地是否存在歌曲
def get_filename(self, name):
if name not in self._all_music:
self.log.debug("get_filename not in. name:%s", name)
return ""
filename = self._all_music[name]
self.log.debug("try get_filename. filename:%s", filename)
@ -490,6 +492,9 @@ class XiaoMusic:
if search_key == "" and name == "":
await self.play_next()
return
if name == "":
name = search_key
self.log.debug("play. search_key:%s name:%s", search_key, name)
filename = self.get_filename(name)
if len(filename) <= 0:
@ -569,3 +574,9 @@ class XiaoMusic:
def get_volume(self):
return self._volume
# 搜索音乐
def searchmusic(self, name):
search_list = fuzzyfinder(name, self._play_list)
self.log.debug("searchmusic. name:%s search_list:%s", name, search_list)
return search_list