@@ -0,0 +1,2338 @@
+# -*- coding: utf-8 -*-%0A%22%22%22%0ACreated on Mar 03, 2015%0A%0A@author: Tyranic-Moron%0A%22%22%22%0Aimport json%0Aimport random%0A%0Afrom IRCMessage import IRCMessage%0Afrom IRCResponse import IRCResponse, ResponseType%0Afrom CommandInterface import CommandInterface%0A%0Afrom Data.api_keys import load_key%0Afrom Utils import WebUtils%0A%0Afrom twisted.words.protocols.irc import assembleFormattedText, attributes as A%0A%0A# Idea for this initially taken from Heufneutje's RE_HeufyBot Aww module, but I made it more generic for aliasing%0A# https://github.com/Heufneutje/RE_HeufyBot/blob/f45219d1a61f0ed0fd60a89dcaeb2e962962356e/modules/Aww/src/heufybot/modules/Aww.java%0A# Future plans:%0A# - multiple fetch types beyond the current random, eg reddit sort types (top rated, hot, best, etc)%0Aclass RedditImage(CommandInterface):%0A    triggers = %5B'redditimage'%5D%0A    help = %22%22%0A    runInThread = True%0A%0A    def onLoad(self):%0A        self.imgurClientID = load_key(u'imgur Client ID')%0A        self.headers = %5B('Authorization', 'Client-ID %7B%7D'.format(self.imgurClientID))%5D%0A%0A    def execute(self, message):%0A        %22%22%22%0A        @type message: IRCMessage%0A        %22%22%22%0A        if len(message.ParameterList) != 1:%0A            return IRCResponse(ResponseType.Say, self.help, message.ReplyTo)%0A%0A        subreddit = message.ParameterList%5B0%5D.lower()%0A%0A        url = %22https://api.imgur.com/3/gallery/r/%7B%7D/time/all/%7B%7D%22%0A        url = url.format(subreddit, random.randint(0, 100))%0A        response = WebUtils.fetchURL(url, self.headers)%0A        jsonResponse = json.loads(response.body)%0A        images = jsonResponse%5B'data'%5D%0A%0A        if not images:%0A            return IRCResponse(ResponseType.Say,%0A                               %22The subreddit '%7B%7D' doesn't seem to have any images posted to it (or it doesn't exist!)%22%0A                               .format(subreddit),%0A                               message.ReplyTo)%0A%0A        image = random.choice(images)%0A%0A        data = %5B%5D%0A        if image%5B'title'%5D is not None:%0A            data.append(image%5B'title'%5D)%0A        if image%5B'nsfw'%5D:%0A            data.append(u'%5Cx034%5Cx02NSFW!%5Cx0F')%0A        if image%5B'animated'%5D:%0A            data.append(u'%5Cx032%5Cx02Animated!%5Cx0F')%0A        data.append(image%5B'link'%5D)%0A%0A        graySplitter = assembleFormattedText(A.normal%5B' ', A.fg.gray%5B'%7C'%5D, ' '%5D)%0A        return IRCResponse(ResponseType.Say, graySplitter.join(data), message.ReplyTo)%0A

Add RedditImage command, fetches a random image from the specified subreddit