Files
YouTube.js/lib/parser/contents/classes/MusicResponsiveListItem.js
Daniel Wykerd 4a102878d8 refactor!: feature complete contents parser
* feat: allow setting search params to custom value

This is useful for getting results other than videos, like playlists and
channels.

* feat: add initial parsers for common renderers

* feat: artist search renderers

Added common renderers used when searching artists

* refactor: snake_case

* feat: channel home page renderers

* feat: parsers for more channel tabs

These are needed for channel tabs: Videos, Playlists, Community,
Channels

Additionally, do not merely return text as string, since they may
include links which may be navigated to

* feat: channel full metadata

* feat: renderers for playlists

* refactor!: Actions.browse

Channels should be viewable when not logged in, also added 'navigation'
type for use in NagivationEndpoint in the future.

* feat: home feed parsers

* feat: watch page renderers

* feat: start implementing HomeFeed API

The HomeFeed class remains compatible with the existing API

* feat: generate types using tsc and jsdoc

* feat: browse continuations from navigationEndpoint

* fix: Actions moved to session

This follows commit 1bfe2676d8

* fix: add more typescript config

* chore: use correct spaces and quotes

* feat: Trending API

* feat: reimplement existing channel API

* feat: add base video feed class

* feat: get channel videos

* feat: channel playlists

* feat: get channel community posts

* feat: get channels from channel

* feat: get channel about page data

* feat: add missing channel parsers

this commit also adds regenerated types I've neglected to push

* feat: initial playlist reimplementation

* feat: complete playlist reimplementation

* refactor: change InnertubeError to ES6 class

* fix: some unresolved types

* chore: update types

* feat: wip video details

* feat: get music tracks in video

Possibly an implementation for issue #48

* refactor:  merge parsers (wip)

This is a work in progress.

* fix: add pnpm to ignore

* fix: merge issues

* fix: merge Video and VideoInfo

VideoInfo should be working again.
Also remove the old parsers.

* feat: set matching in Simplify

Still looking into removing Simplify

* fix: ContinuationItem

This `call` method allows for traversal of continuations with the Simplify API
but may be removed in the future

* fix: optionally returned data

* revert: replace ContinuationItem with main

* feat(parser): contents memoization by classname

* feat(channel): working without Simplify

* feat(feed): working continuations

* fix: liniting issues

* feat(feed): filterable feed for home

* feat(feed): tabbed feed for trending & channel

* refactor: remove Simplify completely

* chore: lint

* refactor: alias `items` with `contents`

* refactor: `Search` to extend `Feed`

* fix: Search working

Also added MenuServiceItemDownload

* refactor: move `Channel` and `Playlist`

* fix: pass all tests

* fix: linting errors
2022-06-15 18:31:34 -03:00

148 lines
4.4 KiB
JavaScript

'use strict';
const Parser = require('..');
const Utils = require('../../../utils/Utils');
const Thumbnail = require('./Thumbnail');
const NavigationEndpoint = require('./NavigationEndpoint');
class MusicResponsiveListItem {
#flex_columns;
#playlist_item_data;
constructor(data) {
this.type = null;
this.#flex_columns = Parser.parse(data.flexColumns);
this.#playlist_item_data = { video_id: data?.playlistItemData?.videoId || null };
this.endpoint = data.navigationEndpoint &&
new NavigationEndpoint(data.navigationEndpoint) || null;
switch (this.endpoint?.browse.page_type) {
case 'MUSIC_PAGE_TYPE_ALBUM':
this.type = 'album';
this.#parseAlbum();
break;
case 'MUSIC_PAGE_TYPE_PLAYLIST':
this.type = 'playlist';
this.#parsePlaylist();
break;
case 'MUSIC_PAGE_TYPE_ARTIST':
this.type = 'artist';
this.#parseArtist();
break;
default:
this.#parseVideoOrSong();
break;
}
this.thumbnails = Thumbnail.fromResponse(data.thumbnail.musicThumbnailRenderer.thumbnail);
this.badges = Parser.parse(data.badges) || [];
this.menu = Parser.parse(data.menu);
this.overlay = Parser.parse(data.overlay);
}
#parseVideoOrSong() {
const is_video = this.#flex_columns[1].title.runs
.some((run) => run.text.match(/(.*?) views/));
if (is_video) {
this.type = 'video';
this.#parseVideo();
} else {
this.type = 'song';
this.#parseSong();
}
}
#parseSong() {
this.id = this.#playlist_item_data.video_id;
this.title = this.#flex_columns[0].title.toString();
const duration_text = this.#flex_columns[1].title.runs
.find((run) => /^\d+$/.test(run.text.replace(/:/g, '')))?.text;
duration_text && (this.duration = {
text: duration_text,
seconds: Utils.timeToSeconds(duration_text)
});
const album = this.#flex_columns[1].title.runs.find((run) => run.endpoint.browse?.id.startsWith('MPR'));
album && (this.album = {
id: album.endpoint.browse.id,
name: album.text,
endpoint: album.endpoint
});
const artists = this.#flex_columns[1].title.runs.filter((run) => run.endpoint.browse?.id.startsWith('UC'));
artists && (this.artists = artists.map((artist) => ({
name: artist.text,
channel_id: artist.endpoint.browse.id,
endpoint: artist.endpoint
})));
}
#parseVideo() {
this.id = this.#playlist_item_data.video_id;
this.title = this.#flex_columns[0].title.toString();
this.views = this.#flex_columns[1].title.runs
.find((run) => run.text.match(/(.*?) views/)).text;
const author = this.#flex_columns[1].title.runs.find((run) => run.endpoint.browse?.id.startsWith('UC'));
author && (this.author = {
name: author.text,
channel_id: author.endpoint.browse.id,
endpoint: author.endpoint
});
const duration_text = this.#flex_columns[1].title.runs
.find((run) => /^\d+$/.test(run.text.replace(/:/g, '')))?.text;
duration_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();
const author = this.#flex_columns[1].title.runs.find((run) => run.endpoint.browse?.id.startsWith('UC'));
author && (this.author = {
name: author.text,
channel_id: author.endpoint.browse.id,
endpoint: author.endpoint
});
this.year = this.#flex_columns[1].title.runs
.find((run) => /^[12][0-9]{3}$/.test(run.text)).text;
}
#parsePlaylist() {
this.id = this.endpoint.browse.id;
this.title = this.#flex_columns[0].title.toString();
this.item_count = parseInt(this.#flex_columns[1].title.runs.find((run) => run.text.match(/\d+ (song|songs)/)).text.match(/\d+/g));
const author = this.#flex_columns[1].title.runs.find((run) => run.endpoint.browse?.id.startsWith('UC'));
author && (this.author = {
name: author.text,
channel_id: author.endpoint.browse.id,
endpoint: author.endpoint
});
}
}
module.exports = MusicResponsiveListItem;