-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
132 lines (111 loc) · 3.21 KB
/
server.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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
* Created by krsterajchevski on 5/2/17.
*/
const express = require('express');
const path = require('path');
const FeedParser = require('feedparser');
const request = require('request'); // for fetching the feed
const cors = require('cors');
const lodash = require('lodash');
const env = (process.env.NODE_ENV === 'production' ? 'production' : 'development');
/**
* RSS LINKS
*/
const BBC_WORLD = 'http://feeds.bbci.co.uk/news/world/rss.xml';
const REUTERS_WORLD = 'http://feeds.reuters.com/Reuters/worldNews';
const TECH_CRUNCH = 'http://feeds.feedburner.com/TechCrunch/';
const YAHOO = 'http://news.yahoo.com/rss/';
const FOX_SPORTS = 'https://api.foxsports.com/v1/rss?partnerKey=zBaFxRyGKCfxBagJG9b8pqLyndmvo7UU';
const sources = [
BBC_WORLD,
REUTERS_WORLD,
TECH_CRUNCH,
YAHOO,
FOX_SPORTS,
];
const app = express();
app.use(cors());
/**
* Returns the RSS feed data from the given souce
* @param source
* @returns {Promise}
*/
function getDatafromSource(source) {
return new Promise((resolve, reject) => {
const rssRequest = request(source);
const feedparser = new FeedParser([]);
const data = [];
rssRequest.on('error', (error) => {
// handle any request errors
console.log(error);
reject(error);
});
rssRequest.on('response', (response) => {
if (response.statusCode !== 200) {
console.log('Error');
reject('Error');
} else {
response.pipe(feedparser);
}
});
feedparser.on('error', (error) => {
console.log(error);
reject(error);
// always handle errors
});
feedparser.on('readable', () => {
// This is where the action is!
// `this` is `feedparser`, which is a stream
const meta = feedparser.meta;
let item;
while (item = feedparser.read()) {
let photo = '';
if (item.image && item.image.url) {
photo = item.image.url;
} else if (item.enclosures[0] && item.enclosures[0].url) {
photo = item.enclosures[0].url;
} else if (meta.image && meta.image.url) {
photo = meta.image.url;
}
data.push({
from: meta.title,
title: item.title,
description: item.description.replace(/<(?:.|\n)*?>/gm, ''),
date: new Date(item.date),
link: item.link,
photo,
categories: item.categories,
});
}
});
feedparser.on('end', () => {
resolve(data);
});
});
}
app.get('/feed', (req, res) => {
const tasks = [];
for (let i = 0; i < sources.length; i += 1) {
tasks.push(getDatafromSource(sources[i]));
}
Promise.all(tasks).then((results) => {
let returnData = [];
for (let i = 0; i < results.length; i += 1) {
returnData = returnData.concat(results[i]);
}
//TODO better way to sort!
res.status(200).json(lodash.sortBy(returnData, ['date']).reverse());
});
});
//TODO save data to DB
app.use(express.static('dist'));
if (env === 'production') {
app.get('/*', (req, res) => {
res.sendFile(path.join(__dirname, 'dist/index.html'));
});
}
const port = 8000;
app.listen(port, () => {
console.log(`Environment set to ${env}`);
console.log(`RSS feed server is listening on port ${port}`);
});