feat(python): impl download video

This commit is contained in:
Changhua 2025-03-10 23:03:53 +08:00
parent 423e16be03
commit 45a728e507
2 changed files with 40 additions and 6 deletions

View File

@ -44,8 +44,8 @@ python -m grpc_tools.protoc --python_out=. --proto_path=../../../WeChatFerry/rpc
## 版本更新 ## 版本更新
### v39.4.2.0 ### v39.4.2.1
* 修复下载图片类型错误 * 实现视频下载方法
<details><summary>点击查看更多</summary> <details><summary>点击查看更多</summary>

View File

@ -1,7 +1,7 @@
#! /usr/bin/env python3 #! /usr/bin/env python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
__version__ = "39.4.1.0" __version__ = "39.4.2.1"
import atexit import atexit
import base64 import base64
@ -10,6 +10,7 @@ import logging
import mimetypes import mimetypes
import os import os
import re import re
import shutil
import subprocess import subprocess
import sys import sys
from queue import Queue from queue import Queue
@ -667,9 +668,9 @@ class Wcf():
friends = [] friends = []
for cnt in self.get_contacts(): for cnt in self.get_contacts():
if (cnt["wxid"].endswith("@chatroom") or # 群聊 if (cnt["wxid"].endswith("@chatroom") or # 群聊
cnt["wxid"].startswith("gh_") or # 公众号 cnt["wxid"].startswith("gh_") or # 公众号
cnt["wxid"] in not_friends.keys() # 其他杂号 cnt["wxid"] in not_friends.keys() # 其他杂号
): ):
continue continue
friends.append(cnt) friends.append(cnt)
@ -858,6 +859,39 @@ class Wcf():
self.LOG.error(f"下载超时") self.LOG.error(f"下载超时")
return "" return ""
def download_video(self, id: int, thumb: str, dir: str, timeout: int = 30) -> str:
"""下载视频
Args:
id (int): 消息中 id
thumb (str): 消息中的 thumb即视频的封面图
dir (str): 存放视频的目录目录不存在会出错
timeout (int): 超时时间
Returns:
str: 成功返回存储路径空字符串为失败原因见日志
"""
base, _ = os.path.splitext(thumb)
file_path = base + ".mp4"
file_name = os.path.basename(file_path)
target_path = os.path.join(dir, file_name)
if (not os.path.exists(target_path)) and (not os.path.exists(file_path)) and (self.download_attach(id, thumb, "") != 0):
self.LOG.error(f"下载失败")
return ""
cnt = 0
while cnt < timeout:
if os.path.exists(file_path):
os.makedirs(dir, exist_ok=True)
shutil.move(file_path, target_path)
return target_path
sleep(1)
cnt += 1
self.LOG.error(f"下载超时")
return ""
def add_chatroom_members(self, roomid: str, wxids: str) -> int: def add_chatroom_members(self, roomid: str, wxids: str) -> int:
"""添加群成员 """添加群成员