优化代码

This commit is contained in:
liuwei
2025-06-09 17:48:41 +08:00
parent 0d5140a894
commit df5ccc4ae8
3 changed files with 27 additions and 65 deletions

View File

@@ -9,13 +9,12 @@ from pathlib import Path
from typing import Union
import aiohttp
import pysilk
from loguru import logger
from pydub import AudioSegment
from pymediainfo import MediaInfo
import pysilk
from pydub import AudioSegment
from utils.video_utils import _get_first_frame, _get_first_frame_bytes
from utils.video_utils import get_first_frame, get_first_frame_bytes
from wechat_ipad import UserLoggedOut
from wechat_ipad.client.base import WechatAPIClientBase
@@ -233,7 +232,7 @@ class MessageMixin(WechatAPIClientBase):
media_info = MediaInfo.parse(BytesIO(video))
# 如果没有传入首帧,则自己提取一次
if not has_image:
first_frame = _get_first_frame_bytes(video, f"frame_{int(time.time())}.jpg")
first_frame = get_first_frame_bytes(video, f"frame_{int(time.time())}.jpg")
if first_frame:
image = Path(first_frame)
elif isinstance(video, os.PathLike):
@@ -247,7 +246,7 @@ class MessageMixin(WechatAPIClientBase):
media_info = MediaInfo.parse(video_path)
# 如果没有传入首帧,则自己提取一次
if not has_image:
first_frame = _get_first_frame(video_path, f"frame_{int(time.time())}.jpg")
first_frame = get_first_frame(video_path, f"frame_{int(time.time())}.jpg")
if first_frame:
image = Path(first_frame)
else:
@@ -542,10 +541,17 @@ class MessageMixin(WechatAPIClientBase):
if not self.wxid:
raise UserLoggedOut("请先登录")
async with aiohttp.ClientSession() as session:
json_param = {"Wxid": self.wxid, "ToWxid": wxid, "CardWxid": card_wxid, "CardAlias": card_alias,
"CardNickname": card_nickname}
response = await session.post(f'http://{self.ip}:{self.port}/SendCardMsg', json=json_param)
async with (aiohttp.ClientSession() as session):
json_param = {
"CardAlias": card_alias,
"CardNickName": card_nickname,
"CardWxId": card_wxid,
"ToWxid": wxid,
"Wxid": self.wxid
}
response = await session.post(f'http://{self.ip}:{self.port}/api/Msg/ShareCard', json=json_param)
json_resp = await response.json()
if json_resp.get("Success"):
@@ -577,7 +583,7 @@ class MessageMixin(WechatAPIClientBase):
"""
return await self._queue_message(self._send_app_message, wxid, xml, type)
async def _send_app_message(self, wxid: str, xml: str, type: int) -> tuple[int, int, int]:
async def _send_app_message(self, wxid: str, xml: str, type: int=0) -> tuple[int, int, int]:
if not self.wxid:
raise UserLoggedOut("请先登录")
# {