2019-03-14 12:02:40 -04:00

128 lines
4.7 KiB
Python

###
# Copyright (c) 2019 oddluck
# All rights reserved.
#
#
###
import supybot.ansi as ansi
import supybot.utils as utils
from supybot.commands import *
import supybot.plugins as plugins
import supybot.ircutils as ircutils
import supybot.callbacks as callbacks
import supybot.ircmsgs as ircmsgs
import urllib
import os
import pexpect
import requests
from bs4 import BeautifulSoup
try:
from supybot.i18n import PluginInternationalization
_ = PluginInternationalization('Weed')
except ImportError:
# Placeholder that allows to run the plugin on a bot
# without the i18n module
_ = lambda x: x
class ASCII(callbacks.Plugin):
"""Uses API to retrieve information"""
threaded = True
def ascii(self, irc, msg, args, optlist, text):
"""[--font <font>] [--color <color1,color2>] [<text>]
Text to ASCII art
"""
channel = msg.args[0]
optlist = dict(optlist)
font = None
words = []
if text:
text = text.strip()
if '|' in text:
words = text.split('|')
if 'color' in optlist:
color = optlist.get('color')
if "," in color:
color = color.split(",")
color1 = color[0].strip()
color2 = color[1].strip()
else:
color1 = color
color2 = None
else:
color1 = None
color2 = None
if 'font' in optlist:
font = optlist.get('font')
if words:
for word in words:
if word.strip():
data = requests.get("https://artii.herokuapp.com/make?text={0}&font={1}".format(word.strip(), font))
for line in data.text.splitlines():
if line.strip():
irc.reply(ircutils.mircColor(line, color1, color2), prefixNick=False)
else:
data = requests.get("https://artii.herokuapp.com/make?text={0}&font={1}".format(text, font))
for line in data.text.splitlines():
if line.strip():
irc.reply(ircutils.mircColor(line, color1, color2), prefixNick=False)
elif 'font' not in optlist:
if words:
for word in words:
if word.strip():
data = requests.get("https://artii.herokuapp.com/make?text={0}&font=univers".format(word.strip()))
for line in data.text.splitlines():
if line.strip():
irc.reply(ircutils.mircColor(line, color1, color2), prefixNick=False)
else:
data = requests.get("https://artii.herokuapp.com/make?text={0}&font=univers".format(text))
for line in data.text.splitlines():
if line.strip():
irc.reply(ircutils.mircColor(line, color1, color2), prefixNick=False)
ascii = wrap(ascii, [getopts({'font':'text', 'color':'text', 'fontlist':''}), additional('text')])
def img2ascii(self, irc, msg, args, url):
"""[--font <font>] [--fontlist] [--color] [<text>]
Image to ASCII Art
"""
path = os.path.dirname(os.path.abspath(__file__))
filepath = "{0}/tmp".format(path)
filename = "{0}/{1}".format(filepath, url.split('/')[-1])
urllib.request.urlretrieve(url, filename)
output = pexpect.run('img2txt.py {0} --targetAspect=0.5 --antialias'.format(str(filename)))
soup = BeautifulSoup(output)
ascii = soup.pre.getText()
for line in ascii.splitlines():
if line.strip:
irc.reply(line, prefixNick=False)
os.remove(filename)
img2ascii = wrap(img2ascii, ['text'])
def fontlist(self, irc, msg, args):
"""
get list of fonts for text-to-ascii-art
"""
fontlist = requests.get("https://artii.herokuapp.com/fonts_list")
response = sorted(fontlist.text.split('\n'))
irc.reply(str(response).replace('\'', '').replace('[', '').replace(']', ''))
fontlist = wrap(fontlist)
def scroll(self, irc, msg, args, url):
"""
Play ASCII/ANSI art files from web links
"""
file = requests.get(url)
if "html" in file.text:
irc.reply("Don't try and scroll html files, jackass.")
else:
for line in file.text.splitlines():
if line.strip():
irc.reply(line, prefixNick = False)
scroll = wrap(scroll, ['text'])
Class = ASCII