Created
May 16, 2017 14:29
-
-
Save NaotoKumagai/dfd33dd97992b32cb9bd4ee5c3ed2905 to your computer and use it in GitHub Desktop.
Get RSS on Cloud Function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
exports.helloWorld = function helloWorld(req, res) { | |
var FeedParser = require('feedparser'); | |
var request = require('request'); | |
var feed = '====RSS_URL==='; | |
var req2 = request(feed); | |
var feedparser = new FeedParser({}); | |
var items = []; | |
var titles = ''; | |
req2.on('response', function (res2) { | |
this.pipe(feedparser); | |
}); | |
feedparser.on('readable', function() { | |
while(item = this.read()) { | |
items.push(item); | |
} | |
}); | |
feedparser.on('end', function() { | |
// show titles | |
items.forEach(function(item) { | |
titles += item.title | |
titles += '\n' | |
}); | |
// Example input: {"message": "Hello!"} | |
if (req.body.message === undefined) { | |
// This is an error case, as "message" is required. | |
res.status(400).send('No message defined!'); | |
} else { | |
// Everything is okay. | |
console.log(req.body.message); | |
res.status(200).send('Success: ' + req.body.message + '\n' + titles); | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment