Created
October 23, 2011 14:20
Revisions
-
callumj revised this gist
Oct 23, 2011 . No changes.There are no files selected for viewing
-
callumj created this gist
Oct 23, 2011 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,42 @@ require 'uri' require 'net/http' require 'fileutils' BASE = "http://openclassroom.stanford.edu/MainFolder/courses/HCI/videos/" for l in 1..42 do for p in 1..20 do uri = "CS147L" + l.to_s + "P" + p.to_s + ".flv" uri_obj = URI.parse(BASE + uri) success = false count = 0 while (!(File.exists?(uri)) && !(File.exists?("#{uri}.lock")) && !success) do begin FileUtils.touch("#{uri}.lock") res = Net::HTTP.start(uri_obj.host, uri_obj.port) do |http| head_data = http.head(uri_obj.path) if head_data.code.eql?("200") puts "Getting contents of #{uri}" http.get(uri_obj.path) else nil end end if (res != nil && res.body != nil) puts "Writing #{uri} to file" open(uri, "wb") { |file| file.write(res.body) } end success = true rescue puts "Pull failed, retrying" count = count + 1 success = true if count > 5 end FileUtils.rm_f("#{uri}.lock") if File.exists?("#{uri}.lock") end puts "File #{uri} in place" if File.exists?(uri) end end This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,42 @@ require 'uri' require 'net/http' require 'fileutils' BASE = "http://openclassroom.stanford.edu/MainFolder/courses/WebApplications/videos/" for l in 1..42 do for p in 1..20 do uri = "CS142L" + l.to_s + "P" + p.to_s + ".flv" uri_obj = URI.parse(BASE + uri) success = false count = 0 while (!(File.exists?(uri)) && !(File.exists?("#{uri}.lock")) && !success) do begin FileUtils.touch("#{uri}.lock") res = Net::HTTP.start(uri_obj.host, uri_obj.port) do |http| head_data = http.head(uri_obj.path) if head_data.code.eql?("200") puts "Getting contents of #{uri}" http.get(uri_obj.path) else nil end end if (res != nil && res.body != nil) puts "Writing #{uri} to file" open(uri, "wb") { |file| file.write(res.body) } end success = true rescue puts "Pull failed, retrying" count = count + 1 success = true if count > 5 end FileUtils.rm_f("#{uri}.lock") if File.exists?("#{uri}.lock") end puts "File #{uri} in place" if File.exists?(uri) end end