-
Notifications
You must be signed in to change notification settings - Fork 95
/
NetEaseMusic.js
286 lines (281 loc) · 10.2 KB
/
NetEaseMusic.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
var request = require('superagent');
//var process = require('process');
var async = require('async');
var crypto = require('./Crypto');
var fm = require('./FileManager');
var PltM = require('./PlaylistModel');
var SongM = require('./SongModel');
var header = {
'Accept': '*/*',
'Accept-Encoding': 'gzip,deflate,sdch',
'Accept-Language': 'zh-CN,en-US;q=0.7,en;q=0.3',
'Connection': 'keep-alive',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Host': 'music.163.com',
'Referer': 'http://music.163.com/',
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:39.0) Gecko/20100101 Firefox/39.0'
}
var httpRequest = function (method, url, data, callback) {
var ret;
if (method == 'post') {
ret = request.post(url).send(data);
} else {
ret = request.get(url).query(data);
}
var cookie = fm.getCookie();
if (cookie)ret.set('Cookie', cookie);
ret.set(header).timeout(10000).end(callback);
}
module.exports = {
login: function (username, password, callback) {
var url, pattern = /^0\d{2,3}\d{7,8}$|^1[34578]\d{9}$/,
body = {
password: crypto.MD5(password),
rememberLogin: 'true'
};
if (pattern.test(username)) {
//手机登录
body.phone = username;
url = 'http://music.163.com/weapi/login/cellphone/';
} else {
//邮箱登录
body.username = username;
url = 'http://music.163.com/weapi/login/';
}
var encBody = crypto.aesRsaEncrypt(JSON.stringify(body));
httpRequest('post', url, encBody, function (err, res) {
if (err) {
callback({msg: '[login]http error ' + err, type: 1});
return;
}
var data = JSON.parse(res.text);
if (data.code != 200) {
//登录失败
callback({msg: "[login]username or password incorrect", type: 0});
} else {
fm.setCookie(res.header['set-cookie']);
callback(null, data.profile);
}
});
},
getNet: function (callback) {
var that = this;
this.userPlaylist(function (err, playlists) {
if (err) {
callback(err);
return;
}
async.map(playlists, function (item, callback) {
that.playlistDetail(item.id, function (err, songList) {
if (err) {
callback(err);
return;
}
var pltm = new PltM({
name: item.name,
type: 'net',
songList: songList
});
callback(null, pltm);
});
}, function (err, results) {
if (err) {
callback(err);
} else
callback(null, results);
});
})
},
userProfile: function (uid, callback) {
uid = uid || fm.getUserID();
if (!uid) {
callback({msg: '[userProfile]user not login', type: 0});
} else {
var url = 'http://music.163.com/api/user/detail/' + uid;
httpRequest('get', url, {'userId': uid}, function (err, res) {
if (err) {
callback({msg: '[userProfile]http error ' + err, type: 1});
} else if (res.body.code != 200) {
callback({msg: '[userProfile]http code ' + res.body.code, type: 1});
} else {
callback(null, res.body.profile);
}
});
}
},
userPlaylist: function () {
// [uid],[offset],[limit],callback
var argv = [].slice.call(arguments);
var callback = argv.pop();
var uid = fm.getUserID();
if (!uid) {
callback({msg: '[userPlaylist]user do not login', type: 0});
return;
}
var uid = argv[0] || uid;
var offset = argv[1] || 0;
var limit = argv[2] || 100;
var url = 'http://music.163.com/api/user/playlist/';
var data = {
"offset": offset,
"limit": limit,
"uid": uid
}
httpRequest('get', url, data, function (err, res) {
if (err) {
callback({msg: '[userPlaylist]http timeout', type: 1});
return;
}
if (res.body.code != 200)callback({msg: '[userPlaylist]http code ' + data.code, type: 1});
else {
callback(null, res.body.playlist);
}
});
},
playlistDetail: function (id, callback) {
var url = 'http://music.163.com/api/playlist/detail';
var data = {"id": id}
var that = this;
httpRequest('get', url, data, function (err, res) {
if (err)callback({msg: '[playlistDetail]http timeout', type: 1});
else {
if (res.body.code != 200)callback({msg: '[playlistDetail]http code ' + data.code, type: 1});
else callback(null, that.transfer(res.body.result.tracks));
}
});
},
// 搜索单曲(1),歌手(100),专辑(10),歌单(1000),用户(1002) *(type)*
search: function () {
//s, stype, offset, total, limit,callback;
var argv = [].slice.call(arguments);
var callback = argv.pop();
var s = argv[0];
var stype = argv[1] || 1;
var offset = argv[2] || 0;
var total = argv[3] || 'true';
var limit = argv[4] || fm.getSearchLimit();
var url = 'http://music.163.com/api/search/get/web';
var data = {
's': s,
'type': stype,
'offset': offset,
'total': total,
'limit': limit
}
var that = this;
httpRequest('post', url, data, function (err, res) {
if (err) {
callback({msg: '[search]http error ' + err, type: 1});
return;
}
var doc = JSON.parse(res.text);
if (doc.code != 200)callback({msg: '[search]http code ' + doc.code, type: 1});
else {
var results = doc.result.songs;
var data = that.transfer(results);
callback(null, data);
}
});
},
transfer: function (results) {
var songList = [];
var idArray = [];
var idMap = {};
for (var i = 0; i < results.length; i++) {
var r = results[i];
idArray.push(r.id);
idMap[r.id] = i;
var o = {src: ''};
o.id = r.id;
o.title = r.name;
o.album = r.album.name;
o.artist = r.artists.map(function (v) {
return v.name;
}).join();
songList.push(new SongM(o));//modefy here!
}
var that = this;
process.nextTick(function () {
// >100时分批查询
var num = Math.ceil(idArray.length / 100);
for (var k = 0; k < num; k++) {
var idTmp = idArray.slice(k * 100, Math.min((k + 1) * 100, idArray.length));
that.songsDetail(idTmp, function (err, songs) {
if (err) {
throw(err.msg);
return;
}
for (var i = 0; i < songs.length; i++) {
var index = idMap[songs[i].id];
songList[index].src = songs[i].mp3Url;
songList[index].pic = songs[i].album.picUrl;
}
});
}
});
return songList;
},
songsDetail: function (ids, callback) {
var url = 'http://music.163.com/api/song/detail';
httpRequest('get', url, {ids: '[' + ids.join() + ']'}, function (err, res) {
if (err) {
callback({msg: '[songsDetail]http error ' + err, type: 1});
return;
}
var doc = JSON.parse(res.text);
if (doc.code != 200)callback({msg: '[songsDetail]http code ' + doc.code, type: 1});
else callback(null, doc.songs);
});
},
songDetail: function (id, callback) {
var url = "http://music.163.com/api/song/detail";
httpRequest('get', url, {id: id, ids: '[' + id + ']'}, function (err, res) {
if (err) {
callback({msg: '[songDetail]http error ' + err, type: 1});
return;
}
var doc = JSON.parse(res.text);
if (doc.code != 200)callback({msg: '[songDetail]http code ' + doc.code, type: 1});
else callback(null, doc);
});
},
songLyric: function (id, callback) {
var url = "http://music.163.com/api/song/lyric";
httpRequest('get', url, {os: 'android', id: id, lv: -1, tv: -1}, function (err, res) {
if (err) {
callback({msg: '[songLyric]http error ' + err, type: 1});
return;
}
var doc = JSON.parse(res.text);
if (doc.lrc) {
callback(null, doc.lrc.lyric);
}
else {
callback({msg: '[songLyric]lrc do not exist', type: 0});
}
});
},
radio: function (callback) {
var url = 'http://music.163.com/api/radio/get';
httpRequest('get', url, null, function (err, res) {
if (err) {
callback({msg: '[radio]http error ' + err, type: 1});
return;
}
var doc = JSON.parse(res.text);
if (doc.code != 200)callback({msg: '[radio]http code ' + doc.code, type: 1});
else callback(null, doc.data.map(function (o) {
return new SongM({
id: o.id,
src: o.mp3Url,
pic: o.album.picUrl,
artist: o.artists.map(function (v) {
return v.name;
}).join(),
album: o.album.name,
title: o.name
});
}));
});
}
}