-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrss-feed-yt-channel-adder.js
50 lines (38 loc) · 1.15 KB
/
rss-feed-yt-channel-adder.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
const https = require("https")
const fs = require("fs")
const URL_FILE_PATH = "/home/mn/.config/newsboat/urls"
const url = process.argv.slice(2)[0]
const addIDtoRSSfile = id => {
const ytRSSurl = "https://www.youtube.com/feeds/videos.xml?channel_id=" + id
fs.appendFile(URL_FILE_PATH, ytRSSurl + "\n", error => {
if (error) {
console.error(`Error adding ID ${id} to ${URL_FILE_PATH}:`)
console.error(error)
} else {
console.log(`Added ID ${id} to ${URL_FILE_PATH} :)`)
}
})
}
const parseHTML = html => {
let htmlSplit = html.split('<meta itemprop="channelId" content="')[1] || html.split('<link rel="alternate" type="application/rss xml" title="RSS" href=')[1];
if (!htmlSplit) {
console.error("No channel ID found in HTML");
return;
}
htmlSplit = htmlSplit.split(`"`)[0]
addIDtoRSSfile(htmlSplit)
}
const request = https.request(url, response => {
let data = ""
response.on("data", chunk => {
data += chunk.toString()
})
response.on("end", () => {
parseHTML(data)
})
})
request.on("error", error => {
console.error("Error during HTTPS Request:")
console.error(error)
})
request.end()