Skip to content

Instantly share code, notes, and snippets.

@schovi
Created January 3, 2011 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schovi/763475 to your computer and use it in GitHub Desktop.
Save schovi/763475 to your computer and use it in GitHub Desktop.
Mechanize in action
require 'rubygems'
require 'mechanize'
require 'fileutils'
def start
a = Mechanize.new
last_file = Dir.glob('wulf*.*')[-1]
if last_file and /.*?_(?<year>\d{4})\-(?<month>\d{2})-(?<day>\d{2})/ =~ last_file
strip = "#{year}/#{month}/#{day}"
else
strip = "2003/10/10"
end
page = a.get('http://wulffmorgenthaler.com/' + strip)
while link = page.link_with(:text => /Next/, :href => /\/\d{4}\/\d{2}\/\d{2}\//)
file = a.get(page.images[0].url)
filename = file.filename.gsub("-WM_strip_", 'wulffmorgenthaler_')
puts "Getting... ", filename
file.save_as(filename)
page = a.click(link)
end
rescue Net::HTTPServiceUnavailable
puts "Error"
start()
end
start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment