Scraping Railscasts

I had a few moments today to get reacquainted with Ruby and Rails programming. It has been a while but I have found time to watch the occasional Railscast. I want to watch these videos while on the move but unfortunately the iTunes podcasts are not compatible with the iPad/iPhone. Downloading each one is tedious so I came up with the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
require 'open-uri'
require 'hpricot'

(1..6).each do |page|

  doc = Hpricot(open("http://railscasts.com/episodes?page=#{page}"))

  (doc/"a").each do |l|
    href = l[:href]
    if href =~ /.*ipod_videos.*/ then
      file_name = File.basename(href)

      unless File.exist?(file_name) then
        puts "Downloading #{file_name}"

        File.open(File.basename(href), "w") do |file|
          file.write(open(href).read)
        end
      end
    end
  end
end
Share

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>