Skip to content

Commit

Permalink
新增删除歌曲按钮
Browse files Browse the repository at this point in the history
  • Loading branch information
hanxi committed Jun 14, 2024
1 parent 9714f3d commit e79afa4
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "xiaomusic"
version = "0.1.41"
version = "0.1.42"
description = "Play Music with xiaomi AI speaker"
authors = [
{name = "涵曦", email = "[email protected]"},
Expand Down
2 changes: 1 addition & 1 deletion xiaomusic/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.1.41"
__version__ = "0.1.42"
6 changes: 6 additions & 0 deletions xiaomusic/httpserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ async def musiclist():
async def curplaylist():
return xiaomusic.get_cur_play_list()

@app.route("/delmusic", methods=["POST"])
def delmusic():
data = request.get_json()
log.info(data)
xiaomusic.del_music(data["name"])
return 'success'

def static_path_handler(filename):
log.debug(filename)
Expand Down
24 changes: 22 additions & 2 deletions xiaomusic/static/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,27 @@ $(function(){
var music_name = $("#music_name").val();
let cmd = "播放列表" + music_list + "|" + music_name;
sendcmd(cmd);
})
});

$("#del_music").on("click", () => {
var del_music_name = $("#music_name").val();
if (confirm(`确定删除歌曲 ${del_music_name} 吗?`)) {
console.log(`删除歌曲 ${del_music_name}`);
$.ajax({
type: 'POST',
url: '/delmusic',
data: JSON.stringify({"name": del_music_name}),
contentType: "application/json; charset=utf-8",
success: () => {
alert(`删除 ${del_music_name} 成功`);
refresh_music_list();
},
error: () => {
alert(`删除 ${del_music_name} 失败`);
}
});
}
});

function append_op_button_name(name) {
append_op_button(name, name);
Expand Down Expand Up @@ -96,7 +116,7 @@ $(function(){
$.ajax({
type: "POST",
url: "/cmd",
contentType: "application/json",
contentType: "application/json; charset=utf-8",
data: JSON.stringify({cmd: cmd}),
success: () => {
if (cmd == "刷新列表") {
Expand Down
1 change: 1 addition & 0 deletions xiaomusic/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ <h2>小爱音箱操控面板<span id="version">(版本未知)</span></h2>
<select id="music_name"></select>
</div>
<button id="play_music_list">播放列表歌曲</button>
<button id="del_music">删除选中歌曲</button>

<footer>
<p>Powered by <a href="https://github.com/hanxi/xiaomusic" target="_blank">xiaomusic</a></p>
Expand Down
14 changes: 14 additions & 0 deletions xiaomusic/xiaomusic.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,20 @@ async def gen_music_list(self, **kwargs):
self._gen_all_music_list()
await self.do_tts("生成播放列表完毕")

# 删除歌曲
def del_music(self, name):
filename = self.get_filename(name)
if filename == "":
self.log.info(f"${name} not exist")
return
try:
os.remove(filename)
self.log.info(f"del ${filename} success")
except OSError:
self.log.error(f"del ${filename} failed")
pass
self._gen_all_music_list()

# 播放一个播放列表
async def play_music_list(self, **kwargs):
parts = kwargs["arg1"].split("|")
Expand Down

0 comments on commit e79afa4

Please sign in to comment.