Skip to content

Instantly share code, notes, and snippets.

@ayosec
Created April 5, 2011 01:12
Show Gist options
  • Save ayosec/902825 to your computer and use it in GitHub Desktop.
Save ayosec/902825 to your computer and use it in GitHub Desktop.
Downloader for TED
# Use
# ruby download.rb http://www.ted.com/search?q=any+query
require 'rubygems'
require 'mechanize'
agent = Mechanize.new { |agent| agent.user_agent_alias = 'Linux Firefox' }
pages = ARGV
loaded = []
class DummyObject
def method_missing(*a)
end
end
class <<agent
def absolute_url(uri)
hash = { :uri => uri, :referer => current_page }
Mechanize::Chain::URIResolver.new(@scheme_handlers).tap {|o| o.chain = DummyObject.new }.handle(nil, hash)
hash[:uri].to_s
end
end
while not pages.empty?
current_url = pages.shift
next if loaded.include?(current_url)
loaded << current_url
puts "Loading #{current_url}..."
current_page = agent.get(current_url)
download_links = current_page.search(".downloads a")
if not download_links.empty?
Process.wait(fork do
video_link = download_links.last[:href]
dest_file = current_url.split("/").last.sub(/\.html$/i, "") + ".mp4"
if File.exist?(dest_file)
puts "#{dest_file} exists"
else
exec "wget", "-O", dest_file, agent.absolute_url(video_link)
end
end)
else
current_page.search("dt a").each do |next_link|
pages << next_link[:href]
end
if next_page = current_page.at("li.next a")
pages << next_page[:ref]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment