Skip to content

Instantly share code, notes, and snippets.

@peterc
Created August 28, 2018 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterc/106cc711a8abdeb03f7d4b9ec62236b7 to your computer and use it in GitHub Desktop.
Save peterc/106cc711a8abdeb03f7d4b9ec62236b7 to your computer and use it in GitHub Desktop.
AWS Lambda function to check RSS feed validity using W3C's API
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)
@peterc
Copy link
Author

peterc commented Aug 28, 2018

If you want to use this code, you'd need to make a few minor changes, but it should be pretty straight forward. Perhaps change the "hostnames" array to be raw URLs. Also note the SLACK_URL env variable.

@peterc
Copy link
Author

peterc commented Aug 28, 2018

Worked out that running this every 5 minutes would cost about $0.08 a month in AWS Lambda fees and bandwidth.

@peterc
Copy link
Author

peterc commented Aug 28, 2018

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