rake 任务中 open-uri 出现 404 错误...是什么原因造成的?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果指定的资源(视频/图片)不存在,embed.ly api 将返回 404。 OpenURI 将此作为异常处理。要捕获错误,您可以执行以下操作:
您还可以在运行任务之前检查视频/网址是否有效。
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:
You could also check if the videos/urls are valid before running the task.
您没有对
video.url
进行 URL 编码:因此您可能生成了一个损坏的 URL,而
api.embed.ly
告诉您它找不到它。例如,如果video.video_url
为http://ab?c=d&e=f
,则将看到e=f
作为http://api.embed.ly/1/oembed
的参数,而不是传递给http://ab
。您可能想这样做:
You're not URL encoding your
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, ifvideo.video_url
ishttp://a.b?c=d&e=f
, thene=f
will be seen as a parameter forhttp://api.embed.ly/1/oembed
rather than getting passed on through tohttp://a.b
.You might want to do this instead: