From b74540d16fe50db6b87c75f1e3cef04a3a384a31 Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Sat, 29 Jul 2017 20:21:44 +0100 Subject: [PATCH] Add subreddit and user support to reddit module --- dave/modules/reddit.py | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 80 insertions(+), 14 deletions(-) diff --git a/dave/modules/reddit.py b/dave/modules/reddit.py index 56f5fbc..d3a50e7 100644 --- a/dave/modules/reddit.py +++ b/dave/modules/reddit.py @@ -1,15 +1,11 @@ """Return some useful reddit info""" import pickle from datetime import datetime - import dave.module import dave.config -import uuid -import random -from dave.models import Quote from twisted.words.protocols.irc import assembleFormattedText, attributes as A from requests import get -from humanize import naturaltime +from humanize import naturaltime, naturaldelta, intcomma @dave.module.match(r'(?:https?://(?:www\.)?reddit.com)?(/r/(.+)/comments/([^\s]+))') @dave.module.match(r'https?://(?:www\.)?redd.it/([^\s]+)') @@ -17,11 +13,11 @@ from humanize import naturaltime def post(bot, args, sender, source): """Ran whenever a reddit post is sent""" if not dave.config.redis.exists("reddit:post:{}".format(args[0])): - req = get("https://reddit.com/{}.json".format(args[0]), + req = get("https://reddit.com/{}.json?limit=1".format(args[0]), headers={'user-agent': 'irc bot (https://github.com/w4)'}) if req.status_code != 200: - bot.send(source, responsestatus(req.status_code, "That post")) + bot.msg(source, responsestatus(req.status_code, "That post")) return req = req.json() @@ -35,7 +31,7 @@ def post(bot, args, sender, source): bot.msg(source, assembleFormattedText( A.normal[ - A.lightred["[NSFW] "] if resp["over_18"] else "", + A.bold[A.fg.lightRed["[NSFW] "]] if resp["over_18"] else "", A.bold[resp["title"][:75] + (resp["title"][75:] and '...')], " by ", A.bold[resp["author"]], " (/r/{}) {} comments, {} points, posted {}".format( @@ -47,14 +43,84 @@ def post(bot, args, sender, source): ] )) +@dave.module.match(r'(?:https?://(?:www\.)?reddit.com)?/?r/(([^\s/]+))/?(?: |$)') +@dave.module.dont_always_run_if_run() +def subreddit(bot, args, sender, source): + """Ran whenever a subreddit is mentioned""" + if not dave.config.redis.exists("reddit:subreddit:{}".format(args[0])): + req = get("https://reddit.com/r/{}/about.json".format(args[0]), + headers={'user-agent': 'irc bot (https://github.com/w4)'}) + + if req.status_code != 200: + bot.msg(source, responsestatus(req.status_code, args[0])) + return + + if "/search.json" in req.url: + bot.msg(source, responsestatus(404, args[0])) + return + + req = req.json() + + dave.config.redis.setex("reddit:subreddit:{}".format(args[0]), 600, + pickle.dumps(req)) + else: + req = pickle.loads(dave.config.redis.get("reddit:subreddit:{}".format(args[0]))) + + resp = req["data"] + + bot.msg(source, assembleFormattedText( + A.normal[ + A.bold[A.fg.lightRed["[NSFW] "]] if resp["over18"] else "", + A.bold[resp["title"]], + " ({}), a community for {}. {} subscribers, {} browsing right now.".format( + resp["display_name_prefixed"], + naturaldelta(datetime.utcnow().timestamp() - resp["created"]), + intcomma(resp["subscribers"]), + intcomma(resp["accounts_active"]) + ) + ] + )) + + +@dave.module.match(r'(?:https?://(?:www\.)?reddit.com)?/?(?:u|user)/(([^\s]+)/?)(?: |$)') +@dave.module.dont_always_run_if_run() +def user(bot, args, sender, source): + if not dave.config.redis.exists("reddit:user:{}".format(args[0])): + req = get("https://reddit.com/u/{}/about.json".format(args[0]), + headers={'user-agent': 'irc bot (https://github.com/w4)'}) + + if req.status_code != 200: + bot.msg(source, responsestatus(req.status_code, args[0])) + return + + req = req.json() + + dave.config.redis.setex("reddit:user:{}".format(args[0]), 600, + pickle.dumps(req)) + else: + req = pickle.loads(dave.config.redis.get("reddit:user:{}".format(args[0]))) + + resp = req["data"] + + bot.msg(source, assembleFormattedText( + A.normal[ + A.bold[resp["name"]], + ", a redditor for {}. {} link karma, {} comment karma.".format( + naturaldelta(datetime.utcnow().timestamp() - resp["created"]), + intcomma(resp["link_karma"]), + intcomma(resp["comment_karma"]) + ), + " Verified user." if resp["verified"] else "", + " Reddit employee." if resp["is_employee"] else "" + ] + )) + def responsestatus(status, item): if status == 404: - out = "{} does not exist.".format(item) + return "{} does not exist.".format(item) elif status == 403: - out = "{} is private.".format(item) + return "{} is private.".format(item) elif status == 429: - out = "Rate-limited by reddit. Please try again in a few minutes." + return "Rate-limited by reddit. Please try again in a few minutes." else: - out = "Reddit returned an error, response: {}".format(status) - - return out + return "Reddit returned an error, response: {}".format(status) -- libgit2 1.7.2