Skip to content

Commit

Permalink
feat(history): Add ability to remove videos from watch history (#706)
Browse files Browse the repository at this point in the history
* Use Button class

* Add ability to remove videos from watch history

* Update src/parser/youtube/History.ts

* Fix linting

---------

Co-authored-by: Roger <sonemonu@gmail.com>
  • Loading branch information
dnicolson and rogerpadilla authored Oct 28, 2024
1 parent 15d3865 commit 22dd71d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/parser/classes/menus/Menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ import { Parser } from '../../index.js';
import type { ObservedArray } from '../../helpers.js';
import { YTNode } from '../../helpers.js';
import type { RawNode } from '../../index.js';
import Button from '../Button.js';

export default class Menu extends YTNode {
static type = 'Menu';

items: ObservedArray<YTNode>;
top_level_buttons: ObservedArray<YTNode>;
top_level_buttons: ObservedArray<Button>;
label?: string;

constructor(data: RawNode) {
super();
this.items = Parser.parseArray(data.items);
this.top_level_buttons = Parser.parseArray(data.topLevelButtons);
this.top_level_buttons = Parser.parseArray(data.topLevelButtons, Button);

if (Reflect.has(data, 'accessibility') && Reflect.has(data.accessibility, 'accessibilityData')) {
this.label = data.accessibility.accessibilityData.label;
Expand Down
34 changes: 33 additions & 1 deletion src/parser/youtube/History.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import BrowseFeedActions from '../classes/BrowseFeedActions.js';

import type { Actions, ApiResponse } from '../../core/index.js';
import type { IBrowseResponse } from '../types/index.js';
import type Video from '../classes/Video.js';

// TODO: make feed actions usable
export default class History extends Feed<IBrowseResponse> {
Expand All @@ -25,4 +26,35 @@ export default class History extends Feed<IBrowseResponse> {
throw new Error('No continuation data found');
return new History(this.actions, response, true);
}
}

/**
* Removes a video from watch history.
*/
async removeVideo(video_id: string): Promise<boolean> {
let feedbackToken;

for (const section of this.sections) {
for (const content of section.contents) {
const video = content as Video;
if (video.id === video_id && video.menu) {
feedbackToken = video.menu.top_level_buttons[0].endpoint.payload.feedbackToken;
break;
}
}
}

if (!feedbackToken) {
throw new Error('Failed to get feedback token');
}

const body = { feedbackTokens: [ feedbackToken ] };
const response = await this.actions.execute('/feedback', body);
const data = response.data;

if (!data.feedbackResponses[0].isProcessed) {
throw new Error('Failed to remove video from watch history');
}

return true;
}
}
3 changes: 2 additions & 1 deletion src/parser/youtube/Library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ export default class Library extends Feed<IBrowseResponse> {
}

async #getAll(shelf: Shelf): Promise<Playlist | History | Feed<IBrowseResponse>> {
if (!shelf.menu?.as(Menu).hasKey('top_level_buttons'))
if (!shelf.menu?.as(Menu).top_level_buttons)

throw new InnertubeError(`The ${shelf.title.text} shelf doesn't have more items`);

const button = shelf.menu.as(Menu).top_level_buttons.firstOfType(Button);
Expand Down

0 comments on commit 22dd71d

Please sign in to comment.