hoodwink.d enhanced
RSS
2.0
XHTML
1.0

RedHanded

Walking Slide Numbers #

by why in inspect

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.)

said on 09 Aug 2005 at 01:20

to make it work under win32 (yes, well, i’m at work :-/ ), change that “w” to a “wb” for the jpegs.

said on 09 Aug 2005 at 02:54

Cute, but it’s easier on the command line with curl:

curl “http://www.rubyist.net/~matz/slides/oscon2005/mpg[00001-00059].{jpg,html}”

said on 09 Aug 2005 at 04:27

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.

said on 09 Aug 2005 at 07:59

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?)

said on 09 Aug 2005 at 09:24

Dave: if you’re on Windows, see emmanuel’s comment…

said on 09 Aug 2005 at 09:52

Doh! Sorry for not RTFPC (where PC = preceding comments)!

Comments are closed for this entry.