Skip to content

Commit

Permalink
feat: 新增歌曲收藏功能 #87
Browse files Browse the repository at this point in the history
  • Loading branch information
hanxi committed Sep 3, 2024
1 parent d3895f2 commit d1b869a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
6 changes: 6 additions & 0 deletions xiaomusic/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ def default_key_word_dict():
"分钟后关机": "stop_after_minute",
"播放列表": "play_music_list",
"刷新列表": "gen_music_list",
"加入收藏": "add_to_favorites",
"取消收藏": "del_from_favorites",
}


Expand Down Expand Up @@ -50,6 +52,8 @@ def default_key_match_order():
"关机",
"刷新列表",
"播放列表",
"加入收藏",
"取消收藏",
]


Expand Down Expand Up @@ -96,6 +100,7 @@ class Config:
httpauth_password: str = os.getenv("XIAOMUSIC_HTTPAUTH_PASSWORD", "")
music_list_url: str = os.getenv("XIAOMUSIC_MUSIC_LIST_URL", "")
music_list_json: str = os.getenv("XIAOMUSIC_MUSIC_LIST_JSON", "")
custom_play_list_json: str = os.getenv("XIAOMUSIC_CUSTOM_PLAY_LIST_JSON", "")
disable_download: bool = (
os.getenv("XIAOMUSIC_DISABLE_DOWNLOAD", "false").lower() == "true"
)
Expand Down Expand Up @@ -153,6 +158,7 @@ def append_user_keyword(self):

def init_keyword(self):
self.key_match_order = default_key_match_order()
self.key_word_dict = default_key_word_dict()
self.append_keyword(self.keywords_playlocal, "playlocal")
self.append_keyword(self.keywords_play, "play")
self.append_keyword(self.keywords_stop, "stop")
Expand Down
3 changes: 3 additions & 0 deletions xiaomusic/static/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ $(function(){
append_op_button_name("下一首");
append_op_button_name("关机");

append_op_button_name("加入收藏");
append_op_button_name("取消收藏");

$container.append($("<hr>"));

append_op_button_name("10分钟后关机");
Expand Down
53 changes: 53 additions & 0 deletions xiaomusic/xiaomusic.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,8 @@ def _gen_all_music_list(self):

self.music_list["全部"] = list(self.all_music.keys())

self._append_custom_play_list()

# 歌单排序
for _, play_list in self.music_list.items():
play_list.sort(key=custom_sort_key)
Expand All @@ -462,6 +464,16 @@ def _gen_all_music_list(self):
for device in self.devices.values():
device.update_playlist()

def _append_custom_play_list(self):
if not self.config.custom_play_list_json:
return

try:
custom_play_list = json.loads(self.config.custom_play_list_json)
self.music_list["收藏"] = list(custom_play_list["收藏"])
except Exception as e:
self.log.exception(f"Execption {e}")

# 给歌单里补充网络歌单
def _append_music_list(self):
if not self.config.music_list_json:
Expand Down Expand Up @@ -715,6 +727,47 @@ async def stop_after_minute(self, did="", arg1=0, **kwargs):
minute = int(arg1)
return await self.devices[did].stop_after_minute(minute)

# 添加歌曲到收藏列表
async def add_to_favorites(self, did="", arg1="", **kwargs):
name = arg1 if arg1 else self.playingmusic(did)
if not name:
return

favorites = self.music_list.get("收藏", [])
if name in favorites:
return

favorites.append(name)
self.save_favorites(favorites)

# 从收藏列表中移除
async def del_from_favorites(self, did="", arg1="", **kwargs):
name = arg1 if arg1 else self.playingmusic(did)
if not name:
return

favorites = self.music_list.get("收藏", [])
if name not in favorites:
return

favorites.remove(name)
self.save_favorites(favorites)

def save_favorites(self, favorites):
self.music_list["收藏"] = favorites
custom_play_list = {}
if self.config.custom_play_list_json:
custom_play_list = json.loads(self.config.custom_play_list_json)
custom_play_list["收藏"] = favorites
self.config.custom_play_list_json = json.dumps(
custom_play_list, ensure_ascii=False
)
self.save_cur_config()

# 更新每个设备的歌单
for device in self.devices.values():
device.update_playlist()

# 获取音量
async def get_volume(self, did="", **kwargs):
return await self.devices[did].get_volume()
Expand Down

0 comments on commit d1b869a

Please sign in to comment.