diff --git a/bin/bin.js b/bin/bin.js index 304c6f2..79b14f6 100644 --- a/bin/bin.js +++ b/bin/bin.js @@ -203,44 +203,51 @@ const main = async () => { logErrorAndExit("--offset too large. No episodes to download."); } - const { startIndex, numItemsToDownload, limitCheck, next } = getLoopControls({ + const { startIndex, limitCheck, next } = getLoopControls({ limit, offset, reverse, length: feed.items.length, }); - const episodeText = numItemsToDownload === 1 ? "episode" : "episodes"; + let limitCheckIndex = startIndex; + const targetItems = []; + while (limitCheck(limitCheckIndex)) { + const item = feed.items[limitCheckIndex]; + item._originalIndex = limitCheckIndex; - logMessage(`Starting download of ${numItemsToDownload} ${episodeText}\n`); + if (episodeRegex) { + const generatedEpisodeRegex = episodeRegex + ? new RegExp(episodeRegex) + : null; + + if (item.title && generatedEpisodeRegex.test(item.title)) { + targetItems.push(item); + } + } else { + targetItems.push(item); + } + + limitCheckIndex = next(limitCheckIndex); + } + + if (!targetItems.length) { + logErrorAndExit("No episodes found with provided criteria to download"); + } - let i = startIndex; - let counter = 1; const nextItem = () => { - i = next(i); counter += 1; logMessage(""); }; - let episodesDownloadedCounter = 0; - while (limitCheck(i)) { - const item = feed.items[i]; + const episodeText = targetItems.length === 1 ? "episode" : "episodes"; + logMessage(`Starting download of ${targetItems.length} ${episodeText}\n`); - logMessage(`${counter} of ${numItemsToDownload}`); - - const generatedEpisodeRegex = episodeRegex - ? new RegExp(episodeRegex) - : null; + let counter = 1; + let episodesDownloadedCounter = 0; - if ( - generatedEpisodeRegex && - (!item.title || !generatedEpisodeRegex.test(item.title)) - ) { - logItemInfo(item); - logMessage("Episode title does not match provided regex. Skipping"); - nextItem(); - continue; - } + for (const item of targetItems) { + logMessage(`${counter} of ${targetItems.length}`); const { url: episodeAudioUrl, ext: audioFileExt } = getEpisodeAudioUrlAndExt(item); @@ -287,7 +294,7 @@ const main = async () => { addMp3Metadata({ feed, item, - itemIndex: i, + itemIndex: item._originalIndex, outputPath: outputPodcastPath, }); } catch (error) { diff --git a/bin/util.js b/bin/util.js index 3ed62ae..dcb34ab 100644 --- a/bin/util.js +++ b/bin/util.js @@ -25,13 +25,11 @@ const getLoopControls = ({ limit, offset, length, reverse }) => { if (reverse) { const startIndex = length - 1 - offset; const min = limit ? Math.max(startIndex - limit, -1) : -1; - const numItemsToDownload = min > -1 ? startIndex - min : startIndex + 1; const limitCheck = (i) => i > min; const decrement = (i) => i - 1; return { startIndex, - numItemsToDownload, limitCheck, next: decrement, }; @@ -39,13 +37,11 @@ const getLoopControls = ({ limit, offset, length, reverse }) => { const startIndex = 0 + offset; const max = limit ? Math.min(startIndex + limit, length) : length; - const numItemsToDownload = max - startIndex; const limitCheck = (i) => i < max; const increment = (i) => i + 1; return { startIndex, - numItemsToDownload, limitCheck, next: increment, };