From 7b26080121ef9c0a6fd6cd54cc8b6159d71a3bd2 Mon Sep 17 00:00:00 2001 From: franchioping Date: Tue, 23 Jul 2024 02:42:08 +0100 Subject: [PATCH] attempt to fix images --- embed_util.py | 21 +++++++++++++++++++-- main.py | 17 +++-------------- manga.py | 14 ++++++++++---- user_manager.py | 0 4 files changed, 32 insertions(+), 20 deletions(-) create mode 100644 user_manager.py diff --git a/embed_util.py b/embed_util.py index 3bf70e7..ce5d842 100644 --- a/embed_util.py +++ b/embed_util.py @@ -1,5 +1,8 @@ from manga import Manga, Chapter import discord +import os.path + +import requests class ListManga(discord.ui.View): @@ -11,7 +14,8 @@ class ListManga(discord.ui.View): async def print_manga(self, interaction: discord.Interaction): await interaction.response.defer() - await interaction.message.edit(embed=manga_embed(self.manga_list[self.index])) + await interaction.message.edit( + embed=manga_embed(self.manga_list[self.index])) @discord.ui.button(label='Prev', style=discord.ButtonStyle.grey) async def previous(self, interaction: discord.Interaction, button: discord.ui.Button): @@ -46,7 +50,20 @@ class ListManga(discord.ui.View): self.stop() +def gen_manga_files(mangaList: list[Manga]): + ret = [] + for manga in mangaList: + if not os.path.isfile(f'tmp/{manga.id}.jpg'): + img_data = requests.get(manga.get_cover_art_url()).content + with open(f'tmp/{manga.id}.jpg', 'wb') as handler: + handler.write(img_data) + ret.append(discord.File(f"tmp/{manga.id}.jpg", f"{manga.id}.jpg")) + return ret + + def manga_embed(manga: Manga): e = discord.Embed(title=manga.get_title(), description=manga.get_description(), url=manga.get_url()) - e.set_thumbnail(url=manga.get_cover_art_url()) + + e.set_thumbnail(url=f"attachment://{manga.id}.jpg") + return e diff --git a/main.py b/main.py index 7a4c64d..71d9660 100644 --- a/main.py +++ b/main.py @@ -7,16 +7,15 @@ from dotenv import load_dotenv import embed_util import manga - mh = manga.MangaHandler() load_dotenv() - intents = discord.Intents.default() client = discord.Client(intents=intents) tree = app_commands.CommandTree(client) + @tree.command( name="ping", description="My first application Command", @@ -26,6 +25,7 @@ async def first_command(interaction: discord.Interaction): chanel = await interaction.user.create_dm() await chanel.send("Hi") + @tree.command( name="search", description="Search for manga to follow", @@ -42,22 +42,11 @@ async def search_command( manga_list = mh.search(title) view = embed_util.ListManga(manga_list) await chanel.send(f"Hey, you searched for {title}") - await chanel.send(view=view, embed=embed_util.manga_embed(manga_list[0])) + await chanel.send(view=view, embed=embed_util.manga_embed(manga_list[0]), files=embed_util.gen_manga_files(manga_list)) await view.wait() await chanel.send("Done") - - -@client.event -async def on_message(message: discord.Message): - if isinstance(message.channel, discord.DMChannel): - user = message.author - channel = message.channel - - - - @client.event async def on_ready(): await tree.sync(guild=discord.Object(id=1042133536926347324)) diff --git a/manga.py b/manga.py index 7ef1105..d65e620 100644 --- a/manga.py +++ b/manga.py @@ -48,7 +48,10 @@ class Manga: self.data = r.json()["data"] def get_title(self) -> str: - return self.data["attributes"]["title"]["en"] + try: + return self.data["attributes"]["title"]["en"] + except KeyError: + return self.data["attributes"]["title"][0] def get_latest_chap(self) -> Chapter: return Chapter(self.data["attributes"]["latestUploadedChapter"]) @@ -63,14 +66,17 @@ class Manga: 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}" + cover_fileName = r.json()["data"]["attributes"]["fileName"] + cover_extension = cover_fileName.split(".")[1] + return f"https://mangadex.org/covers/{self.id}/{cover_fileName}.256.{cover_extension}" 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]] + if list(self.data["attributes"]["description"].keys()) > 0: + return self.data["attributes"]["description"][list(self.data["attributes"]["description"].keys())[0]] + return "" def get_url(self) -> str: return f"https://mangadex.org/title/{self.id}" diff --git a/user_manager.py b/user_manager.py new file mode 100644 index 0000000..e69de29