Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@rbazinet
Forked from peterc/rss-to-s3.rb
Created May 30, 2020 14:12
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 rbazinet/e5cef2ddd6660b7408394b9ef11d28cd to your computer and use it in GitHub Desktop.
Save rbazinet/e5cef2ddd6660b7408394b9ef11d28cd to your computer and use it in GitHub Desktop.
RSS to S3 Ruby Lambda Function
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