* 🐛 Fix #166 Aiohttp 访问超时
This commit is contained in:
Xtao_dada 2022-01-20 16:27:20 +08:00 committed by GitHub
parent 4239cfb5e4
commit 621ffca6b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 35 additions and 144 deletions

View File

@ -62,14 +62,10 @@ mtp_secret: ""
# Apt Git source # Apt Git source
git_source: "https://raw.githubusercontent.com/Xtao-Labs/PagerMaid_Plugins/master/" git_source: "https://raw.githubusercontent.com/Xtao-Labs/PagerMaid_Plugins/master/"
git_api: "https://api.github.com/repos/Xtao-Labs/PagerMaid-Modify/commits/master"
git_ssh: "https://github.com/Xtao-Labs/PagerMaid-Modify.git" git_ssh: "https://github.com/Xtao-Labs/PagerMaid-Modify.git"
# Update Notice # Update Notice
update_check: "True" update_check: "True"
update_time: "86400"
update_username: "PagerMaid_Modify_bot"
update_delete: "True"
# ipv6 # ipv6
ipv6: "False" ipv6: "False"

View File

@ -34,7 +34,6 @@ from requests.exceptions import ChunkedEncodingError
from requests.exceptions import ConnectionError as ConnectedError from requests.exceptions import ConnectionError as ConnectedError
from asyncio import CancelledError as CancelError from asyncio import CancelledError as CancelError
from asyncio import TimeoutError as AsyncTimeoutError from asyncio import TimeoutError as AsyncTimeoutError
from aiohttp.client_exceptions import ServerDisconnectedError
from sqlite3 import OperationalError from sqlite3 import OperationalError
from http.client import RemoteDisconnected from http.client import RemoteDisconnected
from urllib.error import URLError from urllib.error import URLError
@ -282,7 +281,7 @@ def before_send(event, hint):
OSError, AuthKeyDuplicatedError, ResponseError, SlowModeWaitError, OSError, AuthKeyDuplicatedError, ResponseError, SlowModeWaitError,
PeerFloodError, MessageEditTimeExpiredError, PeerIdInvalidError, PeerFloodError, MessageEditTimeExpiredError, PeerIdInvalidError,
AuthKeyUnregisteredError, UserBannedInChannelError, AuthKeyError, AuthKeyUnregisteredError, UserBannedInChannelError, AuthKeyError,
CancelError, AsyncTimeoutError, ServerDisconnectedError)): CancelError, AsyncTimeoutError)):
return return
elif exc_info and isinstance(exc_info[1], UserDeactivatedBanError): elif exc_info and isinstance(exc_info[1], UserDeactivatedBanError):
# The user has been deleted/deactivated # The user has been deleted/deactivated

View File

@ -62,14 +62,10 @@ mtp_secret: ""
# Apt Git source # Apt Git source
git_source: "https://raw.githubusercontent.com/Xtao-Labs/PagerMaid_Plugins/master/" git_source: "https://raw.githubusercontent.com/Xtao-Labs/PagerMaid_Plugins/master/"
git_api: "https://api.github.com/repos/Xtao-Labs/PagerMaid-Modify/commits/master"
git_ssh: "https://github.com/Xtao-Labs/PagerMaid-Modify.git" git_ssh: "https://github.com/Xtao-Labs/PagerMaid-Modify.git"
# Update Notice # Update Notice
update_check: "True" update_check: "True"
update_time: "86400"
update_username: "PagerMaid_Modify_bot"
update_delete: "True"
# ipv6 # ipv6
ipv6: "False" ipv6: "False"
@ -91,3 +87,6 @@ start_form: "%m/%d %H:%M"
bot_admins: bot_admins:
- example1 - example1
- example2 - example2
# Silent to reduce editing times
silent: "True"

View File

