podcast-dl 11.6.1 → 11.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2020 Joshua Pohl
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2020 Joshua Pohl
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/bin/archive.js CHANGED
@@ -1,39 +1,39 @@
1
- import dayjs from "dayjs";
2
- import fs from "fs";
3
- import path from "path";
4
- import { getJsonFile } from "./util.js";
5
-
6
- export const getArchiveKey = ({ prefix, name }) => {
7
- return `${prefix}-${name}`;
8
- };
9
-
10
- export const getArchive = (archive) => {
11
- const archiveContent = getJsonFile(archive);
12
- return archiveContent === null ? [] : archiveContent;
13
- };
14
-
15
- export const writeToArchive = ({ key, archive }) => {
16
- const archivePath = path.resolve(process.cwd(), archive);
17
- const archiveResult = getArchive(archive);
18
-
19
- if (!archiveResult.includes(key)) {
20
- archiveResult.push(key);
21
- }
22
-
23
- fs.writeFileSync(archivePath, JSON.stringify(archiveResult, null, 4));
24
- };
25
-
26
- export const getIsInArchive = ({ key, archive }) => {
27
- const archiveResult = getArchive(archive);
28
- return archiveResult.includes(key);
29
- };
30
-
31
- export const getArchiveFilename = ({ pubDate, name, ext }) => {
32
- const formattedPubDate = pubDate
33
- ? dayjs(new Date(pubDate)).format("YYYYMMDD")
34
- : null;
35
-
36
- const baseName = formattedPubDate ? `${formattedPubDate}-${name}` : name;
37
-
38
- return `${baseName}${ext}`;
39
- };
1
+ import dayjs from "dayjs";
2
+ import fs from "fs";
3
+ import path from "path";
4
+ import { getJsonFile } from "./util.js";
5
+
6
+ export const getArchiveKey = ({ prefix, name }) => {
7
+ return `${prefix}-${name}`;
8
+ };
9
+
10
+ export const getArchive = (archive) => {
11
+ const archiveContent = getJsonFile(archive);
12
+ return archiveContent === null ? [] : archiveContent;
13
+ };
14
+
15
+ export const writeToArchive = ({ key, archive }) => {
16
+ const archivePath = path.resolve(process.cwd(), archive);
17
+ const archiveResult = getArchive(archive);
18
+
19
+ if (!archiveResult.includes(key)) {
20
+ archiveResult.push(key);
21
+ }
22
+
23
+ fs.writeFileSync(archivePath, JSON.stringify(archiveResult, null, 4));
24
+ };
25
+
26
+ export const getIsInArchive = ({ key, archive }) => {
27
+ const archiveResult = getArchive(archive);
28
+ return archiveResult.includes(key);
29
+ };
30
+
31
+ export const getArchiveFilename = ({ pubDate, name, ext }) => {
32
+ const formattedPubDate = pubDate
33
+ ? dayjs(new Date(pubDate)).format("YYYYMMDD")
34
+ : null;
35
+
36
+ const baseName = formattedPubDate ? `${formattedPubDate}-${name}` : name;
37
+
38
+ return `${baseName}${ext}`;
39
+ };
package/bin/bin.js CHANGED
File without changes
package/bin/exec.js CHANGED
@@ -1,30 +1,30 @@
1
- import { exec } from "child_process";
2
- import util from "util";
3
- import { escapeArgForShell } from "./util.js";
4
-
5
- export const execWithPromise = util.promisify(exec);
6
-
7
- export const runExec = async ({
8
- exec,
9
- basePath,
10
- outputPodcastPath,
11
- episodeFilename,
12
- episodeAudioUrl,
13
- }) => {
14
- const episodeFilenameBase = episodeFilename.substring(
15
- 0,
16
- episodeFilename.lastIndexOf(".")
17
- );
18
-
19
- const execCmd = exec
20
- .replace(/{{episode_path}}/g, escapeArgForShell(outputPodcastPath))
21
- .replace(/{{episode_path_base}}/g, escapeArgForShell(basePath))
22
- .replace(/{{episode_filename}}/g, escapeArgForShell(episodeFilename))
23
- .replace(
24
- /{{episode_filename_base}}/g,
25
- escapeArgForShell(episodeFilenameBase)
26
- )
27
- .replace(/{{url}}/g, escapeArgForShell(episodeAudioUrl));
28
-
29
- await execWithPromise(execCmd, { stdio: "ignore" });
30
- };
1
+ import { exec } from "child_process";
2
+ import util from "util";
3
+ import { escapeArgForShell } from "./util.js";
4
+
5
+ export const execWithPromise = util.promisify(exec);
6
+
7
+ export const runExec = async ({
8
+ exec,
9
+ basePath,
10
+ outputPodcastPath,
11
+ episodeFilename,
12
+ episodeAudioUrl,
13
+ }) => {
14
+ const episodeFilenameBase = episodeFilename.substring(
15
+ 0,
16
+ episodeFilename.lastIndexOf(".")
17
+ );
18
+
19
+ const execCmd = exec
20
+ .replace(/{{episode_path}}/g, escapeArgForShell(outputPodcastPath))
21
+ .replace(/{{episode_path_base}}/g, escapeArgForShell(basePath))
22
+ .replace(/{{episode_filename}}/g, escapeArgForShell(episodeFilename))
23
+ .replace(
24
+ /{{episode_filename_base}}/g,
25
+ escapeArgForShell(episodeFilenameBase)
26
+ )
27
+ .replace(/{{url}}/g, escapeArgForShell(episodeAudioUrl));
28
+
29
+ await execWithPromise(execCmd, { stdio: "ignore" });
30
+ };
package/bin/logger.js CHANGED
@@ -1,84 +1,84 @@
1
- /* eslint-disable no-console */
2
-
3
- export const ERROR_STATUSES = {
4
- general: 1,
5
- nothingDownloaded: 2,
6
- completedWithErrors: 3,
7
- };
8
-
9
- export const LOG_LEVEL_TYPES = {
10
- debug: "debug",
11
- quiet: "quiet",
12
- silent: "silent",
13
- static: "static",
14
- };
15
-
16
- export const LOG_LEVELS = {
17
- debug: 0,
18
- info: 1,
19
- important: 2,
20
- };
21
-
22
- export const getShouldOutputProgressIndicator = () => {
23
- return (
24
- process.stdout.isTTY &&
25
- process.env.LOG_LEVEL !== LOG_LEVEL_TYPES.static &&
26
- process.env.LOG_LEVEL !== LOG_LEVEL_TYPES.quiet &&
27
- process.env.LOG_LEVEL !== LOG_LEVEL_TYPES.silent
28
- );
29
- };
30
-
31
- export const logMessage = (message = "", logLevel = 1) => {
32
- if (
33
- !process.env.LOG_LEVEL ||
34
- process.env.LOG_LEVEL === LOG_LEVEL_TYPES.debug ||
35
- process.env.LOG_LEVEL === LOG_LEVEL_TYPES.static
36
- ) {
37
- console.log(message);
38
- return;
39
- }
40
-
41
- if (process.env.LOG_LEVEL === LOG_LEVEL_TYPES.silent) {
42
- return;
43
- }
44
-
45
- if (
46
- process.env.LOG_LEVEL === LOG_LEVEL_TYPES.quiet &&
47
- logLevel > LOG_LEVELS.info
48
- ) {
49
- console.log(message);
50
- return;
51
- }
52
- };
53
-
54
- export const getLogMessageWithMarker = (marker) => {
55
- return (message, logLevel) => {
56
- if (marker) {
57
- logMessage(`${marker} | ${message}`, logLevel);
58
- } else {
59
- logMessage(message, logLevel);
60
- }
61
- };
62
- };
63
-
64
- export const logError = (msg, error) => {
65
- if (process.env.LOG_LEVEL === LOG_LEVEL_TYPES.silent) {
66
- return;
67
- }
68
-
69
- console.error(msg);
70
-
71
- if (error) {
72
- console.error(error.message);
73
- }
74
- };
75
-
76
- export const logErrorAndExit = (msg, error) => {
77
- console.error(msg);
78
-
79
- if (error) {
80
- console.error(error.message);
81
- }
82
-
83
- process.exit(ERROR_STATUSES.general);
84
- };
1
+ /* eslint-disable no-console */
2
+
3
+ export const ERROR_STATUSES = {
4
+ general: 1,
5
+ nothingDownloaded: 2,
6
+ completedWithErrors: 3,
7
+ };
8
+
9
+ export const LOG_LEVEL_TYPES = {
10
+ debug: "debug",
11
+ quiet: "quiet",
12
+ silent: "silent",
13
+ static: "static",
14
+ };
15
+
16
+ export const LOG_LEVELS = {
17
+ debug: 0,
18
+ info: 1,
19
+ important: 2,
20
+ };
21
+
22
+ export const getShouldOutputProgressIndicator = () => {
23
+ return (
24
+ process.stdout.isTTY &&
25
+ process.env.LOG_LEVEL !== LOG_LEVEL_TYPES.static &&
26
+ process.env.LOG_LEVEL !== LOG_LEVEL_TYPES.quiet &&
27
+ process.env.LOG_LEVEL !== LOG_LEVEL_TYPES.silent
28
+ );
29
+ };
30
+
31
+ export const logMessage = (message = "", logLevel = 1) => {
32
+ if (
33
+ !process.env.LOG_LEVEL ||
34
+ process.env.LOG_LEVEL === LOG_LEVEL_TYPES.debug ||
35
+ process.env.LOG_LEVEL === LOG_LEVEL_TYPES.static
36
+ ) {
37
+ console.log(message);
38
+ return;
39
+ }
40
+
41
+ if (process.env.LOG_LEVEL === LOG_LEVEL_TYPES.silent) {
42
+ return;
43
+ }
44
+
45
+ if (
46
+ process.env.LOG_LEVEL === LOG_LEVEL_TYPES.quiet &&
47
+ logLevel > LOG_LEVELS.info
48
+ ) {
49
+ console.log(message);
50
+ return;
51
+ }
52
+ };
53
+
54
+ export const getLogMessageWithMarker = (marker) => {
55
+ return (message, logLevel) => {
56
+ if (marker) {
57
+ logMessage(`${marker} | ${message}`, logLevel);
58
+ } else {
59
+ logMessage(message, logLevel);
60
+ }
61
+ };
62
+ };
63
+
64
+ export const logError = (msg, error) => {
65
+ if (process.env.LOG_LEVEL === LOG_LEVEL_TYPES.silent) {
66
+ return;
67
+ }
68
+
69
+ console.error(msg);
70
+
71
+ if (error) {
72
+ console.error(error.message);
73
+ }
74
+ };
75
+
76
+ export const logErrorAndExit = (msg, error) => {
77
+ console.error(msg);
78
+
79
+ if (error) {
80
+ console.error(error.message);
81
+ }
82
+
83
+ process.exit(ERROR_STATUSES.general);
84
+ };
package/bin/meta.js CHANGED
@@ -1,66 +1,66 @@
1
- import fs from "fs";
2
- import { getIsInArchive, writeToArchive } from "./archive.js";
3
- import { logMessage } from "./logger.js";
4
- import { getPublicObject } from "./util.js";
5
-
6
- export const writeFeedMeta = ({ outputPath, feed, key, archive, override }) => {
7
- if (key && archive && getIsInArchive({ key, archive })) {
8
- logMessage("Feed metadata exists in archive. Skipping...");
9
- return;
10
- }
11
- const output = getPublicObject(feed, ["items"]);
12
-
13
- try {
14
- if (override || !fs.existsSync(outputPath)) {
15
- fs.writeFileSync(outputPath, JSON.stringify(output, null, 4));
16
- } else {
17
- logMessage("Feed metadata exists locally. Skipping...");
18
- }
19
-
20
- if (key && archive && !getIsInArchive({ key, archive })) {
21
- try {
22
- writeToArchive({ key, archive });
23
- } catch (error) {
24
- throw new Error(`Error writing to archive: ${error.toString()}`);
25
- }
26
- }
27
- } catch (error) {
28
- throw new Error(
29
- `Unable to save metadata file for feed: ${error.toString()}`
30
- );
31
- }
32
- };
33
-
34
- export const writeItemMeta = ({
35
- marker,
36
- outputPath,
37
- item,
38
- key,
39
- archive,
40
- override,
41
- }) => {
42
- if (key && archive && getIsInArchive({ key, archive })) {
43
- logMessage(`${marker} | Episode metadata exists in archive. Skipping...`);
44
- return;
45
- }
46
-
47
- const output = getPublicObject(item);
48
-
49
- try {
50
- if (override || !fs.existsSync(outputPath)) {
51
- fs.writeFileSync(outputPath, JSON.stringify(output, null, 4));
52
- } else {
53
- logMessage(`${marker} | Episode metadata exists locally. Skipping...`);
54
- }
55
-
56
- if (key && archive && !getIsInArchive({ key, archive })) {
57
- try {
58
- writeToArchive({ key, archive });
59
- } catch (error) {
60
- throw new Error("Error writing to archive", error);
61
- }
62
- }
63
- } catch (error) {
64
- throw new Error("Unable to save meta file for episode", error);
65
- }
66
- };
1
+ import fs from "fs";
2
+ import { getIsInArchive, writeToArchive } from "./archive.js";
3
+ import { logMessage } from "./logger.js";
4
+ import { getPublicObject } from "./util.js";
5
+
6
+ export const writeFeedMeta = ({ outputPath, feed, key, archive, override }) => {
7
+ if (key && archive && getIsInArchive({ key, archive })) {
8
+ logMessage("Feed metadata exists in archive. Skipping...");
9
+ return;
10
+ }
11
+ const output = getPublicObject(feed, ["items"]);
12
+
13
+ try {
14
+ if (override || !fs.existsSync(outputPath)) {
15
+ fs.writeFileSync(outputPath, JSON.stringify(output, null, 4));
16
+ } else {
17
+ logMessage("Feed metadata exists locally. Skipping...");
18
+ }
19
+
20
+ if (key && archive && !getIsInArchive({ key, archive })) {
21
+ try {
22
+ writeToArchive({ key, archive });
23
+ } catch (error) {
24
+ throw new Error(`Error writing to archive: ${error.toString()}`);
25
+ }
26
+ }
27
+ } catch (error) {
28
+ throw new Error(
29
+ `Unable to save metadata file for feed: ${error.toString()}`
30
+ );
31
+ }
32
+ };
33
+
34
+ export const writeItemMeta = ({
35
+ marker,
36
+ outputPath,
37
+ item,
38
+ key,
39
+ archive,
40
+ override,
41
+ }) => {
42
+ if (key && archive && getIsInArchive({ key, archive })) {
43
+ logMessage(`${marker} | Episode metadata exists in archive. Skipping...`);
44
+ return;
45
+ }
46
+
47
+ const output = getPublicObject(item);
48
+
49
+ try {
50
+ if (override || !fs.existsSync(outputPath)) {
51
+ fs.writeFileSync(outputPath, JSON.stringify(output, null, 4));
52
+ } else {
53
+ logMessage(`${marker} | Episode metadata exists locally. Skipping...`);
54
+ }
55
+
56
+ if (key && archive && !getIsInArchive({ key, archive })) {
57
+ try {
58
+ writeToArchive({ key, archive });
59
+ } catch (error) {
60
+ throw new Error("Error writing to archive", error);
61
+ }
62
+ }
63
+ } catch (error) {
64
+ throw new Error("Unable to save meta file for episode", error);
65
+ }
66
+ };
package/bin/naming.js CHANGED
@@ -1,136 +1,144 @@
1
- import dayjs from "dayjs";
2
- import filenamify from "filenamify";
3
- import path from "path";
4
-
5
- const INVALID_CHAR_REPLACE = "_";
6
-
7
- const FILTER_FUNCTIONS = {
8
- strip: (val) => val.replace(/\s+/g, ""),
9
- strip_special: (val) => val.replace(/[^a-zA-Z0-9\s]/g, ""),
10
- underscore: (val) => val.replace(/\s+/g, "_"),
11
- dash: (val) => val.replace(/\s+/g, "-"),
12
- camelcase: (val) =>
13
- val
14
- .split(/\s+/)
15
- .map((w) =>
16
- w ? w.charAt(0).toUpperCase() + w.slice(1).toLowerCase() : ""
17
- )
18
- .join(""),
19
- lowercase: (val) => val.toLowerCase(),
20
- uppercase: (val) => val.toUpperCase(),
21
- trim: (val) => val.trim(),
22
- };
23
-
24
- const applyFilters = (value, filterStr) => {
25
- if (!filterStr) {
26
- return value;
27
- }
28
-
29
- const filters = filterStr.slice(1).split("|");
30
- return filters.reduce((val, filter) => {
31
- const filterFn = FILTER_FUNCTIONS[filter];
32
- return filterFn ? filterFn(val) : val;
33
- }, value);
34
- };
35
-
36
- const MAX_LENGTH_FILENAME = process.env.MAX_LENGTH_FILENAME
37
- ? parseInt(process.env.MAX_LENGTH_FILENAME)
38
- : 255;
39
-
40
- export const getSafeName = (name, maxLength = MAX_LENGTH_FILENAME) => {
41
- return filenamify(name, {
42
- replacement: INVALID_CHAR_REPLACE,
43
- maxLength,
44
- });
45
- };
46
-
47
- export const getSimpleFilename = (name, ext = "") => {
48
- return `${getSafeName(name, MAX_LENGTH_FILENAME - (ext?.length ?? 0))}${ext}`;
49
- };
50
-
51
- export const getItemFilename = ({
52
- item,
53
- ext,
54
- url,
55
- feed,
56
- template,
57
- width,
58
- customTemplateOptions = [],
59
- offset = 0,
60
- }) => {
61
- const episodeNum = feed.items.length - item._originalIndex + offset;
62
- const title = item.title || "";
63
-
64
- const releaseYear = item.pubDate
65
- ? dayjs(new Date(item.pubDate)).format("YYYY")
66
- : null;
67
-
68
- const releaseMonth = item.pubDate
69
- ? dayjs(new Date(item.pubDate)).format("MM")
70
- : null;
71
-
72
- const releaseDay = item.pubDate
73
- ? dayjs(new Date(item.pubDate)).format("DD")
74
- : null;
75
-
76
- const releaseDate = item.pubDate
77
- ? dayjs(new Date(item.pubDate)).format("YYYYMMDD")
78
- : null;
79
-
80
- const customReplacementTuples = customTemplateOptions.map((option, i) => {
81
- const matchRegex = new RegExp(option);
82
- const match = title.match(matchRegex);
83
-
84
- return match && match[0] ? [`custom_${i}`, match[0]] : [`custom_${i}`, ""];
85
- });
86
-
87
- const templateReplacementsTuples = [
88
- ["title", title],
89
- ["release_date", releaseDate || ""],
90
- ["release_year", releaseYear || ""],
91
- ["release_month", releaseMonth || ""],
92
- ["release_day", releaseDay || ""],
93
- ["episode_num", `${episodeNum}`.padStart(width, "0")],
94
- ["url", url],
95
- ["podcast_title", feed.title || ""],
96
- ["podcast_link", feed.link || ""],
97
- ["duration", item.itunes?.duration || ""],
98
- ["guid", item.guid],
99
- ...customReplacementTuples,
100
- ];
101
-
102
- const replacementsMap = Object.fromEntries(templateReplacementsTuples);
103
- const templateSegments = template.trim().split(path.sep);
104
- const nameSegments = templateSegments.map((segment) => {
105
- const replaceRegex = /{{(\w+)(\|[^}]+)?}}/g;
106
- const name = segment.replace(replaceRegex, (match, varName, filterStr) => {
107
- const replacement = replacementsMap[varName] || "";
108
- return applyFilters(replacement, filterStr);
109
- });
110
-
111
- return getSimpleFilename(name);
112
- });
113
-
114
- nameSegments[nameSegments.length - 1] = getSimpleFilename(
115
- nameSegments[nameSegments.length - 1],
116
- ext
117
- );
118
-
119
- return nameSegments.join(path.sep);
120
- };
121
-
122
- export const getFolderName = ({ feed, template }) => {
123
- const replacementsMap = {
124
- podcast_title: feed.title || "",
125
- podcast_link: feed.link || "",
126
- };
127
-
128
- const replaceRegex = /{{(\w+)(\|[^}]+)?}}/g;
129
- const name = template.replace(replaceRegex, (_, varName, filterStr) => {
130
- const replacement = replacementsMap[varName] || "";
131
- const filtered = applyFilters(replacement, filterStr);
132
- return getSafeName(filtered);
133
- });
134
-
135
- return name;
136
- };
1
+ import dayjs from "dayjs";
2
+ import filenamify from "filenamify";
3
+ import path from "path";
4
+
5
+ const INVALID_CHAR_REPLACE = "_";
6
+
7
+ const FILTER_FUNCTIONS = {
8
+ strip: (val) => val.replace(/\s+/g, ""),
9
+ strip_special: (val) => val.replace(/[^a-zA-Z0-9\s]/g, ""),
10
+ underscore: (val) => val.replace(/\s+/g, "_"),
11
+ dash: (val) => val.replace(/\s+/g, "-"),
12
+ camelcase: (val) =>
13
+ val
14
+ .split(/\s+/)
15
+ .map((w) =>
16
+ w ? w.charAt(0).toUpperCase() + w.slice(1).toLowerCase() : ""
17
+ )
18
+ .join(""),
19
+ lowercase: (val) => val.toLowerCase(),
20
+ uppercase: (val) => val.toUpperCase(),
21
+ trim: (val) => val.trim(),
22
+ };
23
+
24
+ const applyFilters = (value, filterStr) => {
25
+ if (!filterStr) {
26
+ return value;
27
+ }
28
+
29
+ const filters = filterStr.slice(1).split("|");
30
+ return filters.reduce((val, filter) => {
31
+ const filterFn = FILTER_FUNCTIONS[filter];
32
+ return filterFn ? filterFn(val) : val;
33
+ }, value);
34
+ };
35
+
36
+ const MAX_LENGTH_FILENAME = process.env.MAX_LENGTH_FILENAME
37
+ ? parseInt(process.env.MAX_LENGTH_FILENAME)
38
+ : 255;
39
+
40
+ export const getSafeName = (name, maxLength = MAX_LENGTH_FILENAME) => {
41
+ // Replace periods with underscores BEFORE filenamify truncation.
42
+ // filenamify treats periods as extension delimiters and preserves content
43
+ // after the last period while truncating from the START, which destroys
44
+ // dates and other important prefix content in podcast titles.
45
+ const sanitized = name.replace(/\./g, INVALID_CHAR_REPLACE);
46
+ return filenamify(sanitized, {
47
+ replacement: INVALID_CHAR_REPLACE,
48
+ maxLength,
49
+ });
50
+ };
51
+
52
+ export const getSimpleFilename = (name, ext = "") => {
53
+ return `${getSafeName(name, MAX_LENGTH_FILENAME - (ext?.length ?? 0))}${ext}`;
54
+ };
55
+
56
+ export const getItemFilename = ({
57
+ item,
58
+ ext,
59
+ url,
60
+ feed,
61
+ template,
62
+ width,
63
+ customTemplateOptions = [],
64
+ offset = 0,
65
+ }) => {
66
+ const episodeNum = feed.items.length - item._originalIndex + offset;
67
+ const title = item.title || "";
68
+
69
+ const releaseYear = item.pubDate
70
+ ? dayjs(new Date(item.pubDate)).format("YYYY")
71
+ : null;
72
+
73
+ const releaseMonth = item.pubDate
74
+ ? dayjs(new Date(item.pubDate)).format("MM")
75
+ : null;
76
+
77
+ const releaseDay = item.pubDate
78
+ ? dayjs(new Date(item.pubDate)).format("DD")
79
+ : null;
80
+
81
+ const releaseDate = item.pubDate
82
+ ? dayjs(new Date(item.pubDate)).format("YYYYMMDD")
83
+ : null;
84
+
85
+ const customReplacementTuples = customTemplateOptions.map((option, i) => {
86
+ const matchRegex = new RegExp(option);
87
+ const match = title.match(matchRegex);
88
+
89
+ return match && match[0] ? [`custom_${i}`, match[0]] : [`custom_${i}`, ""];
90
+ });
91
+
92
+ const templateReplacementsTuples = [
93
+ ["title", title],
94
+ ["release_date", releaseDate || ""],
95
+ ["release_year", releaseYear || ""],
96
+ ["release_month", releaseMonth || ""],
97
+ ["release_day", releaseDay || ""],
98
+ ["episode_num", `${episodeNum}`.padStart(width, "0")],
99
+ ["url", url],
100
+ ["podcast_title", feed.title || ""],
101
+ ["podcast_link", feed.link || ""],
102
+ ["duration", item.itunes?.duration || ""],
103
+ ["guid", item.guid],
104
+ ...customReplacementTuples,
105
+ ];
106
+
107
+ const replacementsMap = Object.fromEntries(templateReplacementsTuples);
108
+ const templateSegments = template.trim().split(path.sep);
109
+ const nameSegments = templateSegments.map((segment, index) => {
110
+ const replaceRegex = /{{(\w+)(\|[^}]+)?}}/g;
111
+ const name = segment.replace(replaceRegex, (match, varName, filterStr) => {
112
+ const replacement = replacementsMap[varName] || "";
113
+ return applyFilters(replacement, filterStr);
114
+ });
115
+
116
+ // Only truncate non-final segments here (they don't get an extension)
117
+ // Final segment is truncated below with the extension accounted for
118
+ const isLastSegment = index === templateSegments.length - 1;
119
+ return isLastSegment ? name : getSimpleFilename(name);
120
+ });
121
+
122
+ nameSegments[nameSegments.length - 1] = getSimpleFilename(
123
+ nameSegments[nameSegments.length - 1],
124
+ ext
125
+ );
126
+
127
+ return nameSegments.join(path.sep);
128
+ };
129
+
130
+ export const getFolderName = ({ feed, template }) => {
131
+ const replacementsMap = {
132
+ podcast_title: feed.title || "",
133
+ podcast_link: feed.link || "",
134
+ };
135
+
136
+ const replaceRegex = /{{(\w+)(\|[^}]+)?}}/g;
137
+ const name = template.replace(replaceRegex, (_, varName, filterStr) => {
138
+ const replacement = replacementsMap[varName] || "";
139
+ const filtered = applyFilters(replacement, filterStr);
140
+ return getSafeName(filtered);
141
+ });
142
+
143
+ return name;
144
+ };
package/package.json CHANGED
@@ -1,65 +1,65 @@
1
- {
2
- "name": "podcast-dl",
3
- "version": "11.6.1",
4
- "description": "A CLI for downloading podcasts.",
5
- "type": "module",
6
- "bin": "./bin/bin.js",
7
- "scripts": {
8
- "build": "rimraf ./binaries && rimraf ./dist && node build.cjs",
9
- "lint": "eslint ./bin"
10
- },
11
- "lint-staged": {
12
- "*.{js,json,md}": [
13
- "prettier --write"
14
- ]
15
- },
16
- "husky": {
17
- "hooks": {
18
- "pre-commit": "lint-staged",
19
- "pre-push": "npm run lint"
20
- }
21
- },
22
- "keywords": [
23
- "podcast",
24
- "podcasts",
25
- "downloader",
26
- "cli"
27
- ],
28
- "engines": {
29
- "node": ">=18.17.0"
30
- },
31
- "repository": {
32
- "type": "git",
33
- "url": "https://github.com/lightpohl/podcast-dl.git"
34
- },
35
- "files": [
36
- "bin",
37
- "lib"
38
- ],
39
- "author": "Joshua Pohl",
40
- "license": "MIT",
41
- "devDependencies": {
42
- "@eslint/js": "^9.18.0",
43
- "@yao-pkg/pkg": "^6.6.0",
44
- "eslint": "^9.18.0",
45
- "globals": "^15.14.0",
46
- "husky": "^4.2.5",
47
- "lint-staged": "^10.1.7",
48
- "prettier": "2.3.2",
49
- "rimraf": "^3.0.2",
50
- "webpack": "^5.75.0"
51
- },
52
- "dependencies": {
53
- "command-exists": "^1.2.9",
54
- "commander": "^12.1.0",
55
- "dayjs": "^1.8.25",
56
- "filenamify": "^6.0.0",
57
- "global-agent": "^3.0.0",
58
- "got": "^11.0.2",
59
- "p-limit": "^4.0.0",
60
- "pluralize": "^8.0.0",
61
- "entities": "^2.0.3",
62
- "xml2js": "^0.5.0",
63
- "throttle-debounce": "^3.0.1"
64
- }
65
- }
1
+ {
2
+ "name": "podcast-dl",
3
+ "version": "11.6.2",
4
+ "description": "A CLI for downloading podcasts.",
5
+ "type": "module",
6
+ "bin": "./bin/bin.js",
7
+ "scripts": {
8
+ "build": "rimraf ./binaries && rimraf ./dist && node build.cjs",
9
+ "lint": "eslint ./bin"
10
+ },
11
+ "lint-staged": {
12
+ "*.{js,json,md}": [
13
+ "prettier --write"
14
+ ]
15
+ },
16
+ "husky": {
17
+ "hooks": {
18
+ "pre-commit": "lint-staged",
19
+ "pre-push": "npm run lint"
20
+ }
21
+ },
22
+ "keywords": [
23
+ "podcast",
24
+ "podcasts",
25
+ "downloader",
26
+ "cli"
27
+ ],
28
+ "engines": {
29
+ "node": ">=18.17.0"
30
+ },
31
+ "repository": {
32
+ "type": "git",
33
+ "url": "https://github.com/lightpohl/podcast-dl.git"
34
+ },
35
+ "files": [
36
+ "bin",
37
+ "lib"
38
+ ],
39
+ "author": "Joshua Pohl",
40
+ "license": "MIT",
41
+ "devDependencies": {
42
+ "@eslint/js": "^9.18.0",
43
+ "@yao-pkg/pkg": "^6.6.0",
44
+ "eslint": "^9.18.0",
45
+ "globals": "^15.14.0",
46
+ "husky": "^4.2.5",
47
+ "lint-staged": "^10.1.7",
48
+ "prettier": "2.3.2",
49
+ "rimraf": "^3.0.2",
50
+ "webpack": "^5.75.0"
51
+ },
52
+ "dependencies": {
53
+ "command-exists": "^1.2.9",
54
+ "commander": "^12.1.0",
55
+ "dayjs": "^1.8.25",
56
+ "filenamify": "^6.0.0",
57
+ "global-agent": "^3.0.0",
58
+ "got": "^11.0.2",
59
+ "p-limit": "^4.0.0",
60
+ "pluralize": "^8.0.0",
61
+ "entities": "^2.0.3",
62
+ "xml2js": "^0.5.0",
63
+ "throttle-debounce": "^3.0.1"
64
+ }
65
+ }