From 839871f63633867e786a2d0d140d673f68c9f969 Mon Sep 17 00:00:00 2001 From: Luke Rogers Date: Tue, 1 Oct 2013 10:03:46 +1300 Subject: [PATCH] Added RAINBOWS --- CONTRIBUTORS | 1 + plugins/reddit.py | 31 ++++++++++++++---------- plugins/utility.py | 59 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 13 deletions(-) diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 8810f2f..5495bc9 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -22,6 +22,7 @@ frdmn We are using code from the following projects: ./plugins/mlia.py - https://github.com/infinitylabs/UguuBot ./plugins/horoscope.py - https://github.com/infinitylabs/UguuBot +color section in ./plugins/utility.py - https://github.com/hitzler/homero Special Thanks: Rmmh (created skybot!) diff --git a/plugins/reddit.py b/plugins/reddit.py index 578e845..41dbf64 100644 --- a/plugins/reddit.py +++ b/plugins/reddit.py @@ -24,29 +24,33 @@ def reddit_url(match): title, author, timeago, upvotes, downvotes, comments) -@hook.command +@hook.command(autohelp=False) def reddit(inp): """reddit [n] -- Gets a random post from , or gets the [n]th post in the subreddit.""" - - # clean and split the input - parts = inp.lower().strip().split() id_num = None - # find the requested post number (if any) - if len(parts) > 1: - inp = parts[0] - try: - id_num = int(parts[1]) - 1 - except ValueError: - return "Invalid post number." + if inp: + # clean and split the input + parts = inp.lower().strip().split() + + # find the requested post number (if any) + if len(parts) > 1: + subreddit = parts[0] + try: + id_num = int(parts[1]) - 1 + except ValueError: + return "Invalid post number." + else: + subreddit = "all" try: - data = http.get_json(base_url.format(inp.strip()), + data = http.get_json(base_url.format(subreddit.strip()), user_agent=http.ua_chrome) except Exception as e: return "Error: " + str(e) data = data["data"]["children"] + # get the requested/random post if id_num: try: @@ -68,6 +72,7 @@ def reddit(inp): else: item["warning"] = "" - return u'\x02{title}\x02 - posted by \x02{author}\x02' \ + + return u'\x02{title} : {subreddit}\x02 - posted by \x02{author}\x02' \ ' {timesince} ago - {ups} upvotes, {downs} downvotes -' \ ' {link}{warning}'.format(**item) diff --git a/plugins/utility.py b/plugins/utility.py index bf57d38..c8f33af 100644 --- a/plugins/utility.py +++ b/plugins/utility.py @@ -1,5 +1,30 @@ from util import hook, text import hashlib +import collections +import re + +# variables + +colors = collections.OrderedDict([ + ('red', '\x0304'), + ('ornage', '\x0307'), + ('yellow', '\x0308'), + ('green', '\x0309'), + ('cyan', '\x0303'), + ('ltblue', '\x0310'), + ('rylblue','\x0312'), + ('blue', '\x0302'), + ('magenta','\x0306'), + ('pink', '\x0313'), + ('maroon', '\x0305') +]) + +# helper functions + +strip_re = re.compile("(\x03|\x02|\x1f)(?:,?\d{1,2}(?:,\d{1,2})?)?", re.UNICODE) + +def strip(text): + return strip_re.sub('', text) # basic text tools @@ -69,3 +94,37 @@ def munge(inp): """munge -- Munges up .""" return text.munge(inp) +# colors - based on code by Reece Selwood - + +@hook.command +def rainbow(inp): + inp = unicode(inp) + inp = strip(inp) + col = colors.items() + out = "" + l = len(colors) + for i, t in enumerate(inp): + out += col[i % l][1] + t + return out + +@hook.command +def wrainbow(inp): + inp = unicode(inp) + col = colors.items() + inp = strip(inp).split(' ') + out = [] + l = len(colors) + for i, t in enumerate(inp): + out.append(col[i % l][1] + t) + return ' '.join(out) + +@hook.command +def usa(inp): + inp = strip(inp) + c = [colors['red'], '\x0300', colors['blue']] + l = len(c) + out = '' + for i, t in enumerate(inp): + out += c[i % l] + t + return out +