podcast-dl 11.6.1 → 11.7.1

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,51 @@
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 getArchiveKeys = ({ prefix, name, guid }) => {
11
+ const legacyKey = name ? getArchiveKey({ prefix, name }) : null;
12
+ const guidKey = guid ? getArchiveKey({ prefix, name: guid }) : null;
13
+ return [legacyKey, guidKey].filter(Boolean);
14
+ };
15
+
16
+ export const getArchive = (archive) => {
17
+ const archiveContent = getJsonFile(archive);
18
+ return archiveContent === null ? [] : archiveContent;
19
+ };
20
+
21
+ export const writeToArchive = ({ key, archiveKeys, archive }) => {
22
+ const archivePath = path.resolve(process.cwd(), archive);
23
+ const archiveResult = getArchive(archive);
24
+ const keys = Array.from(
25
+ new Set([key, ...(archiveKeys || [])].filter(Boolean))
26
+ );
27
+
28
+ keys.forEach((archiveKey) => {
29
+ if (!archiveResult.includes(archiveKey)) {
30
+ archiveResult.push(archiveKey);
31
+ }
32
+ });
33
+
34
+ fs.writeFileSync(archivePath, JSON.stringify(archiveResult, null, 4));
35
+ };
36
+
37
+ export const getIsInArchive = ({ key, archiveKeys, archive }) => {
38
+ const archiveResult = getArchive(archive);
39
+ const keys = [key, ...(archiveKeys || [])].filter(Boolean);
40
+ return keys.some((archiveKey) => archiveResult.includes(archiveKey));
41
+ };
42
+
43
+ export const getArchiveFilename = ({ pubDate, name, ext }) => {
44
+ const formattedPubDate = pubDate
45
+ ? dayjs(new Date(pubDate)).format("YYYYMMDD")
46
+ : null;
47
+
48
+ const baseName = formattedPubDate ? `${formattedPubDate}-${name}` : name;
49
+
50
+ return `${baseName}${ext}`;
51
+ };
package/bin/async.js CHANGED
@@ -7,7 +7,7 @@ import { throttle } from "throttle-debounce";
7
7
  import { promisify } from "util";
8
8
  import {
9
9
  getArchiveFilename,
10
- getArchiveKey,
10
+ getArchiveKeys,
11
11
  getIsInArchive,
12
12
  writeToArchive,
13
13
  } from "./archive.js";
