diff options
| author | 2018-03-04 00:20:02 +0100 | |
|---|---|---|
| committer | 2018-03-03 23:20:02 +0000 | |
| commit | d7fd7bd2aae5541db5874522aee6506bee3b6ec2 (patch) | |
| tree | 32696746bcada160e600f182e671ab75a72f2a64 /bot/cogs/math.py | |
| parent | Tag feature (#21) (diff) | |
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 <[email protected]>
* Enable loading of math cog
Signed-off-by: Martmists <[email protected]>
* Change size from 300 to 150
* Fix requested issues
Signed-off-by: Martmists <[email protected]>
* forgot setup()
Signed-off-by: Martmists <[email protected]>
* Fix spaces being ignored
Signed-off-by: Martmists <[email protected]>
* Use latex2png instead
Signed-off-by: Martmists <[email protected]>
* Allow ` to be used in expressions to not make it look odd
Signed-off-by: Martmists <[email protected]>
* Add safe calc
Signed-off-by: Martmists <[email protected]>
* Fix flake8
Signed-off-by: Martmists <[email protected]>
* Update math.py
* Fix
Signed-off-by: Martmists <[email protected]>
Diffstat (limited to 'bot/cogs/math.py')
| -rw-r--r-- | bot/cogs/math.py | 95 |
1 files changed, 95 insertions, 0 deletions
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<url>[^"]+)"', 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<url>[^"]+)"', 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)) |