Consolidate typing of Requests/MozillaCookieJar typing to CookieJar

This commit is contained in:
rlaphoenix 2024-02-16 21:02:06 +00:00
parent 9e0515609f
commit 81dca063fa
5 changed files with 18 additions and 19 deletions

View File

@ -13,7 +13,7 @@ from concurrent import futures
from concurrent.futures import ThreadPoolExecutor from concurrent.futures import ThreadPoolExecutor
from copy import deepcopy from copy import deepcopy
from functools import partial from functools import partial
from http.cookiejar import MozillaCookieJar from http.cookiejar import CookieJar, MozillaCookieJar
from itertools import zip_longest from itertools import zip_longest
from pathlib import Path from pathlib import Path
from threading import Lock from threading import Lock
@ -28,7 +28,6 @@ from pymediainfo import MediaInfo
from pywidevine.cdm import Cdm as WidevineCdm from pywidevine.cdm import Cdm as WidevineCdm
from pywidevine.device import Device from pywidevine.device import Device
from pywidevine.remotecdm import RemoteCdm from pywidevine.remotecdm import RemoteCdm
from requests.cookies import RequestsCookieJar
from rich.console import Group from rich.console import Group
from rich.live import Live from rich.live import Live
from rich.padding import Padding from rich.padding import Padding
@ -982,7 +981,7 @@ class dl:
return cookie_jar return cookie_jar
@staticmethod @staticmethod
def save_cookies(path: Path, cookies: RequestsCookieJar): def save_cookies(path: Path, cookies: CookieJar):
cookie_jar = MozillaCookieJar(path) cookie_jar = MozillaCookieJar(path)
cookie_jar.load() cookie_jar.load()
for cookie in cookies: for cookie in cookies:

View File

@ -11,7 +11,7 @@ from urllib.parse import urlparse
import requests import requests
from Crypto.Random import get_random_bytes from Crypto.Random import get_random_bytes
from requests import Session from requests import Session
from requests.cookies import RequestsCookieJar, cookiejar_from_dict, get_cookie_header from requests.cookies import cookiejar_from_dict, get_cookie_header
from rich import filesize from rich import filesize
from rich.text import Text from rich.text import Text
@ -51,7 +51,7 @@ def download(
output_dir: Path, output_dir: Path,
filename: str, filename: str,
headers: Optional[MutableMapping[str, Union[str, bytes]]] = None, headers: Optional[MutableMapping[str, Union[str, bytes]]] = None,
cookies: Optional[Union[MutableMapping[str, str], RequestsCookieJar]] = None, cookies: Optional[Union[MutableMapping[str, str], CookieJar]] = None,
proxy: Optional[str] = None, proxy: Optional[str] = None,
max_workers: Optional[int] = None max_workers: Optional[int] = None
) -> Generator[dict[str, Any], None, None]: ) -> Generator[dict[str, Any], None, None]:
@ -73,8 +73,8 @@ def download(
if not isinstance(headers, (MutableMapping, type(None))): if not isinstance(headers, (MutableMapping, type(None))):
raise TypeError(f"Expected headers to be {MutableMapping}, not {type(headers)}") raise TypeError(f"Expected headers to be {MutableMapping}, not {type(headers)}")
if not isinstance(cookies, (MutableMapping, RequestsCookieJar, type(None))): if not isinstance(cookies, (MutableMapping, CookieJar, type(None))):
raise TypeError(f"Expected cookies to be {MutableMapping} or {RequestsCookieJar}, not {type(cookies)}") raise TypeError(f"Expected cookies to be {MutableMapping} or {CookieJar}, not {type(cookies)}")
if not isinstance(proxy, (str, type(None))): if not isinstance(proxy, (str, type(None))):
raise TypeError(f"Expected proxy to be {str}, not {type(proxy)}") raise TypeError(f"Expected proxy to be {str}, not {type(proxy)}")
@ -277,7 +277,7 @@ def aria2c(
output_dir: Path, output_dir: Path,
filename: str, filename: str,
headers: Optional[MutableMapping[str, Union[str, bytes]]] = None, headers: Optional[MutableMapping[str, Union[str, bytes]]] = None,
cookies: Optional[Union[MutableMapping[str, str], RequestsCookieJar]] = None, cookies: Optional[Union[MutableMapping[str, str], CookieJar]] = None,
proxy: Optional[str] = None, proxy: Optional[str] = None,
max_workers: Optional[int] = None max_workers: Optional[int] = None
) -> Generator[dict[str, Any], None, None]: ) -> Generator[dict[str, Any], None, None]:

View File

