-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateAudioFeed.js
173 lines (150 loc) · 5.44 KB
/
generateAudioFeed.js
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
const fs = require('fs');
const yaml = require('js-yaml');
const xml2js = require('xml2js');
const crypto = require('crypto');
const https = require('https');
const {URL} = require('url');
const path = require('path');
const basepath = './static/audiofeed';
const cacheFile = path.join(__dirname, 'file-size-cache.json');
// Convert dateString to IETF RFC 2822
function convertToPubDateFormat(dateString) {
const date = new Date(dateString);
return date.toUTCString();
}
function toHash(string) {
const hash = crypto.createHash('sha256');
hash.update(string);
return hash.digest('hex');
}
/**
* Returns a sum of seconds for the given input time string.
* Valid input values:
* - 10:00:00 -> hours:minutes:seconds
* - 10:00 -> minutes:seconds
* - 13000 -> just seconds
*
* @param {string} time The input time string in one of the valid formats.
* @return {number} The total number of seconds.
*/
function getSeconds(time) {
const timeParts = time.toString().split(':');
let seconds = 0;
if (timeParts.length === 3) {
seconds += parseInt(timeParts[0]) * 3600; // hours to seconds
seconds += parseInt(timeParts[1]) * 60; // minutes to seconds
seconds += parseInt(timeParts[2]); // seconds
} else if (timeParts.length === 2) {
seconds += parseInt(timeParts[0]) * 60; // minutes to seconds
seconds += parseInt(timeParts[1]); // seconds
} else {
seconds += parseInt(timeParts[0]); // seconds
}
return seconds;
}
function readCache() {
if (fs.existsSync(cacheFile)) {
const cacheContent = fs.readFileSync(cacheFile, 'utf8');
return JSON.parse(cacheContent);
}
return {};
}
function writeCache(cacheData) {
fs.writeFileSync(cacheFile, JSON.stringify(cacheData, null, 2));
}
function generateHash(options) {
return `${options.host}-${options.path}`;
}
async function getCachedFileSize(options, cache) {
const oneDay = 24 * 60 * 60 * 1000; // 1 day
const cacheDuration = oneDay * 30; // 30 days
const hash = generateHash(options);
if (cache[hash] && (Date.now() - cache[hash].timestamp < cacheDuration)) {
console.log(`Cache hit for ${hash}`);
return cache[hash].size;
}
const fileSize = await new Promise((resolve, reject) => {
const req = https.request(options, (res) => {
console.log('status', `${res.statusCode} ${options.host}${options.path}`);
const size = res.headers['content-length'];
if (size) {
cache[hash] = {size, timestamp: Date.now()};
resolve(size);
} else {
reject(new Error('Failed to get content-length header'));
}
});
req.on('error', reject);
req.end();
});
writeCache(cache);
return fileSize;
}
async function yamlObjectToXml(yamlObject, cache) {
const url = new URL(yamlObject.url);
const options = {
method: 'HEAD',
host: url.hostname,
path: url.pathname,
};
const fileSize = await getCachedFileSize(options, cache);
console.log(`Processed file size for ${yamlObject.url}: ${fileSize}`);
return {
'title': yamlObject.title,
'pubDate': convertToPubDateFormat(yamlObject.date),
'lastBuildDate': convertToPubDateFormat(yamlObject.date),
'guid': {
_: toHash(yamlObject.url),
$: {isPermaLink: 'false'},
},
'itunes:image': {
$: {
href: yamlObject.image ?? 'https://raw.githubusercontent.com/LucaNerlich/m10z/main/static/img/M10Z_Logo3000x3000.jpg',
},
},
'description': yamlObject.description,
'author': '[email protected]',
'itunes:explicit': 'false',
'link': yamlObject.blogpost ?? 'https://m10z.de',
'itunes:duration': getSeconds(yamlObject.seconds),
'enclosure': {
$: {
url: yamlObject.url,
length: fileSize,
type: 'audio/mpeg',
},
},
};
}
async function generateFeedXML(yamlObjects) {
const cache = readCache();
const data = fs.readFileSync('./templates/rss-channel.xml');
xml2js.parseString(data, async (err, result) => {
if (err) {
console.error(err);
return;
}
result.rss.channel[0]['pubDate'] = convertToPubDateFormat(new Date().toDateString());
result.rss.channel[0].item = await Promise.all(yamlObjects.map(async (yamlObject, index) => {
console.log(`Processing item ${index + 1}/${yamlObjects.length}`);
const item = await yamlObjectToXml(yamlObject, cache);
console.log(`Successfully processed item ${index + 1}/${yamlObjects.length}`);
return item;
}));
const builder = new xml2js.Builder({renderOpts: {'pretty': true, 'indent': ' ', 'newline': '\n'}, cdata: true});
const xml = builder.buildObject(result);
fs.writeFileSync(basepath + '.xml', xml);
console.log('RSS feed XML file successfully written.');
// Terminate process when done
process.exit(0);
});
}
console.log('Creating audiofeed.xml');
const yamlData = fs.readFileSync(basepath + '.yaml', 'utf8');
const yamlObjects = yaml.load(yamlData);
generateFeedXML(yamlObjects)
.then(() => console.log('Successfully created audiofeed.xml'))
.catch(error => {
console.error('Failed to generate audiofeed.xml', error);
process.exit(1);
});