🐛 文件下载逻辑修改

This commit is contained in:
AkashiCoin 2024-09-03 17:09:55 +08:00
parent 682d19aa2b
commit ce97dedc5f
3 changed files with 22 additions and 21 deletions

View File

@ -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",

View File

@ -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}",

View File

@ -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,