From dc5b63f434971e2c48e2fc827739e2f0cef33e7d Mon Sep 17 00:00:00 2001 From: aikaterna <20862007+aikaterna@users.noreply.github.com> Date: Sun, 1 Jul 2018 18:05:25 -0700 Subject: [PATCH] [V3 Retrosign] Initial commit --- retrosign/retrosign.py | 78 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 retrosign/retrosign.py diff --git a/retrosign/retrosign.py b/retrosign/retrosign.py new file mode 100644 index 0000000..e2f2997 --- /dev/null +++ b/retrosign/retrosign.py @@ -0,0 +1,78 @@ +# This is a rewrite port of a cog from Anismash: +# https://github.com/Anismash/Ani-Cogs/blob/master/retrosign/retrosign.py + +import aiohttp +from bs4 import BeautifulSoup as bs +import discord +from redbot.core import commands +from io import BytesIO +import lxml +import os +from random import choice +import re + + +class Retrosign: + def __init__(self, bot): + self.bot = bot + self.session = aiohttp.ClientSession() + + @commands.cooldown(1, 15, discord.ext.commands.BucketType.guild) + @commands.command(name="retrosign") + async def retrosign(self, ctx, *, content: str): + """Make a retrosign with 3 words seperated by ';' or with one word in the middle.""" + texts = [t.strip() for t in content.split(";")] + if len(texts) == 1: + lenstr = len(texts[0]) + if lenstr <= 15: + data = dict( + bcg=choice([1, 2, 3, 4, 5]), + txt=choice([1, 2, 3, 4]), + text1="", + text2=texts[0], + text3="", + ) + else: + return await ctx.send("\N{CROSS MARK} Your line is too long (14 character limit)") + elif len(texts) == 3: + texts[0] = re.sub("[^a-zA-Z0-9]", "", texts[0]) + if len(texts[0]) >= 15: + return await ctx.send( + "\N{CROSS MARK} Your first line is too long (14 character limit)" + ) + if len(texts[1]) >= 13: + return await ctx.send( + "\N{CROSS MARK} Your second line is too long (12 character limit)" + ) + if len(texts[2]) >= 26: + return await ctx.send( + "\N{CROSS MARK} Your third line is too long (25 character limit)" + ) + data = dict( + bcg=choice([1, 2, 3, 4, 5]), + txt=choice([1, 2, 3, 4]), + text1=texts[0], + text2=texts[1], + text3=texts[2], + ) + else: + return await ctx.send( + "\N{CROSS MARK} please provide three words seperated by ';' or one word" + ) + + async with ctx.channel.typing(): + async with self.session.post( + "http://photofunia.com/effects/retro-wave", data=data + ) as response: + if response.status == 200: + soup = bs(await response.text(), "lxml") + download_url = soup.find("div", class_="downloads-container").ul.li.a["href"] + async with self.session.request("GET", download_url) as image_response: + if image_response.status == 200: + image_data = await image_response.read() + with BytesIO(image_data) as temp_image: + image = discord.File(fp=temp_image, filename="image.png") + await ctx.channel.send(file=image) + + def __unload(self): + self.session.close()