-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
118 lines (107 loc) · 3.69 KB
/
Copy pathindex.js
File metadata and controls
118 lines (107 loc) · 3.69 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
let Parser = require('rss-parser');
const express = require('express')
const dotenv = require('dotenv')
const emojiRegex = require('emoji-regex');
const sanitizeHtml = require('sanitize-html');
dotenv.config()
let parser = new Parser();
const emojiRemovalRegex = new RegExp(emojiRegex(), 'g');
const app = express()
const port = process.env.PORT || 1273
const debug = process.env.DEBUG || false
// Polyfill for String.replaceAll
if (!String.prototype.replaceAll) {
String.prototype.replaceAll = function(search, replacement) {
return this.split(search).join(replacement);
};
}
async function getNews() {
let html = `
<head>
<title>OSN</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<h1>Old School News</h1>
<p><b><i>News back to the future.</b></i></p>
<hr></hr>
`;
let feeds = [];
try{
feeds = process.env.FEEDS.split(',')
} catch {
feeds = [
'https://sparksammy.com/rss',
'https://feeds.a.dj.com/rss/RSSWorldNews.xml',
'https://www.theguardian.com/world/rss',
'https://feeds.nbcnews.com/nbcnews/public/news',
'https://feeds.npr.org/1001/rss.xml',
'https://abcnews.go.com/abcnews/topstories',
'https://www.cbsnews.com/latest/rss/us',
'https://www.reddit.com/r/news.rss',
'https://lwn.net/headlines/newrss',
'http://rss.slashdot.org/Slashdot/slashdotMain',
'https://www.cbsnews.com/latest/rss/technology',
'https://www.theverge.com/rss/index.xml'
];
}
// Process all feeds concurrently and wait for all to complete
await Promise.all(feeds.map(async (nonparsed) => {
try {
feed = await parser.parseURL(nonparsed);
} catch {
feed = "" //HACK
}
html = html + `
<h2>SOURCE: ${feed.title}</h2><br></br>
`;
if (debug) {
console.log(feed.title);
}
try {
feed.items.forEach(item => {
let content = `${item.description || item.content || item.summary || "<b>No content or description available</b>"}`
content = content.replace(/https?:\/\/[^\s]+/g, "")
content = content.replace(emojiRemovalRegex, "")
content = sanitizeHtml(content, {
allowedTags: ['p', 'b', 'i', 'br', 'strong', "em", "a", "div", "ul", "li", "ol", "span"],
allowedAttributes: {
'a': [''],
'p': [''],
'b': [''],
'i': [''],
'br': [''],
'strong': [''],
'em': [''],
'div': ['class'],
'ul': ['class'],
'li': ['class'],
'ol': ['class'],
'span': ['class']
}
});
if (content.length < 25) {
content = "<b>No content or description available</b>"
}
html = html + `
<h3>${item.title}</h3>
<p>${content}</p>
<br></br>
`
});
} catch {
html = html + "" // HACK
}
}));
html = html + `
<hr></hr>
<p><i>Copyright <SCRIPT>document.write(new Date().getFullYear());</SCRIPT> Sammy Lord. All rights reserved.</i></p>
<p><i>DISCLAIMER: This is a news aggregator. The news is not owned by me. I am not responsible for the content of the news.</i></p>
`
return html;
}
app.get('/', async (req, res) => {
res.send(await getNews());
});
app.listen(port, () => {
console.log(`News server is running on port ${port}`);
});