2012-05-01 53 views
1

我有網帶/ HTTP ...打開的文件太多 - 插座(2)淨/ HTTP

我編寫與位DailyMotion API交互的Ruby腳本http請求的問題。 該腳本將上傳視頻「test.flv」。基本上它由四個請求組成。

它完美的工作,直到「#創建視頻對象」的步驟。 以下引發錯誤的最後 「響應= http.request(REQ)」 命令 - >

errno的:: EMFILE:打開的文件太多 - 插座(2)

下面是代碼, THX的任何意見...

require 'net/http' 
require 'curb' 
require 'json' 


# Authenticate the user 
url = URI.parse('https://api.dailymotion.com/oauth/token') 
req = Net::HTTP::Post.new(url.path) 
req.set_form_data({ 'grant_type' => 'password', 
      'client_id' => 'my_client_id', 
      'client_secret' => 'my_client_secret', 
      'username' => 'myusername', 
      'password' => 'mypassword' 
     }) 
http = Net::HTTP.new(url.host, url.port) 
http.use_ssl = true 
http.verify_mode = OpenSSL::SSL::VERIFY_NONE 
response = http.request(req) 

access_token = JSON.parse(response.body)['access_token'] 
access_url = 'https://api.dailymotion.com/file/upload?access_token=' + access_token 



# Get an upload URL 
url = URI.parse(access_url) 
req = Net::HTTP::Get.new(url.request_uri) 
http = Net::HTTP.new(url.host, url.port) 
http.use_ssl = true 
http.verify_mode = OpenSSL::SSL::VERIFY_NONE 
response = http.request(req) 

upload_url = JSON.parse(response.body)['upload_url'] 
progress_url = JSON.parse(response.body)['progress_url'] 


# Post the video 
fields_hash = {} 

post_data = fields_hash.map { |k, v| Curl::PostField.content(k, v.to_s) } 
post_data << Curl::PostField.file('file', 'C:/test.flv') 

c = Curl::Easy.new(upload_url) 
c.multipart_form_post = true 
c.http_post(post_data) 

file_url = JSON.parse(c.body_str)['url'] 


# Create the video object 
url = URI.parse('https://api.dailymotion.com/me/videos') 
req = Net::HTTP::Post.new(url.path) 
req.set_form_data({ 'url' => file_url, 
       'access_token' => access_token 
     }) 
http = Net::HTTP.new(url.host, url.port) 
http.use_ssl = true 
http.verify_mode = OpenSSL::SSL::VERIFY_NONE 
response = http.request(req) 

puts response.body 

回答

0

嘗試調用完成

http.finish 

響應= http.request(REQ)之後。

+0

在最後一次響應= http.request(req)後嘗試將錯誤提出,但錯誤仍然存​​在(可能它不能調用finish,因爲之前會出現錯誤) 也嘗試過響應= http.request(req)在我的腳本早些時候,但這總是給我這個錯誤... IO錯誤:HTTP會話尚未開始 –

+0

這可能是會議開始時,閱讀正文。在JSON.parse(response.body)行之後嘗試完成調用,對於access_token = ...和progress_url = ... –

+0

thx用於回答... 在完成讀取身體後完成調用,但是我仍然得到IOError –