Files
YouTube.js/src/parser/classes/MusicTwoRowItem.ts
Daniel Wykerd 2ccbe2ce62 refactor!: cleanup platform support (#306)
* refactor!: cleanup platform support

* chore: lint

* fix: web platform

* feat: provide UniversalCache

Provide UniversalCache as a wrapper around Platform.shim.Cache.

* fix: invalid import

* refactor: remove isolated-vm support

* fix: type info

* refactor: cleanup exports

* fix: mark jintr as external dependency

In the bundled CJS node build, mark jintr as external.

* chore: add additional exports

web exports provide a way to select web implementation manually without
relying on the bundler to select it correctly from the "exports" field

web points to src/platform/web.js
web.bundle points to bundle/browser.js
web.bundle.browser points to bundle/browser.min.js

agnostic exports provide users of the library to provide their own
platform implementation without first importing the default one.

agnostic points to src/platform/lib.ts

* fix: toDash on web

* revert: eval is synchronous

* fix: use serializeDOM in FormatUtils

* ci: automate releases with `release-please`

* chore: clean up workflow files

* ci: fix NPM publish action

---------

Co-authored-by: LuanRT <luan.lrt4@gmail.com>
2023-02-12 04:21:44 -03:00

131 lines
4.2 KiB
TypeScript

// TODO: refactor this
import Parser from '../index.js';
import Text from './misc/Text.js';
import TextRun from './misc/TextRun.js';
import Thumbnail from './misc/Thumbnail.js';
import NavigationEndpoint from './NavigationEndpoint.js';
import MusicItemThumbnailOverlay from './MusicItemThumbnailOverlay.js';
import Menu from './menus/Menu.js';
import { YTNode } from '../helpers.js';
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?.payload?.browseId ||
this.endpoint?.payload?.videoId;
this.subtitle = new Text(data.subtitle);
this.badges = Parser.parse(data.subtitleBadges);
const page_type = this.endpoint?.payload?.browseEndpointContextSupportedConfigs?.browseEndpointContextMusicConfig?.pageType;
switch (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?.metadata?.api_url === '/next') {
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?.payload?.browseId.startsWith('UC'));
if (artists) {
this.artists = artists.map((artist: any) => ({
name: artist.text,
channel_id: artist.endpoint?.payload?.browseId,
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?.payload?.browseId?.startsWith('UC'));
if (author) {
this.author = {
name: (author as TextRun)?.text,
channel_id: (author as TextRun)?.endpoint?.payload?.browseId,
endpoint: (author as TextRun)?.endpoint
};
}
} else if (this.item_type == 'song') {
const artists = this.subtitle.runs?.filter((run: any) => run.endpoint?.payload?.browseId.startsWith('UC'));
if (artists) {
this.artists = artists.map((artist: any) => ({
name: (artist as TextRun)?.text,
channel_id: (artist as TextRun)?.endpoint?.payload?.browseId,
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;