Skip to content

Instantly share code, notes, and snippets.

@justincampbell
Created December 2, 2015 22:15
Show Gist options
  • Save justincampbell/773c2a9045e226df1229 to your computer and use it in GitHub Desktop.
Save justincampbell/773c2a9045e226df1229 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
require 'net/http'
require 'tempfile'
require 'uri'
feed_url = "http://www.nasa.gov/rss/dyn/lg_image_of_the_day.rss"
feed = Net::HTTP.get(URI.parse(feed_url))
image_filename = feed.split('url="http://www.nasa.gov/sites/default/files/')[1].split('"')[0]
image_url = "http://www.nasa.gov/sites/default/files/#{image_filename}"
image = Net::HTTP.get(URI.parse(image_url))
temp_file = Tempfile.new("apod")
begin
temp_file.write(image)
script = <<-SCRIPT
tell application "Finder"
set apod to POSIX file "#{temp_file.path}" as string
set desktop picture to file apod
end tell
SCRIPT
output = `osascript -e '#{script}'`
process = $?
ensure
temp_file.close
end
unless process.success?
puts output
exit 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment