-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
80 lines (68 loc) · 2.08 KB
/
app.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
const https = require("https");
// is argument present flags
let idFlag = false;
// parse arguments
process.argv.forEach(function (val, index, array) {
if (val == "--id")
{
idFlag = true;
} else if (val == "-h" || val == "--help")
{
console.log("To get YouTube channel RSS feed url run this command:");
console.log(`node app "https://www.youtube.com/@NoCopyrightSounds"`);
console.log("To get only YouTube channel id, add --id parameter:");
console.log(`node app "https://www.youtube.com/@NoCopyrightSounds --id"`);
process.exit();
}
});
if (process.argv[2] == null || !process.argv[2].startsWith("https"))
{
console.error("Error: Valid YouTube URL must be the first argument!");
process.exit();
}
// do the work
getRssUrl(process.argv[2].trim())
.then((rssUrl) => {
var result = rssUrl;
if (idFlag == true)
{
const indexOfFirst = rssUrl.indexOf("=");
result = rssUrl.substring(indexOfFirst + 1);
}
console.log(result);
})
.catch((error) => {
console.error('Error:', error);
});
// parse youtube source html and retrieve channel rss url
function getRssUrl(youtubeUrl) {
return new Promise((resolve, reject) => {
const options = {
headers: {
// if using User Agent YouTube will redirect to tracking cookie consent page, so set cookie beforehand
/*
'User-Agent': `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36`,
'cookie': `SOCS=CAESNQgDEitib3FfaWRlbnRpdHlmcm9udGVuZHVpc2VydmVyXzIwMjMwODA4LjA1X3AwGgJlbiACGgYIgLrgpgY`,
*/
}
};
https.get(youtubeUrl, options, resp => {
let data = "";
// A chunk of data has been recieved.
resp.on("data", chunk => {
data += chunk;
});
// The whole response has been received. Print out the result.
resp.on("end", () => {
// detect first appearance of "rssUrl"
const indexOfFirst = data.indexOf("rssUrl");
// offset beginning and end to extract only rss url
var rssUrl = data.substring(indexOfFirst + 9, indexOfFirst + 85);
resolve(rssUrl);
});
})
.on("error", err => {
reject(err);
});
});
}