Created
October 18, 2017 19:09
-
-
Save derrekbertrand/c71f781c5b666b362113b5aeecbacc8a to your computer and use it in GitHub Desktop.
Show me a cat Webtask!
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
'use latest'; | |
import parser from 'rss-parser'; | |
module.exports = function(context, req, res) { | |
// we could probably easily allow the user to supply their subreddit of choice | |
// but kitties are wholesome, so lets leave it at that | |
parser.parseURL('https://www.reddit.com/r/cats/.rss', function(err, parsed) { | |
// the messages aren't helpful, IMHO but at least you'll know if it breaks | |
if(err) { res.writeHead(500, {}); res.end(err.message); return; } | |
// sloppily pick a random entry | |
const entry = parsed.feed.entries[Math.floor(Math.random() * parsed.feed.entries.length)]; | |
res.writeHead(302, { 'Location': entry.link}); | |
res.end(`Hold on! Sending you to: ${entry.link}`); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The code's rather short, but in case it isn't clear:
We're getting the feed from the
/r/cats
subreddit, picking a random one and sending the user there. We now have a link that shows you random curated cat photos.