Skip to content

Commit

Permalink
fix(ytmusic): Multiple authors/artists (#82)
Browse files Browse the repository at this point in the history
(legacy parser)

Changed return types of SongResultItem's key artist and VideoResultItem's key author to array of strings instead of string.
  • Loading branch information
DanielKrasny authored Jun 27, 2022
1 parent a948c2e commit 8849a01
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
26 changes: 17 additions & 9 deletions lib/parser/ytmusic/search/SongResultItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,23 @@ class SongResultItem {

static parseItem(item) {
const list_item = item.musicResponsiveListItemRenderer;
if (list_item.playlistItemData) return {
id: list_item.playlistItemData.videoId,
title: list_item.flexColumns[0]?.musicResponsiveListItemFlexColumnRenderer.text.runs[0]?.text,
artist: list_item.flexColumns[1]?.musicResponsiveListItemFlexColumnRenderer.text.runs[2]?.text,
album: list_item.flexColumns[1]?.musicResponsiveListItemFlexColumnRenderer.text.runs[4]?.text,
duration: list_item.flexColumns[1]?.musicResponsiveListItemFlexColumnRenderer.text.runs
.find((run) => /^\d+$/.test(run.text.replace(/:/g, ''))).text,
thumbnails: list_item.thumbnail.musicThumbnailRenderer.thumbnail.thumbnails,
};
if (list_item.playlistItemData) {
let artists = list_item.flexColumns[1]?.musicResponsiveListItemFlexColumnRenderer.text.runs;
// Remove parts of array that just includes information that the item is a song
artists.splice(0,2);
// Remove parts of array that contains data like album and duration
const meta = artists.splice(artists.length - 4, 4);
// Keep only even parts of array, the odd ones are just a delimiter between artists (&)
artists = artists.filter((artist, index) => !(index % 2));
return {
id: list_item.playlistItemData.videoId,
title: list_item.flexColumns[0]?.musicResponsiveListItemFlexColumnRenderer.text.runs[0]?.text,
artist: artists.map((artist) => artist.text),
album: meta[1]?.text,
duration: meta[3]?.text,
thumbnails: list_item.thumbnail.musicThumbnailRenderer.thumbnail.thumbnails,
};
}
}
}

Expand Down
26 changes: 17 additions & 9 deletions lib/parser/ytmusic/search/VideoResultItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,23 @@ class VideoResultItem {

static parseItem(item) {
const list_item = item.musicResponsiveListItemRenderer;
if (list_item.playlistItemData) return {
id: list_item.playlistItemData.videoId,
title: list_item.flexColumns[0]?.musicResponsiveListItemFlexColumnRenderer.text.runs[0]?.text,
author: list_item.flexColumns[1]?.musicResponsiveListItemFlexColumnRenderer.text.runs[2]?.text,
views: list_item.flexColumns[1]?.musicResponsiveListItemFlexColumnRenderer.text.runs[4]?.text,
duration: list_item.flexColumns[1]?.musicResponsiveListItemFlexColumnRenderer.text.runs
.find((run) => /^\d+$/.test(run.text.replace(/:/g, ''))).text,
thumbnails: list_item?.thumbnail.musicThumbnailRenderer.thumbnail.thumbnails,
};
if (list_item.playlistItemData) {
let authors = list_item.flexColumns[1]?.musicResponsiveListItemFlexColumnRenderer.text.runs;
// Remove parts of array that just includes information that the item is a video
authors.splice(0,2);
// Remove parts of array that contains data like number of views and duration
const meta = authors.splice(authors.length - 4, 4);
// Keep only even parts of array, the odd ones are just a delimiter between authors (&)
authors = authors.filter((author, index) => !(index % 2));
return {
id: list_item.playlistItemData.videoId,
title: list_item.flexColumns[0]?.musicResponsiveListItemFlexColumnRenderer.text.runs[0]?.text,
author: authors.map((author) => author.text),
views: meta[1]?.text,
duration: meta[3]?.text,
thumbnails: list_item?.thumbnail.musicThumbnailRenderer.thumbnail.thumbnails,
};
}
}
}

Expand Down

0 comments on commit 8849a01

Please sign in to comment.