Skip to content

Instantly share code, notes, and snippets.

@lukeholder
Forked from gertig/peepcode.rb
Created April 9, 2012 22:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukeholder/2347090 to your computer and use it in GitHub Desktop.
Save lukeholder/2347090 to your computer and use it in GitHub Desktop.
A script to download all Peepcode screencasts with ruby & wget (supports resume)
require 'mechanize'
@username = 'username@email'
@password = 'password'
@download_path = File.expand_path 'downloads'
@wget_cookie = File.expand_path(File.dirname(__FILE__)) + '/wget-cookies.txt'
unless File.directory? @download_path
puts "@{download_path} doesn't exist!"
exit
end
agent = Mechanize.new
agent.cookie_jar.load('cookies.yml') if File.exists?('cookies.yml')
page = agent.get 'https://peepcode.com/'
unless page.body.match /\/peepers\/account\/edit/
puts "Logging in"
page = agent.get 'https://peepcode.com/login'
signin_form = page.form_with(:action => '/sessions')
signin_form.email = @username
signin_form.password = @password
agent.submit(signin_form, signin_form.buttons.first)
agent.cookie_jar.save_as('cookies.yml')
end
def wget(sub_folder,path)
sub_folder = sub_folder.gsub(/\s+/, "")
unless File.exists?('wget-cookies.txt')
cmd = "wget --no-check-certificate --keep-session-cookies --save-cookies wget-cookies.txt "
cmd<< "--post-data 'email=#{@username}&password=#{@password}' https://peepcode.com/sessions"
system cmd
end
system ""
unless File.directory? "#{@download_path}/#{sub_folder}"
puts "making dir #{sub_folder}"
system "cd #{@download_path};mkdir #{sub_folder}"
end
system "cd #{@download_path}/#{sub_folder}; wget --load-cookies #{@wget_cookie} --continue '#{path}'"
end
page = agent.get 'https://peepcode.com/products'
page.links_with(:href => %r{/products/}).each do |link|
puts "Processing #{link.text}"
result = agent.click link
sub_folder = "#{link.text}"
links = []
download_links = result.links_with(:href => %r{/unlimited_downloads/}).each do |dl|
ogg = dl.href.match(/\.ogg/)
s3 = (dl.text.match(/USA-West/) || dl.href.match(/s3=true/))
wmv = dl.href.match(/wmv_zip/)
if !s3 && !ogg && !links.include?(dl.href) && !wmv
links.push dl.href
wget sub_folder, dl.href
end
end
puts
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment