dev: finish YouTube Music search parsers

This commit is contained in:
LuanRT
2022-06-09 14:33:26 -03:00
parent b2014c80f4
commit 153238aefc
23 changed files with 509 additions and 54 deletions

View File

@@ -0,0 +1,15 @@
'use strict';
const Text = require('./Text');
const NavigationEndpoint = require('./NavigationEndpoint');
class DidYouMean {
type = 'didYouMeanRenderer';
constructor(data) {
this.corrected_query = new Text(data.correctedQuery);
this.endpoint = new NavigationEndpoint(data.navigationEndpoint);
}
}
module.exports = DidYouMean;

View File

@@ -0,0 +1,13 @@
'use strict';
const Parser = require('..');
class MusicHeader {
type = 'musicHeaderRenderer';
constructor(data) {
this.header = Parser.parse(data.header);
}
}
module.exports = MusicHeader;

View File

@@ -0,0 +1,10 @@
class MusicInlineBadge {
type = 'musicInlineBadgeRenderer';
constructor(data) {
this.icon_type = data.icon.iconType;
this.label = data.accessibilityData.accessibilityData.label;
}
}
module.exports = MusicInlineBadge;

View File

@@ -0,0 +1,15 @@
'use strict';
const Parser = require('..');
class MusicItemThumbnailOverlay {
type = 'musicItemThumbnailOverlayRenderer';
constructor(data) {
this.content = Parser.parse(data.content);
this.content_position = data.contentPosition;
this.display_style = data.displayStyle;
}
}
module.exports = MusicItemThumbnailOverlay;

View File

@@ -0,0 +1,18 @@
'use strict';
const NavigationEndpoint = require('./NavigationEndpoint');
class MusicPlayButton {
type = 'musicPlayButtonRenderer';
constructor(data) {
this.endpoint = new NavigationEndpoint(data.playNavigationEndpoint);
this.play_icon_type = data.playIcon.iconType;
this.pause_icon_type = data.pauseIcon.iconType;
this.play_label = data.accessibilityPlayData.accessibilityData.label;
this.pause_label = data.accessibilityPauseData.accessibilityData.label;
this.icon_color = data.iconColor;
}
}
module.exports = MusicPlayButton;

View File

@@ -0,0 +1,146 @@
'use strict';
const Parser = require('..');
const Text = require('./Text');
const Utils = require('../../../utils/Utils');
const Thumbnail = require('./Thumbnail');
const NavigationEndpoint = require('./NavigationEndpoint');
class MusicResponsiveListItem {
#shelf_title;
#flex_columns;
#is_single_shelf;
#playlist_item_data;
constructor(data, ctx) {
this.#shelf_title = ctx.shelf_title;
this.#is_single_shelf = ctx.is_single_shelf;
this.#flex_columns = Parser.parse(data.flexColumns);
this.#playlist_item_data = { video_id: data?.playlistItemData?.videoId || null };
// Item type is not available when there's only one shelf.
!ctx.is_single_shelf &&
(this.type = this.#flex_columns[1].title.runs[0].text.toLowerCase());
this.endpoint = data.navigationEndpoint &&
new NavigationEndpoint(data.navigationEndpoint) || null;
/**
* When there's only one shelf it means it's a search by type
* or a continuation. We have to parse each case differently
* as the list item structure is not always the same.
*/
ctx.is_single_shelf ?
this.#parseItemByShelfName() :
this.#parseItemByItemType();
this.thumbnails = new Thumbnail(data.thumbnail.musicThumbnailRenderer.thumbnail).thumbnails;
this.badges = Parser.parse(data.badges) || [];
this.menu = Parser.parse(data.menu);
this.overlay = Parser.parse(data.overlay);
}
#parseItemByShelfName = () => (({
['Songs']: () => this.#parseSong(),
['Videos']: () => this.#parseVideo(),
['Featured playlists']: () => this.#parsePlaylist(),
['Community playlists']: () => this.#parsePlaylist(),
['Artists']: () => this.#parseArtist(),
['Albums']: () => this.#parseAlbum(),
})[this.#shelf_title.toString()] ||
(() => console.warn(this.#shelf_title.toString() + ' not found!')))();
#parseItemByItemType = () => (({
song: () => this.#parseSong(),
video: () => this.#parseVideo(),
artist: () => this.#parseArtist(),
playlist: () => this.#parsePlaylist(),
album: () => this.#parseAlbum(),
single: () => this.#parseAlbum(),
ep: () => this.#parseAlbum()
})[this.type] ||
(() => console.warn(this.type + ' not found!')))();
#parseSong() {
const target_runs = {
artist: this.#is_single_shelf ? 0 : 2,
album: this.#is_single_shelf ? 2 : 4
}
this.id = this.#playlist_item_data.video_id;
this.title = this.#flex_columns[0].title.toString();
this.artist = this.#flex_columns[1].title.runs[target_runs.artist].text;
this.album = this.#flex_columns[1].title.runs[target_runs.album].text;
const duration_text = this.#flex_columns[1].title.runs
.find((run) => /^\d+$/.test(run.text.replace(/:/g, ''))).text;
this.duration = {
text: duration_text,
seconds: Utils.timeToSeconds(duration_text)
}
}
#parseVideo() {
const target_runs = {
views: this.#is_single_shelf ? 2 : 4,
author: this.#is_single_shelf ? 0 : 2
}
this.id = this.#playlist_item_data.video_id;
this.title = this.#flex_columns[0].title.toString();
this.views = this.#flex_columns[1].title.runs[target_runs.views].text;
this.author = {
name: this.#flex_columns[1].title.runs[target_runs.author].text,
channel_id: this.#flex_columns[1].title.runs[target_runs.author].endpoint.browse.id
}
const duration_text = this.#flex_columns[1].title.runs
.find((run) => /^\d+$/.test(run.text.replace(/:/g, ''))).text;
this.duration = {
text: duration_text,
seconds: Utils.timeToSeconds(duration_text)
}
}
#parseArtist() {
this.id = this.endpoint.browse.id;
this.name = this.#flex_columns[0].title.toString();
this.subscribers = this.#flex_columns[1].title.runs[2].text;
}
#parseAlbum() {
this.id = this.endpoint.browse.id;
this.title = this.#flex_columns[0].title.toString();
this.author = {
name: this.#flex_columns[1].title.runs[2].text.toString(),
channel_id: this.#flex_columns[1].title.runs[2].endpoint.browse?.id || null
}
this.year = this.#flex_columns[1].title.runs
.find((run) => /^[12][0-9]{3}$/.test(run.text)).text;
}
#parsePlaylist() {
const target_runs = {
item_count: this.#is_single_shelf ? 2 : 4,
author: this.#is_single_shelf ? 0 : 2
}
this.id = this.endpoint.browse.id;
this.title = this.#flex_columns[0].title.toString();
this.item_count = parseInt(this.#flex_columns[1].title.runs[target_runs.item_count].text.match(/\d+/g));
this.author = {
name: this.#flex_columns[1].title.runs[target_runs.author].text,
channel_id: this.#flex_columns[1].title.runs[target_runs.author].endpoint.browse.id
}
}
}
module.exports = MusicResponsiveListItem;

View File

@@ -0,0 +1,17 @@
'use strict';
const Parser = require('..');
const Text = require('./Text');
const Thumbnail = require('./Thumbnail');
const NavigationEndpoint = require('./NavigationEndpoint');
class MusicResponsiveListItemFlexColumn {
type = 'musicResponsiveListItemFlexColumnRenderer';
constructor(data) {
this.title = new Text(data.text);
this.display_priority = data.displayPriority;
}
}
module.exports = MusicResponsiveListItemFlexColumn;

View File

