From 81dca063fa9b5ce8d0f3c039ffe6c558b143b5ae Mon Sep 17 00:00:00 2001 From: rlaphoenix Date: Fri, 16 Feb 2024 21:02:06 +0000 Subject: [PATCH] Consolidate typing of Requests/MozillaCookieJar typing to CookieJar --- devine/commands/dl.py | 5 ++--- devine/core/downloaders/aria2c.py | 10 +++++----- devine/core/downloaders/curl_impersonate.py | 8 ++++---- devine/core/downloaders/requests.py | 8 ++++---- devine/core/service.py | 6 +++--- 5 files changed, 18 insertions(+), 19 deletions(-) diff --git a/devine/commands/dl.py b/devine/commands/dl.py index 8c84e5c..f578f32 100644 --- a/devine/commands/dl.py +++ b/devine/commands/dl.py @@ -13,7 +13,7 @@ from concurrent import futures from concurrent.futures import ThreadPoolExecutor from copy import deepcopy from functools import partial -from http.cookiejar import MozillaCookieJar +from http.cookiejar import CookieJar, MozillaCookieJar from itertools import zip_longest from pathlib import Path from threading import Lock @@ -28,7 +28,6 @@ from pymediainfo import MediaInfo from pywidevine.cdm import Cdm as WidevineCdm from pywidevine.device import Device from pywidevine.remotecdm import RemoteCdm -from requests.cookies import RequestsCookieJar from rich.console import Group from rich.live import Live from rich.padding import Padding @@ -982,7 +981,7 @@ class dl: return cookie_jar @staticmethod - def save_cookies(path: Path, cookies: RequestsCookieJar): + def save_cookies(path: Path, cookies: CookieJar): cookie_jar = MozillaCookieJar(path) cookie_jar.load() for cookie in cookies: diff --git a/devine/core/downloaders/aria2c.py b/devine/core/downloaders/aria2c.py index bb79d75..b930018 100644 --- a/devine/core/downloaders/aria2c.py +++ b/devine/core/downloaders/aria2c.py @@ -11,7 +11,7 @@ from urllib.parse import urlparse import requests from Crypto.Random import get_random_bytes 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.text import Text @@ -51,7 +51,7 @@ def download( output_dir: Path, filename: str, 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, max_workers: Optional[int] = None ) -> Generator[dict[str, Any], None, None]: @@ -73,8 +73,8 @@ def download( if not isinstance(headers, (MutableMapping, type(None))): raise TypeError(f"Expected headers to be {MutableMapping}, not {type(headers)}") - if not isinstance(cookies, (MutableMapping, RequestsCookieJar, type(None))): - raise TypeError(f"Expected cookies to be {MutableMapping} or {RequestsCookieJar}, not {type(cookies)}") + if not isinstance(cookies, (MutableMapping, CookieJar, type(None))): + raise TypeError(f"Expected cookies to be {MutableMapping} or {CookieJar}, not {type(cookies)}") if not isinstance(proxy, (str, type(None))): raise TypeError(f"Expected proxy to be {str}, not {type(proxy)}") @@ -277,7 +277,7 @@ def aria2c( output_dir: Path, filename: str, 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, max_workers: Optional[int] = None ) -> Generator[dict[str, Any], None, None]: diff --git a/devine/core/downloaders/curl_impersonate.py b/devine/core/downloaders/curl_impersonate.py index 49a0811..fc23c77 100644 --- a/devine/core/downloaders/curl_impersonate.py +++ b/devine/core/downloaders/curl_impersonate.py @@ -2,11 +2,11 @@ import math import time from concurrent import futures from concurrent.futures.thread import ThreadPoolExecutor +from http.cookiejar import CookieJar from pathlib import Path from typing import Any, Generator, MutableMapping, Optional, Union from curl_cffi.requests import Session -from requests.cookies import RequestsCookieJar from rich import filesize from devine.core.config import config @@ -134,7 +134,7 @@ def curl_impersonate( output_dir: Path, filename: str, 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, max_workers: Optional[int] = None ) -> Generator[dict[str, Any], None, None]: @@ -185,8 +185,8 @@ def curl_impersonate( if not isinstance(headers, (MutableMapping, type(None))): raise TypeError(f"Expected headers to be {MutableMapping}, not {type(headers)}") - if not isinstance(cookies, (MutableMapping, RequestsCookieJar, type(None))): - raise TypeError(f"Expected cookies to be {MutableMapping} or {RequestsCookieJar}, not {type(cookies)}") + if not isinstance(cookies, (MutableMapping, CookieJar, type(None))): + raise TypeError(f"Expected cookies to be {MutableMapping} or {CookieJar}, not {type(cookies)}") if not isinstance(proxy, (str, type(None))): raise TypeError(f"Expected proxy to be {str}, not {type(proxy)}") diff --git a/devine/core/downloaders/requests.py b/devine/core/downloaders/requests.py index c91a6e3..af6f100 100644 --- a/devine/core/downloaders/requests.py +++ b/devine/core/downloaders/requests.py @@ -2,11 +2,11 @@ import math import time from concurrent import futures from concurrent.futures.thread import ThreadPoolExecutor +from http.cookiejar import CookieJar from pathlib import Path from typing import Any, Generator, MutableMapping, Optional, Union from requests import Session -from requests.cookies import RequestsCookieJar from rich import filesize from devine.core.constants import DOWNLOAD_CANCELLED @@ -131,7 +131,7 @@ def requests( output_dir: Path, filename: str, 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, max_workers: Optional[int] = None ) -> Generator[dict[str, Any], None, None]: @@ -182,8 +182,8 @@ def requests( if not isinstance(headers, (MutableMapping, type(None))): raise TypeError(f"Expected headers to be {MutableMapping}, not {type(headers)}") - if not isinstance(cookies, (MutableMapping, RequestsCookieJar, type(None))): - raise TypeError(f"Expected cookies to be {MutableMapping} or {RequestsCookieJar}, not {type(cookies)}") + if not isinstance(cookies, (MutableMapping, CookieJar, type(None))): + raise TypeError(f"Expected cookies to be {MutableMapping} or {CookieJar}, not {type(cookies)}") if not isinstance(proxy, (str, type(None))): raise TypeError(f"Expected proxy to be {str}, not {type(proxy)}") diff --git a/devine/core/service.py b/devine/core/service.py index b021d91..423b408 100644 --- a/devine/core/service.py +++ b/devine/core/service.py @@ -1,7 +1,7 @@ import base64 import logging from abc import ABCMeta, abstractmethod -from http.cookiejar import CookieJar, MozillaCookieJar +from http.cookiejar import CookieJar from typing import Optional, Union from urllib.parse import urlparse @@ -104,7 +104,7 @@ class Service(metaclass=ABCMeta): session.mount("http://", session.adapters["https://"]) 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). @@ -120,7 +120,7 @@ class Service(metaclass=ABCMeta): """ if cookies is not None: 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) def get_widevine_service_certificate(self, *, challenge: bytes, title: Title_T, track: AnyTrack) \