@ -2,11 +2,11 @@ import math
import time import time
from concurrent import futures from concurrent import futures
from concurrent.futures.thread import ThreadPoolExecutor from concurrent.futures.thread import ThreadPoolExecutor
from http.cookiejar import CookieJar
from pathlib import Path from pathlib import Path
from typing import Any, Generator, MutableMapping, Optional, Union from typing import Any, Generator, MutableMapping, Optional, Union
from curl_cffi.requests import Session from curl_cffi.requests import Session
from requests.cookies import RequestsCookieJar
from rich import filesize from rich import filesize
from devine.core.config import config from devine.core.config import config
@ -134,7 +134,7 @@ def curl_impersonate(
output_dir: Path, output_dir: Path,
filename: str, filename: str,
headers: Optional[MutableMapping[str, Union[str, bytes]]] = None, headers: Optional[MutableMapping[str, Union[str, bytes]]] = None,
cookies: Optional[Union[MutableMapping[str, str], RequestsCookieJar]] = None, cookies: Optional[Union[MutableMapping[str, str], CookieJar]] = None,
proxy: Optional[str] = None, proxy: Optional[str] = None,
max_workers: Optional[int] = None max_workers: Optional[int] = None
) -> Generator[dict[str, Any], None, None]: ) -> Generator[dict[str, Any], None, None]:
@ -185,8 +185,8 @@ def curl_impersonate(
if not isinstance(headers, (MutableMapping, type(None))): if not isinstance(headers, (MutableMapping, type(None))):
raise TypeError(f"Expected headers to be {MutableMapping}, not {type(headers)}") raise TypeError(f"Expected headers to be {MutableMapping}, not {type(headers)}")
if not isinstance(cookies, (MutableMapping, RequestsCookieJar, type(None))): if not isinstance(cookies, (MutableMapping, CookieJar, type(None))):
raise TypeError(f"Expected cookies to be {MutableMapping} or {RequestsCookieJar}, not {type(cookies)}") raise TypeError(f"Expected cookies to be {MutableMapping} or {CookieJar}, not {type(cookies)}")
if not isinstance(proxy, (str, type(None))): if not isinstance(proxy, (str, type(None))):
raise TypeError(f"Expected proxy to be {str}, not {type(proxy)}") raise TypeError(f"Expected proxy to be {str}, not {type(proxy)}")

View File

@ -2,11 +2,11 @@ import math
import time import time
from concurrent import futures from concurrent import futures
from concurrent.futures.thread import ThreadPoolExecutor from concurrent.futures.thread import ThreadPoolExecutor
from http.cookiejar import CookieJar
from pathlib import Path from pathlib import Path
from typing import Any, Generator, MutableMapping, Optional, Union from typing import Any, Generator, MutableMapping, Optional, Union
from requests import Session from requests import Session
from requests.cookies import RequestsCookieJar
from rich import filesize from rich import filesize
from devine.core.constants import DOWNLOAD_CANCELLED from devine.core.constants import DOWNLOAD_CANCELLED
@ -131,7 +131,7 @@ def requests(
output_dir: Path, output_dir: Path,
filename: str, filename: str,
headers: Optional[MutableMapping[str, Union[str, bytes]]] = None, headers: Optional[MutableMapping[str, Union[str, bytes]]] = None,
cookies: Optional[Union[MutableMapping[str, str], RequestsCookieJar]] = None, cookies: Optional[Union[MutableMapping[str, str], CookieJar]] = None,
proxy: Optional[str] = None, proxy: Optional[str] = None,
max_workers: Optional[int] = None max_workers: Optional[int] = None
) -> Generator[dict[str, Any], None, None]: ) -> Generator[dict[str, Any], None, None]:
@ -182,8 +182,8 @@ def requests(
if not isinstance(headers, (MutableMapping, type(None))): if not isinstance(headers, (MutableMapping, type(None))):
raise TypeError(f"Expected headers to be {MutableMapping}, not {type(headers)}") raise TypeError(f"Expected headers to be {MutableMapping}, not {type(headers)}")
if not isinstance(cookies, (MutableMapping, RequestsCookieJar, type(None))): if not isinstance(cookies, (MutableMapping, CookieJar, type(None))):
raise TypeError(f"Expected cookies to be {MutableMapping} or {RequestsCookieJar}, not {type(cookies)}") raise TypeError(f"Expected cookies to be {MutableMapping} or {CookieJar}, not {type(cookies)}")
if not isinstance(proxy, (str, type(None))): if not isinstance(proxy, (str, type(None))):
raise TypeError(f"Expected proxy to be {str}, not {type(proxy)}") raise TypeError(f"Expected proxy to be {str}, not {type(proxy)}")

View File

@ -1,7 +1,7 @@
import base64 import base64
import logging import logging
from abc import ABCMeta, abstractmethod from abc import ABCMeta, abstractmethod
from http.cookiejar import CookieJar, MozillaCookieJar from http.cookiejar import CookieJar
from typing import Optional, Union from typing import Optional, Union
from urllib.parse import urlparse from urllib.parse import urlparse
@ -104,7 +104,7 @@ class Service(metaclass=ABCMeta):
session.mount("http://", session.adapters["https://"]) session.mount("http://", session.adapters["https://"])
return session return session
def authenticate(self, cookies: Optional[MozillaCookieJar] = None, credential: Optional[Credential] = None) -> None: def authenticate(self, cookies: Optional[CookieJar] = None, credential: Optional[Credential] = None) -> None:
""" """
Authenticate the Service with Cookies and/or Credentials (Email/Username and Password). Authenticate the Service with Cookies and/or Credentials (Email/Username and Password).
@ -120,7 +120,7 @@ class Service(metaclass=ABCMeta):
""" """
if cookies is not None: if cookies is not None:
if not isinstance(cookies, CookieJar): if not isinstance(cookies, CookieJar):
raise TypeError(f"Expected cookies to be a {MozillaCookieJar}, not {cookies!r}.") raise TypeError(f"Expected cookies to be a {CookieJar}, not {cookies!r}.")
self.session.cookies.update(cookies) self.session.cookies.update(cookies)
def get_widevine_service_certificate(self, *, challenge: bytes, title: Title_T, track: AnyTrack) \ def get_widevine_service_certificate(self, *, challenge: bytes, title: Title_T, track: AnyTrack) \