refactor: clean up parser and tests (#387)

* tests: improve coverage

* refactor: clean up nodes

* chore: lint

* feat(parser): ignore `BrandVideoShelf`

Seems to be used for ads.

* feat(parser): ignore `BrandVideoSingleton` too
This commit is contained in:
LuanRT
2023-04-23 06:37:33 -03:00
committed by GitHub
parent f66f0bd656
commit 257bd475a0
358 changed files with 2823 additions and 3126 deletions

View File

@@ -1,19 +1,22 @@
import { YTNode } from '../../helpers.js';
import type { RawNode } from '../../index.js';
class AuthorCommentBadge extends YTNode {
export default class AuthorCommentBadge extends YTNode {
static type = 'AuthorCommentBadge';
#data;
icon_type: string | null;
icon_type?: string;
tooltip: string;
style?: string;
constructor(data: RawNode) {
super();
this.icon_type = data.icon?.iconType || null;
if (Reflect.has(data, 'icon') && Reflect.has(data.icon, 'iconType')) {
this.icon_type = data.icon.iconType;
}
this.tooltip = data.iconTooltip;
// *** For consistency
@@ -27,6 +30,4 @@ class AuthorCommentBadge extends YTNode {
get orig_badge() {
return this.#data;
}
}
export default AuthorCommentBadge;
}

View File

@@ -1,24 +1,25 @@
import Parser from '../../index.js';
import Author from '../misc/Author.js';
import Text from '../misc/Text.js';
import Thumbnail from '../misc/Thumbnail.js';
import CommentReplyDialog from './CommentReplyDialog.js';
import AuthorCommentBadge from './AuthorCommentBadge.js';
import Author from '../misc/Author.js';
import Menu from '../menus/Menu.js';
import AuthorCommentBadge from './AuthorCommentBadge.js';
import CommentActionButtons from './CommentActionButtons.js';
import SponsorCommentBadge from './SponsorCommentBadge.js';
import CommentReplyDialog from './CommentReplyDialog.js';
import PdgCommentChip from './PdgCommentChip.js';
import type { ApiResponse } from '../../../core/Actions.js';
import type Actions from '../../../core/Actions.js';
import SponsorCommentBadge from './SponsorCommentBadge.js';
import Proto from '../../../proto/index.js';
import { InnertubeError } from '../../../utils/Utils.js';
import { YTNode, SuperParsedResult } from '../../helpers.js';
import { SuperParsedResult, YTNode } from '../../helpers.js';
import type Actions from '../../../core/Actions.js';
import type { ApiResponse } from '../../../core/Actions.js';
import type { RawNode } from '../../index.js';
class Comment extends YTNode {
export default class Comment extends YTNode {
static type = 'Comment';
#actions?: Actions;
@@ -35,9 +36,7 @@ class Comment extends YTNode {
action_buttons: CommentActionButtons | null;
comment_id: string;
vote_status: string;
vote_count: string;
reply_count: number;
is_liked: boolean;
is_disliked: boolean;
@@ -146,7 +145,7 @@ class Comment extends YTNode {
}
/**
* Translates the comment to the given language.
* Translates the comment to a given language.
* @param target_language - Ex; en, ja
*/
async translate(target_language: string): Promise<{
@@ -170,7 +169,7 @@ class Comment extends YTNode {
const action = Proto.encodeCommentActionParams(22, payload);
const response = await this.#actions.execute('comment/perform_comment_action', { action, client: 'ANDROID' });
// TODO: maybe add these to Parser#parseResponse?
// XXX: Should move this to Parser#parseResponse
const mutations = response.data.frameworkUpdates?.entityBatchUpdate?.mutations;
const content = mutations?.[0]?.payload?.commentEntityPayload?.translatedContent?.content;
@@ -180,6 +179,4 @@ class Comment extends YTNode {
setActions(actions: Actions | undefined) {
this.#actions = actions;
}
}
export default Comment;
}

View File

@@ -2,16 +2,17 @@ import Parser from '../../index.js';
import Button from '../Button.js';
import ToggleButton from '../ToggleButton.js';
import CreatorHeart from './CreatorHeart.js';
import { YTNode } from '../../helpers.js';
import type { RawNode } from '../../index.js';
class CommentActionButtons extends YTNode {
export default class CommentActionButtons extends YTNode {
static type = 'CommentActionButtons';
like_button;
dislike_button;
reply_button;
creator_heart;
like_button: ToggleButton | null;
dislike_button: ToggleButton | null;
reply_button: Button | null;
creator_heart: CreatorHeart | null;
constructor(data: RawNode) {
super();
@@ -20,6 +21,4 @@ class CommentActionButtons extends YTNode {
this.reply_button = Parser.parseItem(data.replyButton, Button);
this.creator_heart = Parser.parseItem(data.creatorHeart, CreatorHeart);
}
}
export default CommentActionButtons;
}

View File

@@ -1,12 +1,13 @@
import Parser from '../../index.js';
import Button from '../Button.js';
import Text from '../misc/Text.js';
import Thumbnail from '../misc/Thumbnail.js';
import Button from '../Button.js';
import EmojiPicker from './EmojiPicker.js';
import { YTNode } from '../../helpers.js';
import type { RawNode } from '../../index.js';
class CommentDialog extends YTNode {
export default class CommentDialog extends YTNode {
static type = 'CommentDialog';
editable_text: Text;
@@ -15,7 +16,7 @@ class CommentDialog extends YTNode {
cancel_button: Button | null;
placeholder: Text;
emoji_button: Button | null;
emoji_picker: any | null;
emoji_picker: EmojiPicker | null;
constructor(data: RawNode) {
super();
@@ -27,6 +28,4 @@ class CommentDialog extends YTNode {
this.emoji_button = Parser.parseItem(data.emojiButton, Button);
this.emoji_picker = Parser.parseItem(data.emojiPicker, EmojiPicker);
}
}
export default CommentDialog;
}

View File

@@ -1,12 +1,14 @@
import Parser from '../../index.js';
import Thumbnail from '../misc/Thumbnail.js';
import Button from '../Button.js';
import { YTNode } from '../../helpers.js';
import Thumbnail from '../misc/Thumbnail.js';
import { YTNode, type ObservedArray } from '../../helpers.js';
import type { RawNode } from '../../index.js';
class CommentReplies extends YTNode {
export default class CommentReplies extends YTNode {
static type = 'CommentReplies';
contents;
contents: ObservedArray<YTNode>;
view_replies: Button | null;
hide_replies: Button | null;
view_replies_creator_thumbnail: Thumbnail[];
@@ -20,6 +22,4 @@ class CommentReplies extends YTNode {
this.view_replies_creator_thumbnail = Thumbnail.fromResponse(data.viewRepliesCreatorThumbnail);
this.has_channel_owner_replied = !!data.viewRepliesCreatorThumbnail;
}
}
export default CommentReplies;
}

View File

@@ -1,11 +1,12 @@
import Parser from '../../index.js';
import Thumbnail from '../misc/Thumbnail.js';
import Text from '../misc/Text.js';
import Button from '../Button.js';
import Text from '../misc/Text.js';
import Thumbnail from '../misc/Thumbnail.js';
import { YTNode } from '../../helpers.js';
import type { RawNode } from '../../index.js';
class CommentReplyDialog extends YTNode {
export default class CommentReplyDialog extends YTNode {
static type = 'CommentReplyDialog';
reply_button: Button | null;
@@ -22,6 +23,4 @@ class CommentReplyDialog extends YTNode {
this.placeholder = new Text(data.placeholderText);
this.error_message = new Text(data.errorMessage);
}
}
export default CommentReplyDialog;
}

View File

@@ -1,27 +1,26 @@
import Parser from '../../index.js';
import Thumbnail from '../misc/Thumbnail.js';
import Text from '../misc/Text.js';
import Button from '../Button.js';
import Text from '../misc/Text.js';
import Thumbnail from '../misc/Thumbnail.js';
import { YTNode } from '../../helpers.js';
import type { RawNode } from '../../index.js';
class CommentSimplebox extends YTNode {
export default class CommentSimplebox extends YTNode {
static type = 'CommentSimplebox';
submit_button: Button | null;
cancel_button: Button | null;
author_thumbnails: Thumbnail[];
author_thumbnail: Thumbnail[];
placeholder: Text;
avatar_size;
avatar_size: string;
constructor(data: RawNode) {
super();
this.submit_button = Parser.parseItem(data.submitButton, Button);
this.cancel_button = Parser.parseItem(data.cancelButton, Button);
this.author_thumbnails = Thumbnail.fromResponse(data.authorThumbnail);
this.author_thumbnail = Thumbnail.fromResponse(data.authorThumbnail);
this.placeholder = new Text(data.placeholderText);
this.avatar_size = data.avatarSize;
}
}
export default CommentSimplebox;
}

View File

@@ -1,15 +1,17 @@
import Parser from '../../index.js';
import Comment from './Comment.js';
import ContinuationItem from '../ContinuationItem.js';
import CommentReplies from './CommentReplies.js';
import Button from '../Button.js';
import type Actions from '../../../core/Actions.js';
import type { ObservedArray } from '../../helpers.js';
import ContinuationItem from '../ContinuationItem.js';
import Comment from './Comment.js';
import CommentReplies from './CommentReplies.js';
import { InnertubeError } from '../../../utils/Utils.js';
import { observe, YTNode } from '../../helpers.js';
import type Actions from '../../../core/Actions.js';
import type { ObservedArray } from '../../helpers.js';
import type { RawNode } from '../../index.js';
class CommentThread extends YTNode {
export default class CommentThread extends YTNode {
static type = 'CommentThread';
#actions?: Actions;
@@ -23,7 +25,7 @@ class CommentThread extends YTNode {
constructor(data: RawNode) {
super();
this.comment = Parser.parseItem<Comment>(data.comment, Comment);
this.comment = Parser.parseItem(data.comment, Comment);
this.comment_replies_data = Parser.parseItem(data.replies, CommentReplies);
this.is_moderated_elq_comment = data.isModeratedElqComment;
this.has_replies = !!this.comment_replies_data;
@@ -37,7 +39,7 @@ class CommentThread extends YTNode {
throw new InnertubeError('Actions instance not set for this thread.');
if (!this.comment_replies_data)
throw new InnertubeError('This comment has no replies.', { comment_id: this.comment?.comment_id });
throw new InnertubeError('This comment has no replies.', this);
const continuation = this.comment_replies_data.contents?.firstOfType(ContinuationItem);
@@ -87,7 +89,7 @@ class CommentThread extends YTNode {
return comment;
}));
this.#continuation = response.on_response_received_endpoints_memo.getType(ContinuationItem)?.[0];
this.#continuation = response.on_response_received_endpoints_memo.getType(ContinuationItem).first();
return this;
}
@@ -101,6 +103,4 @@ class CommentThread extends YTNode {
setActions(actions: Actions) {
this.#actions = actions;
}
}
export default CommentThread;
}

View File

@@ -1,40 +1,44 @@
import Text from '../misc/Text.js';
import Thumbnail from '../misc/Thumbnail.js';
import { YTNode } from '../../helpers.js';
import type { RawNode } from '../../index.js';
import { Parser, RawNode } from '../../index.js';
import CommentsEntryPointTeaser from './CommentsEntryPointTeaser.js';
class CommentsEntryPointHeader extends YTNode {
export default class CommentsEntryPointHeader extends YTNode {
static type = 'CommentsEntryPointHeader';
header?: Text;
comment_count?: Text;
teaser_avatar?: Thumbnail[];
teaser_content?: Text;
content_renderer?: CommentsEntryPointTeaser | null;
simplebox_placeholder?: Text;
constructor(data: RawNode) {
super();
if (data.header) {
if (Reflect.has(data, 'headerText')) {
this.header = new Text(data.headerText);
}
if (data.commentCount) {
if (Reflect.has(data, 'commentCount')) {
this.comment_count = new Text(data.commentCount);
}
if (data.teaserAvatar || data.simpleboxAvatar) {
if (Reflect.has(data, 'teaserAvatar') || Reflect.has(data, 'simpleboxAvatar')) {
this.teaser_avatar = Thumbnail.fromResponse(data.teaserAvatar || data.simpleboxAvatar);
}
if (data.teaserContent) {
if (Reflect.has(data, 'teaserContent')) {
this.teaser_content = new Text(data.teaserContent);
}
if (data.simpleboxPlaceholder) {
if (Reflect.has(data, 'contentRenderer')) {
this.content_renderer = Parser.parseItem(data.contentRenderer, CommentsEntryPointTeaser);
}
if (Reflect.has(data, 'simpleboxPlaceholder')) {
this.simplebox_placeholder = new Text(data.simpleboxPlaceholder);
}
}
}
export default CommentsEntryPointHeader;
}

View File

@@ -0,0 +1,23 @@
import Text from '../misc/Text.js';
import Thumbnail from '../misc/Thumbnail.js';
import { YTNode } from '../../helpers.js';
import type { RawNode } from '../../index.js';
export default class CommentsEntryPointTeaser extends YTNode {
static type = 'CommentsEntryPointTeaser';
teaser_avatar?: Thumbnail[];
teaser_content?: Text;
constructor(data: RawNode) {
super();
if (Reflect.has(data, 'teaserAvatar')) {
this.teaser_avatar = Thumbnail.fromResponse(data.teaserAvatar);
}
if (Reflect.has(data, 'teaserContent')) {
this.teaser_content = new Text(data.teaserContent);
}
}
}

View File

@@ -1,11 +1,12 @@
import Parser from '../../index.js';
import SortFilterSubMenu from '../SortFilterSubMenu.js';
import Text from '../misc/Text.js';
import Thumbnail from '../misc/Thumbnail.js';
import SortFilterSubMenu from '../SortFilterSubMenu.js';
import { YTNode } from '../../helpers.js';
import type { RawNode } from '../../index.js';
class CommentsHeader extends YTNode {
export default class CommentsHeader extends YTNode {
static type = 'CommentsHeader';
title: Text;
@@ -14,13 +15,13 @@ class CommentsHeader extends YTNode {
create_renderer;
sort_menu: SortFilterSubMenu | null;
custom_emojis: {
custom_emojis?: {
emoji_id: string;
shortcuts: string[];
search_terms: string[];
image: Thumbnail[];
is_custom_emoji: boolean;
}[] | null;
}[];
constructor(data: RawNode) {
super();
@@ -30,14 +31,16 @@ class CommentsHeader extends YTNode {
this.create_renderer = Parser.parseItem(data.createRenderer);
this.sort_menu = Parser.parseItem(data.sortMenu, SortFilterSubMenu);
this.custom_emojis = data.customEmojis?.map((emoji: any) => ({
emoji_id: emoji.emojiId,
shortcuts: emoji.shortcuts,
search_terms: emoji.searchTerms,
image: Thumbnail.fromResponse(emoji.image),
is_custom_emoji: emoji.isCustomEmoji
})) || null;
if (Reflect.has(data, 'customEmojis')) {
this.custom_emojis = data.customEmojis.map((emoji: RawNode) => {
return {
emoji_id: emoji.emojiId,
shortcuts: emoji.shortcuts,
search_terms: emoji.searchTerms,
image: Thumbnail.fromResponse(emoji.image),
is_custom_emoji: emoji.isCustomEmoji
};
});
}
}
}
export default CommentsHeader;
}

View File

@@ -1,12 +1,12 @@
import { YTNode } from '../../helpers.js';
import Thumbnail from '../misc/Thumbnail.js';
import type { RawNode } from '../../index.js';
import Thumbnail from '../misc/Thumbnail.js';
class CreatorHeart extends YTNode {
export default class CreatorHeart extends YTNode {
static type = 'CreatorHeart';
creator_thumbnail: Thumbnail[];
heart_icon_type: string;
heart_icon_type?: string;
heart_color: {
basic_color_palette_data: {
foreground_title_color: string;
@@ -20,17 +20,20 @@ class CreatorHeart extends YTNode {
constructor(data: RawNode) {
super();
this.creator_thumbnail = Thumbnail.fromResponse(data.creatorThumbnail);
this.heart_icon_type = data.heartIcon?.iconType;
if (Reflect.has(data.heartIcon, 'iconType')) {
this.heart_icon_type = data.heartIcon.iconType;
}
this.heart_color = {
basic_color_palette_data: {
foreground_title_color: data.heartColor?.basicColorPaletteData?.foregroundTitleColor
}
};
this.hearted_tooltip = data.heartedTooltip;
this.is_hearted = data.isHearted;
this.is_enabled = data.isEnabled;
this.kennedy_heart_color_string = data.kennedyHeartColorString;
}
}
export default CreatorHeart;
}

View File

@@ -1,14 +1,14 @@
import Text from '../misc/Text.js';
import { YTNode } from '../../helpers.js';
import Parser from '../../index.js';
import { type ObservedArray, YTNode } from '../../helpers.js';
import type { RawNode } from '../../index.js';
import Parser from '../../index.js';
import Text from '../misc/Text.js';
class EmojiPicker extends YTNode {
export default class EmojiPicker extends YTNode {
static type = 'EmojiPicker';
id: string;
categories: any[];
category_buttons: any[];
categories: ObservedArray<YTNode>;
category_buttons: ObservedArray<YTNode>;
search_placeholder: Text;
search_no_results: Text;
pick_skin_tone: Text;
@@ -36,6 +36,4 @@ class EmojiPicker extends YTNode {
this.skin_tone_medium_dark_label = data.skinToneMediumDarkLabel;
this.skin_tone_dark_label = data.skinToneDarkLabel;
}
}
export default EmojiPicker;
}

View File

@@ -2,15 +2,15 @@ import Text from '../misc/Text.js';
import { YTNode } from '../../helpers.js';
import type { RawNode } from '../../index.js';
class PdgCommentChip extends YTNode {
export default class PdgCommentChip extends YTNode {
static type = 'PdgCommentChip';
text: Text;
color_pallette: {
background_color: string;
foreground_title_color: string;
background_color?: string;
foreground_title_color?: string;
};
icon_type: string;
icon_type?: string;
constructor(data: RawNode) {
super();
@@ -19,8 +19,9 @@ class PdgCommentChip extends YTNode {
background_color: data.chipColorPalette?.backgroundColor,
foreground_title_color: data.chipColorPalette?.foregroundTitleColor
};
this.icon_type = data.chipIcon?.iconType;
}
}
export default PdgCommentChip;
if (Reflect.has(data.chipIcon, 'iconType')) {
this.icon_type = data.chipIcon.iconType;
}
}
}

View File

@@ -2,7 +2,7 @@ import Thumbnail from '../misc/Thumbnail.js';
import { YTNode } from '../../helpers.js';
import type { RawNode } from '../../index.js';
class SponsorCommentBadge extends YTNode {
export default class SponsorCommentBadge extends YTNode {
static type = 'SponsorCommentBadge';
custom_badge: Thumbnail[];
@@ -13,6 +13,4 @@ class SponsorCommentBadge extends YTNode {
this.custom_badge = Thumbnail.fromResponse(data.customBadge);
this.tooltip = data.tooltip;
}
}
export default SponsorCommentBadge;
}