Files
YouTube.js/lib/parser/youtube/Library.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

83 lines
2.3 KiB
JavaScript

'use strict';
const Parser = require('../contents');
const History = require('./History');
const Playlist = require('./Playlist');
const Feed = require('../../core/Feed');
const { observe } = require('../../utils/Utils');
/** @namespace */
class Library {
#actions;
#page;
/**
* @param {object} response - API response.
* @param {import('../../core/Actions')} actions
*/
constructor(response, actions) {
this.#actions = actions;
this.#page = Parser.parseResponse(response);
const tab = this.#page.contents.tabs.get({ selected: true });
const shelves = tab.content.contents.map((section) => section.contents[0]);
const stats = this.#page.contents.secondary_contents.items.get({ type: 'ProfileColumnStats' }).items;
const user_info = this.#page.contents.secondary_contents.items.get({ type: 'ProfileColumnUserInfo' });
this.profile = { stats, user_info };
/** @type {{ type: string, title: import('../contents/classes/Text'), contents: object[], getAll: Promise.<Playlist | History | Feed> }[] } */
this.sections = observe(shelves.map((shelf) => ({
type: shelf.icon_type,
title: shelf.title,
contents: shelf.content.items,
getAll: () => this.#getAll(shelf)
})));
}
async #getAll(shelf) {
if (!shelf.menu?.top_level_buttons)
throw new Error(`The ${shelf.title.text} section doesn't have more items`);
const button = await shelf.menu.top_level_buttons.get({ text: 'See all' });
const page = await button.endpoint.call(this.#actions);
switch (shelf.icon_type) {
case 'LIKE':
case 'WATCH_LATER':
return new Playlist(this.#actions, page, true);
case 'WATCH_HISTORY':
return new History(this.#actions, page, true);
case 'CONTENT_CUT':
return new Feed(this.#actions, page, true);
default:
}
}
get history() {
return this.sections.get({ type: 'WATCH_HISTORY' });
}
get watch_later() {
return this.sections.get({ type: 'WATCH_LATER' });
}
get liked_videos() {
return this.sections.get({ type: 'LIKE' });
}
get playlists() {
return this.sections.get({ type: 'PLAYLISTS' });
}
get clips() {
return this.sections.get({ type: 'CONTENT_CUT' });
}
get page() {
return this.#page;
}
}
module.exports = Library;