diff --git a/tests/builtin_plugins/plugin_store/utils.py b/tests/builtin_plugins/plugin_store/utils.py index 4271d15f..44b7330a 100644 --- a/tests/builtin_plugins/plugin_store/utils.py +++ b/tests/builtin_plugins/plugin_store/utils.py @@ -36,9 +36,10 @@ def init_mocked_api(mocked_api: MockRouter) -> None: ).respond(json=get_response_json("zhenxun_github_sub_tree.json")) mocked_api.head( - "https://raw.githubusercontent.com/zhenxun-org/zhenxun_bot_plugins/main/plugins.json", - name="head_basic_plugins", + "https://raw.githubusercontent.com/", + name="head_raw", ).respond(200, text="") + mocked_api.get( "https://raw.githubusercontent.com/zhenxun-org/zhenxun_bot_plugins/main/plugins.json", name="basic_plugins", @@ -47,6 +48,7 @@ def init_mocked_api(mocked_api: MockRouter) -> None: "https://cdn.jsdelivr.net/gh/zhenxun-org/zhenxun_bot_plugins@main/plugins.json", name="basic_plugins_jsdelivr", ).respond(200, json=get_response_json("basic_plugins.json")) + mocked_api.get( "https://raw.githubusercontent.com/zhenxun-org/zhenxun_bot_plugins_index/index/plugins.json", name="extra_plugins", @@ -55,6 +57,7 @@ def init_mocked_api(mocked_api: MockRouter) -> None: "https://cdn.jsdelivr.net/gh/zhenxun-org/zhenxun_bot_plugins_index@index/plugins.json", name="extra_plugins_jsdelivr", ).respond(200, json=get_response_json("extra_plugins.json")) + mocked_api.get( "https://raw.githubusercontent.com/zhenxun-org/zhenxun_bot_plugins/main/plugins/search_image/__init__.py", name="search_image_plugin_file_init", diff --git a/zhenxun/builtin_plugins/plugin_store/models.py b/zhenxun/builtin_plugins/plugin_store/models.py index f69ecfa7..adf24826 100644 --- a/zhenxun/builtin_plugins/plugin_store/models.py +++ b/zhenxun/builtin_plugins/plugin_store/models.py @@ -70,11 +70,7 @@ class RepoInfo(BaseModel): """获取最快下载地址格式""" raw_format = "https://raw.githubusercontent.com/{owner}/{repo}/{branch}/{path}" patterns: dict[str, str] = { - ( - "https://raw.githubusercontent.com" - "/zhenxun-org/zhenxun_bot_plugins/main" - "/plugins.json" - ): raw_format, + "https://raw.githubusercontent.com/": raw_format, "https://ghproxy.cc/": f"https://ghproxy.cc/{raw_format}", "https://mirror.ghproxy.com/": f"https://mirror.ghproxy.com/{raw_format}", "https://gh-proxy.com/": f"https://gh-proxy.com/{raw_format}", diff --git a/zhenxun/utils/http_utils.py b/zhenxun/utils/http_utils.py index 89536b7c..da33264a 100644 --- a/zhenxun/utils/http_utils.py +++ b/zhenxun/utils/http_utils.py @@ -196,18 +196,19 @@ class AsyncHttpx: for _ in range(3): if not stream: try: - content = ( - await cls.get( - url, - params=params, - headers=headers, - cookies=cookies, - use_proxy=use_proxy, - proxy=proxy, - timeout=timeout, - **kwargs, - ) - ).content + response = await cls.get( + url, + params=params, + headers=headers, + cookies=cookies, + use_proxy=use_proxy, + proxy=proxy, + timeout=timeout, + follow_redirects=True, + **kwargs, + ) + response.raise_for_status() + content = response.content async with aiofiles.open(path, "wb") as wf: await wf.write(content) logger.info(f"下载 {url} 成功.. Path:{path.absolute()}") @@ -230,8 +231,10 @@ class AsyncHttpx: headers=headers, cookies=cookies, timeout=timeout, + follow_redirects=True, **kwargs, ) as response: + response.raise_for_status() logger.info( f"开始下载 {path.name}.. Path: {path.absolute()}" ) @@ -347,10 +350,9 @@ class AsyncHttpx: begin_time = time.time() response = await client.head(url=url, timeout=6) - response.raise_for_status() elapsed_time = (time.time() - begin_time) * 1000 - content_length = int(response.headers["content-length"]) + content_length = int(response.headers.get("content-length", 0)) return { "url": url,