add code
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -35,3 +35,5 @@ jspm_packages
|
||||
|
||||
# Optional REPL history
|
||||
.node_repl_history
|
||||
|
||||
results.txt
|
||||
12
Readme.md
Normal file
12
Readme.md
Normal file
@@ -0,0 +1,12 @@
|
||||
This writes the titles of the top story to results.txt where obs can display as a ticker.
|
||||
|
||||
using:
|
||||
|
||||
Open a shell in this directory and run the following
|
||||
|
||||
```js
|
||||
npm install
|
||||
node index.js
|
||||
```
|
||||
|
||||
Now every 5 seconds node will poll the rss feed for cnn and gather up the results
|
||||
53
cnn.js
Normal file
53
cnn.js
Normal file
@@ -0,0 +1,53 @@
|
||||
var FeedParser = require('feedparser');
|
||||
var request = require('request'); // for fetching the feed
|
||||
var fs = require('fs');
|
||||
|
||||
module.exports = (cb)=>{
|
||||
var req = request('http://rss.cnn.com/rss/cnn_topstories.xml')
|
||||
var feedparser = new FeedParser({});
|
||||
|
||||
req.on('error', function (error) {
|
||||
console.error(error);
|
||||
});
|
||||
|
||||
req.on('response', function (res) {
|
||||
var stream = this; // `this` is `req`, which is a stream
|
||||
|
||||
if (res.statusCode !== 200) {
|
||||
this.emit('error', new Error('Bad status code'));
|
||||
}
|
||||
else {
|
||||
stream.pipe(feedparser);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
feedparser.on('error', function (error) {
|
||||
console.error(error);
|
||||
});
|
||||
|
||||
var news = "";
|
||||
feedparser.on('readable', function () {
|
||||
// This is where the action is!
|
||||
var stream = this; // `this` is `feedparser`, which is a stream
|
||||
var meta = this.meta; // **NOTE** the "meta" is always available in the context of the feedparser instance
|
||||
var item;
|
||||
|
||||
while (item = stream.read()) {
|
||||
news += " " + item.title;
|
||||
}
|
||||
});
|
||||
|
||||
req.on('end', ()=>{
|
||||
fs.writeFile("results.txt", news, err=>{
|
||||
if(err){
|
||||
console.error("unable to write to results.txt");
|
||||
cb(err);
|
||||
}
|
||||
else{
|
||||
cb();
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
7
index.js
Normal file
7
index.js
Normal file
@@ -0,0 +1,7 @@
|
||||
var cnn = require('./cnn');
|
||||
setInterval(()=>{
|
||||
cnn((err)=>{
|
||||
if(err) console.error(err);
|
||||
console.log('ended');
|
||||
})
|
||||
}, 5000)
|
||||
15
package.json
Normal file
15
package.json
Normal file
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"name": "breakingnews",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"feedparser": "^2.1.0",
|
||||
"request": "^2.80.0"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user