From 5528de2bfec057ca3034018365654f0e252f44b3 Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Sat, 29 Jul 2017 19:40:09 +0100 Subject: [PATCH] give more info when a reddit post is sent --- dave/modules/reddit.py | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ dave/modules/seen.py | 2 -- dave/modules/title.py | 3 ++- 3 files changed, 62 insertions(+), 3 deletions(-) diff --git a/dave/modules/reddit.py b/dave/modules/reddit.py new file mode 100644 index 0000000..56f5fbc 100644 --- /dev/null +++ a/dave/modules/reddit.py @@ -1,0 +1,60 @@ +"""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 + +@dave.module.match(r'(?:https?://(?:www\.)?reddit.com)?(/r/(.+)/comments/([^\s]+))') +@dave.module.match(r'https?://(?:www\.)?redd.it/([^\s]+)') +@dave.module.dont_always_run_if_run() +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]), + headers={'user-agent': 'irc bot (https://github.com/w4)'}) + + if req.status_code != 200: + bot.send(source, responsestatus(req.status_code, "That post")) + return + + req = req.json() + + dave.config.redis.setex("reddit:post:{}".format(args[0]), 200, + pickle.dumps(req)) + else: + req = pickle.loads(dave.config.redis.get("reddit:post:{}".format(args[0]))) + + resp = req[0]["data"]["children"][0]["data"] + + bot.msg(source, assembleFormattedText( + A.normal[ + A.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( + resp["subreddit"], + resp["num_comments"], + resp["score"], + naturaltime(datetime.utcnow().timestamp() - resp["created_utc"]) + ), + ] + )) + +def responsestatus(status, item): + if status == 404: + out = "{} does not exist.".format(item) + elif status == 403: + out = "{} is private.".format(item) + elif status == 429: + out = "Rate-limited by reddit. Please try again in a few minutes." + else: + out = "Reddit returned an error, response: {}".format(status) + + return out diff --git a/dave/modules/seen.py b/dave/modules/seen.py index ba50f4b..b8be1b2 100644 --- a/dave/modules/seen.py +++ a/dave/modules/seen.py @@ -14,8 +14,6 @@ seen = pickle.loads(dave.config.redis.get("lastseen:{}".format(args[0].lower()))) - print(seen) - bot.reply(source, sender, "{} was last seen {} saying: {}".format( seen["name"], naturaltime(datetime.utcnow() - seen["when"]), diff --git a/dave/modules/title.py b/dave/modules/title.py index 904ba90..f5abad1 100644 --- a/dave/modules/title.py +++ a/dave/modules/title.py @@ -18,7 +18,8 @@ for match in matches: if not dave.config.redis.exists("site:{}".format(match)): - res = get(match, timeout=3) + res = get(match, timeout=3, + headers={'user-agent': 'irc bot (https://github.com/w4)'}) soup = BeautifulSoup(res.text, "html.parser") title = soup.title -- rgit 0.1.3