-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
48 lines (48 loc) · 2.13 KB
/
main.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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const axios_1 = require("axios");
const chalk_1 = require("chalk");
const defaultURI = 'https://news.ycombinator.com/item?id=';
const endpoints = {
top: 'https://hacker-news.firebaseio.com/v0/topstories.json',
new: 'https://hacker-news.firebaseio.com/v0/newstories.json',
best: 'https://hacker-news.firebaseio.com/v0/beststories.json',
ask: 'https://hacker-news.firebaseio.com/v0/askstories.json',
show: 'https://hacker-news.firebaseio.com/v0/showstories.json',
job: 'https://hacker-news.firebaseio.com/v0/jobstories.json',
item: 'https://hacker-news.firebaseio.com/v0/item/'
};
const hardlimit = 100;
let limit = process.argv[3] || 30;
if (limit > hardlimit) {
console.log(`Capping limit to ${hardlimit} for now.`);
limit = hardlimit;
}
const useEndpoint = endpoints.hasOwnProperty(process.argv[2]) ? process.argv[2] : 'top';
axios_1.default.get(endpoints[useEndpoint]).then(response => {
const ids = response.data.slice(0, limit);
const promises = ids.map((id) => axios_1.default.get(`${endpoints.item}/${id}.json`));
axios_1.default.all(promises).then(response => {
response
.filter((resp) => resp && resp.data)
.map((resp) => {
let data = resp.data;
let comments = '';
if (data.descendants) {
const ncommentStr = data.descendants > 1 ? 'replies' : 'reply';
let commentURI = '';
if (data.url && data.url !== `${defaultURI}${data.id}`) {
commentURI = chalk_1.default.gray.underline(`${defaultURI}${data.id}`);
commentURI = ' ' + commentURI;
}
comments = chalk_1.default.gray(`(${data.descendants} ${ncommentStr}${commentURI})`);
}
if (!data.url) {
data.url = `${defaultURI}${data.id}`;
}
let uri = chalk_1.default.white.underline(data.url);
return ` ${chalk_1.default.bold(data.title)} — ${uri} ${comments}`;
})
.forEach(story => console.log(story));
});
});