Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize playlist video removal #801

Merged
merged 2 commits into from
Nov 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions src/core/managers/PlaylistManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,29 +110,32 @@ export default class PlaylistManager {

const playlist = new Playlist(this.#actions, info, true);

if (!playlist.info.is_editable)
if (playlist.info.is_editable === false)
throw new InnertubeError('This playlist cannot be edited.', playlist_id);

const payload: EditPlaylistEndpointOptions = { playlist_id, actions: [] };

const getSetVideoIds = async (pl: Feed): Promise<void> => {
const key_id = use_set_video_ids ? 'set_video_id' : 'id';
const videos = pl.videos.filter((video) => video_ids.includes(video.key(key_id).string()));

const getSetVideoIds = async (pl: Feed, set_video_ids: string[] = []): Promise<string[]> => {
const videos = pl.videos.filter((video) => video_ids.includes(video.key('id').string()));
videos.forEach((video) =>
payload.actions.push({
action: 'ACTION_REMOVE_VIDEO',
set_video_id: video.key('set_video_id').string()
})
set_video_ids.push(video.key('set_video_id').string())
);

if (payload.actions.length < video_ids.length) {
const next = await pl.getContinuation();
return getSetVideoIds(next);
return getSetVideoIds(next, set_video_ids);
}

return set_video_ids;
};

await getSetVideoIds(playlist);
const set_video_ids = use_set_video_ids ? video_ids : await getSetVideoIds(playlist);
set_video_ids.forEach((set_video_id: string) =>
payload.actions.push({
action: 'ACTION_REMOVE_VIDEO',
set_video_id
})
);

if (!payload.actions.length)
throw new InnertubeError('Given video ids were not found in this playlist.', video_ids);
Expand Down