@@ -0,0 +1,24 @@
'use strict';
const Parser = require('..');
const Text = require('./Text');
const NavigationEndpoint = require('./NavigationEndpoint');
class MusicShelf {
type = 'musicShelfRenderer';
constructor(data) {
this.title = new Text(data.title);
this.contents = Parser.parse(data.contents, {
is_single_shelf: (!data.bottomEndpoint && this.title.toString() !== 'Top result'),
shelf_title: this.title
});
this.endpoint = data.bottomEndpoint && new NavigationEndpoint(data.bottomEndpoint) || null;
this.continuation = data.continuations?.[0].nextContinuationData.continuation || null;
this.bottom_text = data.bottomText && new Text(data.bottomText) || null;
}
}
module.exports = MusicShelf;

View File

@@ -11,7 +11,7 @@ class NavigationEndpoint {
this.metadata = {
url: data?.commandMetadata?.webCommandMetadata.url || null,
page_type: data?.commandMetadata?.webCommandMetadata.webPageType || 'N/A',
page_type: data?.commandMetadata?.webCommandMetadata.webPageType || null,
api_url: data?.commandMetadata?.webCommandMetadata.apiUrl || null,
send_post: data?.commandMetadata?.webCommandMetadata.sendPost || null
}
@@ -34,6 +34,13 @@ class NavigationEndpoint {
};
}
if (data?.searchEndpoint) {
this.search = {
query: data.searchEndpoint.query,
params: data.searchEndpoint.params
}
}
if (data?.subscribeEndpoint) {
this.subscribe = {
channel_ids: data.subscribeEndpoint.channelIds,
@@ -51,7 +58,10 @@ class NavigationEndpoint {
if (data?.likeEndpoint) {
this.like = {
status: data.likeEndpoint.status,
target: { video_id: data.likeEndpoint.target.videoId },
target: {
video_id: data.likeEndpoint.target.videoId,
playlist_id: data.likeEndpoint.target.playlistId
},
remove_like_params: data.likeEndpoint?.removeLikeParams
}
}
@@ -110,7 +120,10 @@ class NavigationEndpoint {
}
}
async call(actions) {
async call(actions, client) {
if (!actions)
throw new Error('An active caller must be provided');
if (this.continuation) {
switch (this.continuation.request) {
case 'CONTINUATION_REQUEST_TYPE_BROWSE': {
@@ -126,6 +139,11 @@ class NavigationEndpoint {
}
}
if (this.search) {
const response = await actions.search({ query: this.search.query, params: this.search.params, client });
return Parser.parseResponse(response.data);
}
if (this.browse) {
const args = {};

View File

@@ -8,6 +8,7 @@ class SectionList {
constructor(data) {
this.target_id = data.targetId || null;
this.contents = Parser.parse(data.contents);
this.header = Parser.parse(data.header);
}
}

View File

@@ -0,0 +1,16 @@
'use strict';
const Text = require('./Text');
const NavigationEndpoint = require('./NavigationEndpoint');
class ShowingResultsFor {
type = 'showingResultsForRenderer';
constructor(data) {
this.corrected_query = new Text(data.correctedQuery);
this.endpoint = new NavigationEndpoint(data.correctedQueryEndpoint);
this.original_query_endpoint = new NavigationEndpoint(data.originalQueryEndpoint);
}
}
module.exports = ShowingResultsFor;

View File

@@ -8,7 +8,7 @@ class Tab {
constructor(data) {
this.title = data.title || 'N/A';
this.selected = data.selected;
this.selected = data.selected || false;
this.endpoint = new NavigationEndpoint(data.endpoint);
this.content = Parser.parse(data.content);
}

View File

@@ -0,0 +1,18 @@
'use strict';
const Text = require('./Text');
const NavigationEndpoint = require('./NavigationEndpoint');
class ToggleMenuServiceItem {
type = 'toggleMenuServiceItemRenderer';
constructor(data) {
this.text = new Text(data.defaultText);
this.toggled_text = new Text(data.toggledText);
this.icon_type = data.defaultIcon.iconType;
this.toggled_icon_type = data.toggledIcon.iconType;
this.endpoint = new NavigationEndpoint(data.toggledServiceEndpoint);
}
}
module.exports = ToggleMenuServiceItem;