2014-05-01 21:44:26 +00:00
|
|
|
# coding=utf-8
|
|
|
|
|
2014-08-27 23:39:31 +00:00
|
|
|
"""Get information about subreddit"""
|
|
|
|
|
2014-05-01 21:44:26 +00:00
|
|
|
import re
|
2014-12-17 06:32:34 +00:00
|
|
|
|
2015-10-30 20:57:45 +00:00
|
|
|
from nemubot.exception import IMException
|
2015-01-03 19:34:44 +00:00
|
|
|
from nemubot.hooks import hook
|
|
|
|
from nemubot.tools import web
|
2014-05-01 21:44:26 +00:00
|
|
|
|
2014-08-13 13:53:55 +00:00
|
|
|
nemubotversion = 3.4
|
2014-05-01 21:44:26 +00:00
|
|
|
|
2014-09-26 16:00:22 +00:00
|
|
|
from more import Response
|
2014-05-01 21:44:26 +00:00
|
|
|
|
2014-11-13 01:51:49 +00:00
|
|
|
|
2014-08-12 18:10:19 +00:00
|
|
|
def help_full():
|
2014-05-01 21:44:26 +00:00
|
|
|
return "!subreddit /subreddit/: Display information on the subreddit."
|
|
|
|
|
|
|
|
LAST_SUBS = dict()
|
|
|
|
|
2014-11-13 01:51:49 +00:00
|
|
|
|
2015-11-01 12:54:59 +00:00
|
|
|
@hook.command("subreddit")
|
2014-05-01 21:44:26 +00:00
|
|
|
def cmd_subreddit(msg):
|
|
|
|
global LAST_SUBS
|
2015-07-10 21:09:54 +00:00
|
|
|
if not len(msg.args):
|
2014-05-01 21:44:26 +00:00
|
|
|
if msg.channel in LAST_SUBS and len(LAST_SUBS[msg.channel]) > 0:
|
|
|
|
subs = [LAST_SUBS[msg.channel].pop()]
|
|
|
|
else:
|
2015-10-30 20:57:45 +00:00
|
|
|
raise IMException("Which subreddit? Need inspiration? "
|
2014-11-13 01:51:49 +00:00
|
|
|
"type !horny or !bored")
|
2014-05-01 21:44:26 +00:00
|
|
|
else:
|
2015-07-10 21:09:54 +00:00
|
|
|
subs = msg.args
|
2014-05-01 21:44:26 +00:00
|
|
|
|
|
|
|
all_res = list()
|
2014-05-01 22:27:42 +00:00
|
|
|
for osub in subs:
|
|
|
|
sub = re.match(r"^/?(?:(\w)/)?(\w+)/?$", osub)
|
2014-05-01 21:44:26 +00:00
|
|
|
if sub is not None:
|
|
|
|
if sub.group(1) is not None and sub.group(1) != "":
|
|
|
|
where = sub.group(1)
|
|
|
|
else:
|
|
|
|
where = "r"
|
2014-12-17 06:32:34 +00:00
|
|
|
|
|
|
|
sbr = web.getJSON("http://www.reddit.com/%s/%s/about.json" %
|
|
|
|
(where, sub.group(2)))
|
|
|
|
|
|
|
|
if sbr is None:
|
2015-10-30 20:57:45 +00:00
|
|
|
raise IMException("subreddit not found")
|
2014-05-01 21:44:26 +00:00
|
|
|
|
|
|
|
if "title" in sbr["data"]:
|
2014-11-13 01:51:49 +00:00
|
|
|
res = Response(channel=msg.channel,
|
|
|
|
nomore="No more information")
|
|
|
|
res.append_message(
|
|
|
|
("[NSFW] " if sbr["data"]["over18"] else "") +
|
|
|
|
sbr["data"]["url"] + " " + sbr["data"]["title"] + ": " +
|
|
|
|
sbr["data"]["public_description" if sbr["data"]["public_description"] != "" else "description"].replace("\n", " ") +
|
|
|
|
" %s subscriber(s)" % sbr["data"]["subscribers"])
|
2014-05-01 21:44:26 +00:00
|
|
|
if sbr["data"]["public_description"] != "":
|
2014-11-13 01:51:49 +00:00
|
|
|
res.append_message(
|
|
|
|
sbr["data"]["description"].replace("\n", " "))
|
2014-05-01 21:44:26 +00:00
|
|
|
all_res.append(res)
|
|
|
|
else:
|
2014-11-13 01:51:49 +00:00
|
|
|
all_res.append(Response("/%s/%s doesn't exist" %
|
|
|
|
(where, sub.group(2)),
|
|
|
|
channel=msg.channel))
|
2014-05-01 22:27:42 +00:00
|
|
|
else:
|
2014-11-13 01:51:49 +00:00
|
|
|
all_res.append(Response("%s is not a valid subreddit" % osub,
|
|
|
|
channel=msg.channel, nick=msg.nick))
|
2014-05-01 21:44:26 +00:00
|
|
|
|
|
|
|
return all_res
|
|
|
|
|
2014-11-13 01:51:49 +00:00
|
|
|
|
2015-11-01 12:54:59 +00:00
|
|
|
@hook.message()
|
2014-05-01 21:44:26 +00:00
|
|
|
def parselisten(msg):
|
2014-09-03 17:06:26 +00:00
|
|
|
parseresponse(msg)
|
|
|
|
return None
|
|
|
|
|
2014-11-13 01:51:49 +00:00
|
|
|
|
2015-11-01 12:54:59 +00:00
|
|
|
@hook.post()
|
2014-09-03 17:06:26 +00:00
|
|
|
def parseresponse(msg):
|
2014-05-01 21:44:26 +00:00
|
|
|
global LAST_SUBS
|
|
|
|
|
2017-07-12 23:21:43 +00:00
|
|
|
if hasattr(msg, "text") and msg.text and type(msg.text) == str:
|
2014-08-29 14:33:45 +00:00
|
|
|
urls = re.findall("www.reddit.com(/\w/\w+/?)", msg.text)
|
2014-05-01 21:44:26 +00:00
|
|
|
for url in urls:
|
2015-11-01 12:54:59 +00:00
|
|
|
for recv in msg.to:
|
2015-09-21 00:23:15 +00:00
|
|
|
if recv not in LAST_SUBS:
|
|
|
|
LAST_SUBS[recv] = list()
|
|
|
|
LAST_SUBS[recv].append(url)
|
2014-05-01 21:44:26 +00:00
|
|
|
|
2014-09-01 17:21:54 +00:00
|
|
|
return msg
|