-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildPodcast.ts
57 lines (47 loc) · 1.66 KB
/
buildPodcast.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import * as Podcast from "podcast";
import * as fs from "fs";
import { lookup } from "dns";
import { hostname } from "os";
interface PodcastConfig extends IFeedOptions {
fileRegex: string
}
const defaultPort = 8080;
async function getLocalAddress() {
return new Promise<string>((resolve, error) => {
lookup(hostname(), (err, add, fam) => {
resolve(add);
});
});
}
async function buildFeed(rootDir: string) {
console.log(`Building feed for ${rootDir}`);
const podcastConfig = JSON.parse(fs.readFileSync(rootDir + "/settings.json", "utf8")).podcast as PodcastConfig;
let localAddress = await getLocalAddress();
podcastConfig.feed_url = podcastConfig.feed_url || `http://${localAddress}:${defaultPort}/${rootDir}`;
let feed = new Podcast(podcastConfig);
let files = fs.readdirSync(rootDir).filter((fn: string) => {
return fn.search(podcastConfig.fileRegex) >= 0;
});
files.forEach((fn) => {
console.log(`Processing file: ${fn} `);
let itemConfig = {
title: fn,
guid: fn,
url: podcastConfig.feed_url + "/" + encodeURIComponent(fn),
description: fn,
date: new Date(),
enclosure: {
url: podcastConfig.feed_url + "/" + encodeURIComponent(fn),
file: rootDir + "/" + fn
}
};
feed.item(itemConfig);
});
let xmlFile = fs.writeFileSync(rootDir + "/feed.xml", feed.xml("\t"));
console.log("Feed generation complete.");
}
if (process.argv.length <= 2) {
console.log("Usage: " + __filename + " directoryName");
process.exit(-1);
}
buildFeed(process.argv[2]);