@@ -39,7 +39,7 @@ export const download = async (options) => {
39
39
  marker,
40
40
  url,
41
41
  outputPath,
42
- key,
42
+ archiveKeys = [],
43
43
  archive,
44
44
  override,
45
45
  alwaysPostprocess,
@@ -61,7 +61,11 @@ export const download = async (options) => {
61
61
  return outputPath;
62
62
  }
63
63
 
64
- if (key && archive && getIsInArchive({ key, archive })) {
64
+ if (
65
+ archive &&
66
+ archiveKeys.length &&
67
+ getIsInArchive({ archiveKeys, archive })
68
+ ) {
65
69
  logMessage("Download exists in archive. Skipping...");
66
70
  return null;
67
71
  }
@@ -149,11 +153,10 @@ export const download = async (options) => {
149
153
  return null;
150
154
  }
151
155
 
152
- const { outputPath: finalOutputPath, key: finalKey } = trustExt
153
- ? { outputPath, key }
156
+ const finalOutputPath = trustExt
157
+ ? outputPath
154
158
  : correctExtensionFromMime({
155
159
  outputPath,
156
- key,
157
160
  contentType: headResponse?.headers?.["content-type"],
158
161
  onCorrect: (from, to) =>
159
162
  logMessage(
@@ -170,9 +173,9 @@ export const download = async (options) => {
170
173
  await onAfterDownload(finalOutputPath);
171
174
  }
172
175
 
173
- if (finalKey && archive) {
176
+ if (archive && archiveKeys.length) {
174
177
  try {
175
- writeToArchive({ key: finalKey, archive });
178
+ writeToArchive({ archiveKeys, archive });
176
179
  } catch (error) {
177
180
  throw new Error(`Error writing to archive: ${error.toString()}`);
178
181
  }
@@ -245,14 +248,7 @@ export const downloadItemsAsync = async ({
245
248
  marker,
246
249
  trustExt,
247
250
  userAgent,
248
- key: getArchiveKey({
249
- prefix: archivePrefix,
250
- name: getArchiveFilename({
251
- name: item.title,
252
- pubDate: item.pubDate,
253
- ext: audioFileExt,
254
- }),
255
- }),
251
+ archiveKeys: item._archiveKeys || [],
256
252
  maxAttempts: attempts,
257
253
  outputPath: outputPodcastPath,
258
254
  url: episodeAudioUrl,
@@ -264,7 +260,7 @@ export const downloadItemsAsync = async ({
264
260
  override,
265
261
  trustExt,
266
262
  userAgent,
267
- key: item._episodeImage.key,
263
+ archiveKeys: item._episodeImage.archiveKeys || [],
268
264
  marker: item._episodeImage.url,
269
265
  maxAttempts: attempts,
270
266
  outputPath: item._episodeImage.outputPath,
@@ -289,7 +285,7 @@ export const downloadItemsAsync = async ({
289
285
  const finalTranscriptPath = await download({
290
286
  archive,
291
287
  override,
292
- key: item._episodeTranscript.key,
288
+ archiveKeys: item._episodeTranscript.archiveKeys || [],
293
289
  marker: item._episodeTranscript.url,
294
290
  maxAttempts: attempts,
295
291
  outputPath: item._episodeTranscript.outputPath,
@@ -371,13 +367,14 @@ export const downloadItemsAsync = async ({
371
367
  archive,
372
368
  override,
373
369
  item,
374
- key: getArchiveKey({
370
+ archiveKeys: getArchiveKeys({
375
371
  prefix: archivePrefix,
376
372
  name: getArchiveFilename({
377
373
  pubDate: item.pubDate,
378
374
  name: item.title,
379
375
  ext: episodeMetaExt,
380
376
  }),
377
+ guid: item.guid ? `${item.guid}-meta` : null,
381
378
  }),
382
379
  outputPath: outputEpisodeMetaPath,
383
380
  });
package/bin/bin.js CHANGED
@@ -164,10 +164,12 @@ const main = async () => {
164
164
  trustExt,
165
165
  userAgent,
166
166
  marker: podcastImageUrl,
167
- key: getArchiveKey({
168
- prefix: archivePrefix,
169
- name: `${feed.title || "image"}${podcastImageFileExt}`,
170
- }),
167
+ archiveKeys: [
168
+ getArchiveKey({
169
+ prefix: archivePrefix,
170
+ name: `${feed.title || "image"}${podcastImageFileExt}`,
171
+ }),
172
+ ],
171
173
  outputPath: outputImagePath,
172
174
  url: podcastImageUrl,
173
175
  maxAttempts: attempts,
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/items.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import dayjs from "dayjs";
2
2
  import path from "path";
3
- import { getArchive, getArchiveFilename, getArchiveKey } from "./archive.js";
3
+ import { getArchive, getArchiveFilename, getArchiveKeys } from "./archive.js";
4
4
  import { logErrorAndExit } from "./logger.js";
5
5
  import { getItemFilename } from "./naming.js";
6
6
  import {
@@ -48,7 +48,7 @@ export const getItemsToDownload = ({
48
48
  const savedArchive = archive ? getArchive(archive) : [];
49
49
 
50
50
  while (shouldGo(i)) {
51
- const { title, pubDate, itunes } = feed.items[i];
51
+ const { title, pubDate, itunes, guid } = feed.items[i];
52
52
  const actualSeasonNum = itunes?.season ? parseInt(itunes.season) : null;
53
53
  const pubDateDay = dayjs(new Date(pubDate));
54
54
  let isValid = true;
@@ -94,16 +94,17 @@ export const getItemsToDownload = ({
94
94
  const { url: episodeAudioUrl, ext: audioFileExt } =
95
95
  getEpisodeAudioUrlAndExt(feed.items[i], episodeSourceOrder);
96
96
 
97
- const key = getArchiveKey({
97
+ const archiveKeys = getArchiveKeys({
98
98
  prefix: archivePrefix,
99
99
  name: getArchiveFilename({
100
100
  pubDate,
101
101
  name: title,
102
102
  ext: audioFileExt,
103
103
  }),
104
+ guid,
104
105
  });
105
106
 
106
- if (key && savedArchive.includes(key)) {
107
+ if (archiveKeys.some((archiveKey) => savedArchive.includes(archiveKey))) {
107
108
  isValid = false;
108
109
  }
109
110
 
@@ -111,19 +112,21 @@ export const getItemsToDownload = ({
111
112
  const item = feed.items[i];
112
113
  item._originalIndex = i;
113
114
  item.seasonNum = actualSeasonNum;
115
+ item._archiveKeys = archiveKeys;
114
116
 
115
117
  if (includeEpisodeImages || embedMetadataFlag) {
116
118
  const episodeImageUrl = getImageUrl(item);
117
119
 
118
120
  if (episodeImageUrl) {
119
121
  const episodeImageFileExt = getUrlExt(episodeImageUrl);
120
- const episodeImageArchiveKey = getArchiveKey({
122
+ const episodeImageArchiveKeys = getArchiveKeys({
121
123
  prefix: archivePrefix,
122
124
  name: getArchiveFilename({
123
125
  pubDate,
124
126
  name: title,
125
127
  ext: episodeImageFileExt,
126
128
  }),
129
+ guid: guid ? `${guid}-image` : null,
127
130
  });
128
131
 
129
132
  const episodeImageName = getItemFilename({
@@ -141,7 +144,7 @@ export const getItemsToDownload = ({
141
144
  item._episodeImage = {
142
145
  url: episodeImageUrl,
143
146
  outputPath: outputImagePath,
144
- key: episodeImageArchiveKey,
147
+ archiveKeys: episodeImageArchiveKeys,
145
148
  };
146
149
  }
147
150
  }
@@ -154,13 +157,14 @@ export const getItemsToDownload = ({
154
157
 
155
158
  if (episodeTranscriptUrl) {
156
159
  const episodeTranscriptFileExt = getUrlExt(episodeTranscriptUrl);
157
- const episodeTranscriptArchiveKey = getArchiveKey({
160
+ const episodeTranscriptArchiveKeys = getArchiveKeys({
158
161
  prefix: archivePrefix,
159
162
  name: getArchiveFilename({
160
163
  pubDate,
161
164
  name: title,
162
165
  ext: episodeTranscriptFileExt,
163
166
  }),
167
+ guid: guid ? `${guid}-transcript` : null,
164
168
  });
165
169
 
166
170
  const episodeTranscriptName = getItemFilename({
@@ -181,7 +185,7 @@ export const getItemsToDownload = ({
181
185
  item._episodeTranscript = {
182
186
  url: episodeTranscriptUrl,
183
187
  outputPath: outputTranscriptPath,
184
- key: episodeTranscriptArchiveKey,
188
+ archiveKeys: episodeTranscriptArchiveKeys,
185
189
  };
186
190
  }
187
191
  }
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,74 @@
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
+ archiveKeys,
39
+ archive,
40
+ override,
41
+ }) => {
42
+ if (
43
+ archive &&
44
+ archiveKeys?.length &&
45
+ getIsInArchive({ archiveKeys, archive })
46
+ ) {
47
+ logMessage(`${marker} | Episode metadata exists in archive. Skipping...`);
48
+ return;
49
+ }
50
+
51
+ const output = getPublicObject(item);
52
+
53
+ try {
54
+ if (override || !fs.existsSync(outputPath)) {
55
+ fs.writeFileSync(outputPath, JSON.stringify(output, null, 4));
56
+ } else {
57
+ logMessage(`${marker} | Episode metadata exists locally. Skipping...`);
58
+ }
59
+
60
+ if (
61
+ archive &&
62
+ archiveKeys?.length &&
63
+ !getIsInArchive({ archiveKeys, archive })
64
+ ) {
65
+ try {
66
+ writeToArchive({ archiveKeys, archive });
67
+ } catch (error) {
68
+ throw new Error("Error writing to archive", error);
69
+ }
70
+ }
71
+ } catch (error) {
72
+ throw new Error("Unable to save meta file for episode", error);
73
+ }
74
+ };
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/bin/util.js CHANGED
@@ -242,7 +242,6 @@ const getMimeCategory = (mime) => {
242
242
 
243
243
  export const correctExtensionFromMime = ({
244
244
  outputPath,
245
- key,
246
245
  contentType,
247
246
  onCorrect,
248
247
  }) => {
@@ -250,32 +249,28 @@ export const correctExtensionFromMime = ({
250
249
  const mimeExt = mimeType ? getExtFromMime(mimeType) : null;
251
250
 
252
251
  if (!mimeExt) {
253
- return { outputPath, key };
252
+ return outputPath;
254
253
  }
255
254
 
256
255
  const currentExt = path.extname(outputPath);
257
256
  if (mimeExt === currentExt) {
258
- return { outputPath, key };
257
+ return outputPath;
259
258
  }
260
259
 
261
260
  const currentCategory = getExtCategory(currentExt);
262
261
  const mimeCategory = getMimeCategory(mimeType);
263
262
 
264
263
  if (currentCategory && mimeCategory && currentCategory !== mimeCategory) {
265
- return { outputPath, key };
264
+ return outputPath;
266
265
  }
267
266
 
268
267
  const basePath = currentExt
269
268
  ? outputPath.slice(0, -currentExt.length)
270
269
  : outputPath;
271
- const baseKey = key && currentExt ? key.slice(0, -currentExt.length) : key;
272
270
 
273
271
  onCorrect?.(currentExt || "(none)", mimeExt);
274
272
 
275
- return {
276
- outputPath: basePath + mimeExt,
277
- key: baseKey ? baseKey + mimeExt : null,
278
- };
273
+ return basePath + mimeExt;
279
274
  };
280
275
 
281
276
  export const VALID_AUDIO_EXTS = [
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.7.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
+ }