Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions dist/src/lyrics/musixmatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ const BRACKET_JUNK = /\s*\[([^\]]*(?:official|lyrics?|video|audio|mv|visualizer|
const FEAT_PATTERN = /\s*[([]\s*(?:ft\.?|feat\.?|featuring)\s+[^)\]]+[)\]]/gi;
const SEPARATORS = [' - ', ' – ', ' — ', ' ~ '];
const DEFAULT_UA = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36';
const DEFAULT_COOKIE = 'AWSELB=unknown; x-mxm-user-id=undefined; x-mxm-token-guid=undefined; mxm-encrypted-token=';
const isAuthCode = (code) => code === 401 || code === 403;
const extractMacroCalls = (body) => {
const calls = body?.macro_calls;
Expand Down Expand Up @@ -287,14 +286,18 @@ export default class MusixmatchLyrics {
? ''
: Array.from(this.cookies, ([k, v]) => `${k}=${v}`).join('; ');
}
/* INFO: Musixmatch's AWS edge returns 403 for the old placeholder cookie, so only send cookies it actually issued */
_cookieHeader() {
const cookie = this._getCookies();
return cookie ? { cookie } : {};
}
async _fetchToken() {
const url = _buildUrl(ENDPOINTS.TOKEN, { app_id: APP_ID });
const { statusCode, headers, body } = await http1makeRequest(url, {
method: 'GET',
headers: {
accept: '*/*',
'accept-language': 'en',
cookie: DEFAULT_COOKIE,
'user-agent': DEFAULT_UA
}
});
Expand Down Expand Up @@ -407,7 +410,7 @@ export default class MusixmatchLyrics {
headers: {
accept: 'application/json',
'user-agent': DEFAULT_UA,
cookie: this._getCookies()
...this._cookieHeader()
}
});
if (!this.useManualToken && headers?.['set-cookie']) {
Expand All @@ -432,7 +435,7 @@ export default class MusixmatchLyrics {
headers: {
accept: 'application/json',
'user-agent': DEFAULT_UA,
cookie: this._getCookies()
...this._cookieHeader()
}
});
if (retryHeaders?.['set-cookie']) {
Expand Down
38 changes: 32 additions & 6 deletions dist/src/managers/lyricsManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,17 @@ export default class LyricsManager {
await fs.mkdir(lyricsDir, { recursive: true });
}
}
/* INFO: A provider that throws must not abort the search, so a throw becomes an error result like any other failure */
async _fetchFrom(source, name, trackInfo, language) {
try {
return await source.getLyrics(trackInfo, language);
}
catch (e) {
const message = getErrorMessage(e);
logger('warn', 'Lyrics', `Lyrics source ${name} threw: ${message}`);
return { loadType: 'error', data: { message, severity: 'fault' } };
}
}
async loadLyrics(decodedTrack, language, skipTrackSource = false, requestedSource) {
if (!decodedTrack?.info?.sourceName || !decodedTrack.info?.uri) {
logger('warn', 'Lyrics', 'Invalid track object provided to loadLyrics', decodedTrack);
Expand Down Expand Up @@ -133,6 +144,8 @@ export default class LyricsManager {
: undefined;
const isYouTube = sourceName === 'youtube' || sourceName === 'ytmusic';
let youtubeCaptions = null;
let lastError = null;
let sawEmpty = false;
if (lyricsSource && !skipTrackSource) {
if (isYouTube) {
try {
Expand All @@ -146,13 +159,17 @@ export default class LyricsManager {
}
}
else {
const lyrics = await lyricsSource.getLyrics(trackInfo, language);
if (lyrics && lyrics.loadType !== 'empty') {
if (lyrics.loadType === 'lyrics') {
lyrics.data.provider = sourceName;
}
const lyrics = await this._fetchFrom(lyricsSource, sourceName, trackInfo, language);
if (lyrics?.loadType === 'lyrics') {
lyrics.data.provider = sourceName;
return lyrics;
}
if (lyrics?.loadType === 'error') {
lastError = lyrics;
}
else {
sawEmpty = true;
}
}
}
const sources = new Set([
Expand All @@ -164,7 +181,11 @@ export default class LyricsManager {
if (!source || name === sourceName)
continue;
logger('debug', 'Lyrics', `Trying lyrics source ${name} for ${trackInfo?.title || 'Unknown Title'}.`);
const lyrics = await source.getLyrics(trackInfo, language);
const lyrics = await this._fetchFrom(source, name, trackInfo, language);
if (lyrics?.loadType === 'error') {
lastError = lyrics;
continue;
}
if (lyrics && lyrics.loadType !== 'empty') {
if (isYouTube &&
youtubeCaptions?.loadType === 'lyrics' &&
Expand All @@ -186,11 +207,16 @@ export default class LyricsManager {
}
return lyrics;
}
sawEmpty = true;
}
if (isYouTube && youtubeCaptions?.loadType === 'lyrics') {
youtubeCaptions.data.provider = sourceName;
return youtubeCaptions;
}
/* INFO: Only report a failure when every provider that answered failed; otherwise the track simply has no lyrics */
if (lastError && !sawEmpty) {
return lastError;
}
logger('debug', 'Lyrics', `No lyrics found for ${trackInfo?.title || 'Unknown Track'}`);
return { loadType: 'empty', data: {} };
}
Expand Down
14 changes: 8 additions & 6 deletions src/lyrics/musixmatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,6 @@ const SEPARATORS = [' - ', ' – ', ' — ', ' ~ ']
const DEFAULT_UA =
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36'

const DEFAULT_COOKIE =

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setting the default cookie to:

x-mxm-user-id=undefined; x-mxm-token-guid=undefined; mxm-encrypted-token=undefined

seems to fix it too :P

but yet i still think using the _cookieHeader would be more reliable since it just gets the direct response headers from the server

'AWSELB=unknown; x-mxm-user-id=undefined; x-mxm-token-guid=undefined; mxm-encrypted-token='

const isAuthCode = (code: number): boolean => code === 401 || code === 403

const extractMacroCalls = (body: MxmMacroBody | unknown) => {
Expand Down Expand Up @@ -348,14 +345,19 @@ export default class MusixmatchLyrics {
: Array.from(this.cookies, ([k, v]) => `${k}=${v}`).join('; ')
}

/* INFO: Musixmatch's AWS edge returns 403 for the old placeholder cookie, so only send cookies it actually issued */
private _cookieHeader(): { cookie?: string } {
const cookie = this._getCookies()
return cookie ? { cookie } : {}
}

private async _fetchToken(): Promise<string> {
const url = _buildUrl(ENDPOINTS.TOKEN, { app_id: APP_ID })
const { statusCode, headers, body } = await http1makeRequest(url, {
method: 'GET',
headers: {
accept: '*/*',
'accept-language': 'en',
cookie: DEFAULT_COOKIE,
'user-agent': DEFAULT_UA
}
})
Expand Down Expand Up @@ -492,7 +494,7 @@ export default class MusixmatchLyrics {
headers: {
accept: 'application/json',
'user-agent': DEFAULT_UA,
cookie: this._getCookies()
...this._cookieHeader()
}
})

Expand Down Expand Up @@ -525,7 +527,7 @@ export default class MusixmatchLyrics {
headers: {
accept: 'application/json',
'user-agent': DEFAULT_UA,
cookie: this._getCookies()
...this._cookieHeader()
}
})

Expand Down
51 changes: 45 additions & 6 deletions src/managers/lyricsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,22 @@ export default class LyricsManager {
}
}

/* INFO: A provider that throws must not abort the search, so a throw becomes an error result like any other failure */
private async _fetchFrom(
source: LyricsSourceInstance,
name: string | undefined,
trackInfo: TrackInfoLike,
language?: string
): Promise<LyricsLoadResult> {
try {
return await source.getLyrics(trackInfo, language)
} catch (e) {
const message = getErrorMessage(e)
logger('warn', 'Lyrics', `Lyrics source ${name} threw: ${message}`)
return { loadType: 'error', data: { message, severity: 'fault' } }
}
}

async loadLyrics(
decodedTrack: DecodedTrack | null | undefined,
language?: string,
Expand Down Expand Up @@ -320,6 +336,8 @@ export default class LyricsManager {
const isYouTube = sourceName === 'youtube' || sourceName === 'ytmusic'

let youtubeCaptions: LyricsLoadResult | null = null
let lastError: LyricsLoadResult | null = null
let sawEmpty = false

if (lyricsSource && !skipTrackSource) {
if (isYouTube) {
Expand All @@ -336,13 +354,22 @@ export default class LyricsManager {
)
}
} else {
const lyrics = await lyricsSource.getLyrics(trackInfo, language)
if (lyrics && lyrics.loadType !== 'empty') {
if (lyrics.loadType === 'lyrics') {
lyrics.data.provider = sourceName
}
const lyrics = await this._fetchFrom(
lyricsSource,
sourceName,
trackInfo,
language
)
if (lyrics?.loadType === 'lyrics') {
lyrics.data.provider = sourceName
return lyrics
}

if (lyrics?.loadType === 'error') {
lastError = lyrics
} else {
sawEmpty = true
}
}
}

Expand All @@ -359,7 +386,12 @@ export default class LyricsManager {
'Lyrics',
`Trying lyrics source ${name} for ${trackInfo?.title || 'Unknown Title'}.`
)
const lyrics = await source.getLyrics(trackInfo, language)
const lyrics = await this._fetchFrom(source, name, trackInfo, language)

if (lyrics?.loadType === 'error') {
lastError = lyrics
continue
}

if (lyrics && lyrics.loadType !== 'empty') {
if (
Expand Down Expand Up @@ -395,13 +427,20 @@ export default class LyricsManager {
}
return lyrics
}

sawEmpty = true
}

if (isYouTube && youtubeCaptions?.loadType === 'lyrics') {
youtubeCaptions.data.provider = sourceName
return youtubeCaptions
}

/* INFO: Only report a failure when every provider that answered failed; otherwise the track simply has no lyrics */
if (lastError && !sawEmpty) {
return lastError
}

logger(
'debug',
'Lyrics',
Expand Down
Loading