Skip to content

Commit

Permalink
fix: 调整配置,优化获取歌曲时长接口
Browse files Browse the repository at this point in the history
  • Loading branch information
hanxi committed Jul 30, 2024
1 parent 2d5f379 commit eaedef4
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 25 deletions.
10 changes: 10 additions & 0 deletions xiaomusic/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,13 @@ def update_config(self, data):
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
8 changes: 4 additions & 4 deletions xiaomusic/httpserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@
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()
Expand Down
10 changes: 4 additions & 6 deletions xiaomusic/utils.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -16,7 +16,6 @@
from http.cookies import SimpleCookie
from urllib.parse import urlparse

import aiofiles
import aiohttp
import mutagen
from mutagen.id3 import ID3
Expand Down Expand Up @@ -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:
Expand Down
18 changes: 3 additions & 15 deletions xiaomusic/xiaomusic.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,11 @@ def __init__(self, config: Config):
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
Expand Down Expand Up @@ -720,16 +716,9 @@ def isplaying(self, did):
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)
Expand All @@ -751,8 +740,7 @@ async def saveconfig(self, data):

# 配置文件落地
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)

Expand Down

0 comments on commit eaedef4

Please sign in to comment.