Skip to content

Instantly share code, notes, and snippets.

@venj
Created May 10, 2012 01:58
Show Gist options
  • Save venj/2650439 to your computer and use it in GitHub Desktop.
Save venj/2650439 to your computer and use it in GitHub Desktop.
Download NASA Astronomy Picture Of Day media. (Ruby version)
#!/usr/bin/env ruby
##
# This script will grab NASA's Astronomy Picture of the Day Archive
#
# It will create 2 directories in current directory: big and small
# to save pictures.
#
# Requirements:
# 1. Ruby 1.8.x with Rubygems
# 2. nokogiri gem: (sudo) gem install nokogiri
# 2.1 It need libxml2 and libxslt and their source to build
# 3. curb gem: (sudo) gem install curb
# 3.1 curb need curl and its source to build
#
require 'rubygems'
require 'open-uri'
require 'nokogiri'
require 'curl'
require 'fileutils'
BASE = 'http://antwrp.gsfc.nasa.gov/apod/'
FileUtils.mkdir('small') unless File.exist?('small')
FileUtils.mkdir('big') unless File.exist?('big')
f = open 'http://antwrp.gsfc.nasa.gov/apod/archivepix.html'
html_doc = Nokogiri::HTML(f.read)
html_doc.xpath('//b//a').each do |element|
imgurl = BASE + element.attributes['href'].value
doc = Nokogiri::HTML(open(imgurl).read)
doc.xpath('//p//a//img').each do |elem|
small_img = BASE + elem.attributes['src'].value
big_img = BASE + elem.parent.attributes['href'].value
s_img_f = open("small/#{File.basename(small_img)}", 'wb')
b_img_f = open("big/#{File.basename(big_img)}", 'wb')
rs_img = Curl::Easy.new(small_img)
rb_img = Curl::Easy.new(big_img)
rs_img.perform
s_img_f.write(rs_img.body_str)
rb_img.perform
b_img_f.write(rb_img.body_str)
s_img_f.close
puts "Download #{File.basename(small_img)} finished."
b_img_f.close
puts "Download #{File.basename(big_img)} finished."
rs_img.close
rb_img.close
end
end
puts "All done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment