mirror of
https://github.com/zhayujie/chatgpt-on-wechat.git
synced 2026-07-20 21:57:14 +08:00
feat: add clear_quota_v2 method to clear API quota when it's used up
This commit is contained in:
@@ -92,7 +92,6 @@ class WechatMPChannel(ChatChannel):
|
|||||||
elif reply.type == ReplyType.IMAGE_URL: # 从网络下载图片
|
elif reply.type == ReplyType.IMAGE_URL: # 从网络下载图片
|
||||||
img_url = reply.content
|
img_url = reply.content
|
||||||
pic_res = requests.get(img_url, stream=True)
|
pic_res = requests.get(img_url, stream=True)
|
||||||
print(pic_res.headers)
|
|
||||||
image_storage = io.BytesIO()
|
image_storage = io.BytesIO()
|
||||||
for block in pic_res.iter_content(1024):
|
for block in pic_res.iter_content(1024):
|
||||||
image_storage.write(block)
|
image_storage.write(block)
|
||||||
@@ -159,7 +158,6 @@ class WechatMPChannel(ChatChannel):
|
|||||||
elif reply.type == ReplyType.IMAGE_URL: # 从网络下载图片
|
elif reply.type == ReplyType.IMAGE_URL: # 从网络下载图片
|
||||||
img_url = reply.content
|
img_url = reply.content
|
||||||
pic_res = requests.get(img_url, stream=True)
|
pic_res = requests.get(img_url, stream=True)
|
||||||
print(pic_res.headers)
|
|
||||||
image_storage = io.BytesIO()
|
image_storage = io.BytesIO()
|
||||||
for block in pic_res.iter_content(1024):
|
for block in pic_res.iter_content(1024):
|
||||||
image_storage.write(block)
|
image_storage.write(block)
|
||||||
|
|||||||
@@ -1,11 +1,9 @@
|
|||||||
import time
|
import time
|
||||||
import json
|
|
||||||
import requests
|
|
||||||
import threading
|
import threading
|
||||||
from channel.wechatmp.common import *
|
from channel.wechatmp.common import *
|
||||||
from wechatpy.client import WeChatClient
|
from wechatpy.client import WeChatClient
|
||||||
from common.log import logger
|
from common.log import logger
|
||||||
from config import conf
|
from wechatpy.exceptions import APILimitedException
|
||||||
|
|
||||||
|
|
||||||
class WechatMPClient(WeChatClient):
|
class WechatMPClient(WeChatClient):
|
||||||
@@ -16,13 +14,13 @@ class WechatMPClient(WeChatClient):
|
|||||||
)
|
)
|
||||||
self.fetch_access_token_lock = threading.Lock()
|
self.fetch_access_token_lock = threading.Lock()
|
||||||
|
|
||||||
def fetch_access_token(self):
|
def clear_quota(self):
|
||||||
"""
|
return self.post("clear_quota", data={"appid": self.appid})
|
||||||
获取 access token
|
|
||||||
详情请参考 http://mp.weixin.qq.com/wiki/index.php?title=通用接口文档
|
|
||||||
|
|
||||||
:return: 返回的 JSON 数据包
|
def clear_quota_v2(self):
|
||||||
"""
|
return self.post("clear_quota/v2", params={"appid": self.appid, "appsecret": self.secret})
|
||||||
|
|
||||||
|
def fetch_access_token(self): # 重载父类方法,加锁避免多线程重复获取access_token
|
||||||
with self.fetch_access_token_lock:
|
with self.fetch_access_token_lock:
|
||||||
access_token = self.session.get(self.access_token_key)
|
access_token = self.session.get(self.access_token_key)
|
||||||
if access_token:
|
if access_token:
|
||||||
@@ -33,3 +31,11 @@ class WechatMPClient(WeChatClient):
|
|||||||
return access_token
|
return access_token
|
||||||
return super().fetch_access_token()
|
return super().fetch_access_token()
|
||||||
|
|
||||||
|
def _request(self, method, url_or_endpoint, **kwargs): # 重载父类方法,遇到API限流时,清除quota后重试
|
||||||
|
try:
|
||||||
|
return super()._request(method, url_or_endpoint, **kwargs)
|
||||||
|
except APILimitedException as e:
|
||||||
|
logger.error("[wechatmp] API quata has been used up. {}".format(e))
|
||||||
|
response = self.clear_quota_v2()
|
||||||
|
logger.debug("[wechatmp] API quata has been cleard, {}".format(response))
|
||||||
|
return super()._request(method, url_or_endpoint, **kwargs)
|
||||||
@@ -18,6 +18,7 @@ pysilk_mod>=1.6.0 # needed by send voice
|
|||||||
|
|
||||||
# wechatmp
|
# wechatmp
|
||||||
web.py
|
web.py
|
||||||
|
wechatpy
|
||||||
|
|
||||||
# chatgpt-tool-hub plugin
|
# chatgpt-tool-hub plugin
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user