update code
This commit is contained in:
@@ -2,4 +2,5 @@ from importlib.metadata import version
|
||||
|
||||
__version__ = version("anilistapi")
|
||||
__contact__ = "coding_contact@pm.me"
|
||||
from .api import AnilistAPI
|
||||
__all__ = ["AnilistAPI", "MediaFormat"]
|
||||
from .api import AnilistAPI, MediaFormat
|
||||
|
||||
@@ -1,19 +1,37 @@
|
||||
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
|
||||
from enum import Enum
|
||||
|
||||
REQUEST_LIMIT = 90
|
||||
REQUEST_PERIOD = 60
|
||||
import requests
|
||||
from limit import limit
|
||||
|
||||
from anilistapi import __contact__, __version__
|
||||
|
||||
from .queries.manga import (
|
||||
GENRES_QUERY,
|
||||
ID_QUERY,
|
||||
REQUESTS_QUERY,
|
||||
TAGS_QUERY,
|
||||
)
|
||||
from .schemas.manga import Manga, Tag
|
||||
|
||||
REQUEST_LIMIT = 1
|
||||
REQUEST_PERIOD = 1
|
||||
|
||||
|
||||
class MediaFormat(str, Enum):
|
||||
MANGA = "MANGA"
|
||||
NOVEL = "NOVEL"
|
||||
ONE_SHOT = "ONE_SHOT"
|
||||
TV = "TV"
|
||||
TV_SHORT = "TV_SHORT"
|
||||
MOVIE = "MOVIE"
|
||||
OVA = "OVA"
|
||||
ONA = "ONA"
|
||||
MUSIC = "MUSIC"
|
||||
SPECIAL = "SPECIAL"
|
||||
|
||||
# add a function to get
|
||||
|
||||
|
||||
class AnilistAPI:
|
||||
@@ -25,16 +43,24 @@ class AnilistAPI:
|
||||
@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:
|
||||
try:
|
||||
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()
|
||||
except:
|
||||
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)
|
||||
def search_manga(self, search: str, format: MediaFormat) -> list[Manga]:
|
||||
variables = {"search": search, "format": format, "type": "MANGA"}
|
||||
try:
|
||||
response = self.request(REQUESTS_QUERY, variables)
|
||||
except:
|
||||
time.sleep(15)
|
||||
return self.search_manga(search, format)
|
||||
|
||||
# check if reponse has data Page and media
|
||||
if not response.get("data", {}).get("Page", {}).get("media"):
|
||||
return []
|
||||
@@ -44,15 +70,17 @@ class AnilistAPI:
|
||||
return res
|
||||
|
||||
def get_manga(self, id: int) -> Manga:
|
||||
if str(id).isnumeric():
|
||||
id = int(id)
|
||||
assert isinstance(id, int), "id must be an integer"
|
||||
variables = {"id": id}
|
||||
response = self.request(MANGA_ID_QUERY, variables)
|
||||
response = self.request(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}
|
||||
def kompage_search(self, search: str, format: MediaFormat) -> list[Manga]:
|
||||
variables = {"search": search, "format": format, "type": "MANGA"}
|
||||
response = self.request(REQUESTS_QUERY, variables)
|
||||
# check if reponse has data Page and media
|
||||
if not response.get("data", {}).get("Page", {}).get("media"):
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
MANGA_QUERY = """
|
||||
query media($search: String) {
|
||||
QUERY = """
|
||||
query media($search: String, $type: MediaType) {
|
||||
Page {
|
||||
pageInfo {
|
||||
hasNextPage
|
||||
@@ -8,7 +8,7 @@ Page {
|
||||
currentPage
|
||||
lastPage
|
||||
}
|
||||
media(type: MANGA, search: $search) {
|
||||
media(type: $type, search: $search) {
|
||||
id
|
||||
title {
|
||||
romaji
|
||||
@@ -21,9 +21,9 @@ Page {
|
||||
}
|
||||
"""
|
||||
|
||||
MANGA_ID_QUERY = """
|
||||
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)
|
||||
Media (id:$id) { # Insert our variables into the query arguments (id)
|
||||
id
|
||||
title {
|
||||
romaji
|
||||
@@ -53,11 +53,12 @@ Media (type: MANGA, id:$id) { # Insert our variables into the query arguments (i
|
||||
type
|
||||
}
|
||||
countryOfOrigin
|
||||
siteUrl
|
||||
}
|
||||
}
|
||||
"""
|
||||
|
||||
REQUESTS_QUERY = """query query($search: String, $genres:[String], $tags:[String], $format: MediaFormat) {
|
||||
REQUESTS_QUERY = """query query($search: String, $genres:[String], $tags:[String], $format: MediaFormat, $type: MediaType) {
|
||||
Page(perPage: 100) {
|
||||
pageInfo {
|
||||
hasNextPage
|
||||
@@ -66,7 +67,7 @@ Page(perPage: 100) {
|
||||
currentPage
|
||||
lastPage
|
||||
}
|
||||
media(type: MANGA, search: $search, genre_in: $genres, tag_in: $tags, sort: SEARCH_MATCH, format: $format) {
|
||||
media(type: $type, search: $search, genre_in: $genres, tag_in: $tags, sort: SEARCH_MATCH, format: $format) {
|
||||
id
|
||||
title {
|
||||
romaji
|
||||
@@ -102,8 +103,8 @@ Page(perPage: 100) {
|
||||
}
|
||||
}"""
|
||||
|
||||
REQUESTED_QUERY = """query query($search: Int) {
|
||||
Media(type: MANGA, id: $search, sort: SEARCH_MATCH) {
|
||||
REQUESTED_QUERY = """query query($search: Int, $type: MediaType) {
|
||||
Media(type: $type, id: $search, sort: SEARCH_MATCH) {
|
||||
id
|
||||
title {
|
||||
romaji
|
||||
|
||||
@@ -44,3 +44,7 @@ class Manga:
|
||||
@property
|
||||
def isLightNovel(self):
|
||||
return self.format == "NOVEL"
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
return self.title.__repr__()
|
||||
|
||||
@@ -8,10 +8,11 @@ class Title:
|
||||
native: str = None
|
||||
|
||||
def __repr__(self):
|
||||
return self.english if self.english else self.native
|
||||
return self.english if self.english else self.romaji if self.romaji else self.native
|
||||
|
||||
|
||||
def __str__(self):
|
||||
return self.english if self.english else self.native
|
||||
return self.english if self.english else self.romaji if self.romaji else self.native
|
||||
|
||||
@property
|
||||
def alternateTitles(self):
|
||||
|
||||
Reference in New Issue
Block a user