MangaBot/manager.py

102 lines
3.5 KiB
Python
Raw Normal View History

2024-07-23 17:31:23 +01:00
import discord
import json
2024-07-23 17:31:23 +01:00
import embed_util
import manga_api
2024-07-24 02:38:17 +01:00
import os.path
from util import parallel_downloads
2024-07-23 17:31:23 +01:00
class Manager:
2024-07-24 02:38:17 +01:00
def __init__(self, client, savefile: str = "manager.json"):
2024-07-23 17:42:36 +01:00
self.client: discord.Client = client
2024-07-23 17:31:23 +01:00
self.manga = {}
self.chapters = {}
2024-07-24 02:38:17 +01:00
self.savefile = savefile
2024-07-24 03:24:11 +01:00
self.load()
2024-07-24 22:19:38 +01:00
async def get_user_mangas(self, user: discord.User) -> list[manga_api.Manga]:
2024-07-24 02:38:17 +01:00
manga_ids = []
for manga_id in self.manga.keys():
if user.id in self.manga[manga_id]:
manga_ids.append(manga_id)
2024-07-24 22:19:38 +01:00
return [await manga_api.Manga.init(manga_id) for manga_id in manga_ids]
2024-07-23 17:31:23 +01:00
def add_user_to_manga(self, user: discord.User, manga: manga_api.Manga) -> None:
2024-07-23 17:42:36 +01:00
if manga.id in self.manga.keys():
self.manga[manga.id].append(user.id)
else:
self.manga[manga.id] = [user.id]
2024-07-23 17:31:23 +01:00
def remove_user_from_manga(self, user: discord.User, manga: manga_api.Manga) -> None:
2024-07-23 17:42:36 +01:00
if manga.id in self.manga.keys():
if user.id in self.manga[manga.id]:
self.manga[manga.id].remove(user.id)
2024-07-24 22:19:38 +01:00
if len(self.manga[manga.id]) == 0:
self.manga.pop(manga.id)
2024-07-23 17:42:36 +01:00
else:
self.manga[manga.id] = []
2024-07-23 17:31:23 +01:00
async def update(self):
for manga_id in self.manga.keys():
2024-07-24 22:19:38 +01:00
manga = await manga_api.Manga.init(manga_id)
new_chap = await self.check_for_new_chapter(manga)
2024-07-23 17:31:23 +01:00
if new_chap is not None:
users = self.manga[manga_id]
print(users)
for userid in users:
await self.send_message_to_user(await self.client.fetch_user(userid), manga, new_chap)
self.save()
2024-07-23 17:31:23 +01:00
2024-07-24 22:19:38 +01:00
async def check_for_new_chapter(self, manga: manga_api.Manga) -> manga_api.Chapter | None:
latest_chap = await manga.get_latest_chap()
2024-07-24 03:24:11 +01:00
print("Comparing Chapters...")
if manga.id not in self.chapters.keys():
self.chapters[manga.id] = latest_chap.id
2024-07-23 17:31:23 +01:00
return latest_chap
2024-07-24 22:19:38 +01:00
old_chap = await manga_api.Chapter.init(self.chapters[manga.id])
2024-07-24 03:24:11 +01:00
print(f"Latest Chap ID: {latest_chap.id}, Old Chap ID: {self.chapters[manga.id]}")
print(f"Latest Chap: {latest_chap.get_volume()}:{latest_chap.get_number()}")
print(f"Latest Chap: {old_chap.get_volume()}:{old_chap.get_number()}")
if latest_chap.is_more_recent_than(old_chap):
self.chapters[manga.id] = latest_chap.id
2024-07-24 03:24:11 +01:00
print("New Latest Chap!")
2024-07-23 17:31:23 +01:00
return latest_chap
else:
2024-07-24 03:24:11 +01:00
print("No New Chapter.")
2024-07-23 17:31:23 +01:00
return None
async def send_message_to_user(self, user: discord.User, manga: manga_api.Manga,
chapter: manga_api.Chapter) -> None:
2024-07-23 17:31:23 +01:00
dm_channel = await user.create_dm()
await dm_channel.send(
embed=embed_util.chapter_embed(manga, chapter),
files=[parallel_downloads.discord_file_from_filename(filename) for filename in
embed_util.get_chapter_filenames(manga)]
)
def save(self):
2024-07-24 02:38:17 +01:00
with open(self.savefile, "w") as f:
json.dump(self.to_dict(), f, indent=4)
def load(self):
if os.path.isfile(self.savefile):
with open(self.savefile, "r") as f:
self.from_dict(json.load(f))
def from_dict(self, data: dict):
self.manga = data["manga"]
self.chapters = data["chapters"]
def to_dict(self):
return {
"manga": self.manga,
"chapters": self.chapters
}