-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen-podcast.ts
147 lines (139 loc) · 5.5 KB
/
gen-podcast.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { parse as parseYAML } from "https://deno.land/std@0.129.0/encoding/yaml.ts";
import Schema, { Type, string, number, array } from 'https://denoporter.sirjosh.workers.dev/v1/deno.land/x/computed_types/src/index.ts';
import { MP3Duration } from "./mp3-duration.ts"
const config = {
baseDir: "public",
srcConfig: "feed.yml",
dstConfig: "feed.xml",
baseURL: "https://stworzona.pl/podcast",
language: "pl-pl",
episodeFilename: (guid: string) => `episode-${guid}.xml`
}
const podcastConfig = loadPodcastConfig(config.baseDir, config.srcConfig)
const podcastXML = generatePodcastXML(config, podcastConfig)
Deno.writeTextFileSync(`${config.baseDir}/${config.dstConfig}`, podcastXML)
for (const episode of podcastConfig.episodes) {
Deno.writeTextFileSync(
config.baseDir + "/" + config.episodeFilename(episode.guid),
generateEpisodeXML(config, podcastConfig, episode)
)
}
function loadPodcastConfig(baseDir: string, path: string) {
const PodcastConfigSchema = Schema({
title: string.trim().normalize().between(3, 40),
description: string.trim().normalize(),
owner: {
email: string.regexp(/^[a-zA-Z0-9\.! #$%&'*+/=? ^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/g)
},
author: {
name: string.trim().normalize(),
},
maintainer: {
name: string.trim().normalize(),
link: string.trim().normalize(),
group: {
name: string.trim().normalize(),
link: string.trim().normalize(),
},
},
platforms: Schema.record(
string,
Schema({
link: string,
badge_image_path: string.test(audio_path => Deno.statSync(`${baseDir}/${audio_path}`).isFile)
})
),
cover: {
image_path: string.test((image_path) => Deno.statSync(`${baseDir}/${image_path}`).isFile)
},
itunes_categories: array.of(string.trim()),
episodes: array.of({
title: string.trim().normalize().between(3, 70),
description: string.trim().normalize(),
audio_path: string.test(audio_path => audio_path.endsWith(".mp3")).test(audio_path => Deno.statSync(`${baseDir}/${audio_path}`).isFile),
date: string.transform(dateNoTZ => `${dateNoTZ}+01:00`).test(date => new Date(date).getTime() > 0).transform(date => new Date(date)),
guid: string.trim().normalize().between(3, 70),
}),
}, { strict: true });
const rawConfig = parseYAML(Deno.readTextFileSync(`${baseDir}/${path}`))
const validator = PodcastConfigSchema.destruct();
const [err, config] = validator(rawConfig as any);
if (err) {
throw err
}
return config!
}
function generatePodcastXML(globalConfig: typeof config, podcast: ReturnType<typeof loadPodcastConfig>): string {
return `<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="${globalConfig.baseURL}/stylesheet.xsl" type="text/xsl"?>
<rss version="2.0"
xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
<channel>
<title>${podcast.title}</title>
<maintainer>
<name>${podcast.maintainer.name}</name>
<link>${podcast.maintainer.link}</link>
<group>
<name>${podcast.maintainer.group.name}</name>
<link>${podcast.maintainer.group.link}</link>
</group>
</maintainer>
<itunes:owner>
<itunes:email>${podcast.owner.email}</itunes:email>
</itunes:owner>
<itunes:author>${podcast.author.name}</itunes:author>
<description>${podcast.description}</description>
<itunes:image href="${globalConfig.baseURL}/${podcast.cover.image_path}"/>
<itunes:explicit>no</itunes:explicit>
<image>
<url>${globalConfig.baseURL}/${podcast.cover.image_path}</url>
<title>${podcast.title}</title>
<link>${globalConfig.baseURL}</link>
</image>
<language>${globalConfig.language}</language>
<link>${globalConfig.baseURL}</link>
${podcast.itunes_categories.map(categoryName => `<itunes:category text="${categoryName}"/>`).join("\n")}
${podcast.episodes.map(episode => `
<item>
<title>${episode.title}</title>
<description>${episode.description}</description>
<pubDate>${episode.date.toUTCString()}</pubDate>
<enclosure
url="${globalConfig.baseURL}/${episode.audio_path}"
type="audio/mpeg"
length="${getFileSizeBytes(`${globalConfig.baseDir}/${episode.audio_path}`)}"
/>
<itunes:duration>${getMP3DurationSeconds(`${globalConfig.baseDir}/${episode.audio_path}`)}</itunes:duration>
<guid isPermaLink="false">${episode.guid}</guid>
</item>
`).join("\n\n")}
</channel>
</rss>
`
}
function generateEpisodeXML(globalConfig: typeof config, podcast: ReturnType<typeof loadPodcastConfig>, episode: ReturnType<typeof loadPodcastConfig>["episodes"][0]) {
return `<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="${globalConfig.baseURL}/stylesheet-episode.xsl" type="text/xsl"?>
<episode version="2.0">
<podcast>
<title>${podcast.title}</title>
<link>${globalConfig.baseDir}</link>
<feed>${globalConfig.baseDir}/${globalConfig.dstConfig}</feed>
</podcast>
<title>${episode.title}</title>
<description>${episode.description}</description>
<pubDate>${episode.date.toUTCString()}</pubDate>
<enclosure
url="${globalConfig.baseURL}/${episode.audio_path}"
type="audio/mpeg"
length="${getFileSizeBytes(`${globalConfig.baseDir}/${episode.audio_path}`)}"
/>
</episode>
`
}
function getFileSizeBytes(path: string): number {
return Deno.lstatSync(path).size
}
function getMP3DurationSeconds(path: string) {
return new MP3Duration().getDurationInSeconds(Deno.readFileSync(path))
}