From 7efbe19b2c05d705b94ac822b6c0652cd7f75bd9 Mon Sep 17 00:00:00 2001 From: Andrei Marcu Date: Mon, 2 Apr 2012 12:22:16 -0400 Subject: [PATCH] Random reddit link module --- modules/randomreddit.py | 45 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 modules/randomreddit.py diff --git a/modules/randomreddit.py b/modules/randomreddit.py new file mode 100644 index 0000000..cd36fec --- /dev/null +++ b/modules/randomreddit.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python3 +""" +randomreddit.py - return a random reddit url from a subreddit's frontpage +author: andreim +""" + +import web +import re +import json +from random import choice + +def randomreddit(phenny, input): + + subreddit = input.group(2) + if not subreddit: + phenny.say(".random - get a random link from the subreddit's frontpage") + return + + if not re.match('^[A-Za-z0-9_-]*$',subreddit): + phenny.say(input.nick + ": bad subreddit format.") + return + + + url = "http://www.reddit.com/r/" + subreddit + "/.json" + try: + resp = web.get(url) + except: + phenny.reply('Reddit or subreddit unreachable.') + return + + reddit = json.loads(resp) + post = choice(reddit['data']['children']) + + nsfw = False + if post['data']['over_18']: + nsfw = True + + if nsfw: + phenny.reply("!!NSFW!! " + post['data']['url'] + " (" + post['data']['title'] + ") !!NSFW!!") + else: + phenny.reply(post['data']['url'] + " (" + post['data']['title'] + ")") + +randomreddit.commands = ['random'] +randomreddit.priority = 'medium' +randomreddit.thread = False \ No newline at end of file