fix: typo and pass correct headers to aira2
Some checks failed
Deploy Jekyll with GitHub Pages dependencies preinstalled / build (push) Has been cancelled
Deploy Jekyll with GitHub Pages dependencies preinstalled / deploy (push) Has been cancelled

This commit is contained in:
Anonymous 2024-06-13 22:07:11 +08:00
parent f744284c82
commit 1bc250d33c
3 changed files with 14 additions and 5 deletions

3
.gitignore vendored
View File

@ -4,4 +4,5 @@ data/*
caddy/caddy_data
.idea
.env
node_modules
node_modules
**/__pycache__

View File

@ -1,6 +1,7 @@
import json
import logging
import uuid
from typing import Optional
import httpx
@ -33,18 +34,20 @@ async def send_request(method, params=None):
raise e
async def add_download(url, save_dir="/app/cache", out_file=None):
logger.info(f"[Aria2] add_download {url=} {save_dir=} {out_file=}")
async def add_download(
url, save_dir="/app/cache", out_file=None, headers: Optional[dict] = None
):
logger.info(f"[Aria2] add_download {url=} {save_dir=} {out_file=} {headers=}")
method = "aria2.addUri"
options = {
"dir": save_dir,
"header": [],
"out": out_file,
}
if out_file:
options["out"] = out_file
if headers:
options["header"] = [f"{k}: {v}" for k, v in headers.items()]
params = [[url], options]
response = await send_request(method, params)

View File

@ -100,6 +100,11 @@ async def try_file_based_cache(
processed_url,
save_dir=cache_file_dir,
out_file=os.path.basename(cache_file),
headers={
key: value
for key, value in request.headers.items()
if key in ["user-agent", "accept", "authorization"]
},
)
except Exception as e:
logger.error(f"Download error, return 500 for {target_url}", exc_info=e)