Skip to content

Commit

Permalink
feat(cli): switch to using AnimeProvider obj
Browse files Browse the repository at this point in the history
  • Loading branch information
Benex254 committed Jul 25, 2024
1 parent eb7bef7 commit f3d88f9
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 6 deletions.
11 changes: 11 additions & 0 deletions fastanime/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import click

from .. import __version__
from ..libs.anime_provider import anime_sources
from ..libs.anime_provider.allanime.constants import SERVERS_AVAILABLE
from ..Utility.data import anilist_sort_normalizer
from .commands.anilist import anilist
Expand Down Expand Up @@ -40,6 +41,12 @@ def handle_exit(signum, frame):
short_help="Stream Anime",
)
@click.version_option(__version__, "--version")
@click.option(
"-p",
"--provider",
type=click.Choice(list(anime_sources.keys()), case_sensitive=False),
help="Provider of your choice",
)
@click.option(
"-s",
"--server",
Expand Down Expand Up @@ -96,6 +103,7 @@ def handle_exit(signum, frame):
@click.pass_context
def run_cli(
ctx: click.Context,
provider,
server,
format,
continue_,
Expand All @@ -111,6 +119,9 @@ def run_cli(
no_preview,
):
ctx.obj = Config()
if provider:
ctx.obj.provider = provider
ctx.obj.load_config()
if server:
ctx.obj.server = server
if format:
Expand Down
7 changes: 6 additions & 1 deletion fastanime/cli/commands/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from rich import print
from thefuzz import fuzz

from ...libs.anime_provider.allanime.api import anime_provider
from ...libs.anime_provider.types import Anime
from ...libs.fzf import fzf
from ...Utility.downloader.downloader import downloader
Expand All @@ -26,11 +25,17 @@
)
@click.pass_obj
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
)
if not search_results:
print("Search results failed")
input("Enter to retry")
download(config, anime_title, episode_range)
return
search_results = search_results["results"]
search_results_ = {
search_result["title"]: search_result for search_result in search_results
Expand Down
7 changes: 6 additions & 1 deletion fastanime/cli/commands/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from thefuzz import fuzz

from ...cli.config import Config
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
Expand All @@ -23,9 +22,15 @@
@click.argument("anime_title", required=True, type=str)
@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
)
if not search_results:
print("Search results not found")
input("Enter to retry")
search(config, anime_title, episode_range)
return
search_results = search_results["results"]
if not search_results:
print("Anime not found :cry:")
Expand Down
8 changes: 8 additions & 0 deletions fastanime/cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from rich import print

from .. import USER_CONFIG_PATH, USER_VIDEOS_DIR
from ..AnimeProvider import AnimeProvider
from ..Utility.user_data_helper import user_data_helper


Expand All @@ -29,6 +30,7 @@ def load_config(self):
"use_fzf": "False",
"preview": "False",
"format": "best[height<=1080]/bestvideo[height<=1080]+bestaudio/best",
"provider": "allanime",
}
)
self.configparser.add_section("stream")
Expand All @@ -41,6 +43,7 @@ def load_config(self):

# --- set defaults ---
self.downloads_dir = self.get_downloads_dir()
self.provider = self.get_provider()
self.use_fzf = self.get_use_fzf()
self.preview = self.get_preview()
self.translation_type = self.get_translation_type()
Expand All @@ -57,6 +60,8 @@ def load_config(self):
self.watch_history: dict = user_data_helper.user_data.get("watch_history", {})
self.anime_list: list = user_data_helper.user_data.get("animelist", [])

self.anime_provider = AnimeProvider(self.provider)

def update_watch_history(self, anime_id: int, episode: str | None):
self.watch_history.update({str(anime_id): episode})
user_data_helper.update_watch_history(self.watch_history)
Expand All @@ -74,6 +79,9 @@ def update_anime_list(self, anime_id: int, remove=False):
print("Succesfully added :smile:")
input("Enter to continue...")

def get_provider(self):
return self.configparser.get("general", "provider")

def get_downloads_dir(self):
return self.configparser.get("general", "downloads_dir")

Expand Down
4 changes: 3 additions & 1 deletion fastanime/cli/interfaces/anilist_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from ... import USER_CONFIG_PATH
from ...libs.anilist.anilist import AniList
from ...libs.anilist.anilist_data_schema import AnilistBaseMediaDataSchema
from ...libs.anime_provider.allanime.api import anime_provider
from ...libs.anime_provider.types import Anime, SearchResult, Server
from ...libs.fzf import fzf
from ...Utility.data import anime_normalizer
Expand Down Expand Up @@ -152,6 +151,7 @@ def fetch_streams(config: Config, anilist_config: QueryDict):
anime_id: int = anilist_config.anime_id
anime: Anime = anilist_config.anime
translation_type = config.translation_type
anime_provider = config.anime_provider

# get streams for episode from provider
episode_streams = anime_provider.get_episode_streams(
Expand Down Expand Up @@ -266,6 +266,7 @@ 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"])
if not anilist_config.anime:

Expand All @@ -287,6 +288,7 @@ def provide_anime(config: Config, anilist_config: QueryDict):
selected_anime_title = anilist_config.selected_anime_title

anime_data: AnilistBaseMediaDataSchema = anilist_config.selected_anime_anilist
anime_provider = config.anime_provider

# search and get the requested title from provider
search_results = anime_provider.search_for_anime(
Expand Down
14 changes: 14 additions & 0 deletions fastanime/libs/anime_provider/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from .allanime.api import AllAnimeAPI

anime_sources = {"allanime": AllAnimeAPI}


class Anime_Provider:
def search_for_anime(self):
pass

def get_anime(self):
pass

def get_episode_streams(self):
pass
4 changes: 1 addition & 3 deletions fastanime/libs/anime_provider/allanime/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,8 @@ def get_episode_streams(
return []


anime_provider = AllAnimeAPI()


if __name__ == "__main__":
anime_provider = AllAnimeAPI()
# lets see if it works :)
import subprocess
import sys
Expand Down

0 comments on commit f3d88f9

Please sign in to comment.