@ -10,7 +10,7 @@ from telethon.tl.types import ChannelForbidden
from pagermaid import bot, log, config from pagermaid import bot, log, config
from pagermaid.listener import listener from pagermaid.listener import listener
from pagermaid.utils import lang, alias_command, get from pagermaid.utils import lang, alias_command, client
def isfloat(value): def isfloat(value):
@ -247,12 +247,12 @@ async def hitokoto(context):
hitokoto_while = 1 hitokoto_while = 1
hitokoto_json = None hitokoto_json = None
try: try:
hitokoto_json = (await get("https://v1.hitokoto.cn/?charset=utf-8")).json() hitokoto_json = (await client.get("https://v1.hitokoto.cn/?charset=utf-8")).json()
except ValueError: except ValueError:
while hitokoto_while < 10: while hitokoto_while < 10:
hitokoto_while += 1 hitokoto_while += 1
try: try:
hitokoto_json = (await get("https://v1.hitokoto.cn/?charset=utf-8")).json() hitokoto_json = (await client.get("https://v1.hitokoto.cn/?charset=utf-8")).json()
break break
except: except:
continue continue

View File

@ -8,7 +8,7 @@ from shutil import copyfile, move
from glob import glob from glob import glob
from pagermaid import log, working_dir, config from pagermaid import log, working_dir, config
from pagermaid.listener import listener from pagermaid.listener import listener
from pagermaid.utils import upload_attachment, lang, alias_command, get from pagermaid.utils import upload_attachment, lang, alias_command, client
from pagermaid.modules import plugin_list as active_plugins, __list_plugins from pagermaid.modules import plugin_list as active_plugins, __list_plugins
@ -31,7 +31,7 @@ def remove_plugin(name):
async def download(name): async def download(name):
html = await get(f'{git_source}{name}.py') html = await client.get(f'{git_source}{name}.py')
with open(f'plugins/{name}.py', mode='wb') as f: with open(f'plugins/{name}.py', mode='wb') as f:
f.write(html.text.encode('utf-8')) f.write(html.text.encode('utf-8'))
return f'plugins/{name}.py' return f'plugins/{name}.py'
@ -94,7 +94,7 @@ async def plugin(context):
success_list = [] success_list = []
failed_list = [] failed_list = []
noneed_list = [] noneed_list = []
plugin_list = await get(f"{git_source}list.json") plugin_list = await client.get(f"{git_source}list.json")
plugin_list = plugin_list.json()['list'] plugin_list = plugin_list.json()['list']
for i in process_list: for i in process_list:
if exists(f"{plugin_directory}version.json"): if exists(f"{plugin_directory}version.json"):
@ -260,7 +260,7 @@ async def plugin(context):
return return
with open(f"{plugin_directory}version.json", 'r', encoding="utf-8") as f: with open(f"{plugin_directory}version.json", 'r', encoding="utf-8") as f:
version_json = json.load(f) version_json = json.load(f)
plugin_list = await get(f"{git_source}list.json") plugin_list = await client.get(f"{git_source}list.json")
plugin_online = plugin_list.json()['list'] plugin_online = plugin_list.json()['list']
for key, value in version_json.items(): for key, value in version_json.items():
if value == "0.0": if value == "0.0":
@ -304,7 +304,7 @@ async def plugin(context):
elif len(context.parameter) == 2: elif len(context.parameter) == 2:
search_result = [] search_result = []
plugin_name = context.parameter[1] plugin_name = context.parameter[1]
plugin_list = await get(f"{git_source}list.json") plugin_list = await client.get(f"{git_source}list.json")
plugin_online = plugin_list.json()['list'] plugin_online = plugin_list.json()['list']
for i in plugin_online: for i in plugin_online:
if search(plugin_name, i['name'], I): if search(plugin_name, i['name'], I):
@ -321,7 +321,7 @@ async def plugin(context):
elif len(context.parameter) == 2: elif len(context.parameter) == 2:
search_result = '' search_result = ''
plugin_name = context.parameter[1] plugin_name = context.parameter[1]
plugin_list = await get(f"{git_source}list.json") plugin_list = await client.get(f"{git_source}list.json")
plugin_online = plugin_list.json()['list'] plugin_online = plugin_list.json()['list']
for i in plugin_online: for i in plugin_online:
if plugin_name == i['name']: if plugin_name == i['name']:
@ -350,7 +350,7 @@ async def plugin(context):
list_plugin = [] list_plugin = []
with open(f"{plugin_directory}version.json", 'r', encoding="utf-8") as f: with open(f"{plugin_directory}version.json", 'r', encoding="utf-8") as f:
version_json = json.load(f) version_json = json.load(f)
plugin_list = await get(f"{git_source}list.json") plugin_list = await client.get(f"{git_source}list.json")
plugin_online = plugin_list.json()['list'] plugin_online = plugin_list.json()['list']
for key, value in version_json.items(): for key, value in version_json.items():
if value == "0.0": if value == "0.0":

