initial commit
This commit is contained in:
3
src/anilistapi/__init__.py
Normal file
3
src/anilistapi/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
__version__ = "0.1.3"
|
||||
__contact__ = "coding_contact@pm.me"
|
||||
from .api import AnilistAPI
|
||||
44
src/anilistapi/api.py
Normal file
44
src/anilistapi/api.py
Normal file
@@ -0,0 +1,44 @@
|
||||
import requests
|
||||
from .schemas.manga import Manga
|
||||
from .queries.manga import MANGA_QUERY, MANGA_ID_QUERY
|
||||
from limit import limit
|
||||
from anilistapi import __version__, __contact__
|
||||
import os
|
||||
|
||||
REQUEST_LIMIT = 1
|
||||
REQUEST_PERIOD = 2
|
||||
|
||||
|
||||
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})
|
||||
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}
|
||||
response = self.request(MANGA_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"])
|
||||
0
src/anilistapi/py.typed
Normal file
0
src/anilistapi/py.typed
Normal file
0
src/anilistapi/queries/__init__.py
Normal file
0
src/anilistapi/queries/__init__.py
Normal file
54
src/anilistapi/queries/manga.py
Normal file
54
src/anilistapi/queries/manga.py
Normal file
@@ -0,0 +1,54 @@
|
||||
MANGA_QUERY = """
|
||||
query media($search: String) {
|
||||
Page {
|
||||
pageInfo {
|
||||
hasNextPage
|
||||
}
|
||||
media(type: MANGA, search: $search) {
|
||||
id
|
||||
title {
|
||||
romaji
|
||||
english
|
||||
native
|
||||
}
|
||||
synonyms
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
|
||||
MANGA_ID_QUERY = """
|
||||
query media($id: Int) { # Define which variables will be used in the query (id)
|
||||
Media (type: MANGA, id:$id) { # Insert our variables into the query arguments (id)
|
||||
id
|
||||
title {
|
||||
romaji
|
||||
english
|
||||
native
|
||||
}
|
||||
synonyms
|
||||
format
|
||||
type
|
||||
status(version:2)
|
||||
genres
|
||||
tags{
|
||||
name
|
||||
isAdult
|
||||
}
|
||||
description
|
||||
coverImage {
|
||||
large
|
||||
|
||||
}
|
||||
isAdult
|
||||
chapters
|
||||
volumes
|
||||
externalLinks {
|
||||
site
|
||||
url
|
||||
type
|
||||
}
|
||||
countryOfOrigin
|
||||
}
|
||||
}
|
||||
"""
|
||||
0
src/anilistapi/schemas/__init__.py
Normal file
0
src/anilistapi/schemas/__init__.py
Normal file
8
src/anilistapi/schemas/externalLinks.py
Normal file
8
src/anilistapi/schemas/externalLinks.py
Normal file
@@ -0,0 +1,8 @@
|
||||
from dataclasses import dataclass
|
||||
|
||||
|
||||
@dataclass
|
||||
class ExternalLinks:
|
||||
url: str = None
|
||||
type: str = None
|
||||
site: str = None
|
||||
46
src/anilistapi/schemas/manga.py
Normal file
46
src/anilistapi/schemas/manga.py
Normal file
@@ -0,0 +1,46 @@
|
||||
from dataclasses import dataclass
|
||||
from .title import Title
|
||||
from .externalLinks import ExternalLinks
|
||||
from .tag import Tag
|
||||
from typing import Union, Any
|
||||
|
||||
|
||||
@dataclass
|
||||
class Manga:
|
||||
id: int = None
|
||||
title: Title = None
|
||||
status: str = None
|
||||
synonyms: list = None
|
||||
type: str = None
|
||||
format: str = None
|
||||
genres: list[str] = None
|
||||
tags: list | list[Tag] = None
|
||||
description: str = None
|
||||
coverImage: dict = None
|
||||
isAdult: bool = False
|
||||
chapters: int = None
|
||||
volumes: int = None
|
||||
externalLinks: list | list[ExternalLinks] = None
|
||||
countryOfOrigin: str = None
|
||||
|
||||
def __post_init__(self):
|
||||
self.title = Title(**self.title) if self.title else None
|
||||
self.externalLinks = (
|
||||
[ExternalLinks(**link) for link in self.externalLinks]
|
||||
if self.externalLinks
|
||||
else None
|
||||
)
|
||||
self.tags = [Tag(**tag) for tag in self.tags] if self.tags else None
|
||||
self.description = self.description if self.description else None
|
||||
|
||||
@property
|
||||
def url(self):
|
||||
return self.externalLinks[0].url
|
||||
|
||||
@property
|
||||
def isManga(self):
|
||||
return self.format == "MANGA"
|
||||
|
||||
@property
|
||||
def isLightNovel(self):
|
||||
return self.format == "NOVEL"
|
||||
20
src/anilistapi/schemas/tag.py
Normal file
20
src/anilistapi/schemas/tag.py
Normal file
@@ -0,0 +1,20 @@
|
||||
from dataclasses import dataclass
|
||||
|
||||
|
||||
@dataclass
|
||||
class Tag:
|
||||
userID: int = None
|
||||
rank: int = None
|
||||
name: str = None
|
||||
isMediaSpoiler: bool = False
|
||||
isGeneralSpoiler: bool = False
|
||||
isAdult: bool = False
|
||||
id: int = None
|
||||
description: str = None
|
||||
category: str = None
|
||||
|
||||
def __repr__(self):
|
||||
return self.name
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
26
src/anilistapi/schemas/title.py
Normal file
26
src/anilistapi/schemas/title.py
Normal file
@@ -0,0 +1,26 @@
|
||||
from dataclasses import dataclass
|
||||
|
||||
|
||||
@dataclass
|
||||
class Title:
|
||||
romaji: str = None
|
||||
english: str = None
|
||||
native: str = None
|
||||
|
||||
def __repr__(self):
|
||||
return self.english if self.english else self.native
|
||||
|
||||
def __str__(self):
|
||||
return self.english if self.english else self.native
|
||||
|
||||
@property
|
||||
def alternateTitles(self):
|
||||
return [
|
||||
{"romaji": self.romaji},
|
||||
{"english": self.english},
|
||||
{"native": self.native},
|
||||
]
|
||||
|
||||
@property
|
||||
def titles(self):
|
||||
return
|
||||
Reference in New Issue
Block a user