-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsong.js
158 lines (152 loc) · 4.04 KB
/
song.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
const fs = require('fs');
const request = require('request');
const musicApi = require('music-api');
const path = require('path');
const ProgressBar = require('./progress');
const config = require('./config');
let searchData = [];
let page = 1;
let maxPage = 1;
let oldName = '';
async function search(name, callback) {
if (oldName !== name) {
page = 1;
}
oldName = name;
console.log(`开始搜索第${page}页`, name);
try {
const res = await musicApi.searchSong(config.type, {
key: name,
limit: 10,
page
});
if (res.success) {
if (!res.songList.length) {
console.log('查询到的数据为空, 请从新输入要搜索的内容, 或输入`exit`退出;');
type = false;
callback('again');
return;
}
searchData = res.songList.map((v, i) => {
const names = v.artists.reduce((a, b) => {
const left = typeof a === 'object' ? a.name : a;
return `${left}/${b.name}`;
});
const obj = {
album: v.album.name,
name: v.name,
id: v.id,
artists: names.name || names
};
console.table(`索引: ${i}, 歌曲名: ${obj.name}, 歌手: ${obj.artists};`);
return obj;
});
fs.writeFile('./data.json', JSON.stringify(searchData), (error) => {
if (error) {
console.log('写入失败');
console.log(error);
callback(1);
} else {
maxPage = Math.ceil(res.total / 10);
console.log(`当前第${page}页, 共${Math.ceil(res.total / 10)}页`);
console.log('在索引输入框中输入`w`,并回车,切换下一页, 输入`q`, 并回车, 切换上一页');
callback();
}
});
} else {
console.log('搜索歌曲失败');
console.log(res);
callback(1);
}
} catch (e) {
callback(1);
}
}
async function getSongUrl(i, callback) {
try {
console.log('开始查询歌曲的地址');
const item = searchData[i];
const songID = item.id + '';
const res = await musicApi.getSong(config.type, {
id: songID,
raw: false,
br: 999000
});
if (res.success) {
console.log('查询歌曲的地址成功');
const url = res.url;
const songName = `${item.name.replace(/\//g, '_') + '-' + item.artists.replace(/\//g, '_')}${url.substr(url.lastIndexOf('.'), url.length)}`;
console.log('开始下载', songName);
const songStream = fs.createWriteStream(path.join(__dirname, './songs/' + songName));
// 总共要下载的数量
let allSize = 0;
// 已下载的数量
let toSize = 0;
const pb = new ProgressBar('下载进度', 50);
request({
method: 'GET',
uri: url
})
.on('response', (data) => {
// 更新总文件字节大小
allSize = parseInt(data.headers['content-length'], 10);
})
.on('data', (chunk) => {
// 更新下载的文件块字节大小
toSize += chunk.length;
pb.render({
completed: toSize,
total: allSize
});
})
.on('error', function (err) {
console.log(songName, '下载失败');
console.log(err);
callback(1);
})
.pipe(songStream)
.on('close', () => {
console.log(songName, ' 下载完成!');
callback();
});
} else {
console.log('查询歌曲的地址失败');
console.log(res);
callback();
}
} catch (e) {
callback(1);
}
}
function changePage(type, callback) {
if (type === 'q') {
if (page > 1) {
page -= 1;
callback();
} else {
console.log('已经是第一页了');
callback(1);
}
}
if (type === 'w') {
page += 1;
if (page > maxPage) {
console.log('已经是最后一页');
callback(1);
return
}
callback();
}
}
function restart() {
page = 1;
maxPage = 1;
oldName = '';
searchData = []
}
module.exports = {
search,
getSongUrl,
changePage,
restart
};