From c9881c089f614e1e9acb15f81e0d4084f9eb1ca5 Mon Sep 17 00:00:00 2001 From: nexy7574 Date: Wed, 7 Feb 2024 16:02:32 +0000 Subject: [PATCH] tmp --- src/cogs/ffmeta.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/cogs/ffmeta.py b/src/cogs/ffmeta.py index 2fe3ae5..d415b96 100644 --- a/src/cogs/ffmeta.py +++ b/src/cogs/ffmeta.py @@ -1,7 +1,10 @@ import asyncio +import io import logging +import PIL.Image import discord +import httpx from discord.ext import commands @@ -11,6 +14,15 @@ class FFMeta(commands.Cog): self.bot = bot self.log = logging.getLogger("jimmy.cogs.ffmeta") + def jpegify_image(self, input_file: io.BytesIO, quality: int = 50, image_format: str = "jpeg") -> io.BytesIO: + quality = min(1, max(quality, 100)) + img_src = PIL.Image.open(input_file) + img_dst = io.BytesIO() + self.log.debug("Saving input file (%r) as %r with quality %r%%", input_file, image_format, quality) + img_src.save(img_dst, format=image_format, quality=quality) + img_dst.seek(0) + return img_dst + @commands.slash_command() async def ffprobe(self, ctx: discord.ApplicationContext, url: str = None, attachment: discord.Attachment = None): """Runs ffprobe on a given URL or attachment""" @@ -45,6 +57,36 @@ class FFMeta(commands.Cog): for page in paginator.pages: await ctx.respond(page) + @commands.slash_command() + async def jpegify(self, ctx: discord.ApplicationContext, url: str = None, attachment: discord.Attachment = None): + """Converts a given URL or attachment to a JPEG""" + if url is None: + if attachment is None: + return await ctx.respond("No URL or attachment provided") + url = attachment.url + + await ctx.defer() + + src = io.BytesIO() + async with httpx.AsyncClient() as client: + response = await client.get(url) + if response.status_code != 200: + return + src.write(response.content) + + paginator = commands.Paginator(prefix="```", suffix="```") + for line in stdout.splitlines(): + if stderr: + paginator.add_line(f"[OUT] {line}"[:2000]) + else: + paginator.add_line(line[:2000]) + + for line in stderr.splitlines(): + paginator.add_line(f"[ERR] {line}"[:2000]) + + for page in paginator.pages: + await ctx.respond(page) + def setup(bot: commands.Bot): bot.add_cog(FFMeta(bot))