整理代码

This commit is contained in:
涵曦 2024-06-29 12:42:15 +00:00
parent dc49f63a37
commit db1e4e6fc4
3 changed files with 22 additions and 15 deletions

View File

@ -61,7 +61,9 @@ class Config:
"XIAOMUSIC_SEARCH", "ytsearch:" "XIAOMUSIC_SEARCH", "ytsearch:"
) # "bilisearch:" or "ytsearch:" ) # "bilisearch:" or "ytsearch:"
ffmpeg_location: str = os.getenv("XIAOMUSIC_FFMPEG_LOCATION", "./ffmpeg/bin") ffmpeg_location: str = os.getenv("XIAOMUSIC_FFMPEG_LOCATION", "./ffmpeg/bin")
active_cmd: str = os.getenv("XIAOMUSIC_ACTIVE_CMD", "play,random_play,playlocal,play_music_list,stop") active_cmd: str = os.getenv(
"XIAOMUSIC_ACTIVE_CMD", "play,random_play,playlocal,play_music_list,stop"
)
exclude_dirs: str = os.getenv("XIAOMUSIC_EXCLUDE_DIRS", "@eaDir") exclude_dirs: str = os.getenv("XIAOMUSIC_EXCLUDE_DIRS", "@eaDir")
music_path_depth: int = int(os.getenv("XIAOMUSIC_MUSIC_PATH_DEPTH", "10")) music_path_depth: int = int(os.getenv("XIAOMUSIC_MUSIC_PATH_DEPTH", "10"))
disable_httpauth: bool = ( disable_httpauth: bool = (

View File

@ -2,7 +2,7 @@
import os import os
from threading import Thread from threading import Thread
from flask import Flask, request, send_from_directory, send_file from flask import Flask, request, send_file, send_from_directory
from flask_httpauth import HTTPBasicAuth from flask_httpauth import HTTPBasicAuth
from waitress import serve from waitress import serve
@ -162,18 +162,21 @@ def downloadjson():
"content": content, "content": content,
} }
@app.route("/downloadlog", methods=["GET"]) @app.route("/downloadlog", methods=["GET"])
@auth.login_required @auth.login_required
def downloadlog(): def downloadlog():
return send_file(xiaomusic.config.log_file, as_attachment=True) return send_file(xiaomusic.config.log_file, as_attachment=True)
@app.route("/playurl", methods=["GET"]) @app.route("/playurl", methods=["GET"])
@auth.login_required @auth.login_required
async def playurl(): async def playurl():
url = request.args.get('url') url = request.args.get("url")
log.info(f"play_url:{url}") log.info(f"play_url:{url}")
return await xiaomusic.call_main_thread_function(xiaomusic.play_url, arg1=url) return await xiaomusic.call_main_thread_function(xiaomusic.play_url, arg1=url)
def static_path_handler(filename): def static_path_handler(filename):
log.debug(filename) log.debug(filename)
log.debug(static_path) log.debug(static_path)

View File

@ -1,5 +1,6 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import asyncio import asyncio
import copy
import json import json
import logging import logging
import os import os
@ -9,10 +10,9 @@ import re
import time import time
import traceback import traceback
import urllib.parse import urllib.parse
from pathlib import Path
import copy
from logging.handlers import RotatingFileHandler from logging.handlers import RotatingFileHandler
from pathlib import Path
from aiohttp import ClientSession, ClientTimeout from aiohttp import ClientSession, ClientTimeout
from miservice import MiAccount, MiIOService, MiNAService from miservice import MiAccount, MiIOService, MiNAService
@ -117,15 +117,17 @@ class XiaoMusic:
os.makedirs(log_path) os.makedirs(log_path)
if os.path.exists(log_file): if os.path.exists(log_file):
os.remove(log_file) os.remove(log_file)
handler = RotatingFileHandler(self.config.log_file, maxBytes=10*1024*1024, backupCount=1) handler = RotatingFileHandler(
self.config.log_file, maxBytes=10 * 1024 * 1024, backupCount=1
)
self.log = logging.getLogger("xiaomusic") self.log = logging.getLogger("xiaomusic")
self.log.addHandler(handler) self.log.addHandler(handler)
self.log.setLevel(logging.DEBUG if self.config.verbose else logging.INFO) self.log.setLevel(logging.DEBUG if self.config.verbose else logging.INFO)
debug_config = copy.deepcopy(self.config) debug_config = copy.deepcopy(self.config)
debug_config.account = '******' debug_config.account = "******"
debug_config.password = '******' debug_config.password = "******"
debug_config.httpauth_username = '******' debug_config.httpauth_username = "******"
debug_config.httpauth_password = '******' debug_config.httpauth_password = "******"
self.log.info(debug_config) self.log.info(debug_config)
async def poll_latest_ask(self): async def poll_latest_ask(self):
@ -293,7 +295,7 @@ class XiaoMusic:
self.log.debug(f"do_tts. cur_music:{self.cur_music}") self.log.debug(f"do_tts. cur_music:{self.cur_music}")
if self._playing and not self.is_downloading(): if self._playing and not self.is_downloading():
# 继续播放歌曲 # 继续播放歌曲
self.log.info(f"继续播放歌曲") self.log.info("继续播放歌曲")
await self.play() await self.play()
async def do_set_volume(self, value): async def do_set_volume(self, value):
@ -818,7 +820,7 @@ class XiaoMusic:
async def stop(self, **kwargs): async def stop(self, **kwargs):
self._playing = False self._playing = False
if kwargs.get("arg1", "") != "notts": if kwargs.get("arg1", "") != "notts":
await self.do_tts(f"收到关机口令,再见") await self.do_tts("收到关机口令,再见")
if self._next_timer: if self._next_timer:
self._next_timer.cancel() self._next_timer.cancel()
self.log.info("定时器已取消") self.log.info("定时器已取消")