[Latex] Initial commit

This commit is contained in:
aikaterna
2020-08-31 14:25:34 -04:00
parent 940b5ea84c
commit 6a2c99143e
4 changed files with 65 additions and 0 deletions

4
latex/__init__.py Normal file
View File

@@ -0,0 +1,4 @@
from .latex import Latex
def setup(bot):
bot.add_cog(Latex(bot))

10
latex/info.json Normal file
View File

@@ -0,0 +1,10 @@
{
"author": ["aikaterna", "Stevy"],
"description": "Generates an image for a LaTeX expression.",
"install_msg": "Thanks for installing, have fun.",
"short": "Generates an image for a LaTeX expression.",
"tags": ["latex"],
"requirements": ["pillow"],
"type": "COG",
"end_user_data_statement": "This cog does not persistently store data or metadata about users."
}

49
latex/latex.py Normal file
View File

@@ -0,0 +1,49 @@
import aiohttp
import discord
import io
import logging
from PIL import Image, ImageOps
from redbot.core import commands
log = logging.getLogger("red.aikaterna.latex")
class Latex(commands.Cog):
"""LaTeX expressions via an image."""
async def red_delete_data_for_user(self, **kwargs):
"""Nothing to delete."""
return
def __init__(self, bot):
self.bot = bot
self.session = aiohttp.ClientSession()
@commands.guild_only()
@commands.command()
async def latex(self, ctx, *, equation):
"""Takes a LaTeX expression and makes it pretty."""
base_url = "https://latex.codecogs.com/gif.latex?%5Cbg_white%20%5CLARGE%20"
url = f"{base_url}{equation}"
try:
async with self.session.get(url) as r:
image = await r.read()
image = Image.open(io.BytesIO(image)).convert("RGBA")
except Exception as exc:
log.exception("Something went wrong while trying to read the image:\n ", exc_info=exc)
image = None
if not image:
return await ctx.send("I can't get the image from the website, check your console for more information.")
image = ImageOps.expand(image, border=10, fill="white")
image_file_object = io.BytesIO()
image.save(image_file_object, format="png")
image_file_object.seek(0)
image_final = discord.File(fp=image_file_object, filename="latex.png")
await ctx.send(file=image_final)
def cog_unload(self):
self.bot.loop.create_task(self.session.close())