From 7cc90a56a344d5253ae7a90f44cf8c5e862015f3 Mon Sep 17 00:00:00 2001 From: Frederik Kriewitz Date: Wed, 26 Jun 2024 17:40:21 +0200 Subject: [PATCH] type fixes --- audiobookdl/sources/__init__.py | 2 +- audiobookdl/sources/audiobooksdotcom.py | 9 ++++++--- audiobookdl/sources/bookbeat.py | 2 +- audiobookdl/sources/ereolen.py | 2 +- audiobookdl/sources/everand.py | 2 +- audiobookdl/sources/source/__init__.py | 4 ++-- audiobookdl/sources/storytel-legacy.py | 5 +++-- 7 files changed, 15 insertions(+), 11 deletions(-) diff --git a/audiobookdl/sources/__init__.py b/audiobookdl/sources/__init__.py index 89ccb90..19a7ca0 100644 --- a/audiobookdl/sources/__init__.py +++ b/audiobookdl/sources/__init__.py @@ -28,7 +28,7 @@ def find_compatible_source(url: str) -> type[Source]: raise NoSourceFound -def get_source_classes(): +def get_source_classes() -> list[type[Source]]: """Returns a list of all available sources""" return [ AudiobooksdotcomSource, diff --git a/audiobookdl/sources/audiobooksdotcom.py b/audiobookdl/sources/audiobooksdotcom.py index 8da20c0..b18216c 100644 --- a/audiobookdl/sources/audiobooksdotcom.py +++ b/audiobookdl/sources/audiobooksdotcom.py @@ -1,6 +1,6 @@ from .source import Source from audiobookdl import AudiobookFile, logging, AudiobookMetadata, Cover, Audiobook -from audiobookdl.exceptions import NoSourceFound, DataNotPresent +from audiobookdl.exceptions import NoSourceFound, DataNotPresent, GenericAudiobookDLException import re from typing import List @@ -56,8 +56,11 @@ def extract_useragent_from_cookies(self) -> str: :returns: User-Agent string """ - raw = self._session.cookies.get("ci_session", domain="www.audiobooks.com") - return unquote(raw).split("\"")[11] + raw: str | None = self._session.cookies.get("ci_session", domain="www.audiobooks.com") + if not raw: + raise GenericAudiobookDLException(f"ci_session missing from cookie") + else: + return unquote(raw).split("\"")[11] def extract_file(self, scrape_url: str) -> List[AudiobookFile]: diff --git a/audiobookdl/sources/bookbeat.py b/audiobookdl/sources/bookbeat.py index 3c1544c..693785f 100644 --- a/audiobookdl/sources/bookbeat.py +++ b/audiobookdl/sources/bookbeat.py @@ -64,7 +64,7 @@ def download(self, url: str) -> Audiobook: ) - def download_license_url(self, book_info): + def download_license_url(self, book_info)-> str: dl_info = self.get_json( "https://api.bookbeat.com/api/downloadinfo/" + str(book_info["bookid"]) ) diff --git a/audiobookdl/sources/ereolen.py b/audiobookdl/sources/ereolen.py index e1c8bbd..c9724cf 100644 --- a/audiobookdl/sources/ereolen.py +++ b/audiobookdl/sources/ereolen.py @@ -101,7 +101,7 @@ def get_files(self, book_id: str) -> List[AudiobookFile]: ) - def _get_libraries(self): + def _get_libraries(self) -> dict: """Returns list of available libraries for login""" libraries_raw = self.find_in_page( LOGIN_PAGE_URL, diff --git a/audiobookdl/sources/everand.py b/audiobookdl/sources/everand.py index 81f02d3..7e5ccae 100644 --- a/audiobookdl/sources/everand.py +++ b/audiobookdl/sources/everand.py @@ -236,7 +236,7 @@ def format_metadata(book_info: dict) -> AudiobookMetadata: @staticmethod - def get_chapter_title(chapter): + def get_chapter_title(chapter) -> str: """Extract title for chapter""" number = chapter["chapter_number"] if number == 0: diff --git a/audiobookdl/sources/source/__init__.py b/audiobookdl/sources/source/__init__.py index c299f58..c57a5fe 100644 --- a/audiobookdl/sources/source/__init__.py +++ b/audiobookdl/sources/source/__init__.py @@ -137,7 +137,7 @@ def find_elem_in_page(self, url: str, selector: str, data=None, **kwargs): return elem.get(data) - def find_elems_in_page(self, url: str, selector: str, **kwargs) -> list: + def find_elems_in_page(self, url: str, selector: str, **kwargs) -> Any: """ Find all html elements in the page from `url` that's matches `selector`. Will cache the page. @@ -177,7 +177,7 @@ def find_all_in_page(self, url: str, regex: str, **kwargs) -> list: get_stream_files = networking.get_stream_files def create_ssl_context(self, options: Any) -> SSLContext: - ssl_context: SSLContext = urllib3.util.create_urllib3_context() + ssl_context: SSLContext = urllib3.util.create_urllib3_context() # type: ignore[attr-defined] # Prevent the padding extension from appearing in the TLS ClientHello # It's used by Cloudflare for bot detection # See issue #106 diff --git a/audiobookdl/sources/storytel-legacy.py b/audiobookdl/sources/storytel-legacy.py index 03e52a6..064fa7f 100644 --- a/audiobookdl/sources/storytel-legacy.py +++ b/audiobookdl/sources/storytel-legacy.py @@ -1,3 +1,4 @@ +from requests.models import Response from .source import Source from audiobookdl import AudiobookFile, Chapter, logging, AudiobookMetadata, Cover, Audiobook from audiobookdl.exceptions import UserNotAuthorized, MissingBookAccess, DataNotPresent @@ -83,7 +84,7 @@ def get_book_id(url: str) -> str: raise DataNotPresent return parsed.path.split("-")[-1] - def download_bookshelf(self): + def download_bookshelf(self) -> Response: """Download bookshelf data""" return self._session.get( f"https://www.storytel.com/api/getBookShelf.action", @@ -143,7 +144,7 @@ def get_metadata(book_info) -> AudiobookMetadata: return metadata - def download_audiobook_info(self, book_info): + def download_audiobook_info(self, book_info) -> dict[str, Any]: """Download information about the audiobook files""" consumable_id = book_info["book"]["consumableId"] url = f"https://api.storytel.net/playback-metadata/consumable/{consumable_id}"