View File

@ -16,9 +16,9 @@ from re import sub, findall
from requests import get from requests import get
from pathlib import Path from pathlib import Path
from pagermaid import log, config, redis_status, start_time, silent from pagermaid import log, config, redis_status, start_time, silent
from pagermaid.utils import execute, upload_attachment from pagermaid.utils import execute, upload_attachment, lang, alias_command
from pagermaid.listener import listener from pagermaid.listener import listener
from pagermaid.utils import lang, alias_command
DCs = { DCs = {
1: "149.154.175.50", 1: "149.154.175.50",

View File

@ -12,7 +12,7 @@ from PIL import Image, ImageOps
from math import floor from math import floor
from pagermaid import bot, redis, redis_status, silent from pagermaid import bot, redis, redis_status, silent
from pagermaid.listener import listener from pagermaid.listener import listener
from pagermaid.utils import lang, alias_command, get from pagermaid.utils import lang, alias_command, client
from pagermaid import log from pagermaid import log
@ -337,12 +337,12 @@ async def single_sticker(animated, context, custom_emoji, emoji, message, pic_ro
command = '/newanimated' command = '/newanimated'
try: try:
response = await get(f'https://t.me/addstickers/{pack_name}') response = await client.get(f'https://t.me/addstickers/{pack_name}')
except UnicodeEncodeError: except UnicodeEncodeError:
pack_name = 's' + hex(context.sender_id)[2:] pack_name = 's' + hex(context.sender_id)[2:]
if animated: if animated:
pack_name = 's' + hex(context.sender_id)[2:] + '_animated' pack_name = 's' + hex(context.sender_id)[2:] + '_animated'
response = await get(f'https://t.me/addstickers/{pack_name}') response = await client.get(f'https://t.me/addstickers/{pack_name}')
if not response.status_code == 200: if not response.status_code == 200:
try: try:
await context.edit(lang('sticker_telegram_server_error')) await context.edit(lang('sticker_telegram_server_error'))
@ -608,7 +608,7 @@ async def sticker_search(context):
await context.edit(lang('google_processing')) await context.edit(lang('google_processing'))
query = context.parameter[0] query = context.parameter[0]
try: try:
html = (await get("https://combot.org/telegram/stickers?q=" + query)).text html = (await client.get("https://combot.org/telegram/stickers?q=" + query)).text
except: except:
return await context.edit(lang('sticker_telegram_server_error')) return await context.edit(lang('sticker_telegram_server_error'))
xml = BeautifulSoup(html, "lxml") xml = BeautifulSoup(html, "lxml")

View File

@ -1,5 +1,5 @@
""" Libraries for python modules. """ """ Libraries for python modules. """
import aiohttp import httpx
import subprocess import subprocess
from importlib.util import find_spec from importlib.util import find_spec
@ -7,13 +7,11 @@ from sys import executable
from os import remove from os import remove
from os.path import exists from os.path import exists
from typing import Any, Optional from typing import Optional
from emoji import get_emoji_regexp from emoji import get_emoji_regexp
from random import choice from random import choice
from json import load as load_json from json import load as load_json
from json import loads as loads_json
from json import dumps as dumps_json
from re import sub, IGNORECASE from re import sub, IGNORECASE
from asyncio import create_subprocess_shell from asyncio import create_subprocess_shell
from asyncio.subprocess import PIPE from asyncio.subprocess import PIPE
@ -25,25 +23,6 @@ from pagermaid import module_dir, bot, lang_dict, alias_dict, user_bot, config,
http_port http_port
class AiohttpResp:
"""
重写返回类型
"""
def __init__(self, text: Any, content: bytes, status_code: int):
"""
Args:
text (Any): 网页内容
content (bytes): 文件内容
status_code (int): 网页状态码
"""
self.text = text
self.content = content
self.status_code = status_code
def json(self):
return loads_json(self.text)
def lang(text: str) -> str: def lang(text: str) -> str:
""" i18n """ """ i18n """
result = lang_dict.get(text, text) result = lang_dict.get(text, text)
@ -250,97 +229,15 @@ async def admin_check(event):
return False return False
async def request(method: str, """ Init httpx client """
url: str,
params: dict = None,
data: Any = None,
json_body: bool = False,
timeout: int = 10,
**kwargs) -> AiohttpResp:
"""
原始网络请求封装
Args:
method (str) : 请求方法
url (str) : 请求 URL
params (dict, optional) : 请求参数
data (Any, optional) : 请求载荷
json_body (bool, optional) : 载荷是否为 JSON
timeout (int, optional) : 超时时间
Returns:
返回 aiohttp 请求对象
"""
method = method.upper()
# 使用自定义 UA # 使用自定义 UA
DEFAULT_HEADERS = { headers = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36" "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36"
} }
headers = DEFAULT_HEADERS
if params is None:
params = {}
# 合并参数
config_ = {
"method": method,
"url": url,
"params": params,
"data": data,
"headers": headers,
}
# 支持自定义参数
config_.update(kwargs)
if json_body:
config_["headers"]["Content-Type"] = "application/json"
config_["data"] = dumps_json(config_["data"])
# 如果用户提供代理则设置代理 # 如果用户提供代理则设置代理
proxies = None
if not proxy_addr == '' and not proxy_port == '': if not proxy_addr == '' and not proxy_port == '':
config_["proxy"] = f"socks5://{proxy_addr}:{proxy_port}" proxies = f"socks5://{proxy_addr}:{proxy_port}"
elif not http_addr == '' and not http_port == '': elif not http_addr == '' and not http_port == '':
config_["proxy"] = f"http://{http_addr}:{http_port}" proxies = f"http://{http_addr}:{http_port}"
session = aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=timeout)) client = httpx.AsyncClient(proxies=proxies, timeout=10.0, headers=headers)
resp = await session.request(**config_)
await session.close()
# 返回请求
try:
resp_data = await resp.text()
except UnicodeDecodeError:
resp_data = await resp.read()
content = await resp.content.read()
status_code = resp.status
return AiohttpResp(resp_data, content, status_code)
async def get(url: str, timeout: int = 10, **kwargs) -> AiohttpResp:
"""
GET 请求封装
Args:
url (str) : 请求 URL
timeout (int, optional) : 超时时间
Returns:
返回 aiohttp 请求对象
:rtype :aiohttp.client_reqrep.ClientResponse
"""
return await request("GET", url, timeout=timeout, **kwargs)
async def post(url: str,
params: dict = None,
data: Any = None,
json_body: bool = False,
timeout: int = 10,
**kwargs) -> AiohttpResp:
"""
POST 请求封装
Args:
url (str) : 请求 URL
params (dict, optional) : 请求参数
data (Any, optional) : 请求载荷
json_body (bool, optional) : 载荷是否为 JSON
timeout (int, optional) : 超时时间
Returns:
返回 aiohttp 请求对象
:rtype :aiohttp.client_reqrep.ClientResponse
"""
return await request("POST", url, params, data, json_body, timeout, **kwargs)

View File

@ -9,7 +9,7 @@ PyYAML>=5.4.1
redis>=3.5.3 redis>=3.5.3
coloredlogs>=15.0.1 coloredlogs>=15.0.1
requests[socks]>=2.25.1 requests[socks]>=2.25.1
aiohttp>=3.8.1 httpx[socks]>=0.21.3
pytz>=2021.1 pytz>=2021.1
cowpy>=1.1.0 cowpy>=1.1.0
googletrans>=4.0.0-rc1 googletrans>=4.0.0-rc1