Do not know what to see? Watch the best videos from TED

    I came across a very interesting post where, with a small script, the people made the top of the most talked about reports with TED . And since the series are getting dumber and dumber, I decided to fill my free time with something more useful. Indeed, at TED from time to time they talk about interesting and useful things.

    The result is another script that most of these videos have downloaded.

    One option to watch all the videos would be to open the XLS file from the site and click on the links from top to bottom, but it is extremely inconvenient. After 10-20 links, I would be confused about what I watched and what not, and I also want to upload everything to the iPhone and watch / listen on the way to work.

    After a long google Ruby syntax, a small script was written that took the addresses of the reports from the file and downloaded them to daddy. A useful thing, maybe someone will come in handy.

    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/a[text()='Download video to desktop (MP4)']")
        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

    Accordingly, you need to put gems: nokogirii and httparty.
    The script and data file were thrown on GitHub .

    There are no download links for some videos on the site. They will have to watch there or on youtube.
    It would be possible to put all the downloaded videos somewhere in torrents, but it is not so interesting how to assemble the source from the source yourself.
    I am pleased to hear comments from the Ruby guru.

    PS went to see everything in order.

    Update : added a Gemfile to simplify the installation of
    Update2 : thanks detunized for the Ruby lesson
    Update3 : added the creation of the videos folder if it is not

    Also popular now: