Skip to content

Commit

Permalink
fix: fixed an issue where only the first 100 songs in a playlist were…
Browse files Browse the repository at this point in the history
… downloaded (#1329)
  • Loading branch information
JellyBrick authored Oct 18, 2023
1 parent 5702978 commit 1efe835
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions src/plugins/downloader/back.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ async function iterableStreamToTargetFile(

ffmpeg.setProgress(({ ratio }) => {
sendFeedback(`Converting: ${Math.floor(ratio * 100)}%`, ratio);
increasePlaylistProgress(0.15 + ratio * 0.85);
increasePlaylistProgress(0.15 + (ratio * 0.85));
});

const safeVideoNameWithExtension = `${safeVideoName}.${extension}`;
Expand Down Expand Up @@ -468,8 +468,12 @@ export async function downloadPlaylist(givenUrl?: string | URL) {
console.log(`trying to get playlist ID: '${playlistId}'`);
sendFeedback('Getting playlist info…');
let playlist: Playlist;
const items: YTNodes.MusicResponsiveListItem[] = [];
try {
playlist = await yt.music.getPlaylist(playlistId);
if (playlist?.items) {
items.push(...playlist.items.as(YTNodes.MusicResponsiveListItem));
}
} catch (error: unknown) {
sendError(
Error(
Expand All @@ -485,13 +489,6 @@ export async function downloadPlaylist(givenUrl?: string | URL) {
sendError(new Error('Playlist is empty'));
}

const items = playlist.items!.as(YTNodes.MusicResponsiveListItem);
if (items.length === 1) {
sendFeedback('Playlist has only one item, downloading it directly');
await downloadSongFromId(items.at(0)!.id!);
return;
}

const normalPlaylistTitle = playlist.header?.title?.text;
const playlistTitle =
normalPlaylistTitle ??
Expand All @@ -502,6 +499,19 @@ export async function downloadPlaylist(givenUrl?: string | URL) {
'NO_TITLE';
const isAlbum = !normalPlaylistTitle;

while (playlist.has_continuation) {
playlist = await playlist.getContinuation();
if (playlist?.items) {
items.push(...playlist.items.as(YTNodes.MusicResponsiveListItem));
}
}

if (items.length === 1) {
sendFeedback('Playlist has only one item, downloading it directly');
await downloadSongFromId(items.at(0)!.id!);
return;
}

let safePlaylistTitle = filenamify(playlistTitle, { replacement: ' ' });
if (!is.macOS()) {
safePlaylistTitle = safePlaylistTitle.normalize('NFC');
Expand Down Expand Up @@ -542,7 +552,7 @@ export async function downloadPlaylist(givenUrl?: string | URL) {

const increaseProgress = (itemPercentage: number) => {
const currentProgress = (counter - 1) / (items.length ?? 1);
const newProgress = currentProgress + progressStep * itemPercentage;
const newProgress = currentProgress + (progressStep * itemPercentage);
win.setProgressBar(newProgress);
};

Expand Down

0 comments on commit 1efe835

Please sign in to comment.