Last active
December 17, 2020 07:59
-
-
Save aquarla/b2933c54d575a676b0acff6b29e89bd7 to your computer and use it in GitHub Desktop.
AWS Lambda用のマストドンRSS投稿BOTプログラム
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 'net/https' | |
require 'rss' | |
def lambda_handler(event:, context:) | |
iwatedon_api_url = 'https://enter-your-mastodon-domain/api/v1/statuses' | |
access_token = 'enter-your-access-token' | |
urls = [ | |
'rss url 1', | |
'rss url 2', | |
'rss url 3', | |
'rss url 4', # ... | |
] | |
interval = 30 * 60 # 30 minutes | |
urls.each do |url| | |
rss = nil | |
begin | |
rss = RSS::Parser.parse(url) | |
rescue RSS::InvalidRSSError | |
rss = RSS::Parser.parse(url, false) | |
end | |
rss.channel.items.each do |item| | |
pub_date = item.pubDate.to_i | |
break if Time.now.to_i - pub_date > interval | |
title = item.title | |
link = item.link | |
uri = URI.parse(iwatedon_api_url) | |
http = Net::HTTP.new(uri.host, uri.port).tap do |obj| | |
obj.use_ssl = true | |
obj.verify_mode = OpenSSL::SSL::VERIFY_NONE | |
end | |
request = Net::HTTP::Post.new(uri.path) | |
request.set_form_data({ | |
'access_token' => access_token, | |
'status' => "#{title} / #{link} #iwate #岩手県 ", | |
'visibility' => 'unlisted' | |
}) | |
http.request(request) | |
end | |
end | |
{ statusCode: 200, body: JSON.generate('OK') } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment