2024-07-22 22:05:38 +01:00
|
|
|
import os
|
|
|
|
|
from dotenv import load_dotenv
|
2024-07-22 23:01:29 +01:00
|
|
|
import requests
|
2024-07-22 22:05:38 +01:00
|
|
|
|
|
|
|
|
load_dotenv()
|
|
|
|
|
|
2024-07-22 23:01:29 +01:00
|
|
|
base_url = "https://api.mangadex.org"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Chapter:
|
|
|
|
|
def __init__(self, id: str, _base_url=base_url):
|
|
|
|
|
self.id = id
|
|
|
|
|
self.base_url = _base_url
|
2024-07-23 00:32:44 +01:00
|
|
|
self.data = None
|
2024-07-22 23:01:29 +01:00
|
|
|
|
2024-07-23 00:32:44 +01:00
|
|
|
self.update_data()
|
|
|
|
|
|
|
|
|
|
def update_data(self):
|
2024-07-22 23:01:29 +01:00
|
|
|
r = requests.get(
|
|
|
|
|
f"{self.base_url}/chapter/{self.id}"
|
|
|
|
|
)
|
2024-07-23 00:32:44 +01:00
|
|
|
self.data = r.json()["data"]
|
2024-07-22 23:01:29 +01:00
|
|
|
|
|
|
|
|
def get_chapter_num(self) -> str:
|
2024-07-23 00:32:44 +01:00
|
|
|
return self.data["attributes"]["chapter"]
|
2024-07-22 23:01:29 +01:00
|
|
|
|
|
|
|
|
def get_chapter_url(self) -> str:
|
2024-07-23 00:32:44 +01:00
|
|
|
return self.data["attributes"]["readableAt"]
|
2024-07-22 23:01:29 +01:00
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
|
return str({
|
|
|
|
|
"chapter_num": self.get_chapter_num(),
|
|
|
|
|
})
|
2024-07-22 22:05:38 +01:00
|
|
|
|
|
|
|
|
|
2024-07-22 23:01:29 +01:00
|
|
|
class Manga:
|
|
|
|
|
def __init__(self, id: str, _base_url=base_url):
|
|
|
|
|
self.id = id
|
|
|
|
|
self.base_url = _base_url
|
2024-07-23 00:32:44 +01:00
|
|
|
self.data = None
|
|
|
|
|
|
|
|
|
|
self.update_data()
|
2024-07-22 23:01:29 +01:00
|
|
|
|
2024-07-23 00:32:44 +01:00
|
|
|
def update_data(self):
|
2024-07-22 23:01:29 +01:00
|
|
|
r = requests.get(
|
|
|
|
|
f"{self.base_url}/manga/{self.id}"
|
|
|
|
|
)
|
2024-07-23 00:32:44 +01:00
|
|
|
self.data = r.json()["data"]
|
2024-07-22 23:01:29 +01:00
|
|
|
|
|
|
|
|
def get_title(self) -> str:
|
2024-07-23 00:54:59 +01:00
|
|
|
return self.data["attributes"]["title"]["en"]
|
2024-07-22 23:01:29 +01:00
|
|
|
|
|
|
|
|
def get_latest_chap(self) -> Chapter:
|
2024-07-23 00:32:44 +01:00
|
|
|
return Chapter(self.data["attributes"]["latestUploadedChapter"])
|
|
|
|
|
|
|
|
|
|
def get_cover_art_url(self):
|
|
|
|
|
cover_art_id = ""
|
|
|
|
|
for relation in self.data["relationships"]:
|
|
|
|
|
if relation["type"] == "cover_art":
|
|
|
|
|
cover_art_id = relation["id"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
r = requests.get(f"{self.base_url}/cover/{cover_art_id}")
|
|
|
|
|
print( r.json()["data"])
|
|
|
|
|
cover_id = r.json()["data"]["attributes"]["fileName"]
|
|
|
|
|
return f"https://mangadex.org/covers/{self.id}/{cover_id}"
|
|
|
|
|
|
|
|
|
|
def get_description(self) -> str:
|
|
|
|
|
if "en" in self.data["attributes"]["description"].keys():
|
|
|
|
|
return self.data["attributes"]["description"]["en"]
|
|
|
|
|
else:
|
|
|
|
|
return self.data["attributes"]["description"][self.data["attributes"]["description"].keys()[0]]
|
|
|
|
|
|
|
|
|
|
def get_url(self) -> str:
|
|
|
|
|
return f"https://mangadex.org/title/{self.id}"
|
2024-07-22 23:01:29 +01:00
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
|
return str({
|
|
|
|
|
"title": self.get_title(),
|
|
|
|
|
"latest_chapter": str(self.get_latest_chap())
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MangaHandler:
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.base_url = base_url
|
2024-07-22 22:05:38 +01:00
|
|
|
|
2024-07-22 23:01:29 +01:00
|
|
|
def search(self, title: str) -> list[Manga]:
|
|
|
|
|
r = requests.get(
|
|
|
|
|
f"{self.base_url}/manga",
|
|
|
|
|
params={"title": title}
|
|
|
|
|
)
|
|
|
|
|
data = r.json()["data"]
|
|
|
|
|
manga_list: list[Manga] = []
|
|
|
|
|
for manga in data:
|
|
|
|
|
manga_id = manga["id"]
|
|
|
|
|
manga_list.append(Manga(manga_id))
|
2024-07-22 22:29:18 +01:00
|
|
|
|
2024-07-22 23:01:29 +01:00
|
|
|
return manga_list
|
2024-07-22 22:29:18 +01:00
|
|
|
|
2024-07-22 22:05:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2024-07-22 23:01:29 +01:00
|
|
|
mh = MangaHandler()
|
2024-07-23 00:32:44 +01:00
|
|
|
print(mh.search("Release That Witch")[0].get_url())
|