This repository has been archived by the owner on Jun 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
hackernews.coffee
59 lines (55 loc) · 1.65 KB
/
hackernews.coffee
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
# Description:
# Hacker News
#
# Dependencies:
# "nodepie": "0.5.0"
#
# Configuration:
# None
#
# Commands:
# hubot hn top <N> - get the top N items on hacker news (or your favorite RSS feed)
# hn.top - refer to the top item on hn
# hn[i] - refer to the ith item on hn
#
# Author:
# skimbrel
NodePie = require("nodepie")
hnFeedUrl = "https://news.ycombinator.com/rss"
module.exports = (robot) ->
robot.respond /HN top (\d+)?/i, (msg) ->
msg.http(hnFeedUrl).get() (err, res, body) ->
if res.statusCode is not 200
msg.send "Something's gone awry"
else
feed = new NodePie(body)
try
feed.init()
count = msg.match[1] || 5
items = feed.getItems(0, count)
msg.send item.getTitle() + ": " + item.getPermalink() + " (" + item.getComments()?.html + ")" for item in items
catch e
console.log(e)
msg.send "Something's gone awry"
robot.hear /HN(\.top|\[\d+\])/i, (msg) ->
msg.http(hnFeedUrl).get() (err, res, body) ->
if res.statusCode is not 200
msg.send "Something's gone awry"
else
feed = new NodePie(body)
try
feed.init()
catch e
console.log(e)
msg.send "Something's gone awry"
element = msg.match[1]
if element == "HN.top"
idx = 0
else
idx = (Number) msg.match[0].replace(/[^0-9]/g, '')
try
item = feed.getItems()[idx]
msg.send item.getTitle() + ": " + item.getPermalink() + " (" + item.getComments()?.html + ")"
catch e
console.log(e)
msg.send "Something's gone awry"