-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
40 lines (37 loc) · 1.11 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
const http = require("https");
const parser = require("fast-xml-parser");
/**
* @typedef Episode
* @property {string} title Title of episode
* @property {string} description Description of episode
* @property {string} link URL of episode
* @property {string} guid
* @property {string} pubDate Date episode was published
*/
/**
*
* @param {string} rssUrl - RSS URL of your podcast
* @returns {Promise<Episode[]>} episodes - Array of podcast episodes
*/
module.exports = function(rssUrl) {
return new Promise((resolve, reject) => {
http
.get(rssUrl, (res) => {
let data = [];
res.on("data", (chunk) => data.push(chunk));
res.on("end", () => {
data = Buffer.concat(data).toString();
const json = parser.parse(data);
const episodes = Array.isArray(json.rss.channel.item) ? json.rss.channel.item : [json.rss.channel.item];
resolve(episodes)
});
})
.on("error", (err) => {
console.log(
"An error occurred while fetching episodes from Anchor FM",
err,
);
reject(err);
});
});
};