diff --git a/music/bot_music.py b/music/bot_music.py index ff6c3ca..50b1961 100644 --- a/music/bot_music.py +++ b/music/bot_music.py @@ -2,6 +2,7 @@ import logging import tomllib import aiohttp +import requests from wcferry import WxMsg, Wcf from robot_cmd.robot_command import Feature, PermissionStatus, GroupBotManager @@ -22,7 +23,9 @@ class BotMusic: self.command_format = config["command-format"] self.LOG.info(f"[点歌台] 组件初始化完成,指令: {self.command}") - async def get_music(self, message: WxMsg): + import requests + + def get_music(self, message: WxMsg): if not self.enable: return @@ -43,15 +46,22 @@ class BotMusic: song_name = content[len(command[0]):].strip() - async with aiohttp.ClientSession() as session: - async with session.get( - f"https://www.hhlqilongzhu.cn/api/dg_wyymusic.php?gm={song_name}&n=1&br=2&type=json") as resp: - data = await resp.json() + try: + response = requests.get( + f"https://www.hhlqilongzhu.cn/api/dg_wyymusic.php?gm={song_name}&n=1&br=2&type=json", + timeout=5 # 设置超时,防止阻塞 + ) + data = response.json() + except requests.RequestException as e: + self.wcf.send_text(f"-----Bot-----\n❌请求出错:{e}", + (message.roomid if message.from_group() else message.sender), message.sender) + return - if data["code"] != 200: + if data.get("code") != 200: self.wcf.send_text(f"-----Bot-----\n❌点歌失败!\n{data}", (message.roomid if message.from_group() else message.sender), message.sender) return + title = data["title"] singer = data["singer"] url = data["link"] @@ -59,5 +69,35 @@ class BotMusic: cover_url = data["cover"] lyric = data["lrc"] - xml = f"""{title}{singer}view30{url}{music_url}{url}{music_url}{cover_url}{lyric}000{cover_url}{bot.wxid}01""" + xml = f""" + {title} + {singer} + view + 3 + 0 + + {url} + {music_url} + {url} + {music_url} + + {cover_url} + + + + + + {lyric} + + 0 + 0 + 0 + + {cover_url} + + {message.sender} + 0 + 1 + """ + self.wcf.send_xml(message.sender, xml, 3) diff --git a/robot.py b/robot.py index 33f865b..927641f 100644 --- a/robot.py +++ b/robot.py @@ -1,6 +1,4 @@ # -*- coding: utf-8 -*- -import asyncio -import json import logging import re import time @@ -326,7 +324,7 @@ class Robot(Job): except Exception as e: self.LOG.error(f"group_member_change error: {e}") try: - asyncio.create_task(self.music.get_music(message=msg)) # ✅ 推荐在事件循环中使用 + self.music.get_music(message=msg) # ✅ 推荐在事件循环中使用 except Exception as e: self.LOG.error(f"get_music error: {e}") @@ -621,6 +619,8 @@ class Robot(Job): except Exception as e: self.LOG.error(f"xiu_ren_pdf_send error:{e}") + + if __name__ == '__main__': heisi_path = generate_pdf_from_images("xiuren/heisi") - print(heisi_path) \ No newline at end of file + print(heisi_path)