Skip to content

Commit

Permalink
feat: 新增口令【播放列表第几个+列表名】来播放列表里的第几个 #158
Browse files Browse the repository at this point in the history
  • Loading branch information
hanxi committed Sep 20, 2024
1 parent c923ad0 commit cef5278
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
5 changes: 4 additions & 1 deletion xiaomusic/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def default_key_word_dict():
"刷新列表": "gen_music_list",
"加入收藏": "add_to_favorites",
"取消收藏": "del_from_favorites",
"播放列表第": "play_music_list_index",
}


Expand Down Expand Up @@ -53,6 +54,7 @@ def default_key_match_order():
"随机播放",
"关机",
"刷新列表",
"播放列表第",
"播放列表",
"加入收藏",
"取消收藏",
Expand Down Expand Up @@ -91,7 +93,8 @@ class Config:
) # "bilisearch:" or "ytsearch:"
ffmpeg_location: str = os.getenv("XIAOMUSIC_FFMPEG_LOCATION", "./ffmpeg/bin")
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")
music_path_depth: int = int(os.getenv("XIAOMUSIC_MUSIC_PATH_DEPTH", "10"))
Expand Down
38 changes: 38 additions & 0 deletions xiaomusic/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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
25 changes: 25 additions & 0 deletions xiaomusic/xiaomusic.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
from xiaomusic.crontab import Crontab
from xiaomusic.plugin import PluginManager
from xiaomusic.utils import (
chinese_to_number,
convert_file_to_mp3,
custom_sort_key,
deepcopy_data_no_sensitive_info,
Expand Down Expand Up @@ -741,6 +742,30 @@ async def play_music_list(self, did="", arg1="", **kwargs):
music_name = parts[1]
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):
parts = arg1.split("|")
Expand Down

0 comments on commit cef5278

Please sign in to comment.