Skip to content

Instantly share code, notes, and snippets.

@pasterp
Created October 3, 2019 19:33
Show Gist options
  • Save pasterp/ff1f26f817afd50b5422a947cb5a2269 to your computer and use it in GitHub Desktop.
Save pasterp/ff1f26f817afd50b5422a947cb5a2269 to your computer and use it in GitHub Desktop.
require 'httparty'
require 'fileutils'
require 'open-uri'
base_url = "https://apod.nasa.gov/apod/"
page = "astropix.html"
regex = /<img SRC="(.+)"/i
output_dir = Dir.home+'/.apod/'
FileUtils.mkdir_p output_dir
response = HTTParty.get(base_url+page)
if response.code == 200
url = regex.match response.body
url = url[1]
filename = url.split('/').last
File.open(output_dir + filename, "wb" ) do |file|
open(base_url + url, "rb") do |img|
file.write(img.read)
end
end
system("hsetroot -fill #{output_dir + filename}")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment