From 1a6b665e060e881b95d3eb6701f44abaa84d2b54 Mon Sep 17 00:00:00 2001 From: Ell Date: Thu, 23 Jul 2015 18:01:33 -0700 Subject: [PATCH 1/2] added support for retrieving information from hackernews urls --- plugins/hackernews.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 plugins/hackernews.py diff --git a/plugins/hackernews.py b/plugins/hackernews.py new file mode 100644 index 0000000..a707d8b --- /dev/null +++ b/plugins/hackernews.py @@ -0,0 +1,33 @@ +import re +import urlparse + +from util import http, hook + + +base_api = 'https://hacker-news.firebaseio.com/v0/item/' + + +def get_by_id(id): + url = base_api + id + ".json?print=pretty" + + try: + return http.get_json(url) + except ValueError: + return None + + +@hook.regex(r'(?i)https://(?:www\.)?news\.ycombinator\.com(?:/.+)?') +def hackernews(match): + parsed = urlparse.urlparse(match.group()) + id = urlparse.parse_qs(parsed.query)['id'][0] + entry = get_by_id(id) + + if not entry: + return + + if entry['type'] == "story": + return "{title} by {by} with {score} points and {descendants} comments ({url})".format(**entry) + + if entry['type'] == "comment": + return '"{text}" -- {by}'.format(**entry) + From 1ff5bda6200d45abcad6d9779803a4e3afb7852a Mon Sep 17 00:00:00 2001 From: Ell Date: Thu, 23 Jul 2015 19:41:30 -0700 Subject: [PATCH 2/2] cleaned up code and remove unneeded error checks --- plugins/hackernews.py | 25 +++---------------------- 1 file changed, 3 insertions(+), 22 deletions(-) diff --git a/plugins/hackernews.py b/plugins/hackernews.py index a707d8b..1b53167 100644 --- a/plugins/hackernews.py +++ b/plugins/hackernews.py @@ -1,29 +1,10 @@ -import re -import urlparse - from util import http, hook -base_api = 'https://hacker-news.firebaseio.com/v0/item/' - - -def get_by_id(id): - url = base_api + id + ".json?print=pretty" - - try: - return http.get_json(url) - except ValueError: - return None - - -@hook.regex(r'(?i)https://(?:www\.)?news\.ycombinator\.com(?:/.+)?') +@hook.regex(r'(?i)https://(?:www\.)?news\.ycombinator\.com\S*id=(\d+)') def hackernews(match): - parsed = urlparse.urlparse(match.group()) - id = urlparse.parse_qs(parsed.query)['id'][0] - entry = get_by_id(id) - - if not entry: - return + base_api = 'https://hacker-news.firebaseio.com/v0/item/' + entry = http.get_json(base_api + match.group(1) + ".json") if entry['type'] == "story": return "{title} by {by} with {score} points and {descendants} comments ({url})".format(**entry)