I stumbled upon a very
interesting post , where in a small script the people compiled the top most discussed reports with
TED . And as the series are getting dumber and dumber, I decided to fill my free time with something more useful. After all, on TED from time to time they talk about interesting and useful things.
The result was another script that most of these videos downloaded.
One option to see 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 was looking and what was not, and I want to upload everything on the iPhone and watch / listen on the way to work.
After a long googling of Ruby syntax, a small script was written that took the addresses of the reports from the file and downloaded them into the daddy. The case is useful, 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.
Script and data file dropped on
GitHub .
For some videos on the site there are no links to download. They will have to look there or on youtube.
It would be possible to put all the downloaded videos somewhere in torrents, but it is not as interesting as
collecting the source code from the download itself.
With pleasure I will listen to comments from the guru of Ruby.
PS left to watch everything in order.
Update : added a gemfile to simplify installation
Update2 : thanks
detunized for the Ruby tutorial
Update3 : added creating videos folder if there is none