This repository has been archived on 2023-04-13. You can view files and clone it, but cannot push or open issues or pull requests.
CloudBot/plugins/reddit.py
2013-09-05 10:11:18 +08:00

20 lines
834 B
Python

from util import hook, http
import re
reddit_re = (r'.*((www\.)?reddit\.com/r[^ ]+)', re.I)
@hook.regex(*reddit_re)
def reddit_url(match):
thread = http.get_html(match.group(0))
title = thread.xpath('//title/text()')[0]
upvotes = thread.xpath("//span[@class='upvotes']/span[@class='number']/text()")[0]
downvotes = thread.xpath("//span[@class='downvotes']/span[@class='number']/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]
comments = thread.xpath("//div[@id='siteTable']//a[@class='comments']/text()")[0]
return '\x02{}\x02 - posted by \x02{}\x02 {} ago - {} upvotes, {} downvotes - {}'.format(
title, author, timeago, upvotes, downvotes, comments)