-
Notifications
You must be signed in to change notification settings - Fork 0
/
pp
executable file
·86 lines (70 loc) · 1.91 KB
/
pp
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
#!/usr/bin/env node
var rp = require('request-promise');
var Player = require('player');
var app = require('commander');
var inquirer = require('inquirer')
var searchList = [];
app
.version('0.0.1')
.option('-g, --get <name>', 'get songs')
.option('-s, --search <name>', 'search songs', search)
.parse(process.argv)
function search(string) {
var option = {
uri: "http://apis.baidu.com/geekery/music/query",
headers: {apikey: '085d50117c493ec8a1b3d0926f8f493f'},
qs: {s: string, limit: '5'}
}
rp(option).then(function(rep) {
searchList = JSON.parse(rep).data.data.list;
var list = [];
searchList.forEach((item, index) => {
var obj = {};
obj.name = item.songName;
obj.value = {
index: index,
url : item.songUrl
};
list.push(obj);
})
return list;
}).then(function(list) {
console.log(list)
var question = [
{
type: "list",
name: 'songs',
message: "搜索结果",
choices: list,
}
]
inquirer.prompt(question, answer =>{
operate(answer.songs);
})
})
}
function operate(choice) {
var choices = [
{name: '添加到播放列表', value: 'add'},
{name: '播放', value: 'play'},
{name: '下载', value: 'download'}
]
var question = [
{
type: "list",
name: 'option',
message: "操作",
choices: choices,
}
]
inquirer.prompt(question, answer =>{
if (answer.option === 'play') {
play(choice);
};
})
}
function play(obj) {
console.log(obj);
var player = new Player(obj.url);
player.play();
}