From 7a1236c6774f4ff84e2f6173bcadccb6142fe246 Mon Sep 17 00:00:00 2001 From: Benex254 Date: Mon, 5 Aug 2024 09:47:02 +0300 Subject: [PATCH] feat(cli): add bing mode to search subcommand using episode ranges --- fastanime/cli/commands/anilist/search.py | 4 +- fastanime/cli/commands/search.py | 53 +++++++++++++++++++----- 2 files changed, 46 insertions(+), 11 deletions(-) diff --git a/fastanime/cli/commands/anilist/search.py b/fastanime/cli/commands/anilist/search.py index 92d85ea..9aa4d2b 100644 --- a/fastanime/cli/commands/anilist/search.py +++ b/fastanime/cli/commands/anilist/search.py @@ -9,7 +9,9 @@ from ...utils.tools import QueryDict help="Search for anime using anilists api and get top ~50 results", short_help="Search for anime", ) -@click.option("--title", prompt="Enter anime title") +@click.argument( + "title", +) @click.pass_obj def search(config, title): success, search_results = AniList.search(title) diff --git a/fastanime/cli/commands/search.py b/fastanime/cli/commands/search.py index dfadaec..d321d8f 100644 --- a/fastanime/cli/commands/search.py +++ b/fastanime/cli/commands/search.py @@ -7,25 +7,33 @@ from ...libs.anime_provider.allanime.api import anime_provider from ...libs.anime_provider.types import Anime from ...libs.fzf import fzf from ..utils.mpv import mpv +from ..utils.tools import exit_app +from ..utils.utils import clear @click.command( - help="This subcommand directly interacts with the provider to enable basic streaming and perhaps automation if required", - short_help="Directly stream anime with provider", + help="This subcommand directly interacts with the provider to enable basic streaming. Useful for binging anime.", + short_help="Binge anime", +) +@click.option( + "--episode-range", + "-r", + help="A range of episodes to binge", ) @click.argument("anime_title", required=True, type=str) @click.pass_obj -def search( - config: Config, - anime_title: str, -): +def search(config: Config, anime_title: str, episode_range: str): search_results = anime_provider.search_for_anime( anime_title, config.translation_type ) search_results = search_results["results"] + if not search_results: + print("Anime not found :cry:") + exit_app() search_results_ = { search_result["title"]: search_result for search_result in search_results } + if config.auto_select: search_result = max( search_results_.keys(), key=lambda title: fuzz.ratio(title, anime_title) @@ -39,10 +47,32 @@ def search( anime: Anime = anime_provider.get_anime(search_results_[search_result]["id"]) - def stream_anime(): - episodes = anime["availableEpisodesDetail"][config.translation_type] + episode_range_ = None + episodes = anime["availableEpisodesDetail"][config.translation_type] + if episode_range: + episodes_start, episodes_end = episode_range.split("-") + if episodes_start and episodes_end: + episode_range_ = iter( + range(round(float(episodes_start)), round(float(episodes_end)) + 1) + ) + else: + episode_range_ = iter(sorted(episodes, key=float)) - episode = fzf.run(episodes, "Select an episode: ", header=search_result) + def stream_anime(): + clear() + episode = None + + if episode_range_: + try: + episode = str(next(episode_range_)) + print( + f"[cyan]Auto selecting:[/] {search_result} [cyan]Episode:[/] {episode}" + ) + except StopIteration: + print("[green]Completed binge sequence[/]:smile:") + + 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 ) @@ -50,7 +80,10 @@ def search( print("Failed to get streams") return links = [link["link"] for server in streams for link in server["links"]] - link = fzf.run(links, "Select stream", "Streams") + + # TODO: Come up with way to know quality and better server interface + link = links[config.quality] + # link = fzf.run(links, "Select stream", "Streams") mpv(link, search_result) stream_anime()