aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar martmists <[email protected]>2018-03-04 00:20:02 +0100
committerGravatar Gareth Coles <[email protected]>2018-03-03 23:20:02 +0000
commitd7fd7bd2aae5541db5874522aee6506bee3b6ec2 (patch)
tree32696746bcada160e600f182e671ab75a72f2a64
parentTag 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]>
-rw-r--r--.snekrc6
-rw-r--r--bot/__main__.py1
-rw-r--r--bot/cogs/math.py95
-rw-r--r--requirements.txt1
4 files changed, 103 insertions, 0 deletions
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<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))
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