13

我有一个 rake 任务,它从 API 中获取 JSON 数据,对其进行解析,并将其保存到数据库中:

task :embedly => :environment do
  require 'json'
  require 'uri'
  require 'open-uri'

  Video.all.each do |video|
    json_stream = open("http://api.embed.ly/1/oembed?key=08b652e6b3ea11e0ae3f4040d3dc5c07&url=#{video.video_url}&maxwidth=525")
    ruby_hash = JSON.parse(json_stream.read)
    thumbnail_url = ruby_hash['thumbnail_url']
    embed_code = ruby_hash['html']
    video.update_attributes(:thumbnail_url => thumbnail_url, :embed_code => embed_code)
  end  
end

当我运行 rake 任务时,我在堆栈跟踪中收到此错误,但我不知道是什么原因造成的:

rake aborted!
404 Not Found
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/open-uri.rb:277:in `open_http'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/open-uri.rb:616:in `buffer_open'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/open-uri.rb:164:in `open_loop'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/open-uri.rb:162:in `catch'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/open-uri.rb:162:in `open_loop'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/open-uri.rb:132:in `open_uri'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/open-uri.rb:518:in `open'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/open-uri.rb:30:in `open'
/rubyprograms/dreamstill/lib/tasks/tasks.rake:16
/rubyprograms/dreamstill/lib/tasks/tasks.rake:15:in `each'
/rubyprograms/dreamstill/lib/tasks/tasks.rake:15
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in `call'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in `execute'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:in `each'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:in `execute'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:597:in `invoke_with_call_chain'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/monitor.rb:242:in `synchronize'

关于这个问题的任何想法以及如何解决它?

4

2 回答 2

21

如果指定的资源(视频/图片)不存在,embed.ly api 返回 404。OpenURI 将其作为例外处理。要捕获错误,您可以执行以下操作:

task :embedly => :environment do
  require 'json'
  require 'uri'
  require 'open-uri'

  Video.all.each do |video|
    begin
      json_stream = open("http://api.embed.ly/1/oembed?key=08b652e6b3ea11e0ae3f4040d3dc5c07&url=#{video.video_url}&maxwidth=525")
      ruby_hash = JSON.parse(json_stream.read)
      thumbnail_url = ruby_hash['thumbnail_url']
      embed_code = ruby_hash['html']
      video.update_attributes(:thumbnail_url => thumbnail_url, :embed_code => embed_code)
    rescue OpenURI::HTTPError => ex
      puts "Handle missing video here"
    end 
  end  
end

您还可以在运行任务之前检查视频/网址是否有效。

于 2011-09-21T06:24:09.267 回答
1

您没有对您的 URL 进行编码video.url

json_stream = open("...url=#{video.video_url}...")

所以您可能正在生成一个损坏的 URL,并api.embed.ly告诉您它找不到它。例如,如果video.video_urlhttp://a.b?c=d&e=f,那么e=f将被视为一个参数,http://api.embed.ly/1/oembed而不是传递给http://a.b

您可能想要这样做:

require 'cgi'
#...
json_stream = open("...url=#{CGI.escape(video.video_url)}...")
于 2011-09-21T06:27:47.030 回答