Timothy Goddard wrote: > > You could use Hpricot to do this much more easily and reliably. I've > whipped up an example of using it for this. Note also that your random > dat function could produce a date in the future up to the end of the > current year. Error checking when using any of the network libraries is > also a must. Agreed .. more error checking needs to be done .. as you mention below, I need to browse the libraries and find out what is available and use it :-) > For a first program that's pretty good though! You obviously have the > hang of using Ruby, you just need to pick up on a few of the libraries > out there and how best to use them. I've been programming for quite a while, but each language has its own idiomatic ways for doing things ... which is why this feedback is so valuable. > Here's how I would do it: > > # Remove the rubygems require if you manually installed hpricot > require 'rubygems' > require 'hpricot' > require 'net/http' > > class Time > def self.random(years_back = 10) > # Set start and end times > end_time = Time.now > start_time = Time.mktime(end_time.year - 10, end_time.month, > end_time.day, 0, 0,0,0) > > Time.at(start_time + rand(end_time - start_time)) > end > end > > # Select a date > r_date = Time.random.strftime("%y%m%d") > puts r_date > > # Retrieve the page > response = Net::HTTP.get_response("antwrp.gsfc.nasa.gov", > "/apod/ap#{r_date}.html") > unless (200..299) === response.code.to_i > puts "There was an error retrieving the document" > puts "Response code: #{response.code}" > exit > end > > # Parse the document > doc = Hpricot(response.body) > > # Extract title > title = (doc / "title").first.inner_html > puts "Title: #{title}" > > # Extact all images > images = (doc/"img") > if images.length == 0 > puts "Could not extract the image from the document." > exit > end > > # Assume the first image is the one we want. > image = images.first["src"] > > puts "Running eog (whatever that is) to load image #{image}." > > # Execute the program. Note that it depends on the image path being > relative. > system("eog http://apod.nasa.gov/apod/#{image}") > Thank you! Esmail