Files
YouTube.js/lib/parser/classes/MusicTwoRowItem.js
LuanRT 68cb841c00 refactor!: finish parser migration
Finally! :)

This removes all code related to the old parser.

#65
2022-07-11 06:19:10 -03:00

91 lines
2.7 KiB
JavaScript

'use strict';
const Parser = require('..');
const Text = require('./Text');
const Thumbnail = require('./Thumbnail');
const NavigationEndpoint = require('./NavigationEndpoint');
class MusicTwoRowItem {
type = 'MusicTwoRowItem';
constructor(data) {
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.type = 'artist';
this.subscribers = this.subtitle.toString();
break;
case 'MUSIC_PAGE_TYPE_PLAYLIST':
this.type = 'playlist';
this.item_count = parseInt(this.subtitle.runs
.find((run) => run.text
.match(/\d+ (songs|song)/))?.text
.match(/\d+/g)) || null;
break;
case 'MUSIC_PAGE_TYPE_ALBUM':
this.type = 'album';
const artists = this.subtitle.runs.filter((run) => run.endpoint.browse?.id.startsWith('UC'));
if (artists) {
this.artists = artists.map((artist) => ({
name: artist.text,
channel_id: artist.endpoint.browse.id,
endpoint: artist.endpoint
}));
}
this.year = this.subtitle.runs.slice(-1)[0].text;
if (isNaN(this.year))
delete this.year;
break;
default:
if (this.subtitle.runs[0].text !== 'Song') {
this.type = 'video';
} else {
this.type = 'song';
}
if (this.type == 'video') {
this.views = this.subtitle.runs
.find((run) => run.text.match(/(.*?) views/)).text;
const author = this.subtitle.runs.find((run) => run.endpoint.browse?.id.startsWith('UC'));
if (author) {
this.author = {
name: author.text,
channel_id: author.endpoint.browse.id,
endpoint: author.endpoint
};
}
} else {
const artists = this.subtitle.runs.filter((run) => run.endpoint.browse?.id.startsWith('UC'));
if (artists) {
this.artists = artists.map((artist) => ({
name: artist.text,
channel_id: artist.endpoint.browse.id,
endpoint: artist.endpoint
}));
}
}
break;
}
this.thumbnail = Thumbnail.fromResponse(data.thumbnailRenderer.musicThumbnailRenderer.thumbnail);
this.thumbnail_overlay = Parser.parse(data.thumbnailOverlay);
this.menu = Parser.parse(data.menu);
}
}
module.exports = MusicTwoRowItem;