Skip to content

Commit

Permalink
feat: 新增搜索播放口令用于生成临时播放列表
Browse files Browse the repository at this point in the history
  • Loading branch information
hanxi committed Dec 11, 2024
1 parent 12532b4 commit 59e096b
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 26 deletions.
8 changes: 7 additions & 1 deletion xiaomusic/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class Config:
ffmpeg_location: str = os.getenv("XIAOMUSIC_FFMPEG_LOCATION", "./ffmpeg/bin")
active_cmd: str = os.getenv(
"XIAOMUSIC_ACTIVE_CMD",
"play,set_play_type_rnd,playlocal,play_music_list,play_music_list_index,stop_after_minute,stop",
"play,search_play,set_play_type_rnd,playlocal,search_playlocal,play_music_list,play_music_list_index,stop_after_minute,stop",
)
exclude_dirs: str = os.getenv("XIAOMUSIC_EXCLUDE_DIRS", "@eaDir,tmp")
ignore_tag_dirs: str = os.getenv("XIAOMUSIC_IGNORE_TAG_DIRS", "")
Expand Down Expand Up @@ -141,7 +141,11 @@ class Config:
keywords_playlocal: str = os.getenv(
"XIAOMUSIC_KEYWORDS_PLAYLOCAL", "播放本地歌曲,本地播放歌曲"
)
keywords_search_playlocal: str = os.getenv(
"XIAOMUSIC_KEYWORDS_SEARCH_PLAYLOCAL", "本地搜索播放"
)
keywords_play: str = os.getenv("XIAOMUSIC_KEYWORDS_PLAY", "播放歌曲,放歌曲")
keywords_search_play: str = os.getenv("XIAOMUSIC_KEYWORDS_SEARCH_PLAY", "搜索播放")
keywords_stop: str = os.getenv("XIAOMUSIC_KEYWORDS_STOP", "关机,暂停,停止,停止播放")
keywords_playlist: str = os.getenv(
"XIAOMUSIC_KEYWORDS_PLAYLIST", "播放列表,播放歌单"
Expand Down Expand Up @@ -211,7 +215,9 @@ 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_search_playlocal, "search_playlocal")
self.append_keyword(self.keywords_play, "play")
self.append_keyword(self.keywords_search_play, "search_play")
self.append_keyword(self.keywords_stop, "stop")
self.append_keyword(self.keywords_playlist, "play_music_list")
self.append_user_keyword()
Expand Down
4 changes: 4 additions & 0 deletions xiaomusic/static/default/setting.html
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ <h2>小爱音箱设置面板
<input id="keywords_playlist" type="text" value="播放列表,播放歌单" />
<label for="keywords_stop">停止口令:</label>
<input id="keywords_stop" type="text" value="关机,暂停,停止,停止播放" />
<label for="keywords_search_playlocal">本地搜索播放口令(会产生临时播放列表):</label>
<input id="keywords_search_playlocal" type="text" value="本地搜索播放" />
<label for="keywords_search_play">搜索播放口令(会产生临时播放列表):</label>
<input id="keywords_search_play" type="text" value="搜索播放" />

<label for="enable_yt_dlp_cookies">启用yt-dlp-cookies(需要先上传yt-dlp-cookies.txt文件):</label>
<select id="enable_yt_dlp_cookies">
Expand Down
4 changes: 4 additions & 0 deletions xiaomusic/static/default_past/setting.html
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ <h2>小爱音箱设置面板
<input id="keywords_playlist" type="text" value="播放列表,播放歌单" />
<label for="keywords_stop">停止口令:</label>
<input id="keywords_stop" type="text" value="关机,暂停,停止,停止播放" />
<label for="keywords_search_playlocal">本地搜索播放口令(会产生临时播放列表):</label>
<input id="keywords_search_playlocal" type="text" value="本地搜索播放" />
<label for="keywords_search_play">搜索播放口令(会产生临时播放列表):</label>
<input id="keywords_search_play" type="text" value="搜索播放" />

<label for="enable_yt_dlp_cookies">启用yt-dlp-cookies(需要先上传yt-dlp-cookies.txt文件):</label>
<select id="enable_yt_dlp_cookies">
Expand Down
117 changes: 92 additions & 25 deletions xiaomusic/xiaomusic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1035,14 +1035,39 @@ async def play(self, did="", arg1="", **kwargs):
if not name:
name = search_key

return await self.do_play(did, name, search_key)
# 语音播放会根据歌曲匹配更新当前播放列表
return await self.do_play(
did, name, search_key, exact=True, update_cur_list=True
)

async def do_play(self, did, name, search_key=""):
return await self.devices[did].play(name, search_key)
# 搜索播放:会产生临时播放列表
async def search_play(self, did="", arg1="", **kwargs):
parts = arg1.split("|")
search_key = parts[0]
name = parts[1] if len(parts) > 1 else search_key
if not name:
name = search_key

# 语音搜索播放会更新当前播放列表为临时播放列表
return await self.do_play(
did, name, search_key, exact=False, update_cur_list=False
)

