From 5145590b1e0168faec36111e1fbbcfbc0e4b6eb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B6=B5=E6=9B=A6?= Date: Mon, 23 Sep 2024 03:27:01 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E6=8C=89=E9=92=AE?= =?UTF-8?q?=E5=88=B7=E6=96=B0=20tag=20=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + xiaomusic/config.py | 11 +++----- xiaomusic/httpserver.py | 8 ++++++ xiaomusic/static/default/setting.html | 13 +++++++--- xiaomusic/static/default/setting.js | 17 ++++++++++++ xiaomusic/utils.py | 4 +-- xiaomusic/xiaomusic.py | 37 ++++++++++++++------------- 7 files changed, 60 insertions(+), 31 deletions(-) diff --git a/.gitignore b/.gitignore index b5a817d..d68d0a6 100644 --- a/.gitignore +++ b/.gitignore @@ -167,3 +167,4 @@ test.sh conf setting.json .DS_Store +cache diff --git a/xiaomusic/config.py b/xiaomusic/config.py index 1349b6f..3ee20e1 100644 --- a/xiaomusic/config.py +++ b/xiaomusic/config.py @@ -84,7 +84,7 @@ class Config: ) # 只能是music目录下的子目录 download_path: str = os.getenv("XIAOMUSIC_DOWNLOAD_PATH", "music/download") conf_path: str = os.getenv("XIAOMUSIC_CONF_PATH", "conf") - tag_cache_dir: str = os.getenv("XIAOMUSIC_TAG_CACHE_DIR", None) + cache_dir: str = os.getenv("XIAOMUSIC_CACHE_DIR", "cache") hostname: str = os.getenv("XIAOMUSIC_HOSTNAME", "192.168.2.5") port: int = int(os.getenv("XIAOMUSIC_PORT", "8090")) # 监听端口 public_port: int = int(os.getenv("XIAOMUSIC_PUBLIC_PORT", 0)) # 歌曲访问端口 @@ -250,10 +250,7 @@ class Config: @property def tag_cache_path(self): - if self.tag_cache_dir is None: - return None - - if not os.path.exists(self.tag_cache_dir): - os.makedirs(self.tag_cache_dir) - filename = os.path.join(self.tag_cache_dir, "tag_cache.json") + if not os.path.exists(self.cache_dir): + os.makedirs(self.cache_dir) + filename = os.path.join(self.cache_dir, "tag_cache.json") return filename diff --git a/xiaomusic/httpserver.py b/xiaomusic/httpserver.py index 3bbd30e..fec01d1 100644 --- a/xiaomusic/httpserver.py +++ b/xiaomusic/httpserver.py @@ -339,6 +339,14 @@ async def playurl(did: str, url: str, Verifcation=Depends(verification)): return await xiaomusic.play_url(did=did, arg1=decoded_url) +@app.post("/refreshmusictag") +async def refreshmusictag(Verifcation=Depends(verification)): + xiaomusic.refresh_music_tag() + return { + "ret": "OK", + } + + @app.post("/debug_play_by_music_url") async def debug_play_by_music_url(request: Request, Verifcation=Depends(verification)): try: diff --git a/xiaomusic/static/default/setting.html b/xiaomusic/static/default/setting.html index 329b991..d823f48 100644 --- a/xiaomusic/static/default/setting.html +++ b/xiaomusic/static/default/setting.html @@ -16,12 +16,10 @@ gtag('config', 'G-Z09NC1K7ZW'); - @@ -70,6 +68,10 @@ var vConsole = new window.VConsole(); + + + + @@ -181,11 +183,14 @@ var vConsole = new window.VConsole();
- - +
+ + +
+ 下载日志文件 m3u文件转换 diff --git a/xiaomusic/static/default/setting.js b/xiaomusic/static/default/setting.js index a77d191..2caafc0 100644 --- a/xiaomusic/static/default/setting.js +++ b/xiaomusic/static/default/setting.js @@ -137,4 +137,21 @@ $(function(){ } }); }); + + $("#refresh_music_tag").on("click", () => { + $.ajax({ + type: "POST", + url: "/refreshmusictag", + contentType: "application/json", + success: (res) => { + console.log(res); + alert(res.ret); + }, + error: (res) => { + console.log(res); + alert(res); + } + }); + }); + }); diff --git a/xiaomusic/utils.py b/xiaomusic/utils.py index 942951f..87d6ab1 100644 --- a/xiaomusic/utils.py +++ b/xiaomusic/utils.py @@ -16,7 +16,7 @@ import string import subprocess import tempfile from collections.abc import AsyncIterator -from dataclasses import dataclass +from dataclasses import asdict, dataclass from http.cookies import SimpleCookie from urllib.parse import urlparse @@ -504,7 +504,7 @@ def get_audio_metadata(file_path): ret = get_ogg_metadata(file_path) elif file_path.endswith(".m4a"): ret = get_m4a_metadata(file_path) - return ret + return {k: str(v) for k, v in asdict(ret).items()} @dataclass diff --git a/xiaomusic/xiaomusic.py b/xiaomusic/xiaomusic.py index b91b09e..cc8a66f 100644 --- a/xiaomusic/xiaomusic.py +++ b/xiaomusic/xiaomusic.py @@ -396,7 +396,7 @@ class XiaoMusic: return sec, url def get_music_tags(self, name): - return self.all_music_tags.get(name, Metadata()) + return self.all_music_tags.get(name, asdict(Metadata())) def get_music_url(self, name): if self.is_web_music(name): @@ -445,25 +445,28 @@ class XiaoMusic: # 清空 cache with open(filename, "w", encoding="utf-8") as f: json.dump({}, f, ensure_ascii=False, indent=2) - self.log.info(f"刷新:已清空 tag cache") + self.log.info("刷新:已清空 tag cache") else: - self.log.info(f"刷新:tag cache 未启用") - #TODO: 优化性能? + self.log.info("刷新:tag cache 未启用") + # TODO: 优化性能? self._gen_all_music_list() - self.log.debug(f"刷新:已重建 tag cache") + self.log.debug("刷新:已重建 tag cache") def try_load_from_tag_cache(self) -> dict: filename = self.config.tag_cache_path tag_cache = {} - if filename is not None: - if os.path.exists(filename): - with open(filename, "r", encoding="utf-8") as f: - tag_cache = json.load(f) - self.log.info(f"已从【{filename}】加载 tag cache") + try: + if filename is not None: + if os.path.exists(filename): + with open(filename, encoding="utf-8") as f: + tag_cache = json.load(f) + self.log.info(f"已从【{filename}】加载 tag cache") + else: + self.log.info(f"【{filename}】tag cache 已启用,但文件不存在") else: - self.log.info(f"【{filename}】tag cache 已启用,但文件不存在") - else: - self.log.info(f"加载:tag cache 未启用") + self.log.info("加载:tag cache 未启用") + except Exception as e: + self.log.exception(f"Execption {e}") return tag_cache def try_save_tag_cache(self): @@ -473,8 +476,8 @@ class XiaoMusic: json.dump(self.all_music_tags, f, ensure_ascii=False, indent=2) self.log.info(f"保存:tag cache 已保存到【{filename}】") else: - self.log.info(f"保存:tag cache 未启用") - + self.log.info("保存:tag cache 未启用") + # 获取目录下所有歌曲,生成随机播放列表 def _gen_all_music_list(self): self.all_music = {} @@ -503,9 +506,7 @@ class XiaoMusic: (name, _) = os.path.splitext(filename) self.all_music[name] = file if name not in self.all_music_tags: - self.all_music_tags[name] = { - k: str(v) for k, v in get_audio_metadata(file).items()} - print(f"加载 {name} tag") + self.all_music_tags[name] = get_audio_metadata(file) all_music_by_dir[dir_name][name] = True self.log.debug(f"_gen_all_music_list {name}:{dir_name}:{file}")