mirror of
https://github.com/Benexl/FastAnime.git
synced 2025-12-12 15:50:01 -08:00
80 lines
1.9 KiB
Python
80 lines
1.9 KiB
Python
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
ANILIST_ENDPOINT = "https://graphql.anilist.co"
|
|
|
|
|
|
anime_title_query = """
|
|
query ($query: String) {
|
|
Page(perPage: 50) {
|
|
pageInfo {
|
|
total
|
|
}
|
|
media(search: $query, type: ANIME) {
|
|
id
|
|
idMal
|
|
title {
|
|
romaji
|
|
english
|
|
}
|
|
}
|
|
}
|
|
}
|
|
"""
|
|
|
|
|
|
def get_anime_titles(query: str, variables: dict = {}):
|
|
"""the abstraction over all none authenticated requests and that returns data of a similar type
|
|
|
|
Args:
|
|
query: the anilist query
|
|
variables: the anilist api variables
|
|
|
|
Returns:
|
|
a boolean indicating success and none or an anilist object depending on success
|
|
"""
|
|
from httpx import post
|
|
|
|
try:
|
|
response = post(
|
|
ANILIST_ENDPOINT,
|
|
json={"query": query, "variables": variables},
|
|
timeout=10,
|
|
)
|
|
anilist_data = response.json()
|
|
|
|
if response.status_code == 200:
|
|
eng_titles = [
|
|
anime["title"]["english"]
|
|
for anime in anilist_data["data"]["Page"]["media"]
|
|
if anime["title"]["english"]
|
|
]
|
|
romaji_titles = [
|
|
anime["title"]["romaji"]
|
|
for anime in anilist_data["data"]["Page"]["media"]
|
|
if anime["title"]["romaji"]
|
|
]
|
|
return [*eng_titles, *romaji_titles]
|
|
else:
|
|
return []
|
|
except Exception as e:
|
|
logger.error(f"Something unexpected occurred {e}")
|
|
return []
|
|
|
|
|
|
def anime_titles_shell_complete(ctx, param, incomplete):
|
|
incomplete = incomplete.strip()
|
|
if not incomplete:
|
|
incomplete = None
|
|
variables = {}
|
|
else:
|
|
variables = {"query": incomplete}
|
|
return get_anime_titles(anime_title_query, variables)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
t = input("Enter title")
|
|
results = get_anime_titles(anime_title_query, {"query": t})
|
|
print(results)
|