Skip to content

Commit 670eb49

Browse files
committed
feat(YouTube): Warn on alternate release versions
YouTube Music sometimes returns incorrect, alternate versions of a release when looking up a GTIN. This warns if a release with multiple versions is returned, as there is no way of knowing if YouTube returned the correct one.
1 parent ad17d03 commit 670eb49

File tree

3 files changed

+50
-7
lines changed

3 files changed

+50
-7
lines changed

providers/YouTubeMusic/api_types.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ type APIResponse = {
77
} & TrackingParams;
88

99
export type Album = {
10-
contents?: Renderer<'TwoColumnBrowseResults'>;
10+
contents: Renderer<'TwoColumnBrowseResults'>;
1111
microformat: Renderer<'MicroformatData'>;
1212
background?: Renderer<'MusicThumbnail'>;
1313
} & APIResponse;
@@ -87,6 +87,7 @@ export type Nodes = {
8787
| Renderer<'MusicShelf'>
8888
| Renderer<'MusicPlaylistShelf'>
8989
| Renderer<'MusicResponsiveHeader'>
90+
| Renderer<'MusicCarouselShelf'>
9091
)[];
9192
};
9293
/** @see https://github.com/LuanRT/YouTube.js/blob/v14.0.0/src/parser/classes/ItemSection.ts */
@@ -138,6 +139,30 @@ export type Nodes = {
138139
MusicCardShelfHeaderBasic: unknown;
139140
/** @see https://github.com/LuanRT/YouTube.js/blob/v14.0.0/src/parser/classes/MusicItemThumbnailOverlay.ts */
140141
MusicItemThumbnailOverlay: unknown;
142+
/** @see https://github.com/LuanRT/YouTube.js/blob/v14.0.0/src/parser/classes/MusicCarouselShelf.ts */
143+
MusicCarouselShelf: {
144+
header: Renderer<'MusicCarouselShelfBasicHeader'>;
145+
contents: Renderer<'MusicTwoRowItem'>[];
146+
};
147+
/** @see https://github.com/LuanRT/YouTube.js/blob/v14.0.0/src/parser/classes/MusicCarouselShelfBasicHeader.ts */
148+
MusicCarouselShelfBasicHeader: {
149+
title: YTText;
150+
accessibilityData?: {
151+
accessibilityData: {
152+
label?: string;
153+
};
154+
};
155+
};
156+
/** @see https://github.com/LuanRT/YouTube.js/blob/v14.0.0/src/parser/classes/MusicTwoRowItem.ts */
157+
MusicTwoRowItem: {
158+
thumbnailRenderer?: Renderer<'MusicThumbnail'>;
159+
aspectRatio?: string;
160+
title: YTText;
161+
subtitle?: YTText;
162+
navigationEndpoint: BrowseEndpoint;
163+
menu?: Renderer<'Menu'>;
164+
thumbnailOverlay: Renderer<'MusicItemThumbnailOverlay'>;
165+
};
141166
/** @see https://github.com/LuanRT/YouTube.js/blob/v14.0.0/src/parser/classes/MusicResponsiveListItem.ts */
142167
MusicResponsiveListItem: {
143168
thumbnail: Renderer<'MusicThumbnail'>;

providers/YouTubeMusic/constants.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ export const YOUTUBEI_BODY = {
3333
platform: 'DESKTOP',
3434
clientFormFactor: 'UNKNOWN_FORM_FACTOR',
3535
userInterfaceTheme: 'USER_INTERFACE_THEME_LIGHT',
36-
timeZone: 'Europe/Berlin',
37-
originalUrl: 'https://www.youtube.com',
3836
deviceMake: '',
3937
deviceModel: '',
4038
browserName: 'Edge Chromium',

providers/YouTubeMusic/mod.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ export class YouTubeMusicReleaseLookup extends ReleaseLookup<YouTubeMusicProvide
322322

323323
const header = album
324324
.contents
325-
?.twoColumnBrowseResultsRenderer
325+
.twoColumnBrowseResultsRenderer
326326
.tabs
327327
.at(0)
328328
?.tabRenderer
@@ -342,7 +342,7 @@ export class YouTubeMusicReleaseLookup extends ReleaseLookup<YouTubeMusicProvide
342342

343343
const albumTrackData = album
344344
.contents
345-
?.twoColumnBrowseResultsRenderer
345+
.twoColumnBrowseResultsRenderer
346346
.secondaryContents
347347
.sectionListRenderer
348348
.contents
@@ -353,7 +353,7 @@ export class YouTubeMusicReleaseLookup extends ReleaseLookup<YouTubeMusicProvide
353353

354354
const playlistTrackData = playlist
355355
.contents
356-
?.twoColumnBrowseResultsRenderer
356+
.twoColumnBrowseResultsRenderer
357357
.secondaryContents
358358
.sectionListRenderer
359359
.contents
@@ -438,6 +438,25 @@ export class YouTubeMusicReleaseLookup extends ReleaseLookup<YouTubeMusicProvide
438438
release.releaseDate = { year: releaseYear };
439439
}
440440

441+
const otherVersions = album
442+
.contents
443+
?.twoColumnBrowseResultsRenderer
444+
.secondaryContents.sectionListRenderer
445+
.contents
446+
.filter((renderer) => 'musicCarouselShelfRenderer' in renderer)
447+
.find((shelf) =>
448+
// TODO: Try to make this be independent of the returned language
449+
shelf.musicCarouselShelfRenderer.header.musicCarouselShelfBasicHeaderRenderer.title.runs.at(0)?.text ===
450+
'Other versions'
451+
)
452+
?.musicCarouselShelfRenderer.contents
453+
.map((item) => item.musicTwoRowItemRenderer.navigationEndpoint.browseEndpoint.browseId)
454+
.map((id) => this.provider.constructUrl({ id, type: BROWSE }));
455+
456+
if (otherVersions) {
457+
this.warnMultipleResults(otherVersions);
458+
}
459+
441460
return release;
442461
}
443462

@@ -528,8 +547,9 @@ export class YouTubeMusicReleaseLookup extends ReleaseLookup<YouTubeMusicProvide
528547
}, 0);
529548

530549
// TODO: WIP code for fetching data from the "View song credits" popup
531-
// Can be used to improve artist credits,
550+
// Could be used to improve artist credits,
532551
// and to get the videoId of the actual song (instead of the MV) instead of parsing it from the playlist contents
552+
// Using this would however require an additional API call per track
533553
const creditsEndpoint = item
534554
.musicResponsiveListItemRenderer
535555
.menu

0 commit comments

Comments
 (0)