cleaned up code and remove unneeded error checks
This commit is contained in:
parent
1a6b665e06
commit
1ff5bda620
|
@ -1,29 +1,10 @@
|
||||||
import re
|
|
||||||
import urlparse
|
|
||||||
|
|
||||||
from util import http, hook
|
from util import http, hook
|
||||||
|
|
||||||
|
|
||||||
base_api = 'https://hacker-news.firebaseio.com/v0/item/'
|
@hook.regex(r'(?i)https://(?:www\.)?news\.ycombinator\.com\S*id=(\d+)')
|
||||||
|
|
||||||
|
|
||||||
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):
|
def hackernews(match):
|
||||||
parsed = urlparse.urlparse(match.group())
|
base_api = 'https://hacker-news.firebaseio.com/v0/item/'
|
||||||
id = urlparse.parse_qs(parsed.query)['id'][0]
|
entry = http.get_json(base_api + match.group(1) + ".json")
|
||||||
entry = get_by_id(id)
|
|
||||||
|
|
||||||
if not entry:
|
|
||||||
return
|
|
||||||
|
|
||||||
if entry['type'] == "story":
|
if entry['type'] == "story":
|
||||||
return "{title} by {by} with {score} points and {descendants} comments ({url})".format(**entry)
|
return "{title} by {by} with {score} points and {descendants} comments ({url})".format(**entry)
|
||||||
|
|
Loading…
Reference in New Issue