Last active
August 28, 2018 20:49
-
-
Save peterc/a7d63960ca6dcf165477b310d0d08afb to your computer and use it in GitHub Desktop.
An ES6 variant of a serverless function written in Python..
This file contains 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
const fetch = require('node-fetch'); | |
const parseString = require('xml2js').parseString; | |
const util = require('util'); | |
const hostnames = ["react.statuscode.com", "javascriptweekly.com", "nodeweekly.com"]; | |
function checkValidity(url) { | |
return fetch(url) | |
.then(res => res.text()) | |
.then(resultText => promisify(parseString)(resultText)) | |
.then(res => res['env:Envelope']['env:Body'][0]['m:feedvalidationresponse'][0]['m:validity'][0]); | |
} | |
async function postSlackMessage(message, channel = '#experiments') { | |
let slackData = { text: message, channel: channel }; | |
return fetch(process.env['SLACK_URL'], { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(slackData) }) | |
} | |
async function lambda_handler(event, context) { | |
for (let hostname of hostnames) { | |
let url = `https://validator.w3.org/feed/check.cgi?url=https%3A%2F%2F${hostname}%2Frss&output=soap12`; | |
if (await checkValidity(url)) { | |
await postSlackMessage(`:white_check_mark: ${hostname} has valid RSS`); | |
} else { | |
await postSlackMessage(`:x: ${hostname} has INVALID RSS`, '#editorial'); | |
} | |
} | |
} | |
if (process.argv[2] == 'test') { | |
lambda_handler(null, null); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the help folks! Here's where I'm at so far, since this is so scrappy: