-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathindex.js
140 lines (132 loc) · 4.09 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
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
// 用户名和密码
const phone = '';
const password = '';
const rp = require('request-promise');
const tough = require('tough-cookie');
const webapi = require('./webapi');
const Cookie = tough.Cookie;
const crypto = require('crypto');
function MD5(str) {
return crypto.createHash('md5').update(str).digest('hex')
}
async function daka() {
const cookie = await rp.post(
'https://music.163.com/weapi/login/cellphone',
{
form: webapi({
phone: phone,
password: MD5(password),
countrycode: '86',
rememberLogin: 'true',
csrf_token: ''
}),
headers: {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.2 Safari/605.1.15',
Referer: 'https://music.163.com',
'Content-Type': 'application/x-www-form-urlencoded',
'Cookie': 'os=pc'
},
json: true,
resolveWithFullResponse: true,
transform(body, response) {
console.log(body);
return Object.assign(
...response.headers['set-cookie'].map(cookie => {
const c = Cookie.parse(cookie).toJSON();
const obj = {};
obj[c.key] = c.value;
return obj;
})
);
}
}
);
const cookieStr = Object.keys(cookie)
.map(k => `${k}=${cookie[k]}`)
.join(';');
const playlist = await rp.post(
'https://music.163.com/weapi/v1/discovery/recommend/resource',
{
form: webapi({
csrf_token: cookie.__csrf
}),
headers: {
cookie: cookieStr
},
json: true,
transform(body) {
if (body.code === 200) {
return body.recommend.map(r => r.id);
} else {
throw new Error('获取推荐歌曲失败');
}
}
}
);
let songid = [];
for (let index = 0; index < playlist.length; index++) {
const playlist_id = playlist[index];
const id = await rp.post(
'https://music.163.com/weapi/v3/playlist/detail',
{
qs: {
csrf_token: ''
},
form: webapi({
id: playlist_id,
n: 1000,
csrf_token: ''
}),
headers: {
cookie: cookieStr
},
json: true,
transform(body) {
if (body.code === 200) {
return body.playlist.trackIds.map(i => i.id);
} else {
throw new Error('获取歌曲ID失败');
}
}
}
);
songid = songid.concat(id);
}
const daka = await rp.post(
'http://music.163.com/weapi/feedback/weblog',
{
form: webapi({
logs: JSON.stringify(
songid.map(id => {
return {
action: 'play',
json: {
download: 0,
end: 'playend',
id: id.id,
sourceId: '',
time: 240,
type: 'song',
wifi: 0
}
};
})
)
}),
headers: {
cookie: cookieStr
},
json: true
}
);
if (daka.code === 200) {
return daka;
} else {
throw new Error(daka.message);
}
}
daka().then(e => {
console.log('打卡成功', e);
}).catch(err => {
console.log('打卡失败', err);
})