-
Notifications
You must be signed in to change notification settings - Fork 61
/
bb100.js
92 lines (87 loc) · 2.69 KB
/
bb100.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
let songCount = 5;
let sort = 'billboard-hot-100';
if (typeof $argument !== 'undefined' && $argument !== '') {
const params = getParams($argument);
if (params.songCount !== undefined && !isNaN(params.songCount)) {
songCount = parseInt(params.songCount);
}
if (params.sort !== undefined && params.sort !== '') {
sort = params.sort;
}
} else if (typeof $persistentStore !== 'undefined') {
const persistentValue = $persistentStore.read("songCount");
if (persistentValue !== undefined && !isNaN(persistentValue)) {
songCount = parseInt(persistentValue);
}
const persistentValue2 = $persistentStore.read("sort");
if (persistentValue2 !== undefined && persistentValue2 !== '') {
sort = persistentValue2;
}
}
const url = `https://raw.githubusercontent.com/KoreanThinker/billboard-json/main/${sort}/recent.json`;
if (typeof $task !== "undefined") {
$task.fetch({ url: url }).then(
function(response) {
handleResponse(response.body);
},
function(reason) {
console.log(reason.error);
$done();
}
);
} else if (typeof $httpClient !== "undefined") {
$httpClient.get(url, function(error, response, body) {
if (error) {
console.log("请求失败:", error);
$done();
} else {
handleResponse(body);
}
});
}
function handleResponse(body) {
const data = JSON.parse(body);
if (data && data.data && data.data.length >= songCount) {
const songs = data.data.slice(0, songCount);
const notifications = [];
for (const song of songs) {
const { name, artist, rank, last_week_rank } = song;
let rankChange = '';
if (last_week_rank !== null) {
const rankDiff = last_week_rank - rank;
if (rankDiff >= 0) {
rankChange = `↑${rankDiff}`;
} else if (rankDiff < 0) {
rankChange = `↓${Math.abs(rankDiff)}`;
}
} else {
rankChange = '🆕';
}
let notification = `${rank}🎧`;
if (name !== undefined && name !== null) {
notification += `${name} - `;
}
if (artist !== undefined && artist !== null) {
notification += `${artist} `;
}
notification += rankChange;
notifications.push(notification);
}
if (typeof $task !== "undefined") {
$notify(`Top ${songCount} of ${sort}`, '', notifications.join('\n'));
} else if (typeof $httpClient !== "undefined") {
$notification.post(`Top ${songCount} of ${sort}`, '', notifications.join('\n'));
}
} else {
console.log('无法获取歌曲数据');
}
$done();
}
function getParams(param) {
return Object.fromEntries(
param
.split('&')
.map(item => item.split('='))
.map(([k, v]) => [k, decodeURIComponent(v)])
);
}