Skip to content

Commit

Permalink
chore: reorganize codebase to make anilist top level
Browse files Browse the repository at this point in the history
  • Loading branch information
Benex254 committed Jul 26, 2024
1 parent 01333ab commit 0f9e08b
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 49 deletions.
3 changes: 3 additions & 0 deletions fastanime/anilist.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .libs.anilist.api import AniListApi

AniList = AniListApi()
2 changes: 1 addition & 1 deletion fastanime/cli/commands/anilist/favourites.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import click

from ....libs.anilist.anilist import AniList
from ....anilist import AniList
from ...interfaces.anilist_interfaces import select_anime
from ...utils.tools import QueryDict

Expand Down
2 changes: 1 addition & 1 deletion fastanime/cli/commands/anilist/popular.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import click

from ....libs.anilist.anilist import AniList
from ....anilist import AniList
from ...interfaces.anilist_interfaces import select_anime
from ...utils.tools import QueryDict

Expand Down
2 changes: 1 addition & 1 deletion fastanime/cli/commands/anilist/random_anime.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import click

from ....libs.anilist.anilist import AniList
from ....anilist import AniList
from ...interfaces.anilist_interfaces import select_anime
from ...utils.tools import QueryDict

Expand Down
2 changes: 1 addition & 1 deletion fastanime/cli/commands/anilist/recent.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import click

from ....libs.anilist.anilist import AniList
from ....anilist import AniList
from ...interfaces.anilist_interfaces import select_anime
from ...utils.tools import QueryDict

Expand Down
2 changes: 1 addition & 1 deletion fastanime/cli/commands/anilist/scores.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import click

from ....libs.anilist.anilist import AniList
from ....anilist import AniList
from ...interfaces.anilist_interfaces import select_anime
from ...utils.tools import QueryDict

Expand Down
2 changes: 1 addition & 1 deletion fastanime/cli/commands/anilist/search.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import click

from ....libs.anilist.anilist import AniList
from ....anilist import AniList
from ...interfaces.anilist_interfaces import select_anime
from ...utils.tools import QueryDict

Expand Down
2 changes: 1 addition & 1 deletion fastanime/cli/commands/anilist/trending.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import click

from ....libs.anilist.anilist import AniList
from ....anilist import AniList
from ...interfaces.anilist_interfaces import select_anime
from ...utils.tools import QueryDict

Expand Down
2 changes: 1 addition & 1 deletion fastanime/cli/commands/anilist/upcoming.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import click

from ....libs.anilist.anilist import AniList
from ....anilist import AniList
from ...interfaces.anilist_interfaces import select_anime
from ...utils.tools import QueryDict

Expand Down
2 changes: 1 addition & 1 deletion fastanime/cli/interfaces/anilist_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from rich.prompt import Prompt

from ... import USER_CONFIG_PATH
from ...libs.anilist.anilist import AniList
from ...anilist import AniList
from ...libs.anilist.anilist_data_schema import AnilistBaseMediaDataSchema
from ...libs.anime_provider.types import Anime, SearchResult, Server
from ...libs.fzf import fzf
Expand Down
67 changes: 27 additions & 40 deletions fastanime/libs/anilist/anilist.py → fastanime/libs/anilist/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@
# from kivy.network.urlrequest import UrlRequestRequests


class AniList:
class AniListApi:
"""
This class provides an abstraction for the anilist api
"""

