Revamped reddit plugin with search command, inspired by @blha303 :)
This commit is contained in:
parent
f9f15f3597
commit
bfd600cf49
|
@ -1,8 +1,13 @@
|
|||
from util import hook, http
|
||||
from util import hook, http, text, timesince
|
||||
from datetime import datetime
|
||||
import re
|
||||
import random
|
||||
|
||||
reddit_re = (r'.*((www\.)?reddit\.com/r[^ ]+)', re.I)
|
||||
|
||||
base_url = "http://reddit.com/r/{}/.json"
|
||||
short_url = "http://redd.it/{}"
|
||||
|
||||
|
||||
@hook.regex(*reddit_re)
|
||||
def reddit_url(match):
|
||||
|
@ -17,3 +22,47 @@ def reddit_url(match):
|
|||
|
||||
return '\x02{}\x02 - posted by \x02{}\x02 {} ago - {} upvotes, {} downvotes - {}'.format(
|
||||
title, author, timeago, upvotes, downvotes, comments)
|
||||
|
||||
|
||||
@hook.command
|
||||
def reddit(inp):
|
||||
"""reddit <subreddit> [n] -- Gets a random post from <subreddit>, or gets the [n]th post in the subreddit."""
|
||||
|
||||
# clean and split the input
|
||||
parts = inp.lower().strip().split()
|
||||
|
||||
|
||||
# find the requested post number (if any)
|
||||
if len(parts) > 1:
|
||||
inp = parts[0]
|
||||
try:
|
||||
id_num = int(parts[1]) - 1
|
||||
except ValueError:
|
||||
return "Invalid post number."
|
||||
|
||||
try:
|
||||
data = http.get_json(base_url.format(inp.strip()),
|
||||
user_agent=http.ua_chrome)
|
||||
except Exception as e:
|
||||
return "Error: " + str(e)
|
||||
data = data["data"]["children"]
|
||||
|
||||
# get the requested/random post
|
||||
if id_num:
|
||||
try:
|
||||
item = data[id_num]["data"]
|
||||
except IndexError:
|
||||
length = len(data)
|
||||
return "Invalid post number. Number must be between 1 and {}.".format(length)
|
||||
else:
|
||||
item = random.choice(data)["data"]
|
||||
|
||||
item["title"] = text.truncate_str(item["title"], 50)
|
||||
item["link"] = short_url.format(item["id"])
|
||||
|
||||
rawtime = datetime.fromtimestamp(int(item["created_utc"]))
|
||||
item["timesince"] = timesince.timesince(rawtime)
|
||||
|
||||
return u'\x02{title}\x02 - posted by \x02{author}\x02' \
|
||||
' {timesince} ago - {ups} upvotes, {downs} downvotes -' \
|
||||
' {link}'.format(**item)
|
||||
|
|
Reference in a new issue