attempt to fix images

This commit is contained in:
franchioping 2024-07-23 02:42:08 +01:00
parent c3e852b699
commit 7b26080121
4 changed files with 32 additions and 20 deletions

View File

@ -1,5 +1,8 @@
from manga import Manga, Chapter from manga import Manga, Chapter
import discord import discord
import os.path
import requests
class ListManga(discord.ui.View): class ListManga(discord.ui.View):
@ -11,7 +14,8 @@ class ListManga(discord.ui.View):
async def print_manga(self, interaction: discord.Interaction): async def print_manga(self, interaction: discord.Interaction):
await interaction.response.defer() 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) @discord.ui.button(label='Prev', style=discord.ButtonStyle.grey)
async def previous(self, interaction: discord.Interaction, button: discord.ui.Button): async def previous(self, interaction: discord.Interaction, button: discord.ui.Button):
@ -46,7 +50,20 @@ class ListManga(discord.ui.View):
self.stop() 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): def manga_embed(manga: Manga):
e = discord.Embed(title=manga.get_title(), description=manga.get_description(), url=manga.get_url()) 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 return e

17
main.py
View File

@ -7,16 +7,15 @@ from dotenv import load_dotenv
import embed_util import embed_util
import manga import manga
mh = manga.MangaHandler() mh = manga.MangaHandler()
load_dotenv() load_dotenv()
intents = discord.Intents.default() intents = discord.Intents.default()
client = discord.Client(intents=intents) client = discord.Client(intents=intents)
tree = app_commands.CommandTree(client) tree = app_commands.CommandTree(client)
@tree.command( @tree.command(
name="ping", name="ping",
description="My first application Command", description="My first application Command",
@ -26,6 +25,7 @@ async def first_command(interaction: discord.Interaction):
chanel = await interaction.user.create_dm() chanel = await interaction.user.create_dm()
await chanel.send("Hi") await chanel.send("Hi")
@tree.command( @tree.command(
name="search", name="search",
description="Search for manga to follow", description="Search for manga to follow",
@ -42,22 +42,11 @@ async def search_command(
manga_list = mh.search(title) manga_list = mh.search(title)
view = embed_util.ListManga(manga_list) view = embed_util.ListManga(manga_list)
await chanel.send(f"Hey, you searched for {title}") 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 view.wait()
await chanel.send("Done") 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 @client.event
async def on_ready(): async def on_ready():
await tree.sync(guild=discord.Object(id=1042133536926347324)) await tree.sync(guild=discord.Object(id=1042133536926347324))

View File

@ -48,7 +48,10 @@ class Manga:
self.data = r.json()["data"] self.data = r.json()["data"]
def get_title(self) -> str: 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: def get_latest_chap(self) -> Chapter:
return Chapter(self.data["attributes"]["latestUploadedChapter"]) return Chapter(self.data["attributes"]["latestUploadedChapter"])
@ -63,14 +66,17 @@ class Manga:
r = requests.get(f"{self.base_url}/cover/{cover_art_id}") r = requests.get(f"{self.base_url}/cover/{cover_art_id}")
print( r.json()["data"]) print( r.json()["data"])
cover_id = r.json()["data"]["attributes"]["fileName"] cover_fileName = r.json()["data"]["attributes"]["fileName"]
return f"https://mangadex.org/covers/{self.id}/{cover_id}" cover_extension = cover_fileName.split(".")[1]
return f"https://mangadex.org/covers/{self.id}/{cover_fileName}.256.{cover_extension}"
def get_description(self) -> str: def get_description(self) -> str:
if "en" in self.data["attributes"]["description"].keys(): if "en" in self.data["attributes"]["description"].keys():
return self.data["attributes"]["description"]["en"] return self.data["attributes"]["description"]["en"]
else: 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: def get_url(self) -> str:
return f"https://mangadex.org/title/{self.id}" return f"https://mangadex.org/title/{self.id}"

0
user_manager.py Normal file
View File