forked from llnancy/netease-music-box
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
122 lines (106 loc) · 3.05 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
require('dotenv').config();
const { Octokit } = require('@octokit/rest');
const { user_record } = require('NeteaseCloudMusicApi');
const fs = require('fs');
const {
NETEASE_GIST_ID: gistId,
GH_TOKEN: githubToken,
NETEASE_USER_ID: userId,
NETEASE_USER_TOKEN: userToken,
NETEASE_MUSIC_START_TAG: startTag,
NETEASE_MUSIC_END_TAG: endTag,
} = 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`);
console.error(error);
});
/**
* Second, get week play data and parse into song/plays diagram
*/
const { weekData } = record.body;
const icon = ['🥇', '🥈', '🥉', '🏅', '🏅']
const lines = weekData.slice(0, 5).reduce((prev, cur, index) => {
console.log(cur);
const playCount = cur.playCount;
const artists = cur.song.ar.map(a => a.name);
let name = `${cur.song.name} - ${artists.join('/')}`;
console.log(name);
console.log(name.length);
let flag = name.length > 11;
name = name.slice(0, 11);
name = flag ? name + '...' : name;
let tab;
if (name.length <= 8) {
tab = '\t\t\t\t';
} else {
tab = '\t\t\t';
}
const line = [
icon[index],
' ' + name,
tab,
`${playCount}`,
'次 ',
];
return [...prev, line.join('')];
}, []);
/**
* Finally, write into gist
*/
const title = `🎵 我最近一周的听歌排行`;
let content = lines.join('\n');
if (content === '') {
content = 'Oh my God!\n~~~~~~\n我最近一周竟然没有听歌~\n~~~~~~'
}
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: title,
content: content,
},
},
});
} catch (error) {
console.error(`Unable to update gist\n${error}`);
}
// write to markdown
const markdownFile = process.env.MARKDOWN_FILE;
const start = startTag === undefined ? '<!-- netease-music-box start -->' : startTag;
const end = endTag === undefined ? '<!-- netease-music-box end -->' : endTag;
const markdownTitle = `\n#### <a href="https://gist.github.com/${gistId}" target="_blank">${title}</a>\n`;
const markdownContent = content;
if (markdownFile) {
fs.readFile(markdownFile, 'utf8', (err, data) => {
if (err) {
console.error(err);
return;
}
const startIndex = data.indexOf(start) + start.length;
const endIndex = data.indexOf(end);
const markdown = data.substring(0, startIndex) + markdownTitle + '```text\n' + markdownContent + '\n```\n\n' + data.substring(endIndex);
fs.writeFile(markdownFile, markdown, err => {
if (err) {
console.error(err);
return;
}
});
});
}
})();