feat: 新增口令【播放列表第几个+列表名】来播放列表里的第几个 #158

This commit is contained in:
涵曦 2024-09-21 01:10:34 +08:00
parent c923ad00f8
commit cef5278f16
3 changed files with 67 additions and 1 deletions

View File

@ -25,6 +25,7 @@ def default_key_word_dict():
"刷新列表": "gen_music_list", "刷新列表": "gen_music_list",
"加入收藏": "add_to_favorites", "加入收藏": "add_to_favorites",
"取消收藏": "del_from_favorites", "取消收藏": "del_from_favorites",
"播放列表第": "play_music_list_index",
} }
@ -53,6 +54,7 @@ def default_key_match_order():
"随机播放", "随机播放",
"关机", "关机",
"刷新列表", "刷新列表",
"播放列表第",
"播放列表", "播放列表",
"加入收藏", "加入收藏",
"取消收藏", "取消收藏",
@ -91,7 +93,8 @@ class Config:
) # "bilisearch:" or "ytsearch:" ) # "bilisearch:" or "ytsearch:"
ffmpeg_location: str = os.getenv("XIAOMUSIC_FFMPEG_LOCATION", "./ffmpeg/bin") ffmpeg_location: str = os.getenv("XIAOMUSIC_FFMPEG_LOCATION", "./ffmpeg/bin")
active_cmd: str = os.getenv( active_cmd: str = os.getenv(
"XIAOMUSIC_ACTIVE_CMD", "play,set_random_play,playlocal,play_music_list,stop" "XIAOMUSIC_ACTIVE_CMD",
"play,set_random_play,playlocal,play_music_list,play_music_list_index,stop_after_minute,stop",
) )
exclude_dirs: str = os.getenv("XIAOMUSIC_EXCLUDE_DIRS", "@eaDir") exclude_dirs: str = os.getenv("XIAOMUSIC_EXCLUDE_DIRS", "@eaDir")
music_path_depth: int = int(os.getenv("XIAOMUSIC_MUSIC_PATH_DEPTH", "10")) music_path_depth: int = int(os.getenv("XIAOMUSIC_MUSIC_PATH_DEPTH", "10"))

View File

@ -402,3 +402,41 @@ def convert_file_to_mp3(input_file: str, ffmpeg_location: str, music_path: str)
relative_path = os.path.relpath(out_file_path, music_path) relative_path = os.path.relpath(out_file_path, music_path)
return relative_path return relative_path
chinese_to_arabic = {
"": 0,
"": 1,
"": 2,
"": 3,
"": 4,
"": 5,
"": 6,
"": 7,
"": 8,
"": 9,
"": 10,
"": 100,
"": 1000,
"": 10000,
"亿": 100000000,
}
def chinese_to_number(chinese):
result = 0
unit = 1
num = 0
for char in reversed(chinese):
if char in chinese_to_arabic:
val = chinese_to_arabic[char]
if val >= 10:
if val > unit:
unit = val
else:
unit *= val
else:
num += val * unit
result += num
num = 0
return result

View File

@ -35,6 +35,7 @@ from xiaomusic.const import (
from xiaomusic.crontab import Crontab from xiaomusic.crontab import Crontab
from xiaomusic.plugin import PluginManager from xiaomusic.plugin import PluginManager
from xiaomusic.utils import ( from xiaomusic.utils import (
chinese_to_number,
convert_file_to_mp3, convert_file_to_mp3,
custom_sort_key, custom_sort_key,
deepcopy_data_no_sensitive_info, deepcopy_data_no_sensitive_info,
@ -741,6 +742,30 @@ class XiaoMusic:
music_name = parts[1] music_name = parts[1]
await self.devices[did].play_music_list(list_name, music_name) await self.devices[did].play_music_list(list_name, music_name)
# 播放一个播放列表里第几个
async def play_music_list_index(self, did="", arg1="", **kwargs):
patternarg = r"^([零一二三四五六七八九十百千万亿]+)个(.*)"
# 匹配参数
matcharg = re.match(patternarg, arg1)
if not matcharg:
return await self.play_music_list(did, arg1)
chinese_index = matcharg.groups()[0]
list_name = matcharg.groups()[1]
list_name = self._find_real_music_list_name(list_name)
if list_name not in self.music_list:
await self.do_tts(did, f"播放列表{list_name}不存在")
return
index = chinese_to_number(chinese_index)
play_list = self.music_list[list_name]
if 0 <= index - 1 < len(play_list):
music_name = play_list[index - 1]
self.log.info(f"即将播放 ${arg1} 里的第 ${index} 个: ${music_name}")
await self.devices[did].play_music_list(list_name, music_name)
return
await self.do_tts(did, f"播放列表{list_name}中找不到第${index}")
# 播放 # 播放
async def play(self, did="", arg1="", **kwargs): async def play(self, did="", arg1="", **kwargs):
parts = arg1.split("|") parts = arg1.split("|")