-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathfeed.js
50 lines (42 loc) · 1.46 KB
/
feed.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 fetch = require("node-fetch");
const xml2js = require("xml2js");
process.stdin.setEncoding('utf8');
let input = '';
process.stdin.on('data', chunk => input += chunk);
process.stdin.on('end', async () => {
try {
const feeds = JSON.parse(input)
.map(x => x.feed)
.filter(x => x)
.sort(() => Math.random() - 0.5)
.slice(0, 1);
const results = await Promise.all(feeds.map(async (url) => {
try {
console.log(url);
const response = await fetch(url);
const text = await response.text();
const parser = new xml2js.Parser();
const xml = await parser.parseStringPromise(text);
const items = xml.rss ? xml.rss.channel[0].item : xml.feed.entry;
return items.map(item => {
let obj = {};
for (let key in item) {
obj[key] = item[key][0];
}
return obj;
});
} catch (error) {
return [];
}
}));
console.log(results?.[0]);
const output = results
.flatMap(x => x)
.filter(x => x.id || x.guid)
.map(x => `${x.id || x.guid}\t${x.link}`)
.join('\n');
console.log(output);
} catch (error) {
console.error("Error parsing input or processing feeds:", error);
}
});