Files
YouTube.js/src/parser/classes/MusicTwoRowItem.ts
LuanRT f2f48af1bc feat(Music): add automix support and other minor improvements (#184)
* dev(NavigationEndpoint): add `/player` endpoint

* dev: add AudioOnlyPlayability, BrowserMediaSession and MusicDownloadStateBadge

* dev: allow endpoints to be overridden

* dev: minor parser changes

* dev(TrackInfo): add `<info>#getTab(title?)`

* dev: allow `Music#getInfo()` to accept list items

* dev: revert a few changes, I probably overcomplicated this.

* dev: add tests

* dev: add `TrackInfo#getUpNext()`, `TrackInfo#getRelated()` and `TrackInfo#getLyrics()`

* docs: update API ref

* fix(docs): formatting inconsistencies
2022-09-13 02:26:13 -03:00

129 lines
4.0 KiB
TypeScript

// TODO: refactor this
import Parser from '../index';
import Text from './misc/Text';
import TextRun from './misc/TextRun';
import Thumbnail from './misc/Thumbnail';
import NavigationEndpoint from './NavigationEndpoint';
import MusicItemThumbnailOverlay from './MusicItemThumbnailOverlay';
import Menu from './menus/Menu';
import { YTNode } from '../helpers';
class MusicTwoRowItem extends YTNode {
static type = 'MusicTwoRowItem';
title: Text;
endpoint: NavigationEndpoint;
id: string | undefined;
subtitle: Text;
badges;
item_type: string;
subscribers?: string;
item_count?: string | null;
year?: string;
views?: string;
artists?: {
name: string;
channel_id: string | undefined;
endpoint: NavigationEndpoint | undefined;
}[];
author?: {
name: string;
channel_id: string | undefined;
endpoint: NavigationEndpoint | undefined;
};
thumbnail: Thumbnail[];
thumbnail_overlay;
menu;
constructor(data: any) {
super();
this.title = new Text(data.title);
this.endpoint = new NavigationEndpoint(data.navigationEndpoint);
this.id =
this.endpoint?.browse?.id ||
this.endpoint?.watch?.video_id;
this.subtitle = new Text(data.subtitle);
this.badges = Parser.parse(data.subtitleBadges);
switch (this.endpoint?.browse?.page_type) {
case 'MUSIC_PAGE_TYPE_ARTIST':
this.item_type = 'artist';
break;
case 'MUSIC_PAGE_TYPE_PLAYLIST':
this.item_type = 'playlist';
break;
case 'MUSIC_PAGE_TYPE_ALBUM':
this.item_type = 'album';
break;
default:
if (this.endpoint?.watch_playlist) {
this.item_type = 'endpoint';
} else if (this.subtitle.runs?.[0]) {
if (this.subtitle.runs[0].text !== 'Song') {
this.item_type = 'video';
} else {
this.item_type = 'song';
}
} else if (this.endpoint) {
this.item_type = 'endpoint';
} else {
this.item_type = 'unknown';
}
break;
}
if (this.item_type == 'artist') {
this.subscribers = this.subtitle.runs?.find((run) => (/^(\d*\.)?\d+[M|K]? subscribers?$/i).test(run.text))?.text || '';
} else if (this.item_type == 'playlist') {
const item_count_run = this.subtitle.runs?.find((run) => run.text.match(/\d+ songs|song/));
this.item_count = item_count_run ? (item_count_run as TextRun).text : null;
} else if (this.item_type == 'album') {
const artists = this.subtitle.runs?.filter((run: any) => run.endpoint?.browse?.id.startsWith('UC'));
if (artists) {
this.artists = artists.map((artist: any) => ({
name: artist.text,
channel_id: artist.endpoint.browse.id,
endpoint: artist.endpoint
}));
}
this.year = this.subtitle.runs?.slice(-1)[0].text;
if (isNaN(Number(this.year)))
delete this.year;
} else if (this.item_type == 'video') {
this.views = this?.subtitle.runs?.find((run) => run?.text.match(/(.*?) views/))?.text || 'N/A';
const author = this.subtitle.runs?.find((run: any) => run.endpoint?.browse?.id?.startsWith('UC'));
if (author) {
this.author = {
name: (author as TextRun)?.text,
channel_id: (author as TextRun)?.endpoint?.browse?.id,
endpoint: (author as TextRun)?.endpoint
};
}
} else if (this.item_type == 'song') {
const artists = this.subtitle.runs?.filter((run: any) => run.endpoint?.browse?.id.startsWith('UC'));
if (artists) {
this.artists = artists.map((artist: any) => ({
name: (artist as TextRun)?.text,
channel_id: (artist as TextRun)?.endpoint?.browse?.id,
endpoint: (artist as TextRun)?.endpoint
}));
}
}
this.thumbnail = Thumbnail.fromResponse(data.thumbnailRenderer.musicThumbnailRenderer.thumbnail);
this.thumbnail_overlay = Parser.parseItem<MusicItemThumbnailOverlay>(data.thumbnailOverlay, MusicItemThumbnailOverlay);
this.menu = Parser.parseItem<Menu>(data.menu, Menu);
}
}
export default MusicTwoRowItem;