rake 任务中 open-uri 出现 404 错误...是什么原因造成的?

发布于 2024-12-06 02:35:34 字数 2112 浏览 1 评论 0原文

我有一个 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'

关于问题所在以及如何解决?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

左耳近心 2024-12-13 02:35:34

如果指定的资源(视频/图片)不存在,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

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

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.

跨年 2024-12-13 02:35:34

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

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

因此您可能生成了一个损坏的 URL,而 api.embed.ly 告诉您它找不到它。例如,如果 video.video_urlhttp://ab?c=d&e=f,则将看到 e=f作为 http://api.embed.ly/1/oembed 的参数,而不是传递给 http://ab

您可能想这样做:

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

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)}...")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文