feat: 新增按钮刷新 tag 信息

This commit is contained in:
涵曦 2024-09-23 03:27:01 +08:00
parent 329c6b26bd
commit 5145590b1e
7 changed files with 60 additions and 31 deletions

1
.gitignore vendored
View File

@ -167,3 +167,4 @@ test.sh
conf
setting.json
.DS_Store
cache

View File

@ -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

View File

@ -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:

View File

@ -16,12 +16,10 @@
gtag('config', 'G-Z09NC1K7ZW');
</script>
<!--
<script src="https://unpkg.com/vconsole@latest/dist/vconsole.min.js"></script>
<script>
var vConsole = new window.VConsole();
</script>
-->
</head>
<body>
@ -70,6 +68,10 @@ var vConsole = new window.VConsole();
<label for="conf_path">配置文件目录:</label>
<input id="conf_path" type="text"></input>
<label for="cache_dir">缓存文件目录:</label>
<input id="cache_dir" type="text"></input>
<label for="ffmpeg_location">ffmpeg路径:</label>
<input id="ffmpeg_location" type="text" value="./ffmpeg/bin"></input>
@ -181,11 +183,14 @@ var vConsole = new window.VConsole();
</div>
<hr>
<button onclick="location.href='/';">返回首页</button>
<button id="get_music_list">获取歌单</button>
<button onclick="location.href='/static/default/index.html';">返回首页</button>
<button class="save-button">保存</button>
<hr>
<button id="get_music_list">获取歌单</button>
<button id="refresh_music_tag">刷新tag</button>
<hr>
<a class="button" href="/downloadlog" download="xiaomusic.txt">下载日志文件</a>
<button onclick="location.href='/docs';">查看接口文档</button>
<a class="button" href="./m3u.html" target="_blank">m3u文件转换</a>

View File

@ -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);
}
});
});
});

View File

@ -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

View File

@ -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}")