Skip to content

Commit

Permalink
feat!: add 'episode-custom-template-options' (#79)
Browse files Browse the repository at this point in the history
* chore!: upgrade to Node v18
  • Loading branch information
lightpohl authored Aug 24, 2024
1 parent 8fd6312 commit d280eea
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 49 deletions.
75 changes: 41 additions & 34 deletions README.md

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion bin/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const download = async (options) => {
},
});
} catch (error) {
// unable to retrive head response
// unable to retrieve head response
}

const tempOutputPath = getTempPath(outputPath);
Expand Down Expand Up @@ -164,6 +164,7 @@ const downloadItemsAsync = async ({
basePath,
bitrate,
episodeTemplate,
episodeCustomTemplateOptions,
episodeDigits,
episodeNumOffset,
episodeSourceOrder,
Expand Down Expand Up @@ -199,6 +200,7 @@ const downloadItemsAsync = async ({
url: episodeAudioUrl,
ext: audioFileExt,
template: episodeTemplate,
customTemplateOptions: episodeCustomTemplateOptions,
width: episodeDigits,
offset: episodeNumOffset,
});
Expand Down Expand Up @@ -283,6 +285,7 @@ const downloadItemsAsync = async ({
url: episodeAudioUrl,
ext: episodeMetaExt,
template: episodeTemplate,
customTemplateOptions: episodeCustomTemplateOptions,
width: episodeDigits,
offset: episodeNumOffset,
});
Expand Down
11 changes: 7 additions & 4 deletions bin/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import fs from "fs";
import _path from "path";
import commander from "commander";
import { program } from "commander";
import pluralize from "pluralize";
import { bootstrap as bootstrapProxy } from "global-agent";

Expand All @@ -29,7 +29,7 @@ import {
import { getFolderName, getSimpleFilename } from "./naming.js";
import { downloadItemsAsync } from "./async.js";

setupCommander(commander, process.argv);
const opts = setupCommander(program);

const {
after,
Expand All @@ -41,6 +41,7 @@ const {
episodeRegex,
episodeSourceOrder,
episodeTemplate,
episodeCustomTemplateOptions,
episodeTranscriptTypes,
exec,
file,
Expand All @@ -62,9 +63,9 @@ const {
url,
addMp3Metadata: addMp3MetadataFlag,
adjustBitrate: bitrate,
} = commander;
} = opts;

let { archive } = commander;
let { archive } = opts;

const main = async () => {
if (!url && !file) {
Expand Down Expand Up @@ -213,6 +214,7 @@ const main = async () => {
episodeRegex,
episodeSourceOrder,
episodeTemplate,
episodeCustomTemplateOptions,
includeEpisodeImages,
includeEpisodeTranscripts,
episodeTranscriptTypes,
Expand All @@ -234,6 +236,7 @@ const main = async () => {
basePath,
bitrate,
episodeTemplate,
episodeCustomTemplateOptions,
episodeDigits,
episodeNumOffset,
episodeSourceOrder,
Expand Down
15 changes: 11 additions & 4 deletions bin/commander.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
import { createParseNumber, hasFfmpeg } from "./validate.js";
import { logErrorAndExit } from "./logger.js";

export const setupCommander = (commander, argv) => {
commander
export const setupCommander = (program) => {
program
.option("--url <string>", "url to podcast rss feed")
.option("--file <path>", "local path to podcast rss feed")
.option(
Expand All @@ -24,6 +24,10 @@ export const setupCommander = (commander, argv) => {
"template for generating episode related filenames",
"{{release_date}}-{{title}}"
)
.option(
"--episode-custom-template-options <patterns...>",
"create custom options for the episode template"
)
.option(
"--episode-digits <number>",
"minimum number of digits to use for episode numbering (leading zeros)",
Expand Down Expand Up @@ -180,6 +184,9 @@ export const setupCommander = (commander, argv) => {
"--parser-config <string>",
"path to JSON config to override RSS parser"
)
.option("--proxy", "enable proxy support via global-agent")
.parse(argv);
.option("--proxy", "enable proxy support via global-agent");

program.parse();

return program.opts();
};
12 changes: 11 additions & 1 deletion bin/naming.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,32 @@ const getItemFilename = ({
feed,
template,
width,
customTemplateOptions = [],
offset = 0,
}) => {
const episodeNum = feed.items.length - item._originalIndex + offset;
const title = item.title || "";
const formattedPubDate = item.pubDate
? dayjs(new Date(item.pubDate)).format("YYYYMMDD")
: null;

const customReplacementTuples = customTemplateOptions.map((option, i) => {
const matchRegex = new RegExp(option);
const match = title.match(matchRegex);

return match && match[0] ? [`custom_${i}`, match[0]] : [`custom_${i}`, ""];
});

const templateReplacementsTuples = [
["title", item.title || ""],
["title", title],
["release_date", formattedPubDate || ""],
["episode_num", `${episodeNum}`.padStart(width, "0")],
["url", url],
["podcast_title", feed.title || ""],
["podcast_link", feed.link || ""],
["duration", item.itunes?.duration || ""],
["guid", item.guid],
...customReplacementTuples,
];

const templateSegments = template.trim().split(path.sep);
Expand Down
2 changes: 2 additions & 0 deletions bin/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ const getItemsToDownload = ({
episodeRegex,
episodeSourceOrder,
episodeTemplate,
episodeCustomTemplateOptions,
includeEpisodeImages,
includeEpisodeTranscripts,
episodeTranscriptTypes,
Expand Down Expand Up @@ -252,6 +253,7 @@ const getItemsToDownload = ({
url: episodeAudioUrl,
ext: episodeImageFileExt,
template: episodeTemplate,
customTemplateOptions: episodeCustomTemplateOptions,
width: episodeDigits,
offset: episodeNumOffset,
});
Expand Down
6 changes: 3 additions & 3 deletions build.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ const { version } = require("./package.json");

const targetMap = [
{
target: "node14-linux-x64",
target: "node18-linux-x64",
output: `./binaries/podcast-dl-${version}-linux-x64`,
},
{
target: "node14-macos-x64",
target: "node18-macos-x64",
output: `./binaries/podcast-dl-${version}-macos-x64`,
},
{
target: "node14-win-x64",
target: "node18-win-x64",
output: `./binaries/podcast-dl-${version}-win-x64`,
},
];
Expand Down
6 changes: 6 additions & 0 deletions docs/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,9 @@ npx podcast-dl --episode-regex "Zelda" --url "http://eightfour.libsyn.com/rss"
```bash
npx podcast-dl --url "http://eightfour.libsyn.com/rss" --exec "ffmpeg -i {{episode_path}} -b:a 192k -f mp3 {{episode_path_base}}/{{episode_filename_base}}-192k.mp3"
```

## Extract "foo" and "bar" from the episode title and place it in the episode filename

```bash
npx podcast-dl --url "http://eightfour.libsyn.com/rss" --episode-custom-template-options "foo" "bar" --episode-template "{{custom_0}}-{{custom_1}}-{{episode_num}}""
```
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"cli"
],
"engines": {
"node": ">=14.17.6"
"node": ">=18.17.0"
},
"repository": {
"type": "git",
Expand All @@ -49,7 +49,7 @@
},
"dependencies": {
"command-exists": "^1.2.9",
"commander": "^5.1.0",
"commander": "^12.1.0",
"dayjs": "^1.8.25",
"filenamify": "^6.0.0",
"global-agent": "^3.0.0",
Expand Down

0 comments on commit d280eea

Please sign in to comment.