Created
August 28, 2018 13:57
-
-
Save peterc/106cc711a8abdeb03f7d4b9ec62236b7 to your computer and use it in GitHub Desktop.
AWS Lambda function to check RSS feed validity using W3C's API
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
import json | |
import requests | |
import os | |
import sys | |
from xml.dom.minidom import parse, parseString | |
hostnames = ["react.statuscode.com", "javascriptweekly.com", "nodeweekly.com"] | |
def checkValidity(url): | |
response = requests.get(url) | |
dom = parseString(response.text) | |
el = dom.getElementsByTagName("m:validity")[0] | |
valid = el.firstChild.data | |
return valid == 'true' | |
def postSlackMessage(message, channel='#experiments'): | |
slack_data = {'text': message, 'channel': channel} | |
response = requests.post( | |
os.environ['SLACK_URL'], data=json.dumps(slack_data), | |
headers={'Content-Type': 'application/json'} | |
) | |
def lambda_handler(event, context): | |
for hostname in hostnames: | |
url = f"https://validator.w3.org/feed/check.cgi?url=https%3A%2F%2F{hostname}%2Frss&output=soap12" | |
if checkValidity(url): | |
postSlackMessage(f":white_check_mark: {hostname} has valid RSS") | |
else: | |
postSlackMessage(f":x: {hostname} has INVALID RSS", "#editorial") | |
# Run lambda function locally for test purposes | |
if len(sys.argv) > 1 and sys.argv[1] == 'test': | |
lambda_handler(None, None) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Worked out that running this every 5 minutes would cost about $0.08 a month in AWS Lambda fees and bandwidth.