import json, random, urllib2

def declare():
    return {"reddit": "privmsg"}

def callback(self, type, isop, command="", msg="", user="", channel="", mode=""):

    try:
        u = str(msg.split(' ', 1)[1])
    except:
        return self.msg(channel, "Please specify a subreddit!")

    try:

        req = urllib2.Request("https://www.reddit.com/r/" + u + "/new.json", headers={ 'User-Agent': 'UNIX:the_kgb:0.157 http://github.com/stqism/THE_KGB' })
        fd = urllib2.urlopen(req)
        reddit_api = json.loads(fd.read())
        fd.close()

        cringe = []

        for i in reddit_api['data']['children']:
            url = i['data']['url']
            title = i['data']['title']

            if 'imgur' in url:

                if 'http://i.imgur.com' in url:  #force https
                    url = 'https://i.imgur.com/%s' % (url.split('/')[3])

                if 'http://' in url and '/a/' not in url:   #direct URLs
                    if 'gallery' in url:
                        url = 'https://i.imgur.com/%s.jpg' % (url.split('/')[4])
                    else:
                        url = 'https://i.imgur.com/%s.jpg' % (url.split('/')[3])

            cringe.append([title, url])

        item = random.choice(cringe)
        return self.msg(channel, str(item[0] + " " + item[1]))

    except Exception, e:
        return self.msg('#the_kgb', str(e))

class api:
	def msg(self, channel, text):
		return "[%s] %s" % (channel, text)

if __name__ == "__main__":
    api = api()
    u = "joe!username@hostmask"
    c = '#test'

    if callback(api, '', True, channel=c, user=u, msg='^reddit') != '[%s] Please specify a subreddit!' % (c):
        print '[TESTFAIL] no arguments'
        exit(1)

    if callback(api, '', True, channel=c, user=u, msg='^reddit fatpeoplehate') != '[#the_kgb] HTTP Error 404: Not Found':
        print '[TESTFAIL] error catcher'
        exit(1)

    if not callback(api, '', True, channel=c, user=u, msg='^reddit fatlogic').startswith('[%s] ' % (c)):
        print '[TESTFAIL] Subreddit loader'
        exit(1)

@@ -687,16 +687,131 @@
 'title'%5D
+%0A            selfpost = bool(i%5B'data'%5D%5B'is_self'%5D)%0A            post = %22https://reddit.com%22 + i%5B'data'%5D%5B'permalink'%5D
 %0A%0A      
@@ -1312,16 +1312,22 @@
 tle, url
+, post
 %5D)%0A%0A    
@@ -1359,16 +1359,181 @@
 cringe)%0A
+%0A        if not selfpost:%0A            via = %22     (via: %22 + item%5B2%5D + %22)%22%0A            return self.msg(channel, str(item%5B0%5D + %22 %22 + item%5B1%5D + via))%0A        else:%0A    
         
@@ -1658,16 +1658,17 @@
 tr(e))%0A%0A
+%0A
 class ap

Switch to latest reddit code preserving tests