-
-
Notifications
You must be signed in to change notification settings - Fork 744
/
Copy pathProviderMusixmatch.js
191 lines (149 loc) · 4.9 KB
/
ProviderMusixmatch.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
const ProviderMusixmatch = (() => {
const headers = {
authority: "apic-desktop.musixmatch.com",
cookie: "x-mxm-token-guid=",
};
async function findLyrics(info) {
const baseURL =
"https://apic-desktop.musixmatch.com/ws/1.1/macro.subtitles.get?format=json&namespace=lyrics_richsynched&subtitle_format=mxm&app_id=web-desktop-app-v1.0&";
const durr = info.duration / 1000;
const params = {
q_album: info.album,
q_artist: info.artist,
q_artists: info.artist,
q_track: info.title,
track_spotify_id: info.uri,
q_duration: durr,
f_subtitle_length: Math.floor(durr),
usertoken: CONFIG.providers.musixmatch.token,
};
const finalURL =
baseURL +
Object.keys(params)
.map((key) => `${key}=${encodeURIComponent(params[key])}`)
.join("&");
let body = await Spicetify.CosmosAsync.get(finalURL, null, headers);
body = body.message.body.macro_calls;
if (body["matcher.track.get"].message.header.status_code !== 200) {
return {
error: `Requested error: ${body["matcher.track.get"].message.header.mode}`,
uri: info.uri,
};
}
if (body["track.lyrics.get"]?.message?.body?.lyrics?.restricted) {
return {
error: "Unfortunately we're not authorized to show these lyrics.",
uri: info.uri,
};
}
return body;
}
async function getKaraoke(body) {
const meta = body?.["matcher.track.get"]?.message?.body;
if (!meta) {
return null;
}
if (!meta.track.has_richsync || meta.track.instrumental) {
return null;
}
const baseURL = "https://apic-desktop.musixmatch.com/ws/1.1/track.richsync.get?format=json&subtitle_format=mxm&app_id=web-desktop-app-v1.0&";
const params = {
f_subtitle_length: meta.track.track_length,
q_duration: meta.track.track_length,
commontrack_id: meta.track.commontrack_id,
usertoken: CONFIG.providers.musixmatch.token,
};
const finalURL =
baseURL +
Object.keys(params)
.map((key) => `${key}=${encodeURIComponent(params[key])}`)
.join("&");
let result = await Spicetify.CosmosAsync.get(finalURL, null, headers);
if (result.message.header.status_code !== 200) {
return null;
}
result = result.message.body;
const parsedKaraoke = JSON.parse(result.richsync.richsync_body).map((line) => {
const startTime = line.ts * 1000;
const endTime = line.te * 1000;
const words = line.l;
const text = words.map((word, index, words) => {
const wordText = word.c;
const wordStartTime = word.o * 1000;
const nextWordStartTime = words[index + 1]?.o * 1000;
const time = !Number.isNaN(nextWordStartTime) ? nextWordStartTime - wordStartTime : endTime - (wordStartTime + startTime);
return {
word: wordText,
time,
};
});
return {
startTime,
text,
};
});
return parsedKaraoke;
}
function getSynced(body) {
const meta = body?.["matcher.track.get"]?.message?.body;
if (!meta) {
return null;
}
const hasSynced = meta?.track?.has_subtitles;
const isInstrumental = meta?.track?.instrumental;
if (isInstrumental) {
return [{ text: "♪ Instrumental ♪", startTime: "0000" }];
}
if (hasSynced) {
const subtitle = body["track.subtitles.get"]?.message?.body?.subtitle_list?.[0]?.subtitle;
if (!subtitle) {
return null;
}
return JSON.parse(subtitle.subtitle_body).map((line) => ({
text: line.text || "♪",
startTime: line.time.total * 1000,
}));
}
return null;
}
function getUnsynced(body) {
const meta = body?.["matcher.track.get"]?.message?.body;
if (!meta) {
return null;
}
const hasUnSynced = meta.track.has_lyrics || meta.track.has_lyrics_crowd;
const isInstrumental = meta?.track?.instrumental;
if (isInstrumental) {
return [{ text: "♪ Instrumental ♪" }];
}
if (hasUnSynced) {
const lyrics = body["track.lyrics.get"]?.message?.body?.lyrics?.lyrics_body;
if (!lyrics) {
return null;
}
return lyrics.split("\n").map((text) => ({ text }));
}
return null;
}
async function getTranslation(body) {
const track_id = body?.["matcher.track.get"]?.message?.body?.track?.track_id;
if (!track_id) return null;
const baseURL =
"https://apic-desktop.musixmatch.com/ws/1.1/crowd.track.translations.get?translation_fields_set=minimal&selected_language=en&comment_format=text&format=json&app_id=web-desktop-app-v1.0&";
const params = {
track_id,
usertoken: CONFIG.providers.musixmatch.token,
};
const finalURL =
baseURL +
Object.keys(params)
.map((key) => `${key}=${encodeURIComponent(params[key])}`)
.join("&");
let result = await Spicetify.CosmosAsync.get(finalURL, null, headers);
if (result.message.header.status_code !== 200) return null;
result = result.message.body;
if (!result.translations_list?.length) return null;
return result.translations_list.map(({ translation }) => ({ translation: translation.description, matchedLine: translation.matched_line }));
}
return { findLyrics, getKaraoke, getSynced, getUnsynced, getTranslation };
})();