Skip to content

Commit

Permalink
feat: 新增更多的歌单编辑相关接口
Browse files Browse the repository at this point in the history
  • Loading branch information
hanxi committed Dec 18, 2024
1 parent ce4a414 commit 94921eb
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
47 changes: 47 additions & 0 deletions xiaomusic/httpserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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] # 歌曲名列表
Expand All @@ -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(
Expand Down
43 changes: 43 additions & 0 deletions xiaomusic/xiaomusic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 94921eb

Please sign in to comment.