Files
YouTube.js/lib/core/History.js
LuanRT 359020193b dev: start parser refactor on the main codebase, see #65 and #44
Things were getting a bit complicated and slow with the old parser so I decided to continue #44's work on the main codebase.
2022-06-06 04:19:14 -03:00

54 lines
1.2 KiB
JavaScript

'use strict';
/** @namespace */
class History {
#page;
#actions;
#continuation;
/**
* @param {object} page - parsed data.
* @param {import('./Actions')} actions
* @param {boolean} is_continuation
* @constructor
*/
constructor(page, actions, is_continuation) {
this.#page = page;
this.#actions = actions;
const tab = page.contents?.tabs.get({ selected: true });
const contents = is_continuation
&& page.continuation_items
|| tab.content.contents;
this.sections = contents.map((section) => {
if (section.type == 'continuationItemRenderer') {
this.#continuation = section;
return;
}
return {
title: section.header.title,
items: section.contents
}
});
this.has_continuation && this.sections.pop();
}
async getContinuation() {
const response = await this.#continuation.endpoint.call(this.#actions);
return new History(response.on_response_received_actions[0], this.#actions, true);
}
get has_continuation() {
return !!this.#continuation;
}
get page() {
return this.#page;
}
}
module.exports = History;