mirror of
https://github.com/hanxi/xiaomusic.git
synced 2025-12-08 15:08:12 +08:00
Compare commits
30 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dbc68d6b56 | ||
|
|
5dabf66e7c | ||
|
|
d6e4478eb6 | ||
|
|
23ef4719ba | ||
|
|
d799a85ab9 | ||
|
|
4ad6bcc636 | ||
|
|
d2473ec7e8 | ||
|
|
28797edc7c | ||
|
|
be1a643071 | ||
|
|
ee6b9778ac | ||
|
|
881c34bcb5 | ||
|
|
c22fc99235 | ||
|
|
0874efe58b | ||
|
|
f01665c998 | ||
|
|
ac23080f6a | ||
|
|
15ee6c4dd1 | ||
|
|
aeaa8f8925 | ||
|
|
59d7e056c4 | ||
|
|
e79afa46b3 | ||
|
|
9714f3d064 | ||
|
|
2c35c6cfd6 | ||
|
|
88f0ce7e51 | ||
|
|
e484164fad | ||
|
|
aa6bce75cd | ||
|
|
512efb595a | ||
|
|
e5dea8e693 | ||
|
|
a704f8003c | ||
|
|
349a25ad58 | ||
|
|
746f46edb3 | ||
|
|
4a29c7a124 |
@@ -49,6 +49,11 @@ pdm run xiaomusic.py
|
|||||||
- 下一首
|
- 下一首
|
||||||
- 单曲循环
|
- 单曲循环
|
||||||
- 全部循环
|
- 全部循环
|
||||||
|
- 随机播放
|
||||||
|
- 关机
|
||||||
|
- 停止播放
|
||||||
|
- 刷新列表
|
||||||
|
- 播放列表+列表名 比如:播放列表其他
|
||||||
|
|
||||||
> 隐藏玩法: 对小爱同学说播放歌曲小猪佩奇的故事,会播放小猪佩奇的故事。
|
> 隐藏玩法: 对小爱同学说播放歌曲小猪佩奇的故事,会播放小猪佩奇的故事。
|
||||||
|
|
||||||
|
|||||||
45
pdm.lock
generated
45
pdm.lock
generated
@@ -2,10 +2,10 @@
|
|||||||
# It is not intended for manual editing.
|
# It is not intended for manual editing.
|
||||||
|
|
||||||
[metadata]
|
[metadata]
|
||||||
groups = ["default"]
|
groups = ["default", "lint"]
|
||||||
strategy = ["cross_platform"]
|
strategy = ["cross_platform"]
|
||||||
lock_version = "4.4.1"
|
lock_version = "4.4.1"
|
||||||
content_hash = "sha256:d771311a452ca58665efe3b74af341cb202d75d83a250896c293ea9c696e5696"
|
content_hash = "sha256:38bae754be83ffca7d688fc4e1daf0964709d202d651c6c865ff56c1b8332caa"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "aiohttp"
|
name = "aiohttp"
|
||||||
@@ -620,8 +620,8 @@ files = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "requests"
|
name = "requests"
|
||||||
version = "2.31.0"
|
version = "2.32.3"
|
||||||
requires_python = ">=3.7"
|
requires_python = ">=3.8"
|
||||||
summary = "Python HTTP for Humans."
|
summary = "Python HTTP for Humans."
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"certifi>=2017.4.17",
|
"certifi>=2017.4.17",
|
||||||
@@ -630,8 +630,8 @@ dependencies = [
|
|||||||
"urllib3<3,>=1.21.1",
|
"urllib3<3,>=1.21.1",
|
||||||
]
|
]
|
||||||
files = [
|
files = [
|
||||||
{file = "requests-2.31.0-py3-none-any.whl", hash = "sha256:58cd2187c01e70e6e26505bca751777aa9f2ee0b7f4300988b709f44e013003f"},
|
{file = "requests-2.32.3-py3-none-any.whl", hash = "sha256:70761cfe03c773ceb22aa2f671b4757976145175cdfca038c02654d061d6dcc6"},
|
||||||
{file = "requests-2.31.0.tar.gz", hash = "sha256:942c5a758f98d790eaed1a29cb6eefc7ffb0d1cf7af05c3d2791656dbd6ad1e1"},
|
{file = "requests-2.32.3.tar.gz", hash = "sha256:55365417734eb18255590a9ff9eb97e9e1da868d4ccd6402399eaf68af20a760"},
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -648,6 +648,31 @@ files = [
|
|||||||
{file = "rich-13.7.1.tar.gz", hash = "sha256:9be308cb1fe2f1f57d67ce99e95af38a1e2bc71ad9813b0e247cf7ffbcc3a432"},
|
{file = "rich-13.7.1.tar.gz", hash = "sha256:9be308cb1fe2f1f57d67ce99e95af38a1e2bc71ad9813b0e247cf7ffbcc3a432"},
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ruff"
|
||||||
|
version = "0.4.9"
|
||||||
|
requires_python = ">=3.7"
|
||||||
|
summary = "An extremely fast Python linter and code formatter, written in Rust."
|
||||||
|
files = [
|
||||||
|
{file = "ruff-0.4.9-py3-none-macosx_10_12_x86_64.whl", hash = "sha256:b262ed08d036ebe162123170b35703aaf9daffecb698cd367a8d585157732991"},
|
||||||
|
{file = "ruff-0.4.9-py3-none-macosx_11_0_arm64.whl", hash = "sha256:98ec2775fd2d856dc405635e5ee4ff177920f2141b8e2d9eb5bd6efd50e80317"},
|
||||||
|
{file = "ruff-0.4.9-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:4555056049d46d8a381f746680db1c46e67ac3b00d714606304077682832998e"},
|
||||||
|
{file = "ruff-0.4.9-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:e91175fbe48f8a2174c9aad70438fe9cb0a5732c4159b2a10a3565fea2d94cde"},
|
||||||
|
{file = "ruff-0.4.9-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:0e8e7b95673f22e0efd3571fb5b0cf71a5eaaa3cc8a776584f3b2cc878e46bff"},
|
||||||
|
{file = "ruff-0.4.9-py3-none-manylinux_2_17_ppc64.manylinux2014_ppc64.whl", hash = "sha256:2d45ddc6d82e1190ea737341326ecbc9a61447ba331b0a8962869fcada758505"},
|
||||||
|
{file = "ruff-0.4.9-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:78de3fdb95c4af084087628132336772b1c5044f6e710739d440fc0bccf4d321"},
|
||||||
|
{file = "ruff-0.4.9-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:06b60f91bfa5514bb689b500a25ba48e897d18fea14dce14b48a0c40d1635893"},
|
||||||
|
{file = "ruff-0.4.9-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:88bffe9c6a454bf8529f9ab9091c99490578a593cc9f9822b7fc065ee0712a06"},
|
||||||
|
{file = "ruff-0.4.9-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:673bddb893f21ab47a8334c8e0ea7fd6598ecc8e698da75bcd12a7b9d0a3206e"},
|
||||||
|
{file = "ruff-0.4.9-py3-none-musllinux_1_2_armv7l.whl", hash = "sha256:8c1aff58c31948cc66d0b22951aa19edb5af0a3af40c936340cd32a8b1ab7438"},
|
||||||
|
{file = "ruff-0.4.9-py3-none-musllinux_1_2_i686.whl", hash = "sha256:784d3ec9bd6493c3b720a0b76f741e6c2d7d44f6b2be87f5eef1ae8cc1d54c84"},
|
||||||
|
{file = "ruff-0.4.9-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:732dd550bfa5d85af8c3c6cbc47ba5b67c6aed8a89e2f011b908fc88f87649db"},
|
||||||
|
{file = "ruff-0.4.9-py3-none-win32.whl", hash = "sha256:8064590fd1a50dcf4909c268b0e7c2498253273309ad3d97e4a752bb9df4f521"},
|
||||||
|
{file = "ruff-0.4.9-py3-none-win_amd64.whl", hash = "sha256:e0a22c4157e53d006530c902107c7f550b9233e9706313ab57b892d7197d8e52"},
|
||||||
|
{file = "ruff-0.4.9-py3-none-win_arm64.whl", hash = "sha256:5d5460f789ccf4efd43f265a58538a2c24dbce15dbf560676e430375f20a8198"},
|
||||||
|
{file = "ruff-0.4.9.tar.gz", hash = "sha256:f1cb0828ac9533ba0135d148d214e284711ede33640465e706772645483427e3"},
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "typing-extensions"
|
name = "typing-extensions"
|
||||||
version = "4.9.0"
|
version = "4.9.0"
|
||||||
@@ -794,7 +819,7 @@ files = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "yt-dlp"
|
name = "yt-dlp"
|
||||||
version = "2024.5.16.232713.dev0"
|
version = "2024.6.17.232743.dev0"
|
||||||
requires_python = ">=3.8"
|
requires_python = ">=3.8"
|
||||||
summary = "A feature-rich command-line audio/video downloader"
|
summary = "A feature-rich command-line audio/video downloader"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
@@ -803,11 +828,11 @@ dependencies = [
|
|||||||
"certifi",
|
"certifi",
|
||||||
"mutagen",
|
"mutagen",
|
||||||
"pycryptodomex",
|
"pycryptodomex",
|
||||||
"requests<3,>=2.31.0",
|
"requests<3,>=2.32.2",
|
||||||
"urllib3<3,>=1.26.17",
|
"urllib3<3,>=1.26.17",
|
||||||
"websockets>=12.0",
|
"websockets>=12.0",
|
||||||
]
|
]
|
||||||
files = [
|
files = [
|
||||||
{file = "yt_dlp-2024.5.16.232713.dev0-py3-none-any.whl", hash = "sha256:42d3c27ab77583ff67ee2ddc94e376ea2a76a561ed8b1836ee04fd1cd23ad88c"},
|
{file = "yt_dlp-2024.6.17.232743.dev0-py3-none-any.whl", hash = "sha256:dd6e7e194b96e778691f58a0cb6b42956cf956b22f6bb1a12bdef5ab3ac0c9ad"},
|
||||||
{file = "yt_dlp-2024.5.16.232713.dev0.tar.gz", hash = "sha256:d431187fa703c9f52225080ae56471272679e44d9363f97b7b3187d37a5e6480"},
|
{file = "yt_dlp-2024.6.17.232743.dev0.tar.gz", hash = "sha256:2f6f44eff755a7b051cdcd3c4375771033dbeb64d6164351022efdc67cce0c52"},
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -1,12 +1,11 @@
|
|||||||
[project]
|
[project]
|
||||||
name = "xiaomusic"
|
name = "xiaomusic"
|
||||||
version = "0.1.37"
|
version = "0.1.52"
|
||||||
description = "Play Music with xiaomi AI speaker"
|
description = "Play Music with xiaomi AI speaker"
|
||||||
authors = [
|
authors = [
|
||||||
{name = "涵曦", email = "im.hanxi@gmail.com"},
|
{name = "涵曦", email = "im.hanxi@gmail.com"},
|
||||||
]
|
]
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"rich>=13.6.0",
|
|
||||||
"requests>=2.31.0",
|
"requests>=2.31.0",
|
||||||
"aiohttp>=3.8.6",
|
"aiohttp>=3.8.6",
|
||||||
"miservice-fork>=2.5.0",
|
"miservice-fork>=2.5.0",
|
||||||
@@ -24,3 +23,29 @@ requires = ["pdm-backend"]
|
|||||||
build-backend = "pdm.backend"
|
build-backend = "pdm.backend"
|
||||||
|
|
||||||
[tool.pdm]
|
[tool.pdm]
|
||||||
|
[tool.pdm.dev-dependencies]
|
||||||
|
lint = [
|
||||||
|
"ruff>=0.4.8",
|
||||||
|
]
|
||||||
|
[tool.ruff]
|
||||||
|
lint.select = [
|
||||||
|
"B", # flake8-bugbear
|
||||||
|
"C4", # flake8-comprehensions
|
||||||
|
"E", # pycodestyle - Error
|
||||||
|
"F", # Pyflakes
|
||||||
|
"I", # isort
|
||||||
|
"W", # pycodestyle - Warning
|
||||||
|
"UP", # pyupgrade
|
||||||
|
]
|
||||||
|
lint.ignore = [
|
||||||
|
"E501", # line-too-long
|
||||||
|
"W191", # tab-indentation
|
||||||
|
]
|
||||||
|
include = ["**/*.py", "**/*.pyi", "**/pyproject.toml"]
|
||||||
|
|
||||||
|
[tool.ruff.lint.pydocstyle]
|
||||||
|
convention = "google"
|
||||||
|
|
||||||
|
[tool.pdm.scripts]
|
||||||
|
lint = "ruff check ."
|
||||||
|
fmt = "ruff format ."
|
||||||
|
|||||||
@@ -368,9 +368,9 @@ pycryptodomex==3.19.0 \
|
|||||||
pygments==2.16.1 \
|
pygments==2.16.1 \
|
||||||
--hash=sha256:13fc09fa63bc8d8671a6d247e1eb303c4b343eaee81d861f3404db2935653692 \
|
--hash=sha256:13fc09fa63bc8d8671a6d247e1eb303c4b343eaee81d861f3404db2935653692 \
|
||||||
--hash=sha256:1daff0494820c69bc8941e407aa20f577374ee88364ee10a98fdbe0aece96e29
|
--hash=sha256:1daff0494820c69bc8941e407aa20f577374ee88364ee10a98fdbe0aece96e29
|
||||||
requests==2.31.0 \
|
requests==2.32.3 \
|
||||||
--hash=sha256:58cd2187c01e70e6e26505bca751777aa9f2ee0b7f4300988b709f44e013003f \
|
--hash=sha256:55365417734eb18255590a9ff9eb97e9e1da868d4ccd6402399eaf68af20a760 \
|
||||||
--hash=sha256:942c5a758f98d790eaed1a29cb6eefc7ffb0d1cf7af05c3d2791656dbd6ad1e1
|
--hash=sha256:70761cfe03c773ceb22aa2f671b4757976145175cdfca038c02654d061d6dcc6
|
||||||
rich==13.7.1 \
|
rich==13.7.1 \
|
||||||
--hash=sha256:4edbae314f59eb482f54e9e30bf00d33350aaa94f4bfcd4e9e3110e64d0d7222 \
|
--hash=sha256:4edbae314f59eb482f54e9e30bf00d33350aaa94f4bfcd4e9e3110e64d0d7222 \
|
||||||
--hash=sha256:9be308cb1fe2f1f57d67ce99e95af38a1e2bc71ad9813b0e247cf7ffbcc3a432
|
--hash=sha256:9be308cb1fe2f1f57d67ce99e95af38a1e2bc71ad9813b0e247cf7ffbcc3a432
|
||||||
@@ -469,6 +469,6 @@ yarl==1.9.2 \
|
|||||||
--hash=sha256:d4e2c6d555e77b37288eaf45b8f60f0737c9efa3452c6c44626a5455aeb250b9 \
|
--hash=sha256:d4e2c6d555e77b37288eaf45b8f60f0737c9efa3452c6c44626a5455aeb250b9 \
|
||||||
--hash=sha256:e9fdc7ac0d42bc3ea78818557fab03af6181e076a2944f43c38684b4b6bed8e3 \
|
--hash=sha256:e9fdc7ac0d42bc3ea78818557fab03af6181e076a2944f43c38684b4b6bed8e3 \
|
||||||
--hash=sha256:ee4afac41415d52d53a9833ebae7e32b344be72835bbb589018c9e938045a560
|
--hash=sha256:ee4afac41415d52d53a9833ebae7e32b344be72835bbb589018c9e938045a560
|
||||||
yt-dlp==2024.5.16.232713.dev0 \
|
yt-dlp==2024.6.17.232743.dev0 \
|
||||||
--hash=sha256:42d3c27ab77583ff67ee2ddc94e376ea2a76a561ed8b1836ee04fd1cd23ad88c \
|
--hash=sha256:2f6f44eff755a7b051cdcd3c4375771033dbeb64d6164351022efdc67cce0c52 \
|
||||||
--hash=sha256:d431187fa703c9f52225080ae56471272679e44d9363f97b7b3187d37a5e6480
|
--hash=sha256:dd6e7e194b96e778691f58a0cb6b42956cf956b22f6bb1a12bdef5ab3ac0c9ad
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
pdm export -o requirements.txt
|
pdm export --prod -o requirements.txt
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
__version__ = "0.1.37"
|
__version__ = "0.1.52"
|
||||||
|
|||||||
@@ -3,8 +3,7 @@ from __future__ import annotations
|
|||||||
import argparse
|
import argparse
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
from dataclasses import dataclass, field
|
from dataclasses import dataclass
|
||||||
from typing import Any, Iterable
|
|
||||||
|
|
||||||
from xiaomusic.utils import validate_proxy
|
from xiaomusic.utils import validate_proxy
|
||||||
|
|
||||||
@@ -13,7 +12,7 @@ COOKIE_TEMPLATE = "deviceId={device_id}; serviceToken={service_token}; userId={u
|
|||||||
HARDWARE_COMMAND_DICT = {
|
HARDWARE_COMMAND_DICT = {
|
||||||
# hardware: (tts_command, wakeup_command, volume_command)
|
# hardware: (tts_command, wakeup_command, volume_command)
|
||||||
"LX06": ("5-1", "5-5", "2-1"),
|
"LX06": ("5-1", "5-5", "2-1"),
|
||||||
"L05B": ("5-3", "5-4", "2-1"),
|
"L05B": ("5-3", "5-4", "2-1"),
|
||||||
"S12": ("5-1", "5-5", "2-1"), # 第一代小爱,型号MDZ-25-DA
|
"S12": ("5-1", "5-5", "2-1"), # 第一代小爱,型号MDZ-25-DA
|
||||||
"S12A": ("5-1", "5-5", "2-1"),
|
"S12A": ("5-1", "5-5", "2-1"),
|
||||||
"LX01": ("5-1", "5-5", "2-1"),
|
"LX01": ("5-1", "5-5", "2-1"),
|
||||||
@@ -43,6 +42,8 @@ KEY_WORD_DICT = {
|
|||||||
"关机": "stop",
|
"关机": "stop",
|
||||||
"停止播放": "stop",
|
"停止播放": "stop",
|
||||||
"分钟后关机": "stop_after_minute",
|
"分钟后关机": "stop_after_minute",
|
||||||
|
"播放列表": "play_music_list",
|
||||||
|
"刷新列表": "gen_music_list",
|
||||||
"set_volume#": "set_volume",
|
"set_volume#": "set_volume",
|
||||||
"get_volume#": "get_volume",
|
"get_volume#": "get_volume",
|
||||||
}
|
}
|
||||||
@@ -65,6 +66,8 @@ KEY_MATCH_ORDER = [
|
|||||||
"随机播放",
|
"随机播放",
|
||||||
"关机",
|
"关机",
|
||||||
"停止播放",
|
"停止播放",
|
||||||
|
"刷新列表",
|
||||||
|
"播放列表",
|
||||||
]
|
]
|
||||||
|
|
||||||
SUPPORT_MUSIC_TYPE = [
|
SUPPORT_MUSIC_TYPE = [
|
||||||
@@ -82,7 +85,7 @@ class Config:
|
|||||||
mute_xiaoai: bool = True
|
mute_xiaoai: bool = True
|
||||||
cookie: str = ""
|
cookie: str = ""
|
||||||
use_command: bool = False
|
use_command: bool = False
|
||||||
verbose: bool = False
|
verbose: bool = os.getenv("XIAOMUSIC_VERBOSE", "").lower() == "true"
|
||||||
music_path: str = os.getenv("XIAOMUSIC_MUSIC_PATH", "music")
|
music_path: str = os.getenv("XIAOMUSIC_MUSIC_PATH", "music")
|
||||||
hostname: str = os.getenv("XIAOMUSIC_HOSTNAME", "192.168.2.5")
|
hostname: str = os.getenv("XIAOMUSIC_HOSTNAME", "192.168.2.5")
|
||||||
port: int = int(os.getenv("XIAOMUSIC_PORT", "8090"))
|
port: int = int(os.getenv("XIAOMUSIC_PORT", "8090"))
|
||||||
@@ -92,6 +95,7 @@ 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("XIAOMUSIC_ACTIVE_CMD", "play,random_play")
|
active_cmd: str = os.getenv("XIAOMUSIC_ACTIVE_CMD", "play,random_play")
|
||||||
|
exclude_dirs: str = os.getenv("XIAOMUSIC_EXCLUDE_DIRS", "@eaDir")
|
||||||
|
|
||||||
def __post_init__(self) -> None:
|
def __post_init__(self) -> None:
|
||||||
if self.proxy:
|
if self.proxy:
|
||||||
|
|||||||
@@ -1,25 +1,21 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
import os
|
import os
|
||||||
import sys
|
from threading import Thread
|
||||||
import traceback
|
|
||||||
import asyncio
|
|
||||||
|
|
||||||
from flask import Flask, request, send_from_directory
|
from flask import Flask, request, send_from_directory
|
||||||
from waitress import serve
|
from waitress import serve
|
||||||
from threading import Thread
|
|
||||||
|
|
||||||
from xiaomusic.config import (
|
|
||||||
KEY_WORD_DICT,
|
|
||||||
)
|
|
||||||
|
|
||||||
from xiaomusic import (
|
from xiaomusic import (
|
||||||
__version__,
|
__version__,
|
||||||
)
|
)
|
||||||
|
from xiaomusic.config import (
|
||||||
|
KEY_WORD_DICT,
|
||||||
|
)
|
||||||
|
|
||||||
# 隐藏 flask 启动告警
|
# 隐藏 flask 启动告警
|
||||||
# https://gist.github.com/jerblack/735b9953ba1ab6234abb43174210d356
|
# https://gist.github.com/jerblack/735b9953ba1ab6234abb43174210d356
|
||||||
#from flask import cli
|
# from flask import cli
|
||||||
#cli.show_server_banner = lambda *_: None
|
# cli.show_server_banner = lambda *_: None
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
host = "0.0.0.0"
|
host = "0.0.0.0"
|
||||||
@@ -33,6 +29,7 @@ log = None
|
|||||||
def allcmds():
|
def allcmds():
|
||||||
return KEY_WORD_DICT
|
return KEY_WORD_DICT
|
||||||
|
|
||||||
|
|
||||||
@app.route("/getversion", methods=["GET"])
|
@app.route("/getversion", methods=["GET"])
|
||||||
def getversion():
|
def getversion():
|
||||||
log.debug("getversion %s", __version__)
|
log.debug("getversion %s", __version__)
|
||||||
@@ -40,6 +37,7 @@ def getversion():
|
|||||||
"version": __version__,
|
"version": __version__,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@app.route("/getvolume", methods=["GET"])
|
@app.route("/getvolume", methods=["GET"])
|
||||||
def getvolume():
|
def getvolume():
|
||||||
volume = xiaomusic.get_volume_ret()
|
volume = xiaomusic.get_volume_ret()
|
||||||
@@ -47,15 +45,18 @@ def getvolume():
|
|||||||
"volume": volume,
|
"volume": volume,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@app.route("/searchmusic", methods=["GET"])
|
@app.route("/searchmusic", methods=["GET"])
|
||||||
def searchmusic():
|
def searchmusic():
|
||||||
name = request.args.get('name')
|
name = request.args.get("name")
|
||||||
return xiaomusic.searchmusic(name)
|
return xiaomusic.searchmusic(name)
|
||||||
|
|
||||||
|
|
||||||
@app.route("/playingmusic", methods=["GET"])
|
@app.route("/playingmusic", methods=["GET"])
|
||||||
def playingmusic():
|
def playingmusic():
|
||||||
return xiaomusic.playingmusic()
|
return xiaomusic.playingmusic()
|
||||||
|
|
||||||
|
|
||||||
@app.route("/", methods=["GET"])
|
@app.route("/", methods=["GET"])
|
||||||
def redirect_to_index():
|
def redirect_to_index():
|
||||||
return send_from_directory("static", "index.html")
|
return send_from_directory("static", "index.html")
|
||||||
@@ -71,6 +72,7 @@ async def do_cmd():
|
|||||||
return {"ret": "OK"}
|
return {"ret": "OK"}
|
||||||
return {"ret": "Unknow cmd"}
|
return {"ret": "Unknow cmd"}
|
||||||
|
|
||||||
|
|
||||||
@app.route("/getsetting", methods=["GET"])
|
@app.route("/getsetting", methods=["GET"])
|
||||||
async def getsetting():
|
async def getsetting():
|
||||||
config = xiaomusic.getconfig()
|
config = xiaomusic.getconfig()
|
||||||
@@ -88,6 +90,7 @@ async def getsetting():
|
|||||||
}
|
}
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
@app.route("/savesetting", methods=["POST"])
|
@app.route("/savesetting", methods=["POST"])
|
||||||
async def savesetting():
|
async def savesetting():
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
@@ -95,6 +98,25 @@ async def savesetting():
|
|||||||
await xiaomusic.saveconfig(data)
|
await xiaomusic.saveconfig(data)
|
||||||
return "save success"
|
return "save success"
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/musiclist", methods=["GET"])
|
||||||
|
async def musiclist():
|
||||||
|
return xiaomusic.get_music_list()
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/curplaylist", methods=["GET"])
|
||||||
|
async def curplaylist():
|
||||||
|
return xiaomusic.get_cur_play_list()
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/delmusic", methods=["POST"])
|
||||||
|
def delmusic():
|
||||||
|
data = request.get_json()
|
||||||
|
log.info(data)
|
||||||
|
xiaomusic.del_music(data["name"])
|
||||||
|
return "success"
|
||||||
|
|
||||||
|
|
||||||
def static_path_handler(filename):
|
def static_path_handler(filename):
|
||||||
log.debug(filename)
|
log.debug(filename)
|
||||||
log.debug(static_path)
|
log.debug(static_path)
|
||||||
@@ -102,9 +124,11 @@ def static_path_handler(filename):
|
|||||||
log.debug(absolute_path)
|
log.debug(absolute_path)
|
||||||
return send_from_directory(absolute_path, filename)
|
return send_from_directory(absolute_path, filename)
|
||||||
|
|
||||||
|
|
||||||
def run_app():
|
def run_app():
|
||||||
serve(app, host=host, port=port)
|
serve(app, host=host, port=port)
|
||||||
|
|
||||||
|
|
||||||
def StartHTTPServer(_port, _static_path, _xiaomusic):
|
def StartHTTPServer(_port, _static_path, _xiaomusic):
|
||||||
global port, static_path, xiaomusic, log
|
global port, static_path, xiaomusic, log
|
||||||
port = _port
|
port = _port
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
$(function(){
|
$(function(){
|
||||||
$container=$("#cmds");
|
$container=$("#cmds");
|
||||||
append_op_button_name("下一首");
|
|
||||||
append_op_button_name("全部循环");
|
append_op_button_name("全部循环");
|
||||||
append_op_button_name("关机");
|
|
||||||
append_op_button_name("单曲循环");
|
append_op_button_name("单曲循环");
|
||||||
append_op_button_name("播放歌曲");
|
|
||||||
append_op_button_name("随机播放");
|
append_op_button_name("随机播放");
|
||||||
|
append_op_button_name("刷新列表");
|
||||||
|
append_op_button_name("下一首");
|
||||||
|
append_op_button_name("关机");
|
||||||
|
|
||||||
$container.append($("<hr>"));
|
$container.append($("<hr>"));
|
||||||
|
|
||||||
@@ -26,6 +26,61 @@ $(function(){
|
|||||||
$("#version").text(`(${data.version})`);
|
$("#version").text(`(${data.version})`);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 拉取播放列表
|
||||||
|
function refresh_music_list() {
|
||||||
|
$('#music_list').empty();
|
||||||
|
$.get("/musiclist", function(data, status) {
|
||||||
|
console.log(data, status);
|
||||||
|
$.each(data, function(key, value) {
|
||||||
|
$('#music_list').append($('<option></option>').val(key).text(key));
|
||||||
|
});
|
||||||
|
|
||||||
|
$('#music_list').change(function() {
|
||||||
|
const selectedValue = $(this).val();
|
||||||
|
$('#music_name').empty();
|
||||||
|
const sorted_musics = data[selectedValue].sort(custom_sort_key);
|
||||||
|
$.each(sorted_musics, function(index, item) {
|
||||||
|
$('#music_name').append($('<option></option>').val(item).text(item));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
$('#music_list').trigger('change');
|
||||||
|
|
||||||
|
// 获取当前播放列表
|
||||||
|
$.get("curplaylist", function(data, status) {
|
||||||
|
$('#music_list').val(data);
|
||||||
|
$('#music_list').trigger('change');
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
refresh_music_list();
|
||||||
|
|
||||||
|
$("#play_music_list").on("click", () => {
|
||||||
|
var music_list = $("#music_list").val();
|
||||||
|
var music_name = $("#music_name").val();
|
||||||
|
let cmd = "播放列表" + music_list + "|" + music_name;
|
||||||
|
sendcmd(cmd);
|
||||||
|
});
|
||||||
|
|
||||||
|
$("#del_music").on("click", () => {
|
||||||
|
var del_music_name = $("#music_name").val();
|
||||||
|
if (confirm(`确定删除歌曲 ${del_music_name} 吗?`)) {
|
||||||
|
console.log(`删除歌曲 ${del_music_name}`);
|
||||||
|
$.ajax({
|
||||||
|
type: 'POST',
|
||||||
|
url: '/delmusic',
|
||||||
|
data: JSON.stringify({"name": del_music_name}),
|
||||||
|
contentType: "application/json; charset=utf-8",
|
||||||
|
success: () => {
|
||||||
|
alert(`删除 ${del_music_name} 成功`);
|
||||||
|
refresh_music_list();
|
||||||
|
},
|
||||||
|
error: () => {
|
||||||
|
alert(`删除 ${del_music_name} 失败`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
function append_op_button_name(name) {
|
function append_op_button_name(name) {
|
||||||
append_op_button(name, name);
|
append_op_button(name, name);
|
||||||
@@ -48,8 +103,8 @@ $(function(){
|
|||||||
|
|
||||||
$("#play").on("click", () => {
|
$("#play").on("click", () => {
|
||||||
var search_key = $("#music-name").val();
|
var search_key = $("#music-name").val();
|
||||||
var filename=$("#music-filename").val();
|
var filename = $("#music-filename").val();
|
||||||
let cmd = "播放歌曲"+search_key+"|"+filename;
|
let cmd = "播放歌曲" + search_key + "|" + filename;
|
||||||
sendcmd(cmd);
|
sendcmd(cmd);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -62,10 +117,12 @@ $(function(){
|
|||||||
$.ajax({
|
$.ajax({
|
||||||
type: "POST",
|
type: "POST",
|
||||||
url: "/cmd",
|
url: "/cmd",
|
||||||
contentType: "application/json",
|
contentType: "application/json; charset=utf-8",
|
||||||
data: JSON.stringify({cmd: cmd}),
|
data: JSON.stringify({cmd: cmd}),
|
||||||
success: () => {
|
success: () => {
|
||||||
// 请求成功时执行的操作
|
if (cmd == "刷新列表") {
|
||||||
|
setTimeout(refresh_music_list, 3000);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
error: () => {
|
error: () => {
|
||||||
// 请求失败时执行的操作
|
// 请求失败时执行的操作
|
||||||
@@ -107,4 +164,23 @@ $(function(){
|
|||||||
setInterval(() => {
|
setInterval(() => {
|
||||||
get_playing_music();
|
get_playing_music();
|
||||||
}, 3000);
|
}, 3000);
|
||||||
|
|
||||||
|
function custom_sort_key(a, b) {
|
||||||
|
// 使用正则表达式提取数字前缀
|
||||||
|
const numericPrefixA = a.match(/^(\d+)/) ? parseInt(a.match(/^(\d+)/)[1], 10) : null;
|
||||||
|
const numericPrefixB = b.match(/^(\d+)/) ? parseInt(b.match(/^(\d+)/)[1], 10) : null;
|
||||||
|
|
||||||
|
// 如果两个键都有数字前缀,则按数字大小排序
|
||||||
|
if (numericPrefixA !== null && numericPrefixB !== null) {
|
||||||
|
return numericPrefixA - numericPrefixB;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果一个键有数字前缀而另一个没有,则有数字前缀的键排在前面
|
||||||
|
if (numericPrefixA !== null) return -1;
|
||||||
|
if (numericPrefixB !== null) return 1;
|
||||||
|
|
||||||
|
// 如果两个键都没有数字前缀,则按照常规字符串排序
|
||||||
|
return a.localeCompare(b);
|
||||||
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -33,6 +33,16 @@
|
|||||||
<div id="playering-music" class="text"></div>
|
<div id="playering-music" class="text"></div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<hr>
|
||||||
|
<div class="rows">
|
||||||
|
<label for="music_list">播放列表:</label>
|
||||||
|
<select id="music_list"></select>
|
||||||
|
<label for="music_name">歌曲:</label>
|
||||||
|
<select id="music_name"></select>
|
||||||
|
</div>
|
||||||
|
<button id="play_music_list">播放列表歌曲</button>
|
||||||
|
<button id="del_music">删除选中歌曲</button>
|
||||||
|
|
||||||
<footer>
|
<footer>
|
||||||
<p>Powered by <a href="https://github.com/hanxi/xiaomusic" target="_blank">xiaomusic</a></p>
|
<p>Powered by <a href="https://github.com/hanxi/xiaomusic" target="_blank">xiaomusic</a></p>
|
||||||
</footer>
|
</footer>
|
||||||
|
|||||||
@@ -62,7 +62,6 @@ input,select {
|
|||||||
}
|
}
|
||||||
|
|
||||||
footer {
|
footer {
|
||||||
position: fixed;
|
|
||||||
bottom: 0;
|
bottom: 0;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
text-align: center;
|
text-align: center;
|
||||||
|
|||||||
@@ -1,13 +1,11 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import os
|
|
||||||
import re
|
|
||||||
import socket
|
|
||||||
from http.cookies import SimpleCookie
|
|
||||||
from typing import AsyncIterator
|
|
||||||
from urllib.parse import urlparse
|
|
||||||
import difflib
|
import difflib
|
||||||
|
import re
|
||||||
|
from collections.abc import AsyncIterator
|
||||||
|
from http.cookies import SimpleCookie
|
||||||
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
from requests.utils import cookiejar_from_dict
|
from requests.utils import cookiejar_from_dict
|
||||||
|
|
||||||
@@ -62,6 +60,27 @@ def validate_proxy(proxy_str: str) -> bool:
|
|||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
# 模糊搜索
|
# 模糊搜索
|
||||||
def fuzzyfinder(user_input, collection):
|
def fuzzyfinder(user_input, collection):
|
||||||
return difflib.get_close_matches(user_input, collection, 10, cutoff=0.1)
|
return difflib.get_close_matches(user_input, collection, 10, cutoff=0.1)
|
||||||
|
|
||||||
|
|
||||||
|
# 歌曲排序
|
||||||
|
def custom_sort_key(s):
|
||||||
|
# 使用正则表达式分别提取字符串的数字前缀和数字后缀
|
||||||
|
prefix_match = re.match(r"^(\d+)", s)
|
||||||
|
suffix_match = re.search(r"(\d+)$", s)
|
||||||
|
|
||||||
|
numeric_prefix = int(prefix_match.group(0)) if prefix_match else None
|
||||||
|
numeric_suffix = int(suffix_match.group(0)) if suffix_match else None
|
||||||
|
|
||||||
|
if numeric_prefix is not None:
|
||||||
|
# 如果前缀是数字,先按前缀数字排序,再按整个字符串排序
|
||||||
|
return (0, numeric_prefix, s)
|
||||||
|
elif numeric_suffix is not None:
|
||||||
|
# 如果后缀是数字,先按前缀字符排序,再按后缀数字排序
|
||||||
|
return (1, s[: suffix_match.start()], numeric_suffix)
|
||||||
|
else:
|
||||||
|
# 如果前缀和后缀都不是数字,按字典序排序
|
||||||
|
return (2, s)
|
||||||
|
|||||||
@@ -3,44 +3,43 @@ import asyncio
|
|||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
import queue
|
||||||
import random
|
import random
|
||||||
import re
|
import re
|
||||||
import time
|
import time
|
||||||
import urllib.parse
|
|
||||||
import traceback
|
import traceback
|
||||||
import mutagen
|
import urllib.parse
|
||||||
import queue
|
|
||||||
from xiaomusic.httpserver import StartHTTPServer
|
|
||||||
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
import mutagen
|
||||||
from aiohttp import ClientSession, ClientTimeout
|
from aiohttp import ClientSession, ClientTimeout
|
||||||
from miservice import MiAccount, MiIOService, MiNAService, miio_command
|
from miservice import MiAccount, MiIOService, MiNAService, miio_command
|
||||||
from rich import print
|
|
||||||
from rich.logging import RichHandler
|
|
||||||
|
|
||||||
from xiaomusic.config import (
|
|
||||||
COOKIE_TEMPLATE,
|
|
||||||
LATEST_ASK_API,
|
|
||||||
KEY_WORD_DICT,
|
|
||||||
KEY_WORD_ARG_BEFORE_DICT,
|
|
||||||
KEY_MATCH_ORDER,
|
|
||||||
SUPPORT_MUSIC_TYPE,
|
|
||||||
Config,
|
|
||||||
)
|
|
||||||
from xiaomusic.utils import (
|
|
||||||
parse_cookie_string,
|
|
||||||
fuzzyfinder,
|
|
||||||
)
|
|
||||||
|
|
||||||
from xiaomusic import (
|
from xiaomusic import (
|
||||||
__version__,
|
__version__,
|
||||||
)
|
)
|
||||||
|
from xiaomusic.config import (
|
||||||
|
COOKIE_TEMPLATE,
|
||||||
|
KEY_MATCH_ORDER,
|
||||||
|
KEY_WORD_ARG_BEFORE_DICT,
|
||||||
|
KEY_WORD_DICT,
|
||||||
|
LATEST_ASK_API,
|
||||||
|
SUPPORT_MUSIC_TYPE,
|
||||||
|
Config,
|
||||||
|
)
|
||||||
|
from xiaomusic.httpserver import StartHTTPServer
|
||||||
|
from xiaomusic.utils import (
|
||||||
|
custom_sort_key,
|
||||||
|
fuzzyfinder,
|
||||||
|
parse_cookie_string,
|
||||||
|
)
|
||||||
|
|
||||||
EOF = object()
|
EOF = object()
|
||||||
|
|
||||||
PLAY_TYPE_ONE = 0 # 单曲循环
|
PLAY_TYPE_ONE = 0 # 单曲循环
|
||||||
PLAY_TYPE_ALL = 1 # 全部循环
|
PLAY_TYPE_ALL = 1 # 全部循环
|
||||||
|
PLAY_TYPE_RND = 2 # 随机播放
|
||||||
|
|
||||||
|
|
||||||
class XiaoMusic:
|
class XiaoMusic:
|
||||||
def __init__(self, config: Config):
|
def __init__(self, config: Config):
|
||||||
@@ -64,17 +63,20 @@ class XiaoMusic:
|
|||||||
self.search_prefix = config.search_prefix
|
self.search_prefix = config.search_prefix
|
||||||
self.ffmpeg_location = config.ffmpeg_location
|
self.ffmpeg_location = config.ffmpeg_location
|
||||||
self.active_cmd = config.active_cmd.split(",")
|
self.active_cmd = config.active_cmd.split(",")
|
||||||
|
self.exclude_dirs = set(config.exclude_dirs.split(","))
|
||||||
|
|
||||||
# 下载对象
|
# 下载对象
|
||||||
self.download_proc = None
|
self.download_proc = None
|
||||||
# 单曲循环,全部循环
|
# 单曲循环,全部循环
|
||||||
self.play_type = PLAY_TYPE_ALL
|
self.play_type = PLAY_TYPE_RND
|
||||||
self.cur_music = ""
|
self.cur_music = ""
|
||||||
self._next_timer = None
|
self._next_timer = None
|
||||||
self._timeout = 0
|
self._timeout = 0
|
||||||
self._volume = 0
|
self._volume = 0
|
||||||
self._all_music = {}
|
self._all_music = {}
|
||||||
self._play_list = []
|
self._play_list = []
|
||||||
|
self._cur_play_list = ""
|
||||||
|
self._music_list = {} # 播放列表 key 为目录名, value 为 play_list
|
||||||
self._playing = False
|
self._playing = False
|
||||||
|
|
||||||
# 关机定时器
|
# 关机定时器
|
||||||
@@ -82,9 +84,8 @@ class XiaoMusic:
|
|||||||
|
|
||||||
# setup logger
|
# setup logger
|
||||||
logging.basicConfig(
|
logging.basicConfig(
|
||||||
format=f"[{__version__}]\t%(message)s",
|
format=f"%(asctime)s [{__version__}] [%(levelname)s] %(message)s",
|
||||||
datefmt="[%X]",
|
datefmt="[%X]",
|
||||||
handlers=[RichHandler(rich_tracebacks=True)]
|
|
||||||
)
|
)
|
||||||
self.log = logging.getLogger("xiaomusic")
|
self.log = logging.getLogger("xiaomusic")
|
||||||
self.log.setLevel(logging.DEBUG if config.verbose else logging.INFO)
|
self.log.setLevel(logging.DEBUG if config.verbose else logging.INFO)
|
||||||
@@ -94,7 +95,7 @@ class XiaoMusic:
|
|||||||
self.try_init_setting()
|
self.try_init_setting()
|
||||||
|
|
||||||
# 启动时重新生成一次播放列表
|
# 启动时重新生成一次播放列表
|
||||||
self.gen_all_music_list()
|
self._gen_all_music_list()
|
||||||
|
|
||||||
# 启动时初始化获取声音
|
# 启动时初始化获取声音
|
||||||
self.set_last_record("get_volume#")
|
self.set_last_record("get_volume#")
|
||||||
@@ -199,13 +200,12 @@ class XiaoMusic:
|
|||||||
for i in range(retries):
|
for i in range(retries):
|
||||||
try:
|
try:
|
||||||
timeout = ClientTimeout(total=15)
|
timeout = ClientTimeout(total=15)
|
||||||
r = await session.get(
|
url = LATEST_ASK_API.format(
|
||||||
LATEST_ASK_API.format(
|
hardware=self.config.hardware,
|
||||||
hardware=self.config.hardware,
|
timestamp=str(int(time.time() * 1000)),
|
||||||
timestamp=str(int(time.time() * 1000)),
|
|
||||||
),
|
|
||||||
timeout=timeout,
|
|
||||||
)
|
)
|
||||||
|
self.log.debug(f"url:{url}")
|
||||||
|
r = await session.get(url, timeout=timeout)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.log.warning(
|
self.log.warning(
|
||||||
"Execption when get latest ask from xiaoai: %s", str(e)
|
"Execption when get latest ask from xiaoai: %s", str(e)
|
||||||
@@ -223,6 +223,7 @@ class XiaoMusic:
|
|||||||
return self._get_last_query(data)
|
return self._get_last_query(data)
|
||||||
|
|
||||||
def _get_last_query(self, data):
|
def _get_last_query(self, data):
|
||||||
|
self.log.debug(f"_get_last_query:{data}")
|
||||||
if d := data.get("data"):
|
if d := data.get("data"):
|
||||||
records = json.loads(d).get("records")
|
records = json.loads(d).get("records")
|
||||||
if not records:
|
if not records:
|
||||||
@@ -288,7 +289,10 @@ class XiaoMusic:
|
|||||||
def is_downloading(self):
|
def is_downloading(self):
|
||||||
if not self.download_proc:
|
if not self.download_proc:
|
||||||
return False
|
return False
|
||||||
if self.download_proc.returncode != None and self.download_proc.returncode < 0:
|
if (
|
||||||
|
self.download_proc.returncode is not None
|
||||||
|
and self.download_proc.returncode < 0
|
||||||
|
):
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@@ -340,9 +344,17 @@ class XiaoMusic:
|
|||||||
return f"http://{self.hostname}:{self.port}/{encoded_name}"
|
return f"http://{self.hostname}:{self.port}/{encoded_name}"
|
||||||
|
|
||||||
# 递归获取目录下所有歌曲,生成随机播放列表
|
# 递归获取目录下所有歌曲,生成随机播放列表
|
||||||
def gen_all_music_list(self):
|
def _gen_all_music_list(self):
|
||||||
self._all_music = {}
|
self._all_music = {}
|
||||||
|
all_music_by_dir = {}
|
||||||
for root, dirs, filenames in os.walk(self.music_path):
|
for root, dirs, filenames in os.walk(self.music_path):
|
||||||
|
dirs[:] = [d for d in dirs if d not in self.exclude_dirs]
|
||||||
|
self.log.debug("root:%s dirs:%s music_path:%s", root, dirs, self.music_path)
|
||||||
|
dir_name = os.path.basename(root)
|
||||||
|
if self.music_path == root:
|
||||||
|
dir_name = "其他"
|
||||||
|
if dir_name not in all_music_by_dir:
|
||||||
|
all_music_by_dir[dir_name] = {}
|
||||||
for filename in filenames:
|
for filename in filenames:
|
||||||
self.log.debug("gen_all_music_list. filename:%s", filename)
|
self.log.debug("gen_all_music_list. filename:%s", filename)
|
||||||
# 过滤隐藏文件
|
# 过滤隐藏文件
|
||||||
@@ -361,10 +373,28 @@ class XiaoMusic:
|
|||||||
|
|
||||||
# 歌曲名字相同会覆盖
|
# 歌曲名字相同会覆盖
|
||||||
self._all_music[name] = os.path.join(root, filename)
|
self._all_music[name] = os.path.join(root, filename)
|
||||||
|
all_music_by_dir[dir_name][name] = True
|
||||||
|
pass
|
||||||
self._play_list = list(self._all_music.keys())
|
self._play_list = list(self._all_music.keys())
|
||||||
random.shuffle(self._play_list)
|
self._cur_play_list = "全部"
|
||||||
|
self._gen_play_list()
|
||||||
self.log.debug(self._all_music)
|
self.log.debug(self._all_music)
|
||||||
|
|
||||||
|
self._music_list = {}
|
||||||
|
self._music_list["全部"] = self._play_list
|
||||||
|
for dir_name, musics in all_music_by_dir.items():
|
||||||
|
self._music_list[dir_name] = list(musics.keys())
|
||||||
|
self.log.debug("dir_name:%s, list:%s", dir_name, self._music_list[dir_name])
|
||||||
|
pass
|
||||||
|
|
||||||
|
# 歌曲排序或者打乱顺序
|
||||||
|
def _gen_play_list(self):
|
||||||
|
if self.play_type == PLAY_TYPE_RND:
|
||||||
|
random.shuffle(self._play_list)
|
||||||
|
else:
|
||||||
|
self._play_list.sort(key=custom_sort_key)
|
||||||
|
self.log.debug("play_list:%s", self._play_list)
|
||||||
|
|
||||||
# 把下载的音乐加入播放列表
|
# 把下载的音乐加入播放列表
|
||||||
def add_download_music(self, name):
|
def add_download_music(self, name):
|
||||||
self._all_music[name] = os.path.join(self.music_path, f"{name}.mp3")
|
self._all_music[name] = os.path.join(self.music_path, f"{name}.mp3")
|
||||||
@@ -377,7 +407,7 @@ class XiaoMusic:
|
|||||||
def get_next_music(self):
|
def get_next_music(self):
|
||||||
play_list_len = len(self._play_list)
|
play_list_len = len(self._play_list)
|
||||||
if play_list_len == 0:
|
if play_list_len == 0:
|
||||||
self.log.warning(f"没有随机到歌曲")
|
self.log.warning("没有随机到歌曲")
|
||||||
return ""
|
return ""
|
||||||
# 随机选择一个文件
|
# 随机选择一个文件
|
||||||
index = 0
|
index = 0
|
||||||
@@ -388,8 +418,13 @@ class XiaoMusic:
|
|||||||
next_index = index + 1
|
next_index = index + 1
|
||||||
if next_index >= play_list_len:
|
if next_index >= play_list_len:
|
||||||
next_index = 0
|
next_index = 0
|
||||||
filename = self._play_list[next_index]
|
name = self._play_list[next_index]
|
||||||
return filename
|
filename = self.get_filename(name)
|
||||||
|
if len(filename) <= 0:
|
||||||
|
self._play_list.pop(next_index)
|
||||||
|
self.log.info(f"pop not exist music:{name}")
|
||||||
|
return self.get_next_music()
|
||||||
|
return name
|
||||||
|
|
||||||
# 获取文件播放时长
|
# 获取文件播放时长
|
||||||
def get_file_duration(self, filename):
|
def get_file_duration(self, filename):
|
||||||
@@ -406,7 +441,7 @@ class XiaoMusic:
|
|||||||
self.log.info(f"歌曲 {self.cur_music} : {filename} 的时长 {sec} 秒")
|
self.log.info(f"歌曲 {self.cur_music} : {filename} 的时长 {sec} 秒")
|
||||||
if self._next_timer:
|
if self._next_timer:
|
||||||
self._next_timer.cancel()
|
self._next_timer.cancel()
|
||||||
self.log.info(f"定时器已取消")
|
self.log.info("定时器已取消")
|
||||||
self._timeout = sec
|
self._timeout = sec
|
||||||
|
|
||||||
async def _do_next():
|
async def _do_next():
|
||||||
@@ -426,11 +461,11 @@ class XiaoMusic:
|
|||||||
await self.init_all_data(session)
|
await self.init_all_data(session)
|
||||||
task = asyncio.create_task(self.poll_latest_ask())
|
task = asyncio.create_task(self.poll_latest_ask())
|
||||||
assert task is not None # to keep the reference to task, do not remove this
|
assert task is not None # to keep the reference to task, do not remove this
|
||||||
filtered_keywords = [keyword for keyword in KEY_MATCH_ORDER if "#" not in keyword]
|
filtered_keywords = [
|
||||||
|
keyword for keyword in KEY_MATCH_ORDER if "#" not in keyword
|
||||||
|
]
|
||||||
joined_keywords = "/".join(filtered_keywords)
|
joined_keywords = "/".join(filtered_keywords)
|
||||||
self.log.info(
|
self.log.info(f"Running xiaomusic now, 用`{joined_keywords}`开头来控制")
|
||||||
f"Running xiaomusic now, 用`{joined_keywords}`开头来控制"
|
|
||||||
)
|
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
self.polling_event.set()
|
self.polling_event.set()
|
||||||
@@ -498,17 +533,35 @@ class XiaoMusic:
|
|||||||
return ("stop", {})
|
return ("stop", {})
|
||||||
return (None, None)
|
return (None, None)
|
||||||
|
|
||||||
|
# 判断是否播放一下私募歌曲
|
||||||
|
def check_play_next(self):
|
||||||
|
# 当前没我在播放的歌曲
|
||||||
|
if self.cur_music == "":
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
filename = self.get_filename(self.cur_music)
|
||||||
|
# 当前播放的歌曲不存在了
|
||||||
|
if len(filename) <= 0:
|
||||||
|
return True
|
||||||
|
pass
|
||||||
|
return False
|
||||||
|
|
||||||
# 播放歌曲
|
# 播放歌曲
|
||||||
async def play(self, **kwargs):
|
async def play(self, **kwargs):
|
||||||
self._playing = True
|
self._playing = True
|
||||||
parts = kwargs["arg1"].split("|")
|
parts = kwargs["arg1"].split("|")
|
||||||
search_key = parts[0]
|
search_key = parts[0]
|
||||||
name = parts[1] if len(parts) > 1 else search_key
|
name = parts[1] if len(parts) > 1 else search_key
|
||||||
if search_key == "" and name == "":
|
|
||||||
await self.play_next()
|
|
||||||
return
|
|
||||||
if name == "":
|
if name == "":
|
||||||
name = search_key
|
name = search_key
|
||||||
|
|
||||||
|
if search_key == "" and name == "":
|
||||||
|
if self.check_play_next():
|
||||||
|
await self.play_next()
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
name = self.cur_music
|
||||||
|
|
||||||
self.log.debug("play. search_key:%s name:%s", search_key, name)
|
self.log.debug("play. search_key:%s name:%s", search_key, name)
|
||||||
filename = self.get_filename(name)
|
filename = self.get_filename(name)
|
||||||
|
|
||||||
@@ -534,42 +587,83 @@ class XiaoMusic:
|
|||||||
self.log.info("下一首")
|
self.log.info("下一首")
|
||||||
name = self.cur_music
|
name = self.cur_music
|
||||||
self.log.debug("play_next. name:%s, cur_music:%s", name, self.cur_music)
|
self.log.debug("play_next. name:%s, cur_music:%s", name, self.cur_music)
|
||||||
if self.play_type == PLAY_TYPE_ALL or name == "":
|
if (
|
||||||
|
self.play_type == PLAY_TYPE_ALL
|
||||||
|
or self.play_type == PLAY_TYPE_RND
|
||||||
|
or name == ""
|
||||||
|
):
|
||||||
name = self.get_next_music()
|
name = self.get_next_music()
|
||||||
if name == "":
|
if name == "":
|
||||||
await self.do_tts(f"本地没有歌曲")
|
await self.do_tts("本地没有歌曲")
|
||||||
return
|
return
|
||||||
await self.play(arg1=name)
|
await self.play(arg1=name)
|
||||||
|
|
||||||
# 单曲循环
|
# 单曲循环
|
||||||
async def set_play_type_one(self, **kwargs):
|
async def set_play_type_one(self, **kwargs):
|
||||||
self.play_type = PLAY_TYPE_ONE
|
self.play_type = PLAY_TYPE_ONE
|
||||||
await self.do_tts(f"已经设置为单曲循环")
|
await self.do_tts("已经设置为单曲循环")
|
||||||
|
|
||||||
# 全部循环
|
# 全部循环
|
||||||
async def set_play_type_all(self, **kwargs):
|
async def set_play_type_all(self, **kwargs):
|
||||||
self.play_type = PLAY_TYPE_ALL
|
self.play_type = PLAY_TYPE_ALL
|
||||||
await self.do_tts(f"已经设置为全部循环")
|
self._gen_play_list()
|
||||||
|
await self.do_tts("已经设置为全部循环")
|
||||||
|
|
||||||
# 随机播放
|
# 随机播放
|
||||||
async def random_play(self, **kwargs):
|
async def random_play(self, **kwargs):
|
||||||
self.play_type = PLAY_TYPE_ALL
|
self.play_type = PLAY_TYPE_RND
|
||||||
await self.do_tts(f"已经设置为全部循环并随机播放")
|
self._gen_play_list()
|
||||||
# 重新生成随机播放列表
|
await self.do_tts("已经设置为随机播放")
|
||||||
self.gen_all_music_list()
|
|
||||||
await self.play_next()
|
# 刷新列表
|
||||||
|
async def gen_music_list(self, **kwargs):
|
||||||
|
self._gen_all_music_list()
|
||||||
|
await self.do_tts("生成播放列表完毕")
|
||||||
|
|
||||||
|
# 删除歌曲
|
||||||
|
def del_music(self, name):
|
||||||
|
filename = self.get_filename(name)
|
||||||
|
if filename == "":
|
||||||
|
self.log.info(f"${name} not exist")
|
||||||
|
return
|
||||||
|
try:
|
||||||
|
os.remove(filename)
|
||||||
|
self.log.info(f"del ${filename} success")
|
||||||
|
except OSError:
|
||||||
|
self.log.error(f"del ${filename} failed")
|
||||||
|
pass
|
||||||
|
self._gen_all_music_list()
|
||||||
|
|
||||||
|
# 播放一个播放列表
|
||||||
|
async def play_music_list(self, **kwargs):
|
||||||
|
parts = kwargs["arg1"].split("|")
|
||||||
|
list_name = parts[0]
|
||||||
|
if list_name not in self._music_list:
|
||||||
|
await self.do_tts(f"播放列表{list_name}不存在")
|
||||||
|
return
|
||||||
|
self._play_list = self._music_list[list_name]
|
||||||
|
self._cur_play_list = list_name
|
||||||
|
self._gen_play_list()
|
||||||
|
self.log.info(f"开始播放列表{list_name}")
|
||||||
|
|
||||||
|
music_name = ""
|
||||||
|
if len(parts) > 1:
|
||||||
|
music_name = parts[1]
|
||||||
|
else:
|
||||||
|
music_name = self.get_next_music()
|
||||||
|
await self.play(arg1=music_name)
|
||||||
|
|
||||||
async def stop(self, **kwargs):
|
async def stop(self, **kwargs):
|
||||||
self._playing = False
|
self._playing = False
|
||||||
if self._next_timer:
|
if self._next_timer:
|
||||||
self._next_timer.cancel()
|
self._next_timer.cancel()
|
||||||
self.log.info(f"定时器已取消")
|
self.log.info("定时器已取消")
|
||||||
await self.force_stop_xiaoai()
|
await self.force_stop_xiaoai()
|
||||||
|
|
||||||
async def stop_after_minute(self, **kwargs):
|
async def stop_after_minute(self, **kwargs):
|
||||||
if self._stop_timer:
|
if self._stop_timer:
|
||||||
self._stop_timer.cancel()
|
self._stop_timer.cancel()
|
||||||
self.log.info(f"关机定时器已取消")
|
self.log.info("关机定时器已取消")
|
||||||
minute = int(kwargs["arg1"])
|
minute = int(kwargs["arg1"])
|
||||||
|
|
||||||
async def _do_stop():
|
async def _do_stop():
|
||||||
@@ -589,7 +683,9 @@ class XiaoMusic:
|
|||||||
async def get_volume(self, **kwargs):
|
async def get_volume(self, **kwargs):
|
||||||
playing_info = await self.mina_service.player_get_status(self.device_id)
|
playing_info = await self.mina_service.player_get_status(self.device_id)
|
||||||
self.log.debug("get_volume. playing_info:%s", playing_info)
|
self.log.debug("get_volume. playing_info:%s", playing_info)
|
||||||
self._volume = json.loads(playing_info.get("data", {}).get("info", "{}")).get("volume", 5)
|
self._volume = json.loads(playing_info.get("data", {}).get("info", "{}")).get(
|
||||||
|
"volume", 5
|
||||||
|
)
|
||||||
self.log.info("get_volume. volume:%s", self._volume)
|
self.log.info("get_volume. volume:%s", self._volume)
|
||||||
|
|
||||||
def get_volume_ret(self):
|
def get_volume_ret(self):
|
||||||
@@ -597,10 +693,19 @@ class XiaoMusic:
|
|||||||
|
|
||||||
# 搜索音乐
|
# 搜索音乐
|
||||||
def searchmusic(self, name):
|
def searchmusic(self, name):
|
||||||
search_list = fuzzyfinder(name, self._play_list)
|
all_music_list = list(self._all_music.keys())
|
||||||
|
search_list = fuzzyfinder(name, all_music_list)
|
||||||
self.log.debug("searchmusic. name:%s search_list:%s", name, search_list)
|
self.log.debug("searchmusic. name:%s search_list:%s", name, search_list)
|
||||||
return search_list
|
return search_list
|
||||||
|
|
||||||
|
# 获取播放列表
|
||||||
|
def get_music_list(self):
|
||||||
|
return self._music_list
|
||||||
|
|
||||||
|
# 获取当前的播放列表
|
||||||
|
def get_cur_play_list(self):
|
||||||
|
return self._cur_play_list
|
||||||
|
|
||||||
# 正在播放中的音乐
|
# 正在播放中的音乐
|
||||||
def playingmusic(self):
|
def playingmusic(self):
|
||||||
self.log.debug("playingmusic. cur_music:%s", self.cur_music)
|
self.log.debug("playingmusic. cur_music:%s", self.cur_music)
|
||||||
@@ -625,7 +730,7 @@ class XiaoMusic:
|
|||||||
async def saveconfig(self, data):
|
async def saveconfig(self, data):
|
||||||
# 默认暂时配置保存到 music 目录下
|
# 默认暂时配置保存到 music 目录下
|
||||||
filename = os.path.join(self.music_path, "setting.json")
|
filename = os.path.join(self.music_path, "setting.json")
|
||||||
with open(filename, 'w', encoding='utf-8') as f:
|
with open(filename, "w", encoding="utf-8") as f:
|
||||||
json.dump(data, f, ensure_ascii=False, indent=4)
|
json.dump(data, f, ensure_ascii=False, indent=4)
|
||||||
self.update_config_from_setting(data)
|
self.update_config_from_setting(data)
|
||||||
await self.call_main_thread_function(self.reinit)
|
await self.call_main_thread_function(self.reinit)
|
||||||
@@ -670,12 +775,13 @@ class XiaoMusic:
|
|||||||
async def call_main_thread_function(self, func, arg1=None):
|
async def call_main_thread_function(self, func, arg1=None):
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
future = loop.create_future()
|
future = loop.create_future()
|
||||||
|
|
||||||
def callback(ret):
|
def callback(ret):
|
||||||
nonlocal future
|
nonlocal future
|
||||||
loop.call_soon_threadsafe(future.set_result, ret)
|
loop.call_soon_threadsafe(future.set_result, ret)
|
||||||
|
|
||||||
self.queue.put((func, callback, arg1))
|
self.queue.put((func, callback, arg1))
|
||||||
self.last_record = None
|
self.last_record = None
|
||||||
self.new_record_event.set()
|
self.new_record_event.set()
|
||||||
result = await future
|
result = await future
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user