maple/api/mangadex.py
2024-04-06 18:32:49 +02:00

92 lines
3.7 KiB
Python

import requests
def get_manga(mid: str) -> dict:
manga_req = requests.get(f"https://api.mangadex.org/manga/{mid}", params={
"includes[]": ["cover_art", "author", "artist"]
}, headers={"User-Agent": "Maplebot/1.0.0"})
print(manga_req.url)
if not manga_req.ok or manga_req.json()["result"] != "ok":
return {}
print(manga_req.json()["data"])
m = manga_req.json()["data"]
cover_art = None
art_list = [x for x in m["relationships"] if x["type"] == "cover_art"]
if len(art_list) > 0:
cover_art = art_list[0]["attributes"]["fileName"]
tags = [x["attributes"]["name"]["en"] for x in m["attributes"]["tags"]]
original_lang = "Japonština" if m["attributes"]["originalLanguage"] == "ja" else \
"Korejština" if m["attributes"]["originalLanguage"] == "ko" else \
"Čínština" if m["attributes"]["originalLanguage"] == "zh" else \
"Angličtina" if m["attributes"]["originalLanguage"] == "en" else "Jiný"
author = [x["attributes"]["name"] for x in m["relationships"] if x["type"] == "author"]
artist = [x["attributes"]["name"] for x in m["relationships"] if x["type"] == "artist"]
stats = requests.get(f"https://api.mangadex.org/statistics/manga/{m['id']}",
headers={"User-Agent": "Maplebot/1.0.0"})
comments, rating, follows, *other = stats.json()["statistics"][m["id"]].values()
m["cover"] = cover_art
m["tags"] = tags
m["originalLang"] = original_lang
m["artist"] = artist
m["author"] = author
m["rating"] = rating
m["follows"] = follows
return m
def get_chapter(cid: str) -> dict:
manga_req = requests.get(f"https://api.mangadex.org/chapter/{cid}", params={
"includes[]": ["manga", "scanlation_group"]
}, headers={"User-Agent": "Maplebot/1.0.0"})
print(manga_req.url)
if not manga_req.ok or manga_req.json()["result"] != "ok":
return {}
print(manga_req.json()["data"])
m = manga_req.json()["data"]
return m
def search_manga(title: str, content_rating: list[str]) -> dict:
manga_req = requests.get(f"https://api.mangadex.org/manga", params={
"title": title,
"contentRating[]": content_rating,
"limit": 1,
"includes[]": ["cover_art", "author", "artist"]
}, headers={"User-Agent": "Maplebot/1.0.0"})
print(manga_req.url)
if not manga_req.ok or manga_req.json()["result"] != "ok":
return {}
print(manga_req.json()["data"])
if len(manga_req.json()["data"]) == 0:
return {}
m = manga_req.json()["data"][0]
cover_art = None
art_list = [x for x in m["relationships"] if x["type"] == "cover_art"]
if len(art_list) > 0:
cover_art = art_list[0]["attributes"]["fileName"]
tags = [x["attributes"]["name"]["en"] for x in m["attributes"]["tags"]]
original_lang = "Japonština" if m["attributes"]["originalLanguage"] == "ja" else \
"Korejština" if m["attributes"]["originalLanguage"] == "ko" else \
"Čínština" if m["attributes"]["originalLanguage"] == "zh" else \
"Angličtina" if m["attributes"]["originalLanguage"] == "en" else "Jiný"
author = [x["attributes"]["name"] for x in m["relationships"] if x["type"] == "author"]
artist = [x["attributes"]["name"] for x in m["relationships"] if x["type"] == "artist"]
stats = requests.get(f"https://api.mangadex.org/statistics/manga/{m['id']}",
headers={"User-Agent": "Maplebot/1.0.0"})
comments, rating, follows, *other = stats.json()["statistics"][m["id"]].values()
m["cover"] = cover_art
m["tags"] = tags
m["originalLang"] = original_lang
m["artist"] = artist
m["author"] = author
m["rating"] = rating
m["follows"] = follows
return m