mirror of
https://github.com/hanxi/xiaomusic.git
synced 2025-12-06 14:52:50 +08:00
feat: 监测音乐文件夹变化更新歌曲列表
This commit is contained in:
@@ -201,6 +201,9 @@ class Config:
|
||||
recently_added_playlist_len: int = int(
|
||||
os.getenv("XIAOMUSIC_RECENTLY_ADDED_PLAYLIST_LEN", "50")
|
||||
)
|
||||
enable_watchdog: bool = (
|
||||
os.getenv("XIAOMUSIC_ENABLE_WATCHDOG", "false").lower() == "true"
|
||||
)
|
||||
|
||||
def append_keyword(self, keys, action):
|
||||
for key in keys.split(","):
|
||||
|
||||
6
xiaomusic/static/default/setting.html
vendored
6
xiaomusic/static/default/setting.html
vendored
@@ -111,6 +111,12 @@ var vConsole = new window.VConsole();
|
||||
<label for="music_path_depth">目录深度:</label>
|
||||
<input id="music_path_depth" type="number" value="10" />
|
||||
|
||||
<label for="enable_watchdog">启用文件夹监控(需要重启):</label>
|
||||
<select id="enable_watchdog">
|
||||
<option value="true">true</option>
|
||||
<option value="false" selected>false</option>
|
||||
</select>
|
||||
|
||||
<label for="search_prefix">XIAOMUSIC_SEARCH(歌曲下载方式):</label>
|
||||
<select id="search_prefix">
|
||||
<option value="bilisearch:">bilisearch:</option>
|
||||
|
||||
@@ -9,6 +9,7 @@ import random
|
||||
import re
|
||||
import time
|
||||
import urllib.parse
|
||||
from threading import Timer
|
||||
from collections import OrderedDict
|
||||
from dataclasses import asdict
|
||||
from logging.handlers import RotatingFileHandler
|
||||
@@ -16,6 +17,9 @@ from logging.handlers import RotatingFileHandler
|
||||
from aiohttp import ClientSession, ClientTimeout
|
||||
from miservice import MiAccount, MiIOService, MiNAService, miio_command
|
||||
|
||||
from watchdog.observers import Observer
|
||||
from watchdog.events import FileSystemEventHandler
|
||||
|
||||
from xiaomusic import __version__
|
||||
from xiaomusic.analytics import Analytics
|
||||
from xiaomusic.config import (
|
||||
@@ -58,6 +62,26 @@ from xiaomusic.utils import (
|
||||
try_add_access_control_param,
|
||||
)
|
||||
|
||||
class MusicFolderHandler(FileSystemEventHandler):
|
||||
def __init__(self, xiaomusic):
|
||||
self.xiaomusic = xiaomusic
|
||||
self.last_event_time = 0
|
||||
self.timer = None
|
||||
|
||||
def on_any_event(self, event):
|
||||
current_time = time.time()
|
||||
if current_time - self.last_event_time > 10: # 控制任务队列,避免短时间内多次刷新
|
||||
self.last_event_time = current_time
|
||||
self.xiaomusic.log.info("检测到文件变化,10秒后刷新歌曲列表")
|
||||
if self.timer:
|
||||
self.timer.cancel()
|
||||
self.timer = Timer(10, self.run_gen_music_list)
|
||||
self.timer.start()
|
||||
|
||||
def run_gen_music_list(self):
|
||||
loop = asyncio.new_event_loop()
|
||||
asyncio.set_event_loop(loop)
|
||||
loop.run_until_complete(self.xiaomusic.gen_music_list())
|
||||
|
||||
class XiaoMusic:
|
||||
def __init__(self, config: Config):
|
||||
@@ -114,6 +138,9 @@ class XiaoMusic:
|
||||
if self.config.conf_path == self.music_path:
|
||||
self.log.warning("配置文件目录和音乐目录建议设置为不同的目录")
|
||||
|
||||
self.observer = None
|
||||
self.setup_watchdog()
|
||||
|
||||
def init_config(self):
|
||||
self.music_path = self.config.music_path
|
||||
self.download_path = self.config.download_path
|
||||
@@ -1115,6 +1142,10 @@ class XiaoMusic:
|
||||
# 停止
|
||||
async def stop(self, did="", arg1="", **kwargs):
|
||||
return await self.devices[did].stop(arg1=arg1)
|
||||
if self.observer:
|
||||
self.observer.stop()
|
||||
self.observer.join()
|
||||
self.log.info("stop now")
|
||||
|
||||
# 定时关机
|
||||
async def stop_after_minute(self, did="", arg1=0, **kwargs):
|
||||
@@ -1393,6 +1424,14 @@ class XiaoMusic:
|
||||
async def do_tts(self, did, value):
|
||||
return await self.devices[did].do_tts(value)
|
||||
|
||||
def setup_watchdog(self):
|
||||
if self.config.enable_watchdog:
|
||||
event_handler = MusicFolderHandler(self)
|
||||
self.observer = Observer()
|
||||
self.observer.schedule(event_handler, self.music_path, recursive=True)
|
||||
self.observer.start()
|
||||
self.log.info("Watchdog 已启动,监控文件夹变化")
|
||||
|
||||
|
||||
class XiaoMusicDevice:
|
||||
def __init__(self, xiaomusic: XiaoMusic, device: Device, group_name: str):
|
||||
|
||||
Reference in New Issue
Block a user