Skip to content

Commit

Permalink
feat: add support for '--file' parsing
Browse files Browse the repository at this point in the history
closes #64
  • Loading branch information
lightpohl committed Feb 17, 2024
1 parent 24ceea0 commit 70254f3
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 8 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ A CLI for downloading podcasts with a focus on archiving.

## Options

Either `--url` or `--file` must be provided.

Type values surrounded in square brackets (`[]`) can be used as used as boolean options (no argument required).

| Option | Type | Required | Description |
| ------------------------ | ------------------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| --url | String | true | URL to podcast RSS feed. |
| --url | String | true\* | URL to podcast RSS feed. |
| --file | String | true\* | Path to local RSS file. |
| --out-dir | String | false | Specify output directory for episodes and metadata. Defaults to "./{{podcast_title}}". See "Template Options" for more details. |
| --threads | Number | false | Determines the number of downloads that will happen concurrently. Default is 1. |
| --attempts | Number | false | Sets the number of download attempts per individual file. Default is 3. |
Expand Down
19 changes: 14 additions & 5 deletions bin/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import { setupCommander } from "./commander.js";
import { download } from "./async.js";
import {
getArchiveKey,
getFeed,
getFileFeed,
getImageUrl,
getItemsToDownload,
getUrlExt,
getUrlFeed,
logFeedInfo,
logItemsList,
writeFeedMeta,
Expand All @@ -31,6 +32,7 @@ import { downloadItemsAsync } from "./async.js";
setupCommander(commander, process.argv);

const {
file,
url,
outDir,
episodeTemplate,
Expand Down Expand Up @@ -62,17 +64,24 @@ const {
let { archive } = commander;

const main = async () => {
if (!url) {
logErrorAndExit("No URL provided");
if (!url && !file) {
logErrorAndExit("No URL or file location provided");
}

if (url && file) {
logErrorAndExit("Must not use URL and file location");
}

if (proxy) {
bootstrapProxy();
}

const { hostname, pathname } = new URL(url);
const feed = url
? await getUrlFeed(url, parserConfig)
: await getFileFeed(file, parserConfig);

const { hostname, pathname } = new URL(feed.feedUrl);
const archiveUrl = `${hostname}${pathname}`;
const feed = await getFeed(url, parserConfig);
const basePath = _path.resolve(
process.cwd(),
getFolderName({ feed, template: outDir })
Expand Down
1 change: 1 addition & 0 deletions bin/commander.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { logErrorAndExit } from "./logger.js";
export const setupCommander = (commander, argv) => {
commander
.option("--url <string>", "url to podcast rss feed")
.option("--file <path>", "local path to podcast rss feed")
.option(
"--out-dir <path>",
"specify output directory",
Expand Down
47 changes: 45 additions & 2 deletions bin/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,22 @@ const getPublicObject = (object, exclude = []) => {
return output;
};

const getFileString = (filePath) => {
const fullPath = path.resolve(process.cwd(), filePath);

if (!fs.existsSync(fullPath)) {
return null;
}

const data = fs.readFileSync(fullPath, "utf8");

if (!data) {
return null;
}

return data;
};

const getJsonFile = (filePath) => {
const fullPath = path.resolve(process.cwd(), filePath);

Expand Down Expand Up @@ -453,7 +469,33 @@ const getImageUrl = ({ image, itunes }) => {
return null;
};

const getFeed = async (url, parserConfig) => {
const getFileFeed = async (filePath, parserConfig) => {
const defaultConfig = {
defaultRSS: 2.0,
};

const config = parserConfig ? getJsonFile(parserConfig) : defaultConfig;
const rssString = getFileString(filePath);

console.log(filePath, "rss string", rssString);

if (parserConfig && !config) {
logErrorAndExit(`Unable to load parser config: ${parserConfig}`);
}

const parser = new rssParser(config);

let feed;
try {
feed = await parser.parseString(rssString);
} catch (err) {
logErrorAndExit("Unable to parse RSS URL", err);
}

return feed;
};

const getUrlFeed = async (url, parserConfig) => {
const defaultConfig = {
defaultRSS: 2.0,
};
Expand Down Expand Up @@ -589,11 +631,12 @@ export {
getArchiveKey,
writeToArchive,
getEpisodeAudioUrlAndExt,
getFeed,
getFileFeed,
getImageUrl,
getItemsToDownload,
getTempPath,
getUrlExt,
getUrlFeed,
logFeedInfo,
ITEM_LIST_FORMATS,
logItemsList,
Expand Down

0 comments on commit 70254f3

Please sign in to comment.