From d7fd7bd2aae5541db5874522aee6506bee3b6ec2 Mon Sep 17 00:00:00 2001 From: martmists Date: Sun, 4 Mar 2018 00:20:02 +0100 Subject: Add cog for turning math expressions into LaTeX (#18) * Add cog for turning math expressions into LaTeX Note that this does NOT require eval, and does NOT evaluate the expression, and is therefore 100% safe * Fix whitespace * Fix import order Signed-off-by: Martmists * Enable loading of math cog Signed-off-by: Martmists * Change size from 300 to 150 * Fix requested issues Signed-off-by: Martmists * forgot setup() Signed-off-by: Martmists * Fix spaces being ignored Signed-off-by: Martmists * Use latex2png instead Signed-off-by: Martmists * Allow ` to be used in expressions to not make it look odd Signed-off-by: Martmists * Add safe calc Signed-off-by: Martmists * Fix flake8 Signed-off-by: Martmists * Update math.py * Fix Signed-off-by: Martmists --- .snekrc | 6 ++++ bot/__main__.py | 1 + bot/cogs/math.py | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 1 + 4 files changed, 103 insertions(+) create mode 100644 .snekrc create mode 100644 bot/cogs/math.py diff --git a/.snekrc b/.snekrc new file mode 100644 index 000000000..9f582f477 --- /dev/null +++ b/.snekrc @@ -0,0 +1,6 @@ +[all] +linters = flake8, safety, dodgy + +[flake8] +max-line-length=120 +application_import_names=bot diff --git a/bot/__main__.py b/bot/__main__.py index 88991ac53..e04aaef5c 100644 --- a/bot/__main__.py +++ b/bot/__main__.py @@ -33,6 +33,7 @@ bot.load_extension("bot.cogs.cogs") bot.load_extension("bot.cogs.clickup") bot.load_extension("bot.cogs.deployment") bot.load_extension("bot.cogs.eval") +bot.load_extension("bot.cogs.math") bot.load_extension("bot.cogs.fun") bot.load_extension("bot.cogs.tags") bot.load_extension("bot.cogs.verification") diff --git a/bot/cogs/math.py b/bot/cogs/math.py new file mode 100644 index 000000000..94024e8ae --- /dev/null +++ b/bot/cogs/math.py @@ -0,0 +1,95 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + + +from io import BytesIO +from re import search + + +from aiohttp import ClientSession + +from discord import File +from discord.ext.commands import command + +from sympy import latex +from sympy.parsing.sympy_parser import parse_expr + + +LATEX_URL = "http://latex2png.com" + + +class Math: + def __init__(self, bot): + self.bot = bot + + @command() + async def latexify(self, ctx, *, expr: str): + fixed_expr = expr.replace('^', '**').strip('`').replace("__", "") + try: + parsed = parse_expr(fixed_expr, evaluate=False) + + except SyntaxError: + await ctx.send("Invalid expression!") + + else: + ltx = latex(parsed) + + data = { + "latex": ltx, + "res": 300, + "color": 808080 + } + + async with ClientSession() as session: + async with session.post(LATEX_URL, data=data) as resp: + html = await resp.text() + + name = search(r'hist\.request\.basename = "(?P[^"]+)"', html).group('url') + + async with session.get(f"{LATEX_URL}/output/{name}.png") as resp: + bytes_img = await resp.read() + + file = File(fp=BytesIO(bytes_img), filename="latex.png") + + await ctx.send(file=file) + + @command() + async def calc(self, ctx, *, expr: str): + fixed_expr = expr.replace('^', '**').strip('`') + + if any(x in fixed_expr for x in ("**", "__")): + return await ctx.send( + "You used an expression that has been disabled for security, our apologies") + + try: + parsed = parse_expr(fixed_expr, evaluate=False) + result = parsed.doit() + + except SyntaxError: + await ctx.send("Invalid expression!") + + else: + ltx = latex(result) + + data = { + "latex": ltx, + "res": 300, + "color": 808080 + } + + async with ClientSession() as session: + async with session.post(LATEX_URL, data=data) as resp: + html = await resp.text() + + name = search(r'hist\.request\.basename = "(?P[^"]+)"', html).group('url') + + async with session.get(f"{LATEX_URL}/output/{name}.png") as resp: + bytes_img = await resp.read() + + file = File(fp=BytesIO(bytes_img), filename="latex.png") + + await ctx.send(file=file) + + +def setup(bot): + bot.add_cog(Math(bot)) diff --git a/requirements.txt b/requirements.txt index 9e191988d..941d7f2b6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ https://github.com/Rapptz/discord.py/archive/rewrite.zip#egg=discord.py[voice] dulwich multidict +sympy -- cgit v1.2.3