Files
Gwendolyn/gwendolyn/funcs/other/nerd_shit.py
2021-08-17 18:05:41 +02:00

81 lines
3.4 KiB
Python

import discord, discord_slash, wolframalpha, requests, os
from PIL import Image, ImageDraw, ImageFont
class NerdShit():
def __init__(self, bot):
"""Runs misc commands."""
self.bot = bot
async def wolfSearch(self,ctx,content):
await self.bot.defer(ctx)
fnt = ImageFont.truetype('gwendolyn/resources/fonts/times-new-roman.ttf', 20)
self.bot.log("Requesting data")
bot = wolframalpha.Client(self.bot.credentials["wolfram_alpha_key"])
res = bot.query(content)
self.bot.log("Processing data")
titles = []
pods = []
if int(res.numpods) > 0:
for pod in res.pods:
titles += [pod.title]
for x, sub in enumerate(pod.subpods):
pods += [sub]
if x > 0:
titles += [""]
podChunks = [pods[x:x+2] for x in range(0, len(pods), 2)]
titleChucks = [titles[x:x+2] for x in range(0, len(titles), 2)]
await ctx.send(f"Response for \"{content}\"")
for x, chunk in enumerate(podChunks):
width = 0
for title in titleChucks[x]:
width = max(width,fnt.getsize(title)[0])
height = 5
heights = []
for count, pod in enumerate(chunk):
heights += [height]
width = max(width,int(pod.img['@width']))
if titleChucks[x][count] == "":
placeFor_text = 0
else:
placeFor_text = 30
height += int(pod.img["@height"]) + 10 + placeFor_text
width += 10
height += 5
wolfImage = Image.new("RGB",(width,height),color=(255,255,255))
for count, pod in enumerate(chunk):
response = requests.get(pod.img["@src"])
file = open("gwendolyn/resources/wolfTemp.png", "wb")
file.write(response.content)
file.close()
old_image = Image.open("gwendolyn/resources/wolfTemp.png")
oldSize = old_image.size
if titleChucks[x][count] == "":
placeFor_text = 0
else:
placeFor_text = 30
newSize = (width,int(oldSize[1]+10+placeFor_text))
new_image = Image.new("RGB",newSize,color=(255,255,255))
new_image.paste(old_image, (int((int(oldSize[0]+10)-oldSize[0])/2),int(((newSize[1]-placeFor_text)-oldSize[1])/2)+placeFor_text))
if titleChucks[x][count] != "":
d = ImageDraw.Draw(new_image,"RGB")
d.text((5,7),titleChucks[x][count],font=fnt,fill=(150,150,150))
wolfImage.paste(new_image,(0,heights[count]))
new_image.close()
old_image.close()
count += 1
wolfImage.save("gwendolyn/resources/wolf.png")
wolfImage.close()
await ctx.channel.send(file = discord.File("gwendolyn/resources/wolf.png"))
os.remove("gwendolyn/resources/wolf.png")
os.remove("gwendolyn/resources/wolfTemp.png")
else:
self.bot.log("No returned data")
await ctx.send("Could not find anything relating to your search")