mirror of
https://github.com/hanxi/xiaomusic.git
synced 2025-12-06 14:52:50 +08:00
style: 清理代码
This commit is contained in:
@@ -1,9 +1,13 @@
|
||||
import requests
|
||||
target="HTTP://192.168.1.10:58091/items/"
|
||||
def httppost(data,url=target):
|
||||
|
||||
target = "HTTP://192.168.1.10:58091/items/"
|
||||
|
||||
|
||||
def httppost(data, url=target):
|
||||
global log
|
||||
# 发起请求,
|
||||
with requests.post(url,json=data, timeout=5) as response: # 增加超时以避免长时间挂起
|
||||
with requests.post(
|
||||
url, json=data, timeout=5
|
||||
) as response: # 增加超时以避免长时间挂起
|
||||
response.raise_for_status() # 如果响应不是200,引发HTTPError异常
|
||||
log.info(f"httppost url:{url} data :{data} response:{response.text}")
|
||||
|
||||
|
||||
@@ -9,7 +9,9 @@ import urllib.parse
|
||||
from contextlib import asynccontextmanager
|
||||
from dataclasses import asdict
|
||||
from typing import TYPE_CHECKING, Annotated
|
||||
|
||||
import socketio
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from xiaomusic.xiaomusic import XiaoMusic
|
||||
|
||||
@@ -55,13 +57,17 @@ from xiaomusic.utils import (
|
||||
xiaomusic: "XiaoMusic" = None
|
||||
config = None
|
||||
log = None
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
# 3thplay指令
|
||||
class Item(BaseModel):
|
||||
action:str
|
||||
args:str
|
||||
action: str
|
||||
args: str
|
||||
|
||||
|
||||
# 在线用户
|
||||
onlines= set()
|
||||
onlines = set()
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def app_lifespan(app):
|
||||
@@ -112,40 +118,45 @@ app = FastAPI(
|
||||
|
||||
# 创建Socket.IO实例
|
||||
sio = socketio.AsyncServer(
|
||||
async_mode='asgi',
|
||||
cors_allowed_origins='*' # 允许所有跨域请求,生产环境应限制
|
||||
async_mode="asgi",
|
||||
cors_allowed_origins="*", # 允许所有跨域请求,生产环境应限制
|
||||
)
|
||||
# 将Socket.IO挂载到FastAPI应用
|
||||
socketio_app = socketio.ASGIApp(
|
||||
socketio_server=sio,
|
||||
other_asgi_app=app,
|
||||
socketio_path='/socket.io'
|
||||
socketio_server=sio, other_asgi_app=app, socketio_path="/socket.io"
|
||||
)
|
||||
|
||||
|
||||
# Socket.IO事件处理
|
||||
@sio.event
|
||||
async def connect(sid, environ, auth):
|
||||
global onlines
|
||||
print(f'客户端连接: {sid}')
|
||||
print(f"客户端连接: {sid}")
|
||||
onlines.update([sid])
|
||||
await sio.emit('message', {'data': '欢迎连接'}, room=sid)
|
||||
await sio.emit("message", {"data": "欢迎连接"}, room=sid)
|
||||
|
||||
|
||||
@sio.event
|
||||
async def disconnect(sid):
|
||||
print(f'客户端断开: {sid}')
|
||||
print(f"客户端断开: {sid}")
|
||||
onlines.discard(sid)
|
||||
|
||||
|
||||
@sio.on("message")
|
||||
async def custom_event(sid, data):
|
||||
log.info(f'收到来自 {sid} 的数据: {data}')
|
||||
await sio.emit('response', {"action":'切歌','status':data})
|
||||
log.info(f"收到来自 {sid} 的数据: {data}")
|
||||
await sio.emit("response", {"action": "切歌", "status": data})
|
||||
|
||||
|
||||
@app.post("/items/")
|
||||
async def create_item(item: Item):
|
||||
result = {**item.dict()}
|
||||
await sio.emit('response',{"action":item.action,"args":item.args,"status":item.args},)
|
||||
await sio.emit(
|
||||
"response",
|
||||
{"action": item.action, "args": item.args, "status": item.args},
|
||||
)
|
||||
return onlines
|
||||
|
||||
|
||||
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["*"], # 允许访问的源
|
||||
@@ -177,11 +188,11 @@ class AuthStaticFiles(StaticFiles):
|
||||
|
||||
|
||||
def HttpInit(_xiaomusic):
|
||||
global xiaomusic, config, log,onlines
|
||||
global xiaomusic, config, log, onlines
|
||||
xiaomusic = _xiaomusic
|
||||
config = xiaomusic.config
|
||||
log = xiaomusic.log
|
||||
onlines=set()
|
||||
onlines = set()
|
||||
folder = os.path.dirname(__file__)
|
||||
app.mount("/static", AuthStaticFiles(directory=f"{folder}/static"), name="static")
|
||||
reset_http_server()
|
||||
|
||||
@@ -234,19 +234,24 @@ def traverse_music_directory(directory, depth, exclude_dirs, support_extension):
|
||||
_append_files_result(result, root, root, files, support_extension)
|
||||
return result
|
||||
|
||||
|
||||
# 发送给网页3thplay,用于三者设备播放
|
||||
async def thdplay(action,args="/static/3thdplay.mp3",target="HTTP://192.168.1.10:58091/items/"):
|
||||
# 接口地址 target,在参数文件指定
|
||||
data={"action":action,"args":args}
|
||||
async def thdplay(
|
||||
action, args="/static/3thdplay.mp3", target="HTTP://192.168.1.10:58091/items/"
|
||||
):
|
||||
# 接口地址 target,在参数文件指定
|
||||
data = {"action": action, "args": args}
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.post(
|
||||
target,json=data, timeout=5
|
||||
target, json=data, timeout=5
|
||||
) as response: # 增加超时以避免长时间挂起
|
||||
# 如果响应不是200,引发异常
|
||||
response.raise_for_status()
|
||||
# 读取响应文本
|
||||
text = await response.text()
|
||||
return '[]' not in text
|
||||
return "[]" not in text
|
||||
|
||||
|
||||
async def downloadfile(url):
|
||||
# 清理和验证URL
|
||||
# 解析URL
|
||||
|
||||
@@ -56,9 +56,9 @@ from xiaomusic.utils import (
|
||||
parse_str_to_dict,
|
||||
save_picture_by_base64,
|
||||
set_music_tag_to_file,
|
||||
thdplay,
|
||||
traverse_music_directory,
|
||||
try_add_access_control_param,
|
||||
thdplay,
|
||||
)
|
||||
|
||||
|
||||
@@ -133,9 +133,9 @@ class XiaoMusic:
|
||||
self.public_port = self.config.public_port
|
||||
if self.public_port == 0:
|
||||
self.public_port = self.port
|
||||
#自动3thplay生成播放 post url
|
||||
self.thdtarget= f"{self.hostname}:{self.public_port}/items/" # "HTTP://192.168.1.10:58091/items/"
|
||||
|
||||
# 自动3thplay生成播放 post url
|
||||
self.thdtarget = f"{self.hostname}:{self.public_port}/items/" # "HTTP://192.168.1.10:58091/items/"
|
||||
|
||||
self.active_cmd = self.config.active_cmd.split(",")
|
||||
self.exclude_dirs = set(self.config.exclude_dirs.split(","))
|
||||
self.music_path_depth = self.config.music_path_depth
|
||||
@@ -1292,33 +1292,34 @@ class XiaoMusic:
|
||||
# 获取音量
|
||||
async def get_volume(self, did="", **kwargs):
|
||||
return await self.devices[did].get_volume()
|
||||
|
||||
# 3thdplay.html 的音量设置消息发送 需要配置文件加入自定义指令
|
||||
# "user_key_word_dict": {
|
||||
#"音量": "set_myvolume",
|
||||
#"继续": "stop",
|
||||
#"大点音": "exec#setmyvolume(\"up\")",
|
||||
#"小点音": "exec#setmyvolume(\"down\")",
|
||||
|
||||
# "音量": "set_myvolume",
|
||||
# "继续": "stop",
|
||||
# "大点音": "exec#setmyvolume(\"up\")",
|
||||
# "小点音": "exec#setmyvolume(\"down\")",
|
||||
|
||||
async def set_myvolume(self, did="", arg1=0, **kwargs):
|
||||
if did not in self.devices:
|
||||
self.log.info(f"设备 did:{did} 不存在, 不能设置音量")
|
||||
return
|
||||
if arg1=="up":
|
||||
await thdplay('up','',self.thdtarget)
|
||||
|
||||
elif arg1=="down":
|
||||
await thdplay('down','',self.thdtarget)
|
||||
if arg1 == "up":
|
||||
await thdplay("up", "", self.thdtarget)
|
||||
|
||||
elif arg1 == "down":
|
||||
await thdplay("down", "", self.thdtarget)
|
||||
else:
|
||||
volume = chinese_to_number(arg1)
|
||||
await thdplay('volume',str(volume),self.thdtarget)
|
||||
|
||||
volume = chinese_to_number(arg1)
|
||||
await thdplay("volume", str(volume), self.thdtarget)
|
||||
|
||||
# 设置音量
|
||||
async def set_volume(self, did="", arg1=0, **kwargs):
|
||||
if did not in self.devices:
|
||||
self.log.info(f"设备 did:{did} 不存在, 不能设置音量")
|
||||
return
|
||||
volume = int(arg1)
|
||||
await thdplay('volume',str(volume),self.thdtarget)
|
||||
await thdplay("volume", str(volume), self.thdtarget)
|
||||
return await self.devices[did].set_volume(volume)
|
||||
|
||||
# 搜索音乐
|
||||
@@ -1688,21 +1689,20 @@ class XiaoMusicDevice:
|
||||
await self.group_force_stop_xiaoai()
|
||||
self.log.info(f"播放 {url}")
|
||||
# 有3方设备打开 /static/3thplay.html 通过socketio连接返回true 忽律小爱音箱的播放
|
||||
online=await thdplay('play',url,self.xiaomusic.thdtarget)
|
||||
online = await thdplay("play", url, self.xiaomusic.thdtarget)
|
||||
if not online:
|
||||
|
||||
results = await self.group_player_play(url, name)
|
||||
if all(ele is None for ele in results):
|
||||
self.log.info(f"播放 {name} 失败. 失败次数: {self._play_failed_cnt}")
|
||||
await asyncio.sleep(1)
|
||||
if (
|
||||
self.isplaying()
|
||||
and self._last_cmd != "stop"
|
||||
and self._play_failed_cnt < 10
|
||||
):
|
||||
self._play_failed_cnt = self._play_failed_cnt + 1
|
||||
await self._play_next()
|
||||
return
|
||||
results = await self.group_player_play(url, name)
|
||||
if all(ele is None for ele in results):
|
||||
self.log.info(f"播放 {name} 失败. 失败次数: {self._play_failed_cnt}")
|
||||
await asyncio.sleep(1)
|
||||
if (
|
||||
self.isplaying()
|
||||
and self._last_cmd != "stop"
|
||||
and self._play_failed_cnt < 10
|
||||
):
|
||||
self._play_failed_cnt = self._play_failed_cnt + 1
|
||||
await self._play_next()
|
||||
return
|
||||
# 重置播放失败次数
|
||||
self._play_failed_cnt = 0
|
||||
|
||||
@@ -2084,9 +2084,8 @@ class XiaoMusicDevice:
|
||||
await self.do_tts(self.config.stop_tts_msg)
|
||||
await asyncio.sleep(3) # 等它说完
|
||||
# 取消组内所有的下一首歌曲的定时器
|
||||
if await thdplay('stop','',self.xiaomusic.thdtarget):
|
||||
|
||||
return
|
||||
if await thdplay("stop", "", self.xiaomusic.thdtarget):
|
||||
return
|
||||
self.cancel_group_next_timer()
|
||||
await self.group_force_stop_xiaoai()
|
||||
self.log.info("stop now")
|
||||
|
||||
Reference in New Issue
Block a user