feat(parser): allow parser to find renderers by name

Now we can organize renderers in individual folders and fix the mess that `../contents/classes` is!
This commit is contained in:
LuanRT
2022-07-11 02:47:58 -03:00
parent 03f9fc5c2e
commit f924a39409
13 changed files with 31 additions and 55 deletions

View File

@@ -10,17 +10,23 @@ class Poll {
constructor(data) {
this.choices = data.choices.map((choice) => ({
text: new Text(choice.text).toString(),
select_endpoint: new NavigationEndpoint(choice.selectServiceEndpoint),
deselect_endpoint: new NavigationEndpoint(choice.deselectServiceEndpoint),
vote_ratio_if_selected: choice.voteRatioIfSelected,
select_endpoint: choice.selectServiceEndpoint ? new NavigationEndpoint(choice.selectServiceEndpoint) : null,
deselect_endpoint: choice.deselectServiceEndpoint ? new NavigationEndpoint(choice.deselectServiceEndpoint) : null,
vote_ratio_if_selected: choice?.voteRatioIfSelected || null,
vote_percentage_if_selected: new Text(choice.votePercentageIfSelected),
vote_ratio_if_not_selected: choice.voteRatioIfSelected,
vote_ratio_if_not_selected: choice?.voteRatioIfSelected || null,
vote_percentage_if_not_selected: new Text(choice.votePercentageIfSelected),
image: Thumbnail.fromResponse(choice.image)
image: choice.image ? Thumbnail.fromResponse(choice.image) : null
}));
this.total_votes = new Text(data.totalVotes);
this.poll_type = data.type;
if (poll.type)
this.poll_type = data.type;
if (data.totalVotes)
this.total_votes = new Text(data.totalVotes);
if (data.liveChatPollId)
this.live_chat_poll_id = data.liveChatPollId;
}
}

View File

@@ -4,7 +4,7 @@ const Parser = require('../..');
class AddBannerToLiveChatCommand {
constructor(data) {
return Parser.parse(data.bannerRenderer, 'livechat/items');
return Parser.parse(data.bannerRenderer);
}
}

View File

@@ -6,7 +6,7 @@ class AddChatItemAction {
type = 'AddChatItemAction';
constructor(data) {
this.item = Parser.parse(data.item, 'livechat/items');
this.item = Parser.parse(data.item);
this.client_id = data.clientId || null;
}
}

View File

@@ -6,7 +6,7 @@ class AddLiveChatTickerItemAction {
type = 'AddLiveChatTickerItemAction';
constructor(data) {
this.item = Parser.parse(data.item, 'livechat/items');
this.item = Parser.parse(data.item);
this.duration_sec = data.durationSec;
}
}

View File

@@ -7,7 +7,7 @@ class LiveChatActionPanel {
constructor(data) {
this.id = data.id;
this.contents = Parser.parse(data.contents, 'livechat/items');
this.contents = Parser.parse(data.contents);
this.target_id = data.targetId;
}
}

View File

@@ -5,7 +5,7 @@ const Parser = require('../..');
class ReplaceChatItemAction {
constructor(data) {
this.target_item_id = data.targetItemId;
this.replacement_item = Parser.parse(data.replacementItem, 'livechat/items');
this.replacement_item = Parser.parse(data.replacementItem);
}
}

View File

@@ -9,7 +9,7 @@ class ReplayChatItemAction {
this.actions = Parser.parse(data.actions?.map((action) => {
delete action.clickTrackingParams;
return action;
}), 'livechat') || [];
})) || [];
this.video_offset_time_msec = data.videoOffsetTimeMsec;
}

View File

@@ -6,7 +6,7 @@ class ShowLiveChatActionPanelAction {
type = 'ShowLiveChatActionPanelAction';
constructor(data) {
this.panel_to_show = Parser.parse(data.panelToShow, 'livechat');
this.panel_to_show = Parser.parse(data.panelToShow);
}
}

View File

@@ -6,7 +6,7 @@ class UpdateLiveChatPollAction {
type = 'UpdateLiveChatPollAction';
constructor(data) {
this.poll_to_update = Parser.parse(data.pollToUpdate, 'livechat/items');
this.poll_to_update = Parser.parse(data.pollToUpdate);
}
}

View File

@@ -1,25 +0,0 @@
'use strict';
const Parser = require('../../..');
const Text = require('../../Text');
const NavigationEndpoint = require('../../NavigationEndpoint');
class Poll {
type = 'Poll';
constructor(data) {
this.header = Parser.parse(data.header, 'livechat/items');
this.choices = data.choices.map((choice) => ({
text: new Text(choice.text).toString(),
selected: choice.selected,
vote_ratio: choice.voteRatio,
vote_percentage: new Text(choice.votePercentage).toString(),
select_endpoint: new NavigationEndpoint(choice.selectServiceEndpoint)
}));
this.live_chat_poll_id = data.liveChatPollId;
}
}
module.exports = Poll;

View File

@@ -47,7 +47,7 @@ class LiveChatContinuation {
this.actions = Parser.parse(data.actions?.map((action) => {
delete action.clickTrackingParams;
return action;
}), 'livechat') || [];
})) || [];
this.action_panel = Parser.parse(data.actionPanel);
this.item_list = Parser.parse(data.itemList);
@@ -135,7 +135,7 @@ class Parser {
continuation: data.continuation ? Parser.parseC(data.continuation) : null,
/** @type {*} */
continuation_contents: data.continuationContents ? Parser.parseLC(data.continuationContents) : null,
actions: data.actions && Parser.parseLA(data.actions),
actions: data.actions && Parser.parseActions(data.actions),
metadata: Parser.parse(data.metadata),
header: Parser.parse(data.header),
/** @type {import('./classes/PlayerMicroformat')} */
@@ -198,12 +198,12 @@ class Parser {
}).filter((item) => item));
}
static parseLA(data) {
static parseActions(data) {
if (Array.isArray(data)) {
return Parser.parse(data.map((action) => {
delete action.clickTrackingParams;
return action;
}), 'livechat');
}));
}
return Parser.parse(data) || null;
@@ -217,10 +217,9 @@ class Parser {
* Parses the `contents` property of the response.
*
* @param {object} data - contents to be parsed.
* @param {string} module - a folder for specific DA classes.
* @returns {*}
*/
static parse(data, module) {
static parse(data) {
if (!data)
return null;
@@ -233,9 +232,7 @@ class Parser {
if (!this.shouldIgnore(classname)) {
try {
const path = module ? `${module}/` : '';
const TargetClass = requireParserClass(path + classname);
const TargetClass = requireParserClass(classname);
const result = new TargetClass(item[keys[0]]);
results.push(result);
@@ -254,9 +251,7 @@ class Parser {
if (!this.shouldIgnore(classname)) {
try {
const path = module ? `${module}/` : '';
const TargetClass = requireParserClass(path + classname);
const TargetClass = requireParserClass(classname);
const result = new TargetClass(data[keys[0]]);
this.#addToMemo(classname, result);

File diff suppressed because one or more lines are too long