- Introduced a Gitea CI workflow for building and publishing the package. - Updated AnilistAPI to support additional queries for genres and tags. - Improved request handling and response parsing in the API. - Enhanced Manga schema to ensure proper type annotations and data structure.
85 lines
2.8 KiB
Python
85 lines
2.8 KiB
Python
import requests
|
|
from .schemas.manga import Manga, Tag
|
|
from .queries.manga import (
|
|
MANGA_QUERY,
|
|
MANGA_ID_QUERY,
|
|
REQUESTS_QUERY,
|
|
GENRES_QUERY,
|
|
TAGS_QUERY,
|
|
)
|
|
from limit import limit
|
|
from anilistapi import __version__, __contact__
|
|
import os
|
|
import time
|
|
|
|
REQUEST_LIMIT = 90
|
|
REQUEST_PERIOD = 60
|
|
|
|
|
|
class AnilistAPI:
|
|
def __init__(self):
|
|
# add system information to the user agent
|
|
self.userAgent = f"AnilistAPI/{__version__} {os.uname().sysname}/{os.uname().machine} Python/{os.uname().release} - {__contact__}"
|
|
pass
|
|
|
|
@limit(REQUEST_LIMIT, REQUEST_PERIOD)
|
|
def request(self, query: str, variables: dict) -> dict:
|
|
url = "https://graphql.anilist.co"
|
|
response = requests.post(url, json={"query": query, "variables": variables})
|
|
time.sleep(1)
|
|
if response.status_code != 200:
|
|
return {}
|
|
# raise Exception(f"Error: {response}, response: {response.json()}, query: {query}, variables: {variables}")
|
|
return response.json()
|
|
|
|
def search_manga(self, search: str) -> list[Manga]:
|
|
variables = {"search": search, "format": "MANGA"}
|
|
response = self.request(REQUESTS_QUERY, variables)
|
|
# check if reponse has data Page and media
|
|
if not response.get("data", {}).get("Page", {}).get("media"):
|
|
return []
|
|
res = []
|
|
for manga in response["data"]["Page"]["media"]:
|
|
res.append(Manga(**manga))
|
|
return res
|
|
|
|
def get_manga(self, id: int) -> Manga:
|
|
assert isinstance(id, int), "id must be an integer"
|
|
variables = {"id": id}
|
|
response = self.request(MANGA_ID_QUERY, variables)
|
|
if not response.get("data", {}).get("Media"):
|
|
return None
|
|
return Manga(**response["data"]["Media"])
|
|
|
|
def kompage_search(self, search: str) -> list[Manga]:
|
|
variables = {"search": search}
|
|
response = self.request(REQUESTS_QUERY, variables)
|
|
# check if reponse has data Page and media
|
|
if not response.get("data", {}).get("Page", {}).get("media"):
|
|
return []
|
|
res = []
|
|
for manga in response["data"]["Page"]["media"]:
|
|
res.append(Manga(**manga))
|
|
return res
|
|
|
|
def get_genres(self) -> list[str]:
|
|
variables = {}
|
|
response = self.request(GENRES_QUERY, variables)
|
|
if not response.get("data", {}).get("genres"):
|
|
return []
|
|
res = []
|
|
for genre in response["data"]["genres"]:
|
|
res.append(genre)
|
|
return res
|
|
|
|
def get_tags(self):
|
|
variables = {}
|
|
response = self.request(TAGS_QUERY, variables)
|
|
if not response.get("data", {}).get("tags"):
|
|
return []
|
|
res = []
|
|
for tag in response["data"]["tags"]:
|
|
ctag = Tag(**tag)
|
|
res.append(ctag.name)
|
|
return res
|