-
Notifications
You must be signed in to change notification settings - Fork 65
/
index.js
76 lines (64 loc) · 1.65 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
require('dotenv').config();
const { Octokit } = require('@octokit/rest');
const { user_record } = require('NeteaseCloudMusicApi');
const {
GIST_ID: gistId,
GH_TOKEN: githubToken,
USER_ID: userId,
USER_TOKEN: userToken,
} = process.env;
(async () => {
/**
* First, get user record
*/
const record = await user_record({
cookie: `MUSIC_U=${userToken}`,
uid: userId,
type: 1, // last week
}).catch(error => console.error(`Unable to get user record \n${error}`));
/**
* Second, get week play data and parse into song/plays diagram
*/
let totalPlayCount = 0;
const { weekData } = record.body;
weekData.forEach(data => {
totalPlayCount += data.playCount;
});
const icon = ['🥇', '🥈', '🥉', '', '']
const lines = weekData.slice(0, 5).reduce((prev, cur, index) => {
const playCount = cur.playCount;
const artists = cur.song.ar.map(a => a.name);
let name = `${cur.song.name} - ${artists.join('/')}`;
const line = [
icon[index].padEnd(2),
name,
' · ',
`${playCount}`,
'plays',
];
return [...prev, line.join(' ')];
}, []);
/**
* Finally, write into gist
*/
try {
const octokit = new Octokit({
auth: `token ${githubToken}`,
});
const gist = await octokit.gists.get({
gist_id: gistId,
});
const filename = Object.keys(gist.data.files)[0];
await octokit.gists.update({
gist_id: gistId,
files: {
[filename]: {
filename: `🎵 My last week in music`,
content: lines.join('\n'),
},
},
});
} catch (error) {
console.error(`Unable to update gist\n${error}`);
}
})();