2011-05-26 32 views
0

我正在研究一個Ruby腳本,以自動從用戶或術語列表中收集推文,並且除了Twitter容量超過時,該腳本才起作用。然後,Twitter返回一個沒有錯誤代碼的HTML頁面,供我捕獲。 HTML拋出解析器並且腳本失敗。我如何檢查該HTML響應並正常處理?我嘗試使用「直到成功」的方法(在我的代碼中註釋掉了),但最終每次都有救援。處理來自Twitter API的「超容量」響應

我正在使用記錄器,twitter和twitter4r寶石,並通過OAuth進行身份驗證。下面的代碼工作,除非Twitter超過容量。如果沒有RestErrors返回,我只能進入本節。

這裏是我的代碼:我根據答案試圖

#  until success 
    #   begin 
       if search_type == "users" 
    #    begin 
        tweets_array = client.timeline_for(:user, :id => row.chomp, :since_id => since_status_id, :count => 200) 
        success = true 
       # rescue 
       # log.info("error getting tweets. waiting to try again.") 
       # sleep 180 
       # end 
       elsif search_type == "terms" 
    #    begin 
        tweets_array = client.search(:q => row.chomp, :since_id => since_status_id, :count => 200) 
        success = true 
       # rescue 
       # log.info("error getting tweets. waiting to try again.") 
       # sleep 180 
       # end 
       elsif 
       log.fatal("unsupported search type. exiting.") 
       break 
       end # search type 
    #   end 

代碼:

 begin 
      tweets_array = client.timeline_for(:user, :id => row.chomp, :since_id => since_status_id, :count => 200) 
      success = true 
     rescue Twitter::RESTError => re 
      log.info(row.chomp + ": " + re.code.to_s + " " + re.message.to_s + " " + re.uri.to_s) 
      if re.code = 503 
      sleep 180 
      end 
     end 

回答

0

您可以檢查結果的HTTP status code,它應該是503

http://twitter.com/503

+0

謝謝@Christopher Manning – Libby 2011-06-01 17:47:40

+0

Twitter已結束c apacity剛纔(星期六12月17日09:38:27 WIT 2011)和響應代碼是502「壞門戶」。我在開發過程中遇到了這個問題。 – adib 2011-12-17 02:39:18