From fd7a35a292205b14cad7cc8371ccc8001273ff66 Mon Sep 17 00:00:00 2001 From: liuwei Date: Tue, 11 Mar 2025 13:30:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8E=BB=E9=99=A4=E8=A7=86=E9=A2=91=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD=E5=8A=9F=E8=83=BD=EF=BC=8C=E6=8A=96=E9=9F=B3=E5=81=9A?= =?UTF-8?q?=E4=BA=86=E9=98=B2=E7=88=AC=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- douyin_parser/main.py | 50 ------------------------------------------- 1 file changed, 50 deletions(-) diff --git a/douyin_parser/main.py b/douyin_parser/main.py index 4220b74..bac2e1b 100644 --- a/douyin_parser/main.py +++ b/douyin_parser/main.py @@ -2,7 +2,6 @@ import logging import re import time import tomllib -import os import traceback import requests from typing import Dict, Any @@ -10,7 +9,6 @@ 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): @@ -181,55 +179,7 @@ class DouyinParser: self.wcf.send_rich_text("bot", "gh_11", title[:30], f"{title[:30]} - {author[:10]}", video_url, cover, message.roomid) - self.LOG.info(f"video_url:{video_url}, title:{title}, author:{author}, cover:{cover}") - path = self._download_stream(video_url, "down_load_dir/video.mp4") - self.LOG.info(f"video_abs_url:{path}") - self.wcf.send_file(path, (message.roomid if message.from_group() else message.sender)) except Exception as e: self.LOG.error("[抖音] 解析过程发生未知错误: %s\n%s", str(e), traceback.format_exc()) raise DouyinParserError(f"未知错误: {str(e)}") return - - def _download_stream(self, url, save_path): - """ - 从指定URL读取视频流并保存到本地 - :param url: 视频流的URL - :param save_path: 本地保存路径(包含文件名,例如 "video.mp4") - """ - try: - # 发送GET请求,启用流式传输 - headers = { - "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36", - "Referer": "https://www.douyin.com/", - "Accept": "*/*", - "Connection": "keep-alive", - } - response = requests.get(url, headers=headers) - - # 检查请求是否成功 - response.raise_for_status() # 如果状态码不是200,将抛出异常 - - # 确保保存路径的目录存在 - os.makedirs(os.path.dirname(save_path) or ".", exist_ok=True) - - # 检查是否是视频流(可选,根据Content-Type判断) - content_type = response.headers.get("Content-Type", "").lower() - self.LOG.info(response.text) - if "video" not in content_type and "application/octet-stream" not in content_type: - self.LOG.info(f"警告: 返回的可能不是视频流,Content-Type: {content_type}") - self.LOG.info("响应内容预览:", response.text[:100]) # 打印前100字符查看 - return - - # 以二进制写入模式保存流数据 - with open(save_path, "wb") as file: - for chunk in response.iter_content(chunk_size=1024): # 分块读取,每块1KB - if chunk: # 过滤空块 - file.write(chunk) - self.LOG.info(f"视频已下载到: {save_path}") - return os.path.abspath(save_path) - except requests.RequestException as e: - self.LOG.info(f"请求失败: {e}") - except IOError as e: - self.LOG.info(f"文件写入失败: {e}") - except Exception as e: - self.LOG.info(f"发生未知错误: {e}")