give more info when a reddit post is sent
Diff
dave/modules/reddit.py | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
dave/modules/seen.py | 2 --
dave/modules/title.py | 3 ++-
3 files changed, 62 insertions(+), 3 deletions(-)
@@ -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
@@ -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"]),
@@ -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