1/*
2 This code comes from Vincent Lab
3 And it has a video version linked here: https://www.youtube.com/watch?v=sL6SgDEHH00
4*/
5
6// Import dependencies
7const fs = require("fs");
8const Parser = require("rss-parser");
9
10(async function main() {
11
12 // Make a new RSS Parser
13 const parser = new Parser();
14
15 // Get all the items in the RSS feed
16 const feed = await parser.parseURL("https://www.youtube.com/feeds/videos.xml?channel_id=UCMA8gVyu_IkVIixXd2p18NQ"); // https://www.reddit.com/.rss
17
18 let items = [];
19
20 // Clean up the string and replace reserved characters
21 const fileName = `${feed.title.replace(/\s+/g, "-").replace(/[/\\?%*:|"<>]/g, '').toLowerCase()}.json`;
22
23 if (fs.existsSync(fileName)) {
24 items = require(`./${fileName}`);
25 }
26
27 // Add the items to the items array
28 await Promise.all(feed.items.map(async (currentItem) => {
29
30 // Add a new item if it doesn't already exist
31 if (items.filter((item) => isEquivalent(item, currentItem)).length <= 0) {
32 items.push(currentItem);
33 }
34
35 }));
36
37 // Save the file
38 fs.writeFileSync(fileName, JSON.stringify(items));
39
40})();
41
42function isEquivalent(a, b) {
43 // Create arrays of property names
44 let aProps = Object.getOwnPropertyNames(a);
45 let bProps = Object.getOwnPropertyNames(b);
46
47 // if number of properties is different, objects are not equivalent
48 if (aProps.length != bProps.length) {
49 return false;
50 }
51
52 for (let i = 0; i < aProps.length; i++) {
53 let propName = aProps[i];
54
55 // if values of same property are not equal, objects are not equivalent
56 if (a[propName] !== b[propName]) {
57 return false;
58 }
59 }
60
61 // if we made it this far, objects are considered equivalent
62 return true;
63}