Walking Slide Numbers #
See how Vincent Foley makes a local copy of the slides Matz put up:
require "open-uri" BASE_URL = "http://www.rubyist.net/~matz/slides/oscon2005/mgp" "00001".upto("00059") do |n| puts "Fetching #{n}.html..." File.open(n + ".html", "w") do |f| f.write(open(BASE_URL + n + ".html").read) end end
The part I’m digging is that "00001".upto("00059")
. I totally forgot about String.upto
, which does a succ
behind the scenes.
>> "r01".succ => "r02" >> "r02".succ => "r03"
But if you want Matz’ slides, let’s also grab the JPEGs.
require "open-uri" BASE_URL = "http://www.rubyist.net/~matz/slides/oscon2005/" "mgp00001".upto("mgp00059") do |n| ['jpg', 'html'].each do |ext| puts "Fetching #{n}.#{ext}..." File.open("#{n}.#{ext}", "w") do |f| f << open("#{BASE_URL}#{n}.#{ext}").read end end end
We’ve liked succ before, haven’t we? (Lifted from ruby-talk:151295.)
emmanuel
to make it work under win32 (yes, well, i’m at work :-/ ), change that “w” to a “wb” for the jpegs.
Dominic Mitchell
Cute, but it’s easier on the command line with curl:
curl “http://www.rubyist.net/~matz/slides/oscon2005/mpg[00001-00059].{jpg,html}”
Vincent Foley
Dominic: of course it easier, but is it more fun? :) Seriously, I think it’s often when people tackle on these easy scripts that you can discover new ways to do things in Ruby, and that’s just plain cool.
Dave Lehman
In the mode of “learning by tinkering”, I ran the above code. It downloads the slides/jpgs. However, interestingly, all the jpg images are screwed up (example image on Flickr).
As you say, there are other (easier?) ways to download the slides. But I am interested in why this happened. Could a Ruby guru comment on what went wrong with my jpg download? (or how to get it to work properly?)
MenTaLguY
Dave: if you’re on Windows, see emmanuel’s comment…
Dave Lehman
Doh! Sorry for not RTFPC (where PC = preceding comments)!
Comments are closed for this entry.