-
Notifications
You must be signed in to change notification settings - Fork 1
/
reddit.py
86 lines (68 loc) · 2.78 KB
/
reddit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from datetime import datetime
import re
import random
from util import hook, http, text, timesince
reddit_re = (r'.*(((www\.)?reddit\.com/r|redd\.it)[^ ]+)', re.I)
base_url = "http://reddit.com/r/{}/.json"
short_url = "http://redd.it/{}"
@hook.regex(*reddit_re)
def reddit_url(match):
thread = http.get_html(match.group(0))
title = thread.xpath('//title/text()')[0]
author = thread.xpath("//div[@id='siteTable']//a[contains(@class,'author')]/text()")[0]
timeago = thread.xpath("//div[@id='siteTable']//p[@class='tagline']/time/text()")[0]
try:
comments = thread.xpath("//div[@id='siteTable']//a[@class='comments may-blank']/text()")[0]
except IndexError:
comments = thread.xpath("//div[@id='siteTable']//a[@class='comments empty may-blank']/text()")[0]
if comments == "comment":
comments = "No comments"
else:
pass
pointsnum = thread.xpath("//div[@class='score']//span[@class='number']/text()")[0]
pointsword = thread.xpath("//div[@class='score']//span[@class='word']/text()")[0]
return u'\x02{}\x02 - posted by \x02{}\x02 {} ago - {} - {} {}'.format(
title, author, timeago, comments, pointsnum, pointsword)
@hook.command(autohelp=False)
def reddit(inp):
"""reddit <subreddit> [n] -- Gets a random post from <subreddit>, or gets the [n]th post in the subreddit."""
id_num = None
if inp:
# clean and split the input
parts = inp.lower().strip().split()
# find the requested post number (if any)
if len(parts) > 1:
url = base_url.format(parts[0].strip())
try:
id_num = int(parts[1]) - 1
except ValueError:
return "Invalid post number."
else:
url = base_url.format(parts[0].strip())
else:
url = "http://reddit.com/.json"
try:
data = http.get_json(url, 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 is not None:
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"])
raw_time = datetime.fromtimestamp(int(item["created_utc"]))
item["timesince"] = timesince.timesince(raw_time)
if item["over_18"]:
item["warning"] = " \x02NSFW\x02"
else:
item["warning"] = ""
return u"\x02{title} : {subreddit}\x02 - posted by \x02{author}\x02" \
" {timesince} ago -" \
" {link}{warning}".format(**item)