Skip to content

Commit

Permalink
Optimize playlist video removal
Browse files Browse the repository at this point in the history
  • Loading branch information
dnicolson committed Nov 13, 2024
1 parent ad07dee commit 49f54dc
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/core/managers/PlaylistManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,24 +115,27 @@ export default class PlaylistManager {

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

0 comments on commit 49f54dc

Please sign in to comment.