diff options
author | 0w0 <[email protected]> | 2022-09-21 12:04:35 +0800 |
---|---|---|
committer | GitHub <[email protected]> | 2022-09-21 12:04:35 +0800 |
commit | 1f458f9b279368354ccad224100abd59566f4ef3 (patch) | |
tree | 712f3ac05765c0e3d8cbd6e292602dc86a132dce | |
parent | 87bb8add6cbfa0797f8b3eba66c643a8045db154 (diff) | |
parent | bd68ad78c8879143e37523d679cb643578db13c5 (diff) | |
download | ATRI-1f458f9b279368354ccad224100abd59566f4ef3.tar.gz ATRI-1f458f9b279368354ccad224100abd59566f4ef3.tar.bz2 ATRI-1f458f9b279368354ccad224100abd59566f4ef3.zip |
🔀 Merge pull request #61 from yangrq1018/main
Request Fix
-rw-r--r-- | ATRI/config.py | 1 | ||||
-rw-r--r-- | ATRI/plugins/anime_search.py | 7 | ||||
-rw-r--r-- | ATRI/utils/request.py | 7 | ||||
-rw-r--r-- | config.yml | 1 |
4 files changed, 11 insertions, 5 deletions
diff --git a/ATRI/config.py b/ATRI/config.py index be37af3..22969d3 100644 --- a/ATRI/config.py +++ b/ATRI/config.py @@ -28,6 +28,7 @@ class BotSelfConfig: seconds=config.get("session_expire_timeout", 60) ) proxy: str = config.get("proxy", None) + request_timeout = config.get("request_timeout", None) class InlineGoCQHTTP: diff --git a/ATRI/plugins/anime_search.py b/ATRI/plugins/anime_search.py index 670c766..bf06ece 100644 --- a/ATRI/plugins/anime_search.py +++ b/ATRI/plugins/anime_search.py @@ -9,7 +9,7 @@ from ATRI.utils import request, Translate from ATRI.exceptions import RequestError -URL = "https://api.trace.moe/search?anilistInfo=true&url=" +URL = "https://api.trace.moe/search?anilistInfo=true" _anime_flmt_notice = choice(["慢...慢一..点❤", "冷静1下", "歇会歇会~~"]) @@ -21,9 +21,10 @@ class Anime(Service): @staticmethod async def _request(url: str) -> dict: - aim = URL + url try: - res = await request.get(aim) + resp = await request.get(url) + image_bytes = resp.read() + res = await request.post(URL, data=image_bytes, headers={"Content-Type": "image/jpeg"}) except Exception: raise RequestError("Request failed!") result = res.json() diff --git a/ATRI/utils/request.py b/ATRI/utils/request.py index 04d7093..d8f7be5 100644 --- a/ATRI/utils/request.py +++ b/ATRI/utils/request.py @@ -3,6 +3,9 @@ import httpx from ATRI.config import BotSelfConfig from ATRI.log import logger as log +timeout = BotSelfConfig.request_timeout +if timeout: + timeout = httpx.Timeout(timeout) if not BotSelfConfig.proxy: proxy = dict() @@ -12,11 +15,11 @@ else: async def get(url: str, **kwargs): log.debug(f"GET {url} by {proxy if proxy else 'No proxy'} | MORE: \n {kwargs}") - async with httpx.AsyncClient(proxies=proxy) as client: # type: ignore + async with httpx.AsyncClient(proxies=proxy, timeout=timeout) as client: # type: ignore return await client.get(url, **kwargs) async def post(url: str, **kwargs): log.debug(f"POST {url} by {proxy if proxy else 'No proxy'} | MORE: \n {kwargs}") - async with httpx.AsyncClient(proxies=proxy) as client: # type: ignore + async with httpx.AsyncClient(proxies=proxy, timeout=timeout) as client: # type: ignore return await client.post(url, **kwargs) @@ -8,6 +8,7 @@ BotSelfConfig: command_sep: ["."] session_expire_timeout: 60 proxy: "" # 请参考文档 + request_timeout: 5 InlineGoCQHTTP: enabled: true |