fix: 调整配置,优化获取歌曲时长接口

This commit is contained in:
涵曦 2024-07-30 01:09:01 +00:00
parent 2d5f3799a3
commit eaedef452c
4 changed files with 21 additions and 25 deletions

View File

@ -219,3 +219,13 @@ class Config:
if converted_value is not None:
setattr(self, k, converted_value)
self.init_keyword()
# 获取设置文件
def getsettingfile(self):
# 兼容旧配置空的情况
if not self.conf_path:
self.conf_path = "conf"
if not os.path.exists(self.conf_path):
os.makedirs(self.conf_path)
filename = os.path.join(self.conf_path, "setting.json")
return filename

View File

@ -30,10 +30,10 @@ log = None
async def app_lifespan(app):
if xiaomusic is not None:
asyncio.create_task(xiaomusic.run_forever())
try:
yield
except Exception as e:
log.exception(f"Execption {e}")
try:
yield
except Exception as e:
log.exception(f"Execption {e}")
security = HTTPBasic()

View File

@ -1,9 +1,9 @@
#!/usr/bin/env python3
from __future__ import annotations
import asyncio
import copy
import difflib
import io
import logging
import mimetypes
import os
@ -16,7 +16,6 @@ from collections.abc import AsyncIterator
from http.cookies import SimpleCookie
from urllib.parse import urlparse
import aiofiles
import aiohttp
import mutagen
from mutagen.id3 import ID3
@ -241,14 +240,13 @@ async def get_web_music_duration(url):
# 获取文件播放时长
async def get_local_music_duration(filename):
loop = asyncio.get_event_loop()
duration = 0
try:
async with aiofiles.open(filename, "rb") as f:
buffer = io.BytesIO(await f.read())
if is_mp3(filename):
m = mutagen.mp3.MP3(buffer)
m = await loop.run_in_executor(None, mutagen.mp3.MP3, filename)
else:
m = mutagen.File(buffer)
m = await loop.run_in_executor(None, mutagen.File, filename)
if m and m.info:
duration = m.info.length
except Exception as e:

View File

@ -84,15 +84,11 @@ class XiaoMusic:
debug_config = deepcopy_data_no_sensitive_info(self.config)
self.log.info(f"Startup OK. {debug_config}")
if self.conf_path == self.music_path:
if self.config.conf_path == self.music_path:
self.log.warning("配置文件目录和音乐目录建议设置为不同的目录")
def init_config(self):
self.music_path = self.config.music_path
self.conf_path = self.config.conf_path
# 兼容旧配置空的情况
if not self.conf_path:
self.conf_path = "conf"
self.download_path = self.config.download_path
if not self.download_path:
self.download_path = self.music_path
@ -720,16 +716,9 @@ class XiaoMusic:
def getconfig(self):
return self.config
# 获取设置文件
def getsettingfile(self):
if not os.path.exists(self.conf_path):
os.makedirs(self.conf_path)
filename = os.path.join(self.conf_path, "setting.json")
return filename
def try_init_setting(self):
try:
filename = self.getsettingfile()
filename = self.config.getsettingfile()
with open(filename) as f:
data = json.loads(f.read())
self.update_config_from_setting(data)
@ -751,8 +740,7 @@ class XiaoMusic:
# 配置文件落地
def do_saveconfig(self, data):
# 默认暂时配置保存到 music 目录下
filename = self.getsettingfile()
filename = self.config.getsettingfile()
with open(filename, "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=2)