This repository has been archived on 2025-11-02. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
dashboard/jobs/random_aww.rb
2022-09-20 00:27:25 +01:00

23 lines
747 B
Ruby

require 'net/http'
require 'json'
require 'open-uri'
placeholder = '/assets/nyantocat.gif'
SCHEDULER.every '1h', first_in: 0 do |job|
# uri = URI('https://www.reddit.com/r/aww.json')
# response = Net::HTTP.get(uri)
response = URI.parse("https://www.reddit.com/r/aww.json").read
# puts(response)
json = JSON.parse(response)
if json['data']['children'].count <= 0
send_event('aww', image: placeholder)
else
urls = json['data']['children'].map{|child| child['data']['url'] }
# Ensure we're linking directly to an image, not a gallery etc.
valid_urls = urls.select{|url| url.downcase.end_with?('png', 'gif', 'jpg', 'jpeg')}
send_event('aww', image: "background-image:url(#{valid_urls.sample(1).first})")
end
end