16
Mar

Don’t know what to watch next? Download and watch top ranked TED videos.

Recently I found a great post where the author (using a script) ranked TED videos by the number of times they were mentioned in social services. And since TV series are getting more and more stupid I decided to start watching these videos. I’ve seen many great TED talks in the past and anyway videos people talk about the most shouldn’t be bad. So I decided to download all of them.

One would open that large XLS file from the site and start clicking links. It might seem easier to just watch one video from the list at a time than to write a script which visits URLs and downloads videos. But I wanted to upload all of them to my iPhone and watch/listen to them while on my way to work. So here’s the final script:

require 'rubygems'
require 'nokogiri'
require 'open-uri'
require 'HTTParty'

urls = IO.readlines("data.txt").map {|line| line.chomp}
if !File.exists?("videos")
  Dir.mkdir("videos")
end

urls.each_with_index do |url, count|
  begin
    doc = Nokogiri.parse(open(url).read)
    node = doc.xpath("//dt/a1")
    video = "http://www.ted.com" + node.attribute("href").to_s
    videoName = "videos/(#{count+1})" + url.match(/http:\/\/www.ted.com\/talks\/(.*)\.html/i)[1] + ".mp4"
    puts "Downloading #{url} to #{videoName}"

    File.open( videoName, "w+") do |f|
      f << HTTParty.get( video )
    end
  rescue
    puts "Failed to download #{url}"
  end
end

I uploaded the same script with data for TED videos (with rating >=5) and Gemfile to install required gems to GitHub. Some of the videos don’t have links to video files, so you have to watch them online.

Tags: , ,