Skip to content

Instantly share code, notes, and snippets.

@ouyangzhiping
Forked from tlrobinson/peepcode.rb
Created August 12, 2013 04:41
Show Gist options
  • Save ouyangzhiping/6208284 to your computer and use it in GitHub Desktop.
Save ouyangzhiping/6208284 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'mechanize'
@username = USERNAME
@password = PASSWORD
@download_path = DOWNLOAD_PATH
unless File.directory? @download_path
puts "@{download_path} doesn't exist!"
exit
end
@agent = Mechanize.new
@agent.pluggable_parser.default = Mechanize::Download
@agent.cookie_jar.load('cookies.yml') if File.exists?('cookies.yml')
def download(url)
name = File.basename(url).sub(/_z(ip)?$/, ".zip")
path = File.join @download_path, name
if File.exists? path
puts "Already downloaded: #{url}"
else
puts "Downloading: #{url}"
@agent.get(url).save("#{path}.tmp")
File.rename "#{path}.tmp", path
end
end
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
page = @agent.get 'https://peepcode.com/products'
page.links_with(:href => %r{/products/}).each do |link|
puts "Processing: #{link.text}"
result = @agent.click link
links = []
download_links = result.links_with(:href => %r{/unlimited_downloads/}).each do |dl|
ogg = dl.href.match(/\.ogg/)
ipod = dl.href.match(/\.ipod/)
wmv = dl.href.match(/\.wmv/)
s3 = (dl.text.match(/USA-West/) || dl.href.match(/s3=true/))
if !s3 && !ogg && !wmv && !ipod && !links.include?(dl.href)
links.push dl.href
download dl.href
end
end
puts
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment