-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
70 lines (70 loc) · 2.24 KB
/
index.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
"use strict";
exports.__esModule = true;
var fs = require("fs");
var nunjucks = require("nunjucks");
var rssparser = require("rss-parser");
var URL = "http://timferriss.libsyn.com/rss";
parseRSS(URL);
function parseRSS(url) {
rssparser.parseURL(url, function (err, parsed) {
if (err)
done(err);
buildHTML(parsed.feed.entries);
});
}
function done(err) {
if (err) {
console.error(err, err.stack);
}
process.exit(1);
}
function buildHTML(items) {
var template = "./templates/body.njk";
var html = nunjucks.render(template, { items: processItems(items) });
fs.writeFile("index.html", html, function (err) {
if (err) {
return console.error("Unable to create index.html!");
}
console.log("index.html created successfully!");
});
}
function processItems(origItems) {
var episodeCount = origItems.length;
var processedItems = [];
origItems.forEach(function (item) {
var newItem = Object.assign({}, item);
var titleInfo = stripEpisodeNumber(item.title);
newItem.title = titleInfo.title;
newItem.episodeNumber = titleInfo.episodeNumber;
newItem.description = newItem.content;
newItem.downloadFilename = newItem.episodeNumber + " " + newItem.title + ".mp3";
newItem.downloadURL = newItem.enclosure.url;
processedItems.push(newItem);
episodeCount--;
});
return processedItems;
}
function stripEpisodeNumber(title) {
var episodeNumber = 0;
var episodeTitle = title;
var colonIndex = title.indexOf(":");
if (colonIndex > -1) {
episodeTitle = title.substring(colonIndex + 2);
}
if (title.indexOf("#") === 0) {
episodeNumber = parseInt(title.substring(1, colonIndex + 1));
}
else if (title.startsWith("Episode ")) {
episodeNumber = parseInt(title.substring(7, colonIndex + 1));
}
else if (title.startsWith("Ep. ")) {
episodeNumber = parseInt(title.substring(3, colonIndex + 1));
}
else if (title.startsWith("Ep ")) {
episodeNumber = parseInt(title.substring(2, colonIndex + 1));
}
if (isNaN(episodeNumber)) {
episodeNumber = 0;
}
return { title: episodeTitle, episodeNumber: episodeNumber };
}