Skip to content

Commit

Permalink
整理代码
Browse files Browse the repository at this point in the history
  • Loading branch information
hanxi committed Jun 29, 2024
1 parent dc49f63 commit db1e4e6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
4 changes: 3 additions & 1 deletion xiaomusic/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ class Config:
"XIAOMUSIC_SEARCH", "ytsearch:"
) # "bilisearch:" or "ytsearch:"
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")
music_path_depth: int = int(os.getenv("XIAOMUSIC_MUSIC_PATH_DEPTH", "10"))
disable_httpauth: bool = (
Expand Down
7 changes: 5 additions & 2 deletions xiaomusic/httpserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os
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 waitress import serve

Expand Down Expand Up @@ -162,18 +162,21 @@ def downloadjson():
"content": content,
}


@app.route("/downloadlog", methods=["GET"])
@auth.login_required
def downloadlog():
return send_file(xiaomusic.config.log_file, as_attachment=True)


@app.route("/playurl", methods=["GET"])
@auth.login_required
async def playurl():
url = request.args.get('url')
url = request.args.get("url")
log.info(f"play_url:{url}")
return await xiaomusic.call_main_thread_function(xiaomusic.play_url, arg1=url)


def static_path_handler(filename):
log.debug(filename)
log.debug(static_path)
Expand Down
24 changes: 13 additions & 11 deletions xiaomusic/xiaomusic.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python3
import asyncio
import copy
import json
import logging
import os
Expand All @@ -9,10 +10,9 @@
import time
import traceback
import urllib.parse
from logging.handlers import RotatingFileHandler
from pathlib import Path
import copy

from logging.handlers import RotatingFileHandler
from aiohttp import ClientSession, ClientTimeout
from miservice import MiAccount, MiIOService, MiNAService

Expand Down Expand Up @@ -117,15 +117,17 @@ def setup_logger(self):
os.makedirs(log_path)
if os.path.exists(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.addHandler(handler)
self.log.setLevel(logging.DEBUG if self.config.verbose else logging.INFO)
debug_config = copy.deepcopy(self.config)
debug_config.account = '******'
debug_config.password = '******'
debug_config.httpauth_username = '******'
debug_config.httpauth_password = '******'
debug_config.account = "******"
debug_config.password = "******"
debug_config.httpauth_username = "******"
debug_config.httpauth_password = "******"
self.log.info(debug_config)

async def poll_latest_ask(self):
Expand Down Expand Up @@ -288,12 +290,12 @@ async def do_tts(self, value):
except Exception as e:
self.log.error(f"Execption {e}")
# 最大等8秒
sec = min(8, int(len(value)/3.3))
sec = min(8, int(len(value) / 3.3))
await asyncio.sleep(sec)
self.log.debug(f"do_tts. cur_music:{self.cur_music}")
if self._playing and not self.is_downloading():
# 继续播放歌曲
self.log.info(f"继续播放歌曲")
self.log.info("继续播放歌曲")
await self.play()

async def do_set_volume(self, value):
Expand Down Expand Up @@ -712,7 +714,7 @@ async def _playmusic(self, name):
sec, url = await self.get_music_sec_url(name)
self.log.info(f"播放 {url}")
await self.force_stop_xiaoai()
await self.play_url(arg1 = url)
await self.play_url(arg1=url)
self.log.info("已经开始播放了")
# 设置下一首歌曲的播放定时器
await self.set_next_music_timeout(sec)
Expand Down Expand Up @@ -818,7 +820,7 @@ async def play_music_list(self, **kwargs):
async def stop(self, **kwargs):
self._playing = False
if kwargs.get("arg1", "") != "notts":
await self.do_tts(f"收到关机口令,再见")
await self.do_tts("收到关机口令,再见")
if self._next_timer:
self._next_timer.cancel()
self.log.info("定时器已取消")
Expand Down

0 comments on commit db1e4e6

Please sign in to comment.