# 后台搜索播放
async def do_play(
self, did, name, search_key="", exact=False, update_cur_list=False
):
return await self.devices[did].play(name, search_key, exact, update_cur_list)

# 本地播放
async def playlocal(self, did="", arg1="", **kwargs):
return await self.devices[did].playlocal(arg1)
return await self.devices[did].playlocal(arg1, update_cur_list=True)

# 本地搜索播放
async def search_playlocal(self, did="", arg1="", **kwargs):
return await self.devices[did].playlocal(
arg1, exact=False, update_cur_list=False
)

async def play_next(self, did="", **kwargs):
return await self.devices[did].play_next()
Expand Down Expand Up @@ -1362,11 +1387,16 @@ def update_playlist(self, reorder=True):
)

# 播放歌曲
async def play(self, name="", search_key=""):
async def play(self, name="", search_key="", exact=True, update_cur_list=False):
self._last_cmd = "play"
return await self._play(name=name, search_key=search_key, update_cur=True)
return await self._play(
name=name,
search_key=search_key,
exact=exact,
update_cur_list=update_cur_list,
)

async def _play(self, name="", search_key="", exact=False, update_cur=False):
async def _play(self, name="", search_key="", exact=True, update_cur_list=False):
if search_key == "" and name == "":
if self.check_play_next():
await self._play_next()
Expand All @@ -1381,15 +1411,20 @@ async def _play(self, name="", search_key="", exact=False, update_cur=False):
else:
names = self.xiaomusic.find_real_music_name(name)
if len(names) > 0:
if update_cur and len(names) > 1: # 大于一首歌才更新
self._play_list = names
self.device.cur_playlist = "临时搜索列表"
self.update_playlist()
elif update_cur: # 只有一首歌,append
self._play_list = self._play_list + names
self.device.cur_playlist = "临时搜索列表"
self.update_playlist(reorder=False)
if not exact:
if len(names) > 1: # 大于一首歌才更新
self._play_list = names
self.device.cur_playlist = "临时搜索列表"
self.update_playlist()
else: # 只有一首歌,append
self._play_list = self._play_list + names
self.device.cur_playlist = "临时搜索列表"
self.update_playlist(reorder=False)
name = names[0]
if update_cur_list:
# 根据当前歌曲匹配歌曲列表
self.device.cur_playlist = self.find_cur_playlist(name)
self.update_playlist()
self.log.debug(
f"当前播放列表为:{list2str(self._play_list, self.config.verbose)}"
)
Expand Down Expand Up @@ -1448,7 +1483,7 @@ async def _play_prev(self):
await self._play(name, exact=True)

# 播放本地歌曲
async def playlocal(self, name):
async def playlocal(self, name, exact=True, update_cur_list=False):
self._last_cmd = "playlocal"
if name == "":
if self.check_play_next():
Expand All @@ -1460,17 +1495,25 @@ async def playlocal(self, name):
self.log.info(f"playlocal. name:{name}")

# 本地歌曲不存在时下载
names = self.xiaomusic.find_real_music_name(name)
if exact:
names = self.xiaomusic.find_real_music_name(name, n=1)
else:
names = self.xiaomusic.find_real_music_name(name)
if len(names) > 0:
if len(names) > 1: # 大于一首歌才更新
self._play_list = names
self.device.cur_playlist = "临时搜索列表"
self.update_playlist()
else: # 只有一首歌,append
self._play_list = self._play_list + names
self.device.cur_playlist = "临时搜索列表"
self.update_playlist(reorder=False)
if not exact:
if len(names) > 1: # 大于一首歌才更新
self._play_list = names
self.device.cur_playlist = "临时搜索列表"
self.update_playlist()
else: # 只有一首歌,append
self._play_list = self._play_list + names
self.device.cur_playlist = "临时搜索列表"
self.update_playlist(reorder=False)
name = names[0]
if update_cur_list:
# 根据当前歌曲匹配歌曲列表
self.device.cur_playlist = self.find_cur_playlist(name)
self.update_playlist()
self.log.debug(
f"当前播放列表为:{list2str(self._play_list, self.config.verbose)}"
)
Expand Down Expand Up @@ -1933,3 +1976,27 @@ def dict_clear(cls, d):
for key in list(d):
val = d.pop(key)
val.cancel_all_timer()

# 根据当前歌曲匹配歌曲列表
def find_cur_playlist(self, name):
# 匹配顺序:
# 1. 收藏
# 2. 最近新增
# 3. 排除(全部,所有歌曲,所有电台,临时搜索列表)
# 4. 所有歌曲
# 5. 所有电台
# 6. 全部
if name in self.xiaomusic.music_list.get("收藏", []):
return "收藏"
if name in self.xiaomusic.music_list.get("最近新增", []):
return "最近新增"
for list_name, play_list in self.xiaomusic.music_list.items():
if (list_name not in ["全部", "所有歌曲", "所有电台", "临时搜索列表"]) and (
name in play_list
):
return list_name
if name in self.xiaomusic.music_list.get("所有歌曲", []):
return "所有歌曲"
if name in self.xiaomusic.music_list.get("所有电台", []):
return "所有电台"
return "全部"

0 comments on commit 59e096b

Please sign in to comment.