From 3b63d7188200787156279759ed8c47d2912b3699 Mon Sep 17 00:00:00 2001 From: franchioping Date: Tue, 23 Jul 2024 17:31:23 +0100 Subject: [PATCH] attempt --- embed_util.py | 16 ++++++++++++++-- main.py | 21 ++++++++++++++++---- manager.py | 39 ++++++++++++++++++++++++++++++++++++++ manga.py => manga_api.py | 0 user_manager.py | 0 util/parallel_downloads.py | 2 +- 6 files changed, 71 insertions(+), 7 deletions(-) create mode 100644 manager.py rename manga.py => manga_api.py (100%) delete mode 100644 user_manager.py diff --git a/embed_util.py b/embed_util.py index 8a6f3fb..badc11d 100644 --- a/embed_util.py +++ b/embed_util.py @@ -1,4 +1,4 @@ -from manga import Manga, Chapter +from manga_api import Manga, Chapter import discord import os.path @@ -61,7 +61,6 @@ class ListManga(discord.ui.View): def gen_manga_files(manga_list: list[Manga]): - return parallel_downloads.parallel_download(manga_list) @@ -71,3 +70,16 @@ def manga_embed(manga: Manga): e.set_thumbnail(url=f"attachment://{manga.id}.{extension}") return e + + +def get_chapter_files(manga: Manga): + return parallel_downloads.parallel_download([manga]) + + +def chapter_embed(manga: Manga, chapter: Chapter): + e = discord.Embed( + title=f'New "{manga.get_title()}" Chapter Released!', + description=f"Chapter {chapter.get_chapter_num()} of {manga.get_title()} Released." + f"Go read it now!" + ) + e.set_thumbnail(url=f"attachment://{manga.id}.{manga.get_cover_art_extension()}") diff --git a/main.py b/main.py index 7f13a83..73ea6f1 100644 --- a/main.py +++ b/main.py @@ -5,9 +5,11 @@ from discord import app_commands from dotenv import load_dotenv import embed_util -import manga +import manager +import manga_api -mh = manga.MangaHandler() +mh = manga_api.MangaHandler() +man = manager.Manager() load_dotenv() @@ -44,9 +46,20 @@ async def search_command( await chanel.send(f"Hey, you searched for {title}") msg = await chanel.send(view=view, embed=embed_util.manga_embed(manga_list[0])) await view.force_reload(msg) - ret = await view.wait() - print(ret) + await view.wait() + + ret: list[dict] = view.ret + for action in ret: + if action["action"] == 1: + manga_id = action["manga"] + man.add_user_to_manga(interaction.user, manga_api.Manga(manga_id)) + if action["action"] == -1: + manga_id = action["manga"] + man.remove_user_from_manga(interaction.user, manga_api.Manga(manga_id)) + + await chanel.send("Done") + await man.update() @client.event diff --git a/manager.py b/manager.py new file mode 100644 index 0000000..e771f3d --- /dev/null +++ b/manager.py @@ -0,0 +1,39 @@ +import discord + +import embed_util +import manga_api + +class Manager: + def __init__(self): + self.manga = {} + self.chapters = {} + + def add_user_to_manga(self, user: discord.User, manga: manga_api.Manga) -> None: + self.manga[manga.id].append(user.id) + + def remove_user_from_manga(self, user: discord.User, manga: manga_api.Manga) -> None: + self.manga[manga.id].remove(user.id) + + async def update(self): + for manga_id in self.manga.keys(): + manga = manga_api.Manga(manga_id) + new_chap = self.check_for_new_chapter(manga) + if new_chap is not None: + users = self.manga[manga_id] + for user in users: + await self.send_message_to_user(user, manga, new_chap) + + def check_for_new_chapter(self, manga: manga_api.Manga) -> manga_api.Chapter | None: + latest_chap = manga.get_latest_chap() + if latest_chap not in self.chapters.keys(): + self.chapters[manga.id] = latest_chap + return latest_chap + if float(latest_chap.get_chapter_num()) > self.chapters[manga.id]: + self.chapters[manga.id] = latest_chap + return latest_chap + else: + return None + + async def send_message_to_user(self, user: discord.User, manga: manga_api.Manga, chapter: manga_api.Chapter) -> None: + dm_channel = await user.create_dm() + await dm_channel.send(embed=embed_util.chapter_embed(manga, chapter), files=embed_util.get_chapter_files(manga)) diff --git a/manga.py b/manga_api.py similarity index 100% rename from manga.py rename to manga_api.py diff --git a/user_manager.py b/user_manager.py deleted file mode 100644 index e69de29..0000000 diff --git a/util/parallel_downloads.py b/util/parallel_downloads.py index af576d4..6a5747d 100644 --- a/util/parallel_downloads.py +++ b/util/parallel_downloads.py @@ -4,7 +4,7 @@ import os.path import discord -from manga import Manga +from manga_api import Manga def download_file(manga: Manga):