Skip to content

Commit

Permalink
feat: filter with '--episode-regex' before starting downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
lightpohl committed Aug 8, 2021
1 parent 0dda386 commit 6690a82
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 28 deletions.
55 changes: 31 additions & 24 deletions bin/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -287,7 +294,7 @@ const main = async () => {
addMp3Metadata({
feed,
item,
itemIndex: i,
itemIndex: item._originalIndex,
outputPath: outputPodcastPath,
});
} catch (error) {
Expand Down
4 changes: 0 additions & 4 deletions bin/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,23 @@ 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,
};
}

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,
};
Expand Down

0 comments on commit 6690a82

Please sign in to comment.