@classmethod
def get_data(
cls, query: str, variables: dict = {}
self, query: str, variables: dict = {}
) -> tuple[bool, AnilistDataSchema]:
"""
The core abstraction for getting data from the anilist api
Expand All @@ -43,7 +42,7 @@ def get_data(
variables to pass to the anilist api
"""
url = "https://graphql.anilist.co"
# req=UrlRequestRequests(url, cls.got_data,)
# req=UrlRequestRequests(url, self.got_data,)
try:
# TODO: check if data is as expected
response = requests.post(
Expand All @@ -68,9 +67,8 @@ def get_data(
except Exception as e:
return (False, {"Error": f"{e}"}) # type: ignore

@classmethod
def search(
cls,
self,
query: str | None = None,
sort: str | None = None,
genre_in: list[str] | None = None,
Expand Down Expand Up @@ -99,86 +97,75 @@ def search(
for key, val in list(locals().items())[1:]:
if val is not None and key not in ["variables"]:
variables[key] = val
search_results = cls.get_data(search_query, variables=variables)
search_results = self.get_data(search_query, variables=variables)
return search_results

@classmethod
def get_anime(cls, id: int):
def get_anime(self, id: int):
"""
Gets a single anime by a valid anilist anime id
"""
variables = {"id": id}
return cls.get_data(anime_query, variables)
return self.get_data(anime_query, variables)

@classmethod
def get_trending(cls, *_, **kwargs):
def get_trending(self, *_, **kwargs):
"""
Gets the currently trending anime
"""
trending = cls.get_data(trending_query)
trending = self.get_data(trending_query)
return trending

@classmethod
def get_most_favourite(cls, *_, **kwargs):
def get_most_favourite(self, *_, **kwargs):
"""
Gets the most favoured anime on anilist
"""
most_favourite = cls.get_data(most_favourite_query)
most_favourite = self.get_data(most_favourite_query)
return most_favourite

@classmethod
def get_most_scored(cls, *_, **kwargs):
def get_most_scored(self, *_, **kwargs):
"""
Gets most scored anime on anilist
"""
most_scored = cls.get_data(most_scored_query)
most_scored = self.get_data(most_scored_query)
return most_scored

@classmethod
def get_most_recently_updated(cls, *_, **kwargs):
def get_most_recently_updated(self, *_, **kwargs):
"""
Gets most recently updated anime from anilist
"""
most_recently_updated = cls.get_data(most_recently_updated_query)
most_recently_updated = self.get_data(most_recently_updated_query)
return most_recently_updated

@classmethod
def get_most_popular(cls, *_, **kwargs):
def get_most_popular(self):
"""
Gets most popular anime on anilist
"""
most_popular = cls.get_data(most_popular_query)
most_popular = self.get_data(most_popular_query)
return most_popular

# FIXME:dont know why its not giving useful data
@classmethod
def get_recommended_anime_for(cls, id: int, *_, **kwargs):
recommended_anime = cls.get_data(recommended_query)
def get_recommended_anime_for(self, id: int, *_, **kwargs):
recommended_anime = self.get_data(recommended_query)
return recommended_anime

@classmethod
def get_charcters_of(cls, id: int, *_, **kwargs):
def get_charcters_of(self, id: int, *_, **kwargs):
variables = {"id": id}
characters = cls.get_data(anime_characters_query, variables)
characters = self.get_data(anime_characters_query, variables)
return characters

@classmethod
def get_related_anime_for(cls, id: int, *_, **kwargs):
def get_related_anime_for(self, id: int, *_, **kwargs):
variables = {"id": id}
related_anime = cls.get_data(anime_relations_query, variables)
related_anime = self.get_data(anime_relations_query, variables)
return related_anime

@classmethod
def get_airing_schedule_for(cls, id: int, *_, **kwargs):
def get_airing_schedule_for(self, id: int, *_, **kwargs):
variables = {"id": id}
airing_schedule = cls.get_data(airing_schedule_query, variables)
airing_schedule = self.get_data(airing_schedule_query, variables)
return airing_schedule

@classmethod
def get_upcoming_anime(cls, page: int = 1, *_, **kwargs):
def get_upcoming_anime(self, page: int = 1, *_, **kwargs):
"""
Gets upcoming anime from anilist
"""
variables = {"page": page}
upcoming_anime = cls.get_data(upcoming_anime_query, variables)
upcoming_anime = self.get_data(upcoming_anime_query, variables)
return upcoming_anime

0 comments on commit 0f9e08b

Please sign in to comment.