LoginSignup
5
5

More than 5 years have passed since last update.

RailsCastsの動画をタグごとにダウンロードしてくるスクリプト

Last updated at Posted at 2012-06-08

個人的に使ってたやつをちょっと整えて公開。なんでタグごとかっちゅうと、

  • 同じテーマの動画を古い順に続けてみることでRailsの歴史的な流れがつかめたりする
  • 複数のtagがついてる動画は何度も遭遇するので復習になる

とか。Ryanさんのトークも、最初はこなれてないけどだんだん本職のナレーターみたいになってくる、そのへんの成長も見守ることが出来ます。

大仰だけどMechanize使った。保存場所(@save_dir)はお好きなところへ。

railscasts.rb
# -*- coding: utf-8 -*-
require 'rubygems'
require 'mechanize'
# require 'pry'

@save_dir = "~/Dropbox/Movie/"
runtag = ARGV.shift.to_i

a = Mechanize.new {|agent| agent.user_agent_alias = "Mac Safari" }
a.get("http://railscasts.com/?tag_id=#{runtag}") {|page| @total_page = page.at(".next_page").previous_element.text.to_i}

for i in 1..@total_page do
  a.get("http://railscasts.com/?page=#{i}&tag_id=#{runtag}") do |page|
    page.links.each do |link|
      if (path = link.attributes.attributes["href"].value) =~ /(episodes).*?autoplay=true$/
        puts path
        a.get("http://railscasts.com"+path) do |mvpage|
          mvpage.links.each do |link|
            if (mp4 = link.attributes.attributes["href"].value) =~ /\.mp4$/
              puts filename =  runtag.to_s + "_" + mp4.split("/")[-1]
              `wget #{mp4} -o mv.log -O #{@save_dir}#{filename}` # if mp4.scan(/\d+/)[0].to_i < 158
            end
          end
        end
      end
    end
  end
end

使い方は $ ruby railscasts.rb 15 こんな感じ。引数に落としたいタグ番号を与える。タグ番号は

↑の右にある"Categories"から選んでます。(もっと簡単な方法ないかね

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5