我试图写我的第一个Ruby程序,但有一个问题.该代码必须通过HTTP下载32个MP3文件.它实际上下载了一些,然后超时.
我尝试设置一个超时时间,但没有任何区别.在Windows,Cygwin和Mac OS X下运行代码具有相同的结果.
这是代码:
require 'rubygems' require 'open-uri' require 'nokogiri' require 'set' require 'net/http' require 'uri' puts "\n Up and running!\n\n" links_set = {} pages = ['http://www.vimeo.com/siai/videos/sort:oldest','http://www.vimeo.com/siai/videos/page:2/sort:oldest','http://www.vimeo.com/siai/videos/page:3/sort:oldest'] pages.each do |page| doc = Nokogiri::HTML(open(page)) doc.search('//*[@href]').each do |m| video_id = m[:href] if video_id.match(/^\/(\d+)$/i) links_set[video_id[/\d+/]] = m.children[0].to_s.split(" at ")[0].split(" -- ")[0] end end end links = links_set.to_a p links cookie = '' file_name = '' open("http://www.tubeminator.com") {|f| cookie = f.Meta['set-cookie'].split(';')[0] } links.each do |link| open("http://www.tubeminator.com/ajax.PHP?function=downloadvideo&url=http%3A%2F%2Fwww.vimeo.com%2F" + link[0],"Cookie" => cookie) {|f| puts f.read } open("http://www.tubeminator.com/ajax.PHP?function=convertvideo&start=0&duration=1120&size=0&format=mp3&vq=high&aq=high","Cookie" => cookie) {|f| file_name = f.read } puts file_name Net::HTTP.start("www.tubeminator.com") { |http| #http.read_timeout = 3600 # 1 hour resp = http.get("/download-video-" + file_name) open(link[1] + ".mp3","wb") { |file| file.write(resp.body) } } end puts "\n Yay!!"
这是例外:
/Users/test/.rvm/rubies/ruby-1.9.2-preview1/lib/ruby/1.9.1/net/protocol.rb:140:in `rescue in rbuf_fill': Timeout::Error (Timeout::Error) from /Users/test/.rvm/rubies/ruby-1.9.2-preview1/lib/ruby/1.9.1/net/protocol.rb:134:in `rbuf_fill' from /Users/test/.rvm/rubies/ruby-1.9.2-preview1/lib/ruby/1.9.1/net/protocol.rb:116:in `readuntil' from /Users/test/.rvm/rubies/ruby-1.9.2-preview1/lib/ruby/1.9.1/net/protocol.rb:126:in `readline' from /Users/test/.rvm/rubies/ruby-1.9.2-preview1/lib/ruby/1.9.1/net/http.rb:2138:in `read_status_line' from /Users/test/.rvm/rubies/ruby-1.9.2-preview1/lib/ruby/1.9.1/net/http.rb:2127:in `read_new' from /Users/test/.rvm/rubies/ruby-1.9.2-preview1/lib/ruby/1.9.1/net/http.rb:1120:in `transport_request' from /Users/test/.rvm/rubies/ruby-1.9.2-preview1/lib/ruby/1.9.1/net/http.rb:1106:in `request' from /Users/test/.rvm/rubies/ruby-1.9.2-preview1/lib/ruby/1.9.1/open-uri.rb:312:in `block in open_http' from /Users/test/.rvm/rubies/ruby-1.9.2-preview1/lib/ruby/1.9.1/net/http.rb:564:in `start' from /Users/test/.rvm/rubies/ruby-1.9.2-preview1/lib/ruby/1.9.1/open-uri.rb:306:in `open_http' from /Users/test/.rvm/rubies/ruby-1.9.2-preview1/lib/ruby/1.9.1/open-uri.rb:767:in `buffer_open' from /Users/test/.rvm/rubies/ruby-1.9.2-preview1/lib/ruby/1.9.1/open-uri.rb:203:in `block in open_loop' from /Users/test/.rvm/rubies/ruby-1.9.2-preview1/lib/ruby/1.9.1/open-uri.rb:201:in `catch' from /Users/test/.rvm/rubies/ruby-1.9.2-preview1/lib/ruby/1.9.1/open-uri.rb:201:in `open_loop' from /Users/test/.rvm/rubies/ruby-1.9.2-preview1/lib/ruby/1.9.1/open-uri.rb:146:in `open_uri' from /Users/test/.rvm/rubies/ruby-1.9.2-preview1/lib/ruby/1.9.1/open-uri.rb:669:in `open' from /Users/test/.rvm/rubies/ruby-1.9.2-preview1/lib/ruby/1.9.1/open-uri.rb:33:in `open' from test.rb:38:in `block in <main>' from test.rb:37:in `each' from test.rb:37:in `<main>'
我还要感谢您对其余代码的意见.