-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.js
65 lines (58 loc) · 1.86 KB
/
request.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
/*
* liuzy <mostic1122@gmail.com>
* MIT Licensed
*/
import http from 'http'
import fs from 'fs'
import crypto from './Crypto'
import querystring from 'querystring'
const getLyricById = function (songId, songName, albumName) {
let text = JSON.stringify({ id: songId, lv: 1 })
let postBody = querystring.stringify(crypto.aesRsaEncrypt(text))
let options = {
hostname: 'music.163.com',
port: 80,
path: '/weapi/song/lyric?csrf_token=',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(postBody)
}
}
let req = http.request(options, (res) => {
//console.log(`STATUS: ${res.statusCode}`);
//console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
res.setEncoding('utf8')
let lyricContent = ''
res.on('data', (chunk) => lyricContent += chunk)
res.on('end', () => {
let lyric = JSON.parse(lyricContent)['lrc']['lyric']
lyric = lyric.replace(/\[.*\]/g, '')
fs.writeFile(albumName + '/' + songName + '.txt', lyric, (err) => {
if (err) console.log(`err: ${err}`)
console.log(`${songName} done! \u{2705}`)
})
});
})
req.write(postBody)
req.end()
}
const getSongListByAlbumId = (albumId, albumName, rl) => {
let api = "http://music.163.com/album?id=" + albumId;
http.get(api, (res) => {
res.setEncoding('utf8')
let lyric = ""
res.on('data', (chunk) => lyric += chunk)
res.on('end', () => {
let reg = /<textarea\s+id="song-list-pre-data"\s+style="display:none;">(.*)<\/textarea>/, matched, songList;
matched = lyric.match(reg)
if (matched && matched.length > 0) songList = matched[1]
songList = JSON.parse(songList)
for (let song of songList) {
getLyricById(song['id'], song['name'], albumName)
}
rl.close()
})
})
}
export { getSongListByAlbumId }