From 9e9b6d4f923fb2524600f4cee8a70dc11a68c0d7 Mon Sep 17 00:00:00 2001 From: Benex254 Date: Mon, 5 Aug 2024 09:47:03 +0300 Subject: [PATCH] chore(anime_provider): remove print statements from provider and switch to logging --- fastanime/cli/commands/download.py | 45 ++-- fastanime/cli/commands/search.py | 37 ++-- .../cli/interfaces/anilist_interfaces.py | 38 ++-- fastanime/libs/anime_provider/allanime/api.py | 200 ++++++++---------- 4 files changed, 165 insertions(+), 155 deletions(-) diff --git a/fastanime/cli/commands/download.py b/fastanime/cli/commands/download.py index 6a8a6d5..25416ff 100644 --- a/fastanime/cli/commands/download.py +++ b/fastanime/cli/commands/download.py @@ -1,5 +1,6 @@ import click from rich import print +from rich.progress import Progress from thefuzz import fuzz from ...libs.anime_provider.types import Anime @@ -28,9 +29,11 @@ def download(config: Config, anime_title, episode_range): anime_provider = config.anime_provider translation_type = config.translation_type download_dir = config.downloads_dir - search_results = anime_provider.search_for_anime( - anime_title, translation_type=translation_type - ) + with Progress() as progress: + progress.add_task("Fetching Search Results...", total=None) + search_results = anime_provider.search_for_anime( + anime_title, translation_type=translation_type + ) if not search_results: print("Search results failed") input("Enter to retry") @@ -51,7 +54,11 @@ def download(config: Config, anime_title, episode_range): list(search_results_.keys()), "Please Select title: ", "FastAnime" ) - anime: Anime | None = anime_provider.get_anime(search_results_[search_result]["id"]) + with Progress() as progress: + progress.add_task("Fetching Anime...", total=None) + anime: Anime | None = anime_provider.get_anime( + search_results_[search_result]["id"] + ) if not anime: print("Sth went wring anime no found") input("Enter to continue...") @@ -70,20 +77,24 @@ def download(config: Config, anime_title, episode_range): if episode not in episodes: print(f"[cyan]Warning[/]: Episode {episode} not found, skipping") continue - streams = anime_provider.get_episode_streams( - anime, episode, config.translation_type - ) - if not streams: - print("No streams skipping") - continue + with Progress() as progress: + progress.add_task("Fetching Episode Streams...", total=None) + streams = anime_provider.get_episode_streams( + anime, episode, config.translation_type + ) + if not streams: + print("No streams skipping") + continue + + streams = list(streams) + links = [ + (link.get("priority", 0), link["link"]) + for server in streams + for link in server["links"] + ] + link = max(links, key=lambda x: x[0])[1] + print(f"[purple]Now Downloading:[/] {search_result} Episode {episode}") - streams = list(streams) - links = [ - (link.get("priority", 0), link["link"]) - for server in streams - for link in server["links"] - ] - link = max(links, key=lambda x: x[0])[1] downloader._download_file( link, download_dir, diff --git a/fastanime/cli/commands/search.py b/fastanime/cli/commands/search.py index efe78ba..d58c704 100644 --- a/fastanime/cli/commands/search.py +++ b/fastanime/cli/commands/search.py @@ -1,5 +1,6 @@ import click from rich import print +from rich.progress import Progress from thefuzz import fuzz from ...cli.config import Config @@ -23,9 +24,11 @@ from ..utils.utils import clear @click.pass_obj def search(config: Config, anime_title: str, episode_range: str): anime_provider = config.anime_provider - search_results = anime_provider.search_for_anime( - anime_title, config.translation_type - ) + with Progress() as progress: + progress.add_task("Fetching Search Results...", total=None) + search_results = anime_provider.search_for_anime( + anime_title, config.translation_type + ) if not search_results: print("Search results not found") input("Enter to retry") @@ -50,7 +53,12 @@ def search(config: Config, anime_title: str, episode_range: str): list(search_results_.keys()), "Please Select title: ", "FastAnime" ) - anime: Anime | None = anime_provider.get_anime(search_results_[search_result]["id"]) + with Progress() as progress: + progress.add_task("Fetching Anime...", total=None) + anime: Anime | None = anime_provider.get_anime( + search_results_[search_result]["id"] + ) + if not anime: print("Sth went wring anime no found") input("Enter to continue...") @@ -82,17 +90,20 @@ def search(config: Config, anime_title: str, episode_range: str): if not episode or episode not in episodes: episode = fzf.run(episodes, "Select an episode: ", header=search_result) - streams = anime_provider.get_episode_streams( - anime, episode, config.translation_type - ) - if not streams: - print("Failed to get streams") - return - links = [link["link"] for server in streams for link in server["links"]] + with Progress() as progress: + progress.add_task("Fetching Episode Streams...", total=None) + streams = anime_provider.get_episode_streams( + anime, episode, config.translation_type + ) + if not streams: + print("Failed to get streams") + return + links = [link["link"] for server in streams for link in server["links"]] - # TODO: Come up with way to know quality and better server interface - link = links[config.quality] + # TODO: Come up with way to know quality and better server interface + link = links[config.quality] # link = fzf.run(links, "Select stream", "Streams") + print(f"[purple]Now Playing:[/] {search_result} Episode {episode}") mpv(link, search_result) stream_anime() diff --git a/fastanime/cli/interfaces/anilist_interfaces.py b/fastanime/cli/interfaces/anilist_interfaces.py index 8ff7d7c..5baa824 100644 --- a/fastanime/cli/interfaces/anilist_interfaces.py +++ b/fastanime/cli/interfaces/anilist_interfaces.py @@ -4,10 +4,11 @@ import os import random from rich import print +from rich.progress import Progress from rich.prompt import Prompt -from ... import USER_CONFIG_PATH from ...anilist import AniList +from ...constants import USER_CONFIG_PATH from ...libs.anilist.anilist_data_schema import AnilistBaseMediaDataSchema from ...libs.anime_provider.types import Anime, SearchResult, Server from ...libs.fzf import fzf @@ -154,17 +155,20 @@ def fetch_streams(config: Config, anilist_config: QueryDict): anime_provider = config.anime_provider # get streams for episode from provider - episode_streams = anime_provider.get_episode_streams( - anime, episode_number, translation_type - ) - if not episode_streams: - print("Failed to fetch :cry:") - input("Enter to retry...") - return fetch_streams(config, anilist_config) + with Progress() as progress: + progress.add_task("Fetching Episode Streams...", total=None) + episode_streams = anime_provider.get_episode_streams( + anime, episode_number, translation_type + ) + if not episode_streams: + print("Failed to fetch :cry:") + input("Enter to retry...") + return fetch_streams(config, anilist_config) - episode_streams = { - episode_stream["server"]: episode_stream for episode_stream in episode_streams - } + episode_streams = { + episode_stream["server"]: episode_stream + for episode_stream in episode_streams + } # prompt for preferred server server = None @@ -267,7 +271,9 @@ def fetch_episode(config: Config, anilist_config: QueryDict): def fetch_anime_episode(config, anilist_config: QueryDict): selected_anime: SearchResult = anilist_config._anime anime_provider = config.anime_provider - anilist_config.anime = anime_provider.get_anime(selected_anime["id"]) + with Progress() as progress: + progress.add_task("Fetching Anime Info...", total=None) + anilist_config.anime = anime_provider.get_anime(selected_anime["id"]) if not anilist_config.anime: print( @@ -291,9 +297,11 @@ def provide_anime(config: Config, anilist_config: QueryDict): anime_provider = config.anime_provider # search and get the requested title from provider - search_results = anime_provider.search_for_anime( - selected_anime_title, translation_type - ) + with Progress() as progress: + progress.add_task("Fetching Search Results...", total=None) + search_results = anime_provider.search_for_anime( + selected_anime_title, translation_type + ) if not search_results: print( "Sth went wrong :cry: while fetching this could mean you have poor internet connection or the provider is down" diff --git a/fastanime/libs/anime_provider/allanime/api.py b/fastanime/libs/anime_provider/allanime/api.py index 6c5ec0c..876daf9 100644 --- a/fastanime/libs/anime_provider/allanime/api.py +++ b/fastanime/libs/anime_provider/allanime/api.py @@ -4,8 +4,6 @@ from typing import Iterator import requests from requests.exceptions import Timeout -from rich import print -from rich.progress import Progress from ....libs.anime_provider.allanime.types import AllAnimeEpisode from ....libs.anime_provider.types import Anime, Server @@ -43,14 +41,12 @@ class AllAnimeAPI: timeout=10, ) return response.json()["data"] - except Timeout as e: - print( - "Timeout has been exceeded :cry:. This could mean allanime is down or your internet is down" + except Timeout: + Logger.error( + "allanime(Error):Timeout exceeded this could mean allanime is down or you have lost internet connection" ) - Logger.error(f"allanime(Error): {e}") return {} except Exception as e: - print("sth went wrong :confused:") Logger.error(f"allanime:Error: {e}") return {} @@ -75,22 +71,20 @@ class AllAnimeAPI: "countryorigin": countryorigin, } try: - with Progress() as progress: - progress.add_task("[cyan]searching..", start=False, total=None) - search_results = self._fetch_gql(ALLANIME_SEARCH_GQL, variables) - return normalize_search_results(search_results) # pyright:ignore - except Exception: + search_results = self._fetch_gql(ALLANIME_SEARCH_GQL, variables) + return normalize_search_results(search_results) # pyright:ignore + except Exception as e: + Logger.error(f"FA(AllAnime): {e}") return {} def get_anime(self, allanime_show_id: str): variables = {"showId": allanime_show_id} try: - with Progress() as progress: - progress.add_task("[cyan]fetching anime..", start=False, total=None) - anime = self._fetch_gql(ALLANIME_SHOW_GQL, variables) - return normalize_anime(anime["show"]) - except Exception: + anime = self._fetch_gql(ALLANIME_SHOW_GQL, variables) + return normalize_anime(anime["show"]) + except Exception as e: + Logger.error(f"FA(AllAnime): {e}") return None def get_anime_episode( @@ -102,11 +96,10 @@ class AllAnimeAPI: "episodeString": episode_string, } try: - with Progress() as progress: - progress.add_task("[cyan]fetching episode..", start=False, total=None) - episode = self._fetch_gql(ALLANIME_EPISODES_GQL, variables) - return episode["episode"] # pyright: ignore - except Exception: + episode = self._fetch_gql(ALLANIME_EPISODES_GQL, variables) + return episode["episode"] # pyright: ignore + except Exception as e: + Logger.error(f"FA(AllAnime): {e}") return {} def get_episode_streams( @@ -121,99 +114,86 @@ class AllAnimeAPI: embeds = allanime_episode["sourceUrls"] try: - with Progress() as progress: - progress.add_task("[cyan]fetching streams..", start=False, total=None) - for embed in embeds: - try: - # filter the working streams - if embed.get("sourceName", "") not in ( - "Sak", - "Kir", - "S-mp4", - "Luf-mp4", - ): - continue - url = embed.get("sourceUrl") + for embed in embeds: + try: + # filter the working streams + if embed.get("sourceName", "") not in ( + "Sak", + "Kir", + "S-mp4", + "Luf-mp4", + ): + continue + url = embed.get("sourceUrl") - if not url: - continue - if url.startswith("--"): - url = url[2:] + if not url: + continue + if url.startswith("--"): + url = url[2:] - # get the stream url for an episode of the defined source names - parsed_url = decode_hex_string(url) - embed_url = f"https://{ALLANIME_BASE}{parsed_url.replace('clock','clock.json')}" - resp = requests.get( - embed_url, - headers={ - "Referer": ALLANIME_REFERER, - "User-Agent": USER_AGENT, - }, - timeout=10, - ) - if resp.status_code == 200: - match embed["sourceName"]: - case "Luf-mp4": - Logger.debug( - "allanime:Found streams from gogoanime" + # get the stream url for an episode of the defined source names + parsed_url = decode_hex_string(url) + embed_url = f"https://{ALLANIME_BASE}{parsed_url.replace('clock','clock.json')}" + resp = requests.get( + embed_url, + headers={ + "Referer": ALLANIME_REFERER, + "User-Agent": USER_AGENT, + }, + timeout=10, + ) + if resp.status_code == 200: + match embed["sourceName"]: + case "Luf-mp4": + Logger.debug("allanime:Found streams from gogoanime") + yield { + "server": "gogoanime", + "episode_title": ( + allanime_episode["notes"] or f'{anime["title"]}' ) - print("[yellow]GogoAnime Fetched") - yield { - "server": "gogoanime", - "episode_title": ( - allanime_episode["notes"] - or f'{anime["title"]}' - ) - + f"; Episode {episode_number}", - "links": resp.json()["links"], - } # pyright:ignore - case "Kir": - Logger.debug( - "allanime:Found streams from wetransfer" + + f"; Episode {episode_number}", + "links": resp.json()["links"], + } # pyright:ignore + case "Kir": + Logger.debug("allanime:Found streams from wetransfer") + yield { + "server": "wetransfer", + "episode_title": ( + allanime_episode["notes"] or f'{anime["title"]}' ) - print("[yellow]WeTransfer Fetched") - yield { - "server": "wetransfer", - "episode_title": ( - allanime_episode["notes"] - or f'{anime["title"]}' - ) - + f"; Episode {episode_number}", - "links": resp.json()["links"], - } # pyright:ignore - case "S-mp4": - Logger.debug( - "allanime:Found streams from sharepoint" + + f"; Episode {episode_number}", + "links": resp.json()["links"], + } # pyright:ignore + case "S-mp4": + Logger.debug("allanime:Found streams from sharepoint") + yield { + "server": "sharepoint", + "episode_title": ( + allanime_episode["notes"] or f'{anime["title"]}' ) - print("[yellow]Sharepoint Fetched") - yield { - "server": "sharepoint", - "episode_title": ( - allanime_episode["notes"] - or f'{anime["title"]}' - ) - + f"; Episode {episode_number}", - "links": resp.json()["links"], - } # pyright:ignore - case "Sak": - Logger.debug("allanime:Found streams from dropbox") - print("[yellow]Dropbox Fetched") - yield { - "server": "dropbox", - "episode_title": ( - allanime_episode["notes"] - or f'{anime["title"]}' - ) - + f"; Episode {episode_number}", - "links": resp.json()["links"], - } # pyright:ignore - except Timeout: - print( - "Timeout has been exceeded :cry: this could mean allanime is down or your internet connection is poor" - ) - except Exception as e: - print("Sth went wrong :confused:", e) - except Exception: + + f"; Episode {episode_number}", + "links": resp.json()["links"], + } # pyright:ignore + case "Sak": + Logger.debug("allanime:Found streams from dropbox") + yield { + "server": "dropbox", + "episode_title": ( + allanime_episode["notes"] or f'{anime["title"]}' + ) + + f"; Episode {episode_number}", + "links": resp.json()["links"], + } # pyright:ignore + except Timeout: + Logger.error( + "Timeout has been exceeded this could mean allanime is down or you have lost internet connection" + ) + return [] + except Exception as e: + Logger.error(f"FA(Allanime): {e}") + return [] + except Exception as e: + Logger.error(f"FA(Allanime): {e}") return []