-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhacker-news.js
41 lines (35 loc) · 1.05 KB
/
hacker-news.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
const API = require('./api');
const types = require('../types');
class HackerNews extends API {
constructor() {
const baseURL = 'https://hacker-news.firebaseio.com/v0/';
super(baseURL, types.HackerNews, 'https://news.ycombinator.com/');
this.normalize = this.normalize.bind(this);
}
normalize(data) {
// Need to time * 1000 for unix time conversion
return data.map(({ title, url, time, id }) => (
{
title,
link: url || `https://news.ycombinator.com/item?=${id}`,
publishedAt: new Date(time * 1000),
type: this.type,
commentsID: id,
}
));
}
async getTopStories() {
const topStoryIds = await this.fetch('/topstories.json');
const top30 = topStoryIds.slice(0, 30).map(id => this.getItem(id));
const topStories = await Promise.all(top30);
return topStories;
}
getItem(id) {
return this.fetch(`/item/${id}.json`)
.catch(err => { throw err });
}
get listing() {
return this.getTopStories().then(this.normalize);
}
}
module.exports = new HackerNews();