-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
65 lines (57 loc) · 1.4 KB
/
index.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
#!/usr/bin/env node
var FeedParser = require('feedparser')
, request = require('request')
, posts = []
, colors = require('colors')
, prompt = require('prompt')
, exec = require('child_process').exec
, platform = require('os').platform()
, i = 1;
const shellOpenCommand = {
'win32': 'start ',
'linux': 'xdg-open ',
'darwin': 'open '
}[platform];
request('https://news.ycombinator.com/rss')
.pipe(new FeedParser())
.on('error', function(error) {
console.log("An error occured");
})
.on('readable', function () {
var stream = this, item;
if(i < 29){
while (item = stream.read()) {
posts.push(item);
console.log(i.toString().red + ". " + item.title);
i++;
}
}
})
.on('finish', function(){
promptForPost();
});
function promptForPost() {
prompt.start();
var schema = {
properties: {
post: {
message: 'Type post number to open, or 0 to quit',
required: true
},
}
};
prompt.get(schema, function (err, result) {
if( !result || !result.post ) return console.log('\r');
if(result.post !== "0"){
var i = parseInt(result.post);
if(isNaN(i) || i > posts.length || i < 1) {
console.log("Invalid post number");
} else {
exec(shellOpenCommand + posts[i - 1].link, function(error){
if(error) throw error;
});
}
promptForPost();
}
});
}