feature:抖音分享链接转视频
This commit is contained in:
@@ -9,6 +9,7 @@ from typing import Dict, Any
|
||||
from wcferry import WxMsg, Wcf
|
||||
|
||||
from robot_cmd.robot_command import GroupBotManager, Feature, PermissionStatus
|
||||
import lz4.block as lb
|
||||
|
||||
|
||||
class DouyinParserError(Exception):
|
||||
@@ -33,7 +34,7 @@ class DouyinParser:
|
||||
|
||||
self.enable = config.get("enable", True)
|
||||
self.http_proxy = config.get("http_proxy", None)
|
||||
self.LOG.debug("[抖音] 插件初始化完成,代理设置: %s", self.http_proxy)
|
||||
self.LOG.info("[抖音] 插件初始化完成,代理设置: %s", self.http_proxy)
|
||||
|
||||
def _clean_response_data(self, data: Dict[str, Any]) -> Dict[str, Any]:
|
||||
if not data:
|
||||
@@ -126,33 +127,74 @@ class DouyinParser:
|
||||
author = video_info.get('name', '未知作者')
|
||||
if not video_url:
|
||||
raise DouyinParserError("无法获取视频地址")
|
||||
file_abspath = self.download_video(video_url, "douyin_parser/down_load_dir")
|
||||
self.wcf.send_file(file_abspath, (message.roomid if message.from_group() else message.sender))
|
||||
self.LOG.info("已发送解析结果: 标题[%s] 作者[%s]", title, author)
|
||||
except DouyinParserError as e:
|
||||
self.LOG.error("抖音解析失败: %s", str(e))
|
||||
self.send_xml_video(message, title, author, video_url)
|
||||
except Exception as e:
|
||||
self.LOG.error("抖音解析发生未知错误: %s", str(e))
|
||||
self.LOG.error("[抖音] 解析过程发生未知错误: %s\n%s", str(e), traceback.format_exc())
|
||||
raise DouyinParserError(f"未知错误: {str(e)}")
|
||||
return
|
||||
|
||||
def download_video(self, video_url, save_dir):
|
||||
# 确保 save_dir 是一个目录
|
||||
if os.path.isfile(save_dir):
|
||||
print(f"错误: {save_dir} 是一个文件,不能作为目录使用。")
|
||||
return None
|
||||
def send_xml_video(self, message: WxMsg, title, author, video_url):
|
||||
|
||||
os.makedirs(save_dir, exist_ok=True)
|
||||
save_path = os.path.join(save_dir, "video.mp4")
|
||||
if video_url:
|
||||
video_response = requests.get(video_url, stream=True)
|
||||
if video_response.status_code == 200:
|
||||
with open(save_path, "wb") as file:
|
||||
for chunk in video_response.iter_content(chunk_size=1024):
|
||||
file.write(chunk)
|
||||
abs_path = os.path.abspath(save_path)
|
||||
print(f"视频已下载至: {abs_path}")
|
||||
return abs_path
|
||||
else:
|
||||
print("无法下载视频,HTTP 状态码:", video_response.status_code)
|
||||
else:
|
||||
print("API 响应中没有找到视频 URL")
|
||||
return None
|
||||
xml_message = f"""
|
||||
<?xml version="1.0"?>
|
||||
<msg>
|
||||
<appmsg appid="" sdkver="1">
|
||||
<title>{title} @{author}</title>
|
||||
<des>点击观看无水印视频</des>
|
||||
<action>view</action>
|
||||
<type>5</type>
|
||||
<showtype>0</showtype>
|
||||
<content />
|
||||
<url>{video_url}</url>
|
||||
<dataurl />
|
||||
<lowurl />
|
||||
<lowdataurl />
|
||||
<recorditem />
|
||||
<thumburl></thumburl>
|
||||
<messageaction />
|
||||
<laninfo />
|
||||
<extinfo />
|
||||
<sourceusername />
|
||||
<sourcedisplayname />
|
||||
<commenturl />
|
||||
<appattach>
|
||||
<totallen>0</totallen>
|
||||
<attachid />
|
||||
<emoticonmd5 />
|
||||
<fileext />
|
||||
<aeskey />
|
||||
</appattach>
|
||||
<webviewshared>
|
||||
<publisherId />
|
||||
<publisherReqId>0</publisherReqId>
|
||||
</webviewshared>
|
||||
<weappinfo>
|
||||
<pagepath />
|
||||
<username />
|
||||
<appid />
|
||||
<appservicetype>0</appservicetype>
|
||||
</weappinfo>
|
||||
<websearch />
|
||||
</appmsg>
|
||||
<fromusername>Jyunere</fromusername>
|
||||
<scene>0</scene>
|
||||
<appinfo>
|
||||
<version>1</version>
|
||||
<appname></appname>
|
||||
</appinfo>
|
||||
<commenturl></commenturl>
|
||||
</msg>
|
||||
|
||||
"""
|
||||
# 修改消息数据库里面的消息content 内容
|
||||
text_bytes = xml_message.encode('utf-8')
|
||||
compressed_data = lb.compress(text_bytes, store_size=False).hex()
|
||||
|
||||
data = self.wcf.query_sql('MSG0.db', "SELECT * FROM MSG where type = 49 limit 1")
|
||||
self.wcf.query_sql('MSG0.db',
|
||||
f"""UPDATE MSG SET CompressContent = x'{compressed_data}', BytesExtra=x'', type=49, SubType=3,
|
||||
IsSender=0, TalkerId=2 WHERE MsgSvrID={data[0]['MsgSvrID']}"""
|
||||
)
|
||||
|
||||
result = self.wcf.forward_msg(data[0]["MsgSvrID"], message.roomid)
|
||||
print(f"视频链接:{result}")
|
||||
|
||||
Reference in New Issue
Block a user