Created
May 23, 2020 15:56
-
-
Save peterc/6efa619b3ff27e2a48aa3d3bef8e56a9 to your computer and use it in GitHub Desktop.
RSS to S3 Ruby Lambda Function
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
require 'json' | |
require 'aws-sdk-s3' | |
require 'open-uri' | |
# Ideally put these in environment variables | |
# but since this is just for us, who cares. | |
BUCKET = "RETRACTED" | |
REGION = "RETRACTED" | |
def do_newsletters | |
# We want to consider any failure to be total failure | |
# BUT still do the ones we can.. so we can't just crash | |
# out on the first exception, hence a flag to keep track | |
# of the current success state. | |
success = true | |
# Grab the latest newsletter info | |
newsletters = JSON.parse(URI.open('RETRACTED').read.to_s) | |
# For each newsletter.. | |
newsletters.each do |k, nl| | |
output_filename = nl['domain'] + ".xml" | |
feed_url = "https://" + nl['domain'] + "/drss" | |
puts feed_url | |
begin | |
# Grab the RSS feed | |
xml = URI.open(feed_url).read | |
# Upload to S3 | |
s3 = Aws::S3::Client.new(region: REGION) | |
s3.put_object(bucket: BUCKET, key: output_filename, body: xml, content_type: 'application/xml;charset=utf-8', cache_control: "max-age=14400") | |
# Make file on S3 publicly readable | |
s3.put_object_acl({ acl: "public-read", bucket: BUCKET, key: output_filename }) | |
puts " Success - https://cprss.s3.amazonaws.com/#{output_filename}" | |
rescue | |
# Any failure, duck out | |
puts " Failed" | |
success = false | |
end | |
end | |
success | |
end | |
def lambda_handler(event:, context:) | |
if do_newsletters | |
{ statusCode: 200, body: 'OK' } | |
else | |
{ statusCode: 500, body: 'FAIL' } | |
end | |
end | |
do_newsletters if __FILE__ == $0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment