Files
YouTube.js/lib/parser/contents/classes/MusicTwoRowItem.js
LuanRT 1d62e469a9 refactor: rewrite Comments Section logic (#88)
* feat: add core comments section classes

* chore: update type declarations

* chore: fix linter warnings

* style: fix linter

* chore: update tests

* chore(tests): fix typo

* chore(tests): fix typo x2

* fix(tests): `getReplies()` method is only present in `CommentThread` and not `Comment`

* chore(tests): fix comment id path

* chore(tests): remove outdated code

* chore(tests): fix results path

* chore: enforce code style

* chore: update type declarations

* docs: add examples and documentation

* chore(docs): fix paths

* chore(docs): fix more paths

* chore(docs): fix `Comments.js` path

* chore(docs): fix typo

* chore(docs): mention example file

* chore(examples): fix imports

* chore(examples): fix typo
2022-07-02 19:55:33 -03:00

92 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;