From 94921eb12aa716e363ec4b118a3591012c850afd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B6=B5=E6=9B=A6?= Date: Thu, 19 Dec 2024 06:15:09 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E6=9B=B4=E5=A4=9A?= =?UTF-8?q?=E7=9A=84=E6=AD=8C=E5=8D=95=E7=BC=96=E8=BE=91=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- xiaomusic/httpserver.py | 47 +++++++++++++++++++++++++++++++++++++++++ xiaomusic/xiaomusic.py | 43 +++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) diff --git a/xiaomusic/httpserver.py b/xiaomusic/httpserver.py index 5f4424f61..5a2423717 100644 --- a/xiaomusic/httpserver.py +++ b/xiaomusic/httpserver.py @@ -560,6 +560,32 @@ async def playlistdel(data: PlayListObj, Verifcation=Depends(verification)): return {"ret": "Del failed, may be not exist."} +class PlayListUpdateObj(BaseModel): + oldname: str # 旧歌单名字 + newname: str # 新歌单名字 + + +# 修改歌单名字 +@app.post("/playlistupdatename") +async def playlistupdatename( + data: PlayListUpdateObj, Verifcation=Depends(verification) +): + ret = xiaomusic.play_list_update_name(data.oldname, data.newname) + if ret: + return {"ret": "OK"} + return {"ret": "Update failed, may be not exist."} + + +# 获取所有自定义歌单 +@app.get("/playlistnames") +async def getplaylistnames(Verifcation=Depends(verification)): + names = xiaomusic.get_play_list_names() + return { + "ret": "OK", + "names": names, + } + + class PlayListMusicObj(BaseModel): name: str = "" # 歌单名 music_list: list[str] # 歌曲名列表 @@ -583,6 +609,27 @@ async def playlistdelmusic(data: PlayListMusicObj, Verifcation=Depends(verificat return {"ret": "Del failed, may be playlist not exist."} +# 歌单更新歌曲 +@app.post("/playlistupdatemusic") +async def playlistupdatemusic( + data: PlayListMusicObj, Verifcation=Depends(verification) +): + ret = xiaomusic.play_list_update_music(data.name, data.music_list) + if ret: + return {"ret": "OK"} + return {"ret": "Del failed, may be playlist not exist."} + + +# 获取歌单中所有歌曲 +@app.get("/playlistmusics") +async def getplaylist(name: str, Verifcation=Depends(verification)): + ret, musics = xiaomusic.play_list_musics(name) + return { + "ret": "OK", + "musics": musics, + } + + # 更新版本 @app.post("/updateversion") async def updateversion( diff --git a/xiaomusic/xiaomusic.py b/xiaomusic/xiaomusic.py index 67ae4d947..ba081ff62 100644 --- a/xiaomusic/xiaomusic.py +++ b/xiaomusic/xiaomusic.py @@ -1146,6 +1146,49 @@ def play_list_del(self, name): self.save_custom_play_list() return True + # 修改歌单名字 + def play_list_update_name(self, oldname, newname): + custom_play_list = self.get_custom_play_list() + if oldname not in custom_play_list: + self.log.info(f"旧歌单名字不存在 {oldname}") + return False + if newname in custom_play_list: + self.log.info(f"新歌单名字已存在 {newname}") + return False + play_list = custom_play_list[oldname] + custom_play_list.pop(oldname) + custom_play_list[newname] = play_list + return True + + # 获取所有自定义歌单 + def get_play_list_names(self): + custom_play_list = self.get_custom_play_list() + return custom_play_list.keys() + + # 获取歌单中所有歌曲 + def play_list_musics(self, name): + custom_play_list = self.get_custom_play_list() + if name not in custom_play_list: + return "歌单不存在", [] + play_list = custom_play_list[name] + return "OK", play_list + + # 歌单更新歌曲 + def play_list_update_music(self, name, music_list): + custom_play_list = self.get_custom_play_list() + if name not in custom_play_list: + # 歌单不存在则新建 + if not self.play_list_add(name): + return False + play_list = [] + for music_name in music_list: + if (music_name in self.all_music) and (music_name not in play_list): + play_list.append(music_name) + # 直接覆盖 + custom_play_list[name] = play_list + self.save_custom_play_list() + return True + # 歌单新增歌曲 def play_list_add_music(self, name, music_list): custom_play_list = self.get_custom_play_list()