This repository was archived by the owner on Aug 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhackernews.js
121 lines (105 loc) · 3.57 KB
/
hackernews.js
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const https = require('https');
const winston = require('winston');
const Plugin = require('../utils.js').Plugin;
const META = {
name: 'hackernews',
short: 'Retrieves top N stories from YC Hacker News',
examples: [
'hnews 10',
],
};
const hnAPIURL = 'https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty';
const hnStoryURL = 'https://hacker-news.firebaseio.com/v0/item/<STORY_ID>.json?print=pretty';
function retrieveStories(nStories) {
return new Promise((resolve, reject) => {
https.get(hnAPIURL, (res) => {
// Combine the chunks that are retrieved
const responseParts = [];
res.setEncoding('utf8');
res.on('data', (d) => {
responseParts.push(d);
});
// Combine the chunks and resolve
res.on('end', () => {
const storyIDs = JSON.parse(responseParts.join(''));
resolve(storyIDs.slice(0, nStories));
});
}).on('error', (e) => {
reject(e);
});
});
}
function retrieveStoryText(storyID, fields) {
return new Promise((resolve, reject) => {
const customURL = hnStoryURL.replace(/<STORY_ID>/, storyID);
https.get(customURL, (res) => {
// Combine the chunks that are retrieved
const responseParts = [];
res.setEncoding('utf8');
res.on('data', (d) => {
responseParts.push(d);
});
// Combine the chunks and resolve
res.on('end', () => {
const parsed = JSON.parse(responseParts.join(''));
const storyField = {
title: `${parsed.score} - ${parsed.title}`,
value: parsed.url,
short: false,
};
fields.push(storyField);
resolve();
});
}).on('error', (e) => {
reject(e);
});
});
}
function retrieveStoryDetails(storyIDs) {
return new Promise((resolve) => {
const fields = [];
const requests = [];
storyIDs.forEach((item) => {
requests.push(retrieveStoryText(item, fields));
});
Promise.all(requests).then(() => resolve(fields));
});
}
function hnews(options, message, nStories) {
if (nStories) {
retrieveStories(nStories)
.then(response => retrieveStoryDetails(response))
.then((response) => {
const attachment = {
as_user: true,
thread_ts: message.ts,
attachments: [
{
color: '#36a64f',
author_name: 'Bosta',
title: `Top ${nStories} Hacker News Stories`,
fields: response,
footer: 'Automation',
footer_icon: 'https://platform.slack-edge.com/img/default_application_icon.png',
},
],
};
// Post the message
options.web.chat.postMessage(message.channel, '', attachment, (err) => {
if (err) {
winston.error('HN Plugin Error:', err);
} else {
winston.info('Hackernews articles retrieved and pushed to relevant channel.');
}
});
});
}
}
function register(bot, rtm, web, config) {
const plugin = new Plugin({ bot, rtm, web, config });
plugin.route(/^hnews ([0-9]+):?/, hnews, {});
}
module.exports = {
register,
META,
};