Pass cookies to the aria2c and requests downloaders

For aria2c I've simplified the operation by offloading most of the work for creating a cookie header by just re-doing what Python-requests does. This results in the exact same cookies Python-requests would have used in a requests.get() call or such. It supports multiple of the same-name cookies under different domains/paths based on the URI of the mock request.
This commit is contained in:
rlaphoenix
2023-05-29 22:23:39 +01:00
parent 1ff4858ca7
commit 6cfbaa7db1
5 changed files with 32 additions and 3 deletions

View File

@@ -2,9 +2,12 @@ import asyncio
import subprocess
import textwrap
from functools import partial
from http.cookiejar import CookieJar
from pathlib import Path
from typing import Optional, Union
from typing import Optional, Union, MutableMapping
import requests
from requests.cookies import RequestsCookieJar, get_cookie_header, cookiejar_from_dict
from rich.text import Text
from devine.core.config import config
@@ -16,6 +19,7 @@ async def aria2c(
uri: Union[str, list[str]],
out: Path,
headers: Optional[dict] = None,
cookies: Optional[Union[MutableMapping[str, str], RequestsCookieJar]] = None,
proxy: Optional[str] = None,
silent: bool = False,
segmented: bool = False,
@@ -73,7 +77,20 @@ async def aria2c(
"-i", "-"
]
if cookies:
# use python-requests pre-existing code to convert a Jar/Dict to a header while
# also supporting multiple cookies of the same name with different domain/paths.
if isinstance(cookies, CookieJar):
cookiejar = cookies
else:
cookiejar = cookiejar_from_dict(cookies)
mock_request = requests.Request(url=uri)
cookie_header = get_cookie_header(cookiejar, mock_request)
arguments.extend(["--header", f"Cookie: {cookie_header}"])
for header, value in (headers or {}).items():
if header.lower() == "cookie":
raise ValueError("You cannot set Cookies as a header manually, please use the `cookies` param.")
if header.lower() == "accept-encoding":
# we cannot set an allowed encoding, or it will return compressed
# and the code is not set up to uncompress the data