Add Gitea CI workflow and enhance AnilistAPI with new queries and data handling
- 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.
This commit is contained in:
@@ -1,12 +1,19 @@
|
||||
import requests
|
||||
from .schemas.manga import Manga
|
||||
from .queries.manga import MANGA_QUERY, MANGA_ID_QUERY
|
||||
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 = 1
|
||||
REQUEST_PERIOD = 2
|
||||
REQUEST_LIMIT = 90
|
||||
REQUEST_PERIOD = 60
|
||||
|
||||
|
||||
class AnilistAPI:
|
||||
@@ -19,14 +26,15 @@ class AnilistAPI:
|
||||
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}
|
||||
response = self.request(MANGA_QUERY, variables)
|
||||
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 []
|
||||
@@ -42,3 +50,35 @@ class AnilistAPI:
|
||||
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
|
||||
|
||||
@@ -3,6 +3,10 @@ query media($search: String) {
|
||||
Page {
|
||||
pageInfo {
|
||||
hasNextPage
|
||||
total
|
||||
perPage
|
||||
currentPage
|
||||
lastPage
|
||||
}
|
||||
media(type: MANGA, search: $search) {
|
||||
id
|
||||
@@ -52,3 +56,87 @@ Media (type: MANGA, id:$id) { # Insert our variables into the query arguments (i
|
||||
}
|
||||
}
|
||||
"""
|
||||
|
||||
REQUESTS_QUERY = """query query($search: String, $genres:[String], $tags:[String], $format: MediaFormat) {
|
||||
Page(perPage: 100) {
|
||||
pageInfo {
|
||||
hasNextPage
|
||||
total
|
||||
perPage
|
||||
currentPage
|
||||
lastPage
|
||||
}
|
||||
media(type: MANGA, search: $search, genre_in: $genres, tag_in: $tags, sort: SEARCH_MATCH, format: $format) {
|
||||
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
|
||||
siteUrl
|
||||
}
|
||||
|
||||
}
|
||||
}"""
|
||||
|
||||
REQUESTED_QUERY = """query query($search: Int) {
|
||||
Media(type: MANGA, id: $search, sort: SEARCH_MATCH) {
|
||||
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
|
||||
}
|
||||
|
||||
}"""
|
||||
|
||||
GENRES_QUERY = """query query{genres:GenreCollection}"""
|
||||
|
||||
TAGS_QUERY = """query query{tags:MediaTagCollection{name}}"""
|
||||
|
||||
@@ -2,7 +2,6 @@ from dataclasses import dataclass
|
||||
from .title import Title
|
||||
from .externalLinks import ExternalLinks
|
||||
from .tag import Tag
|
||||
from typing import Union, Any
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -14,14 +13,15 @@ class Manga:
|
||||
type: str = None
|
||||
format: str = None
|
||||
genres: list[str] = None
|
||||
tags: list | list[Tag] = None
|
||||
tags: list[Tag] = None
|
||||
description: str = None
|
||||
coverImage: dict = None
|
||||
coverImage: dict[str, str] = None
|
||||
isAdult: bool = False
|
||||
chapters: int = None
|
||||
volumes: int = None
|
||||
externalLinks: list | list[ExternalLinks] = None
|
||||
externalLinks: list[ExternalLinks] = None
|
||||
countryOfOrigin: str = None
|
||||
siteUrl: str = None
|
||||
|
||||
def __post_init__(self):
|
||||
self.title = Title(**self.title) if self.title else None
|
||||
|
||||
Reference in New Issue
Block a user