404 error with open-uri in a rake task… what's

2019-04-04 17:19发布

问题:

I have a rake task that fetches JSON data from an API, parses it, and saves it to the database:

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

I get this error in the stack trace when I run the rake task and I have no idea what is causing it:

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'

Any ideas on the problem and how to resolve it?

回答1:

The embed.ly api returns a 404 if the specified resource(video/picture) doesn't exist. OpenURI handles this as an exception. To catch the error you could do something like this:

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

You could also check if the videos/urls are valid before running the task.



回答2:

You're not URL encoding your video.url:

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

so you're probably producing a mangled URL and api.embed.ly is telling you that it can't find it. For example, if video.video_url is http://a.b?c=d&e=f, then e=f will be seen as a parameter for http://api.embed.ly/1/oembed rather than getting passed on through to http://a.b.

You might want to do this instead:

require 'cgi'
#...
json_stream = open("...url=#{CGI.escape(video.video_url)}...")