-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·43 lines (29 loc) · 1.03 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
#!/usr/bin/env node
const program = require('commander');
const request = require('request-promise');
const colors = require('colors/safe');
program
.command('search <search_query>')
.alias('s')
.description('Search for a question')
.action((searchKey)=>{
console.log('\t '+colors.yellow.bold(searchKey)+'\n');
getTopAnswer(searchKey);
})
program.parse(process.argv);
function getTopAnswer(searchKey){
var queryLink='http://api.stackexchange.com/2.2/search/advanced?sort=relevance&site=stackoverflow&q='+queryBuilder(searchKey);
request(queryLink,{json:true,gzip:true})
.then(function(body){
console.log('\n')
body["items"].reverse().forEach((item,index)=>{
console.log((body["items"].length-index)+" "+colors.bold(item.title)+"\n"+colors.blue.underline(item.link)+'\n\n')
})
}).
catch(function(err){
console.log(err)
});
}
function queryBuilder(searchKey){
return searchKey.replace(/ /g,"+")
}