Skip to content

Commit

Permalink
fix: gewechat image download (hanfangyuan4396#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
AAlexDing authored Nov 17, 2024
1 parent dee7829 commit 51d1d20
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
2 changes: 1 addition & 1 deletion channel/gewechat/gewechat_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def __init__(self):
self.base_url, self.download_url, self.token, self.app_id
)
)
self.client = GewechatClient(self.base_url, self.download_url, self.token)
self.client = GewechatClient(self.base_url, self.token)

def startup(self):
urls = ("/v2/api/callback/collect", "channel.gewechat.gewechat_channel.Query")
Expand Down
27 changes: 23 additions & 4 deletions channel/gewechat/gewechat_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from common.tmp_dir import TmpDir
from config import conf
from lib.gewechat import GewechatClient
import requests

class GeWeChatMessage(ChatMessage):
def __init__(self, msg, client: GewechatClient):
Expand Down Expand Up @@ -79,17 +80,35 @@ def __init__(self, msg, client: GewechatClient):

def download_voice(self):
try:
voice_data = self.client.download_file(self.msg['Wxid'], self.msg_id)
voice_data = self.client.download_voice(self.msg['Wxid'], self.msg_id)
with open(self.content, "wb") as f:
f.write(voice_data)
except Exception as e:
logger.error(f"[gewechat] Failed to download voice file: {e}")

def download_image(self):
try:
image_data = self.client.download_file(self.msg['Wxid'], self.msg_id)
with open(self.content, "wb") as f:
f.write(image_data)
try:
# 尝试下载高清图片
image_info = self.client.download_image(app_id=self.app_id, xml=self.msg['Data']['Content']['string'], type=1)
except Exception as e:
logger.warning(f"[gewechat] Failed to download high-quality image: {e}")
# 尝试下载普通图片
image_info = self.client.download_image(app_id=self.app_id, xml=self.msg['Data']['Content']['string'], type=2)
if image_info['ret'] == 200 and image_info['data']:
file_url = image_info['data']['fileUrl']
logger.info(f"[gewechat] Download image file from {file_url}")
download_url = conf().get("gewechat_download_url").rstrip('/')
full_url = download_url + '/' + file_url
try:
file_data = requests.get(full_url).content
except Exception as e:
logger.error(f"[gewechat] Failed to download image file: {e}")
return
with open(self.content, "wb") as f:
f.write(file_data)
else:
logger.error(f"[gewechat] Failed to download image file: {image_info}")
except Exception as e:
logger.error(f"[gewechat] Failed to download image file: {e}")

Expand Down
5 changes: 2 additions & 3 deletions lib/gewechat/client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from .api.contact_api import ContactApi
from .api.download_api import DownloadApi
from .api.download_api import DownloadApi
from .api.favor_api import FavorApi
from .api.group_api import GroupApi
from .api.label_api import LabelApi
Expand Down Expand Up @@ -30,9 +29,9 @@ class GewechatClient:
注意: 在使用任何方法之前,请确保你已经正确初始化了客户端,并且有有效的 base_url 和 token。
"""
def __init__(self, base_url, download_url, token):
def __init__(self, base_url, token):
self._contact_api = ContactApi(base_url, token)
self._download_api = DownloadApi(download_url, token)
self._download_api = DownloadApi(base_url, token)
self._favor_api = FavorApi(base_url, token)
self._group_api = GroupApi(base_url, token)
self._label_api = LabelApi(base_url, token)
Expand Down

0 comments on commit 51d1d20

Please sign in to comment.