Skip to content

Instantly share code, notes, and snippets.

@derrekbertrand
Created October 18, 2017 19:09
Show Gist options
  • Save derrekbertrand/c71f781c5b666b362113b5aeecbacc8a to your computer and use it in GitHub Desktop.
Save derrekbertrand/c71f781c5b666b362113b5aeecbacc8a to your computer and use it in GitHub Desktop.
Show me a cat Webtask!
'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}`);
});
};
@derrekbertrand
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment