Skip to content

Commit

Permalink
fix(SegmentedLikeDislikeButton): like/dislike buttons can also be a s…
Browse files Browse the repository at this point in the history
…imple `Button`
  • Loading branch information
LuanRT committed Mar 7, 2023
1 parent 95f1d40 commit 9b2738f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
14 changes: 7 additions & 7 deletions src/parser/classes/SegmentedLikeDislikeButton.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import Parser from '../index.js';
import ToggleButton from './ToggleButton.js';
import Parser, { YTNodes } from '../index.js';
import { YTNode } from '../helpers.js';
import type { RawNode } from '../index.js';

class SegmentedLikeDislikeButton extends YTNode {
static type = 'SegmentedLikeDislikeButton';

like_button: ToggleButton | null;
dislike_button: ToggleButton | null;
like_button: YTNodes.ToggleButton | YTNodes.Button | null;
dislike_button: YTNodes.ToggleButton | YTNodes.Button | null;

constructor (data: any) {
constructor (data: RawNode) {
super();
this.like_button = Parser.parseItem<ToggleButton>(data.likeButton, ToggleButton);
this.dislike_button = Parser.parseItem<ToggleButton>(data.dislikeButton, ToggleButton);
this.like_button = Parser.parseItem<YTNodes.ToggleButton | YTNodes.Button>(data.likeButton, [ YTNodes.ToggleButton, YTNodes.Button ]);
this.dislike_button = Parser.parseItem<YTNodes.ToggleButton | YTNodes.Button>(data.dislikeButton, [ YTNodes.ToggleButton, YTNodes.Button ]);
}
}

Expand Down
26 changes: 19 additions & 7 deletions src/parser/youtube/VideoInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,11 @@ class VideoInfo {

const segmented_like_dislike_button = this.primary_info?.menu?.top_level_buttons.firstOfType(SegmentedLikeDislikeButton);

this.basic_info.like_count = segmented_like_dislike_button?.like_button?.as(ToggleButton)?.like_count;
this.basic_info.is_liked = segmented_like_dislike_button?.like_button?.as(ToggleButton)?.is_toggled;
this.basic_info.is_disliked = segmented_like_dislike_button?.dislike_button?.as(ToggleButton)?.is_toggled;
if (segmented_like_dislike_button?.like_button?.is(ToggleButton) && segmented_like_dislike_button?.dislike_button?.is(ToggleButton)) {
this.basic_info.like_count = segmented_like_dislike_button?.like_button?.like_count;
this.basic_info.is_liked = segmented_like_dislike_button?.like_button?.is_toggled;
this.basic_info.is_disliked = segmented_like_dislike_button?.dislike_button?.is_toggled;
}

const comments_entry_point = results.get({ target_id: 'comments-entry-point' })?.as(ItemSection);

Expand Down Expand Up @@ -247,11 +249,14 @@ class VideoInfo {
*/
async like(): Promise<ApiResponse> {
const segmented_like_dislike_button = this.primary_info?.menu?.top_level_buttons.firstOfType(SegmentedLikeDislikeButton);
const button = segmented_like_dislike_button?.like_button?.as(ToggleButton);
const button = segmented_like_dislike_button?.like_button;

if (!button)
throw new InnertubeError('Like button not found', { video_id: this.basic_info.id });

if (!button.is(ToggleButton))
throw new InnertubeError('Like button is not a toggle button. This action is likely disabled for this video.', { video_id: this.basic_info.id });

if (button.is_toggled)
throw new InnertubeError('This video is already liked', { video_id: this.basic_info.id });

Expand All @@ -265,11 +270,14 @@ class VideoInfo {
*/
async dislike(): Promise<ApiResponse> {
const segmented_like_dislike_button = this.primary_info?.menu?.top_level_buttons.firstOfType(SegmentedLikeDislikeButton);
const button = segmented_like_dislike_button?.dislike_button?.as(ToggleButton);
const button = segmented_like_dislike_button?.dislike_button;

if (!button)
throw new InnertubeError('Dislike button not found', { video_id: this.basic_info.id });

if (!button.is(ToggleButton))
throw new InnertubeError('Dislike button is not a toggle button. This action is likely disabled for this video.', { video_id: this.basic_info.id });

if (button.is_toggled)
throw new InnertubeError('This video is already disliked', { video_id: this.basic_info.id });

Expand All @@ -285,8 +293,12 @@ class VideoInfo {
let button;

const segmented_like_dislike_button = this.primary_info?.menu?.top_level_buttons.firstOfType(SegmentedLikeDislikeButton);
const like_button = segmented_like_dislike_button?.like_button?.as(ToggleButton);
const dislike_button = segmented_like_dislike_button?.dislike_button?.as(ToggleButton);

const like_button = segmented_like_dislike_button?.like_button;
const dislike_button = segmented_like_dislike_button?.dislike_button;

if (!like_button?.is(ToggleButton) || !dislike_button?.is(ToggleButton))
throw new InnertubeError('Like/Dislike button is not a toggle button. This action is likely disabled for this video.', { video_id: this.basic_info.id });

if (like_button?.is_toggled) {
button = like_button;
Expand Down

0 comments on commit 9b2738f

Please sign in to comment.