Skip to content

Instantly share code, notes, and snippets.

@Koronen
Last active February 27, 2016 10:45
Show Gist options
  • Save Koronen/e6338ece8a2e44b21dec to your computer and use it in GitHub Desktop.
Save Koronen/e6338ece8a2e44b21dec to your computer and use it in GitHub Desktop.
RailsCasts downloader

RailsCasts downloader

A utility script to automatically download all episodes of Ryan Bates' screencast series RailsCasts.

require 'dotenv'
require 'nokogiri'
Dotenv.load
class Episode
attr_reader :slug, :free
def initialize(options = {})
@slug = options[:slug]
@free = options[:free]
end
def file_slug
nr, title = slug.split('-', 2)
[nr.rjust(3, '0'), title].join('-')
end
def source_code_basename
"#{file_slug}.zip"
end
def source_code_url
"http://media.railscasts.com/assets/episodes/sources/#{file_slug}.zip"
end
def video_basename
"#{file_slug}.mp4"
end
def video_url
if free
"http://media.railscasts.com/assets/episodes/videos/#{video_basename}"
else
"http://media.railscasts.com/assets/subscriptions/#{subscription_secret}/videos/#{video_basename}"
end
end
def subscription_secret
ENV.fetch('SUBSCRIPTION_SECRET')
end
end
f = File.open("list.html")
doc = Nokogiri::HTML(f)
f.close
episodes = doc.css('tr.episode td:nth-child(2)').map do |episode_cell|
slug = episode_cell.at_css('a').attributes['href'].text.gsub(%r{/episodes/}, '')
revised = episode_cell.at_css('a').text.end_with?('(revised)')
pro = !!episode_cell.at_css('em')
free = !revised && !pro
Episode.new(slug: slug, free: free)
end
episodes.sort_by!(&:file_slug)
base_dir = '/mnt/sdb1/koronen/Downloads/RailsCasts'
File.open('d.sh', 'w') do |f|
f.puts "#!/bin/bash"
f.puts "set -e"
episodes.each do |episode|
episode_dir = File.join(base_dir, episode.file_slug)
FileUtils.mkdir_p episode_dir
source_code_file = File.join(episode_dir, episode.source_code_basename)
f.puts "[ -f \"#{source_code_file}\" ] || wget -O \"#{source_code_file}\" \"#{episode.source_code_url}\" || true"
video_file = File.join(episode_dir, episode.video_basename)
f.puts "[ -f \"#{video_file}\" ] || wget -O \"#{video_file}\" \"#{episode.video_url}\""
end
end
source 'https://rubygems.org'
ruby '2.1.0'
gem 'dotenv', '~> 0.11.1'
gem 'mechanize', '~> 2.7.3'
gem 'nokogiri', '~> 1.6.2'
GEM
remote: https://rubygems.org/
specs:
domain_name (0.5.18)
unf (>= 0.0.5, < 1.0.0)
dotenv (0.11.1)
dotenv-deployment (~> 0.0.2)
dotenv-deployment (0.0.2)
http-cookie (1.0.2)
domain_name (~> 0.5)
mechanize (2.7.3)
domain_name (~> 0.5, >= 0.5.1)
http-cookie (~> 1.0)
mime-types (~> 2.0)
net-http-digest_auth (~> 1.1, >= 1.1.1)
net-http-persistent (~> 2.5, >= 2.5.2)
nokogiri (~> 1.4)
ntlm-http (~> 0.1, >= 0.1.1)
webrobots (>= 0.0.9, < 0.2)
mime-types (2.3)
mini_portile (0.6.0)
net-http-digest_auth (1.4)
net-http-persistent (2.9.4)
nokogiri (1.6.2.1)
mini_portile (= 0.6.0)
ntlm-http (0.1.1)
unf (0.1.4)
unf_ext
unf_ext (0.0.6)
webrobots (0.1.1)
PLATFORMS
ruby
DEPENDENCIES
dotenv (~> 0.11.1)
mechanize (~> 2.7.3)
nokogiri (~> 1.6.2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment