Browse Source

修复收到从微信发出的图片消息保存到本地失败的问题

master
chenzhenkun 1 year ago
parent
commit
90cdff327c
1 changed files with 27 additions and 4 deletions
  1. +27
    -4
      channel/wework/wework_message.py

+ 27
- 4
channel/wework/wework_message.py View File

@@ -8,6 +8,7 @@ import pilk
from bridge.context import ContextType from bridge.context import ContextType
from channel.chat_message import ChatMessage from channel.chat_message import ChatMessage
from common.log import logger from common.log import logger
from ntwork.const import send_type




def get_with_retry(get_func, max_retries=5, delay=5): def get_with_retry(get_func, max_retries=5, delay=5):
@@ -41,15 +42,37 @@ def cdn_download(wework, message, file_name):
data = message["data"] data = message["data"]
aes_key = data["cdn"]["aes_key"] aes_key = data["cdn"]["aes_key"]
file_size = data["cdn"]["size"] file_size = data["cdn"]["size"]
file_type = 2
file_id = data["cdn"]["file_id"]


# 获取当前工作目录,然后与文件名拼接得到保存路径 # 获取当前工作目录,然后与文件名拼接得到保存路径
current_dir = os.getcwd() current_dir = os.getcwd()
save_path = os.path.join(current_dir, "tmp", file_name) save_path = os.path.join(current_dir, "tmp", file_name)


result = wework.c2c_cdn_download(file_id, aes_key, file_size, file_type, save_path)
logger.debug(result)
# 下载保存图片到本地
if "url" in data["cdn"].keys() and "auth_key" in data["cdn"].keys():
url = data["cdn"]["url"]
auth_key = data["cdn"]["auth_key"]
# result = wework.wx_cdn_download(url, auth_key, aes_key, file_size, save_path) # ntwork库本身接口有问题,缺失了aes_key这个参数
"""
下载wx类型的cdn文件,以https开头
"""
data = {
'url': url,
'auth_key': auth_key,
'aes_key': aes_key,
'size': file_size,
'save_path': save_path
}
result = wework._WeWork__send_sync(send_type.MT_WXCDN_DOWNLOAD_MSG, data) # 直接用wx_cdn_download的接口内部实现来调用
elif "file_id" in data["cdn"].keys():
file_type = 2
file_id = data["cdn"]["file_id"]
result = wework.c2c_cdn_download(file_id, aes_key, file_size, file_type, save_path)
else:
logger.error(f"something is wrong, data: {data}")
return

# 输出下载结果
logger.debug(f"result: {result}")




def c2c_download_and_convert(wework, message, file_name): def c2c_download_and_convert(wework, message, file_name):


Loading…
Cancel
Save