Skip to content

Commit

Permalink
refactor, chore: media_episodes should be passed to `_previous_seas…
Browse files Browse the repository at this point in the history
…on()` instead in `EpisodeSelector()`, add return types to some util functions, simplify types in `scraper` module and remove passing of `Config()` class in `HTTPClient()`.
  • Loading branch information
THEGOLDENPRO committed Jun 14, 2024
1 parent 76ae1bb commit 56d145e
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 17 deletions.
6 changes: 5 additions & 1 deletion mov_cli/cli/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ def mov_cli(

query: str = " ".join(query)

http_client = HTTPClient(config)
http_client = HTTPClient(
headers = config.http_headers,
timeout = config.http_timeout,
hide_ip = config.hide_ip
)

selected_scraper = select_scraper(plugins, config.scrapers, config.fzf_enabled, config.default_scraper)

Expand Down
8 changes: 4 additions & 4 deletions mov_cli/cli/play.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from typing import Optional, Literal

from ..config import Config
from ..scraper import Scraper
from ..media import Media, Metadata
from ..scraper import Scraper, ScrapeEpisodesT

from ..utils.episode_selector import EpisodeSelector

Expand Down Expand Up @@ -109,7 +109,7 @@ def play(media: Media, metadata: Metadata, scraper: Scraper, episode: EpisodeSel

return None

def __handle_next_season(episode: EpisodeSelector, season_episode_count: int, media_episodes: dict) -> bool:
def __handle_next_season(episode: EpisodeSelector, season_episode_count: int, media_episodes: ScrapeEpisodesT) -> bool:

if episode.episode > season_episode_count:
next_season = episode.season + 1
Expand All @@ -124,6 +124,6 @@ def __handle_next_season(episode: EpisodeSelector, season_episode_count: int, me
if episode.season <= 1:
return False

episode._previous_season()
episode._previous_season(media_episodes)

return True
return True
19 changes: 13 additions & 6 deletions mov_cli/http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,22 @@ def __init__(self, url: str, error: httpx.ConnectError) -> None:
)

class HTTPClient():
def __init__(self, config: Config) -> None:
self.config = config
def __init__(
self,
headers: Optional[Dict[str, str]] = None,
timeout: int = 15,
hide_ip: bool = True
) -> None:
self.hide_ip = hide_ip
self.headers = headers or {}

self.logger = LoggerAdapter(mov_cli_logger, prefix = self.__class__.__name__)

self.__httpx_client = httpx.Client(
timeout = config.http_timeout,
timeout = timeout,
cookies = None
)

super().__init__()

def request(
Expand All @@ -58,11 +65,11 @@ def request(
if headers.get("Referer") is None:
headers.update({"Referer": url})

headers.update(self.config.http_headers)
headers.update(self.headers)

try:
self.logger.debug(
Colours.ORANGE.apply(method.upper()) + f" -> {hide_ip(url, self.config.hide_ip)}"
Colours.ORANGE.apply(method.upper()) + f" -> {hide_ip(url, self.hide_ip)}"
)

response = self.__httpx_client.request(
Expand Down
3 changes: 2 additions & 1 deletion mov_cli/scraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from .media import Metadata, Multi, Single

ScraperOptionsT = Dict[str, str | bool]
ScrapeEpisodesT = Dict[int, int] | Dict[None, Literal[1]]

from bs4 import BeautifulSoup
from abc import ABC, abstractmethod
Expand Down Expand Up @@ -55,6 +56,6 @@ def scrape(self, metadata: Metadata, episode: EpisodeSelector) -> Optional[Multi
"""
...

def scrape_episodes(self, metadata: Metadata) -> Dict[int, int] | Dict[None, Literal[1]]:
def scrape_episodes(self, metadata: Metadata) -> ScrapeEpisodesT:
"""Returns episode count for each season in that Media."""
return {None: 1}
9 changes: 6 additions & 3 deletions mov_cli/utils/episode_selector.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
from __future__ import annotations
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from ..scraper import ScrapeEpisodesT

from dataclasses import dataclass, field

Expand All @@ -9,14 +13,13 @@ class EpisodeSelector:
"""Swift util to use when asking the scraper which episode of a show to scrape."""
episode: int = field(default = 1)
season: int = field(default = 1)
media_episodes: dict = field(default = dict)

# NOTE: I made it private as I don't want library devs using
# this method yet because it may drastically change in the future.
def _next_season(self) -> None:
self.episode = 1
self.season += 1

def _previous_season(self) -> None:
def _previous_season(self, media_episodes: ScrapeEpisodesT) -> None:
self.season -= 1
self.episode = self.media_episodes.get(self.season, 1)
self.episode = media_episodes.get(self.season, 1)
2 changes: 1 addition & 1 deletion mov_cli/utils/ip.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

__all__ = ("hide_ip",)

def hide_ip(text: str, hide_it: bool):
def hide_ip(text: str, hide_it: bool) -> str:
ipv4_re = "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"
ipv6_re = "([a-f0-9:]+:+)+[a-f0-9]+"

Expand Down
2 changes: 1 addition & 1 deletion mov_cli/utils/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def plugin_update_available(plugins: Dict[str, str]) -> Tuple[bool, List[str]]:

return False, []

def update_command(mov_cli_path: Path, package: str | list = "mov-cli"):
def update_command(mov_cli_path: Path, package: str | list = "mov-cli") -> str:
path = str(mov_cli_path)

if "pipx" in path:
Expand Down

0 comments on commit 56d145e

Please sign in to comment.