Skip to content

Instantly share code, notes, and snippets.

@jtblin
Created June 19, 2014 06:24
Show Gist options
  • Save jtblin/815a0d771dcefe59f691 to your computer and use it in GitHub Desktop.
Save jtblin/815a0d771dcefe59f691 to your computer and use it in GitHub Desktop.
Hubot Hacker News script
# Description
# Hacker News client
#
# Author:
# jtblin
#
# Commands:
# HN top <n> - Returns Hacker News top n results.
# HN search <query> <n> - Searches Hacker News for the query and returns the first n results.
# TODO: change API endpoint as api.hnsearch.com does not seem to be working anymore
request = require "request"
hnCommentsUrl = 'https://news.ycombinator.com/item?id='
baseUrl = 'http://api.thriftdb.com/api.hnsearch.com/'
top = 'items/_search?filter[fields][type]=submission&sortby=div(sub(points,1),pow(sum(div(abs(ms(create_ts,NOW)),3600000),2),1.8))%20desc'
search = 'items/_search?filter[fields][type]=submission&weights[title]=1.5&weights[domain]=2.0' +
'&boosts[fields][points]=0.3&boosts[fields][num_comments]=0.15' +
'&boosts[functions][pow(2,div(div(ms(create_ts,NOW),3600000),72))]=200.0&q='
module.exports = (robot) ->
robot.hear /HN top\s?(\d+)?/i, (msg) ->
count = msg.match[1] || 5
request.get baseUrl + top + '&start=0&limit=' + count, (err, res, body) ->
newsMe msg, res, body
robot.hear /HN search (\w+\s?\w*)(\s\d+)?$/i, (msg) ->
count = msg.match[2] || 5
request.get baseUrl + search + msg.match[1] + '&start=0&limit=' + count, (err, res, body) ->
newsMe msg, res, body
newsMe = (msg, res, body) ->
if res.statusCode is not 200
msg.send "Something's gone awry"
else
results = JSON.parse(body).results
for result in results
if result.item.url is null
url = hnCommentsUrl + result.item.id
else
url = result.item.url
msg.send "#{result.item.title}: #{url}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment