2010-11-03 63 views
4

我试图实现youtube_it YouTube红宝石api包装,并让它工作,除了我难以理解如何查询结果应该被访问。如何访问Youtube_it ruby​​查询结果?

这里是我的查询:

client.videos_by(:query => "penguin", :max_results => 1) 
Submitting request [url=http://gdata.youtube.com/feeds/api/videos?max-results=1&start-index=1&vq=penguin]. 
=> #<YouTubeIt::Response::VideoSearch:0xb6c41b14 @feed_id="http://gdata.youtube.com/feeds/api/videos", @updated_at=Wed Nov 03 18:01:39 UTC 2010, @videos=[#<YouTubeIt::Model::Video:0xb6c424d8 @thumbnails=[#<YouTubeIt::Model::Thumbnail:0xb6c6b694 @url="http://i.ytimg.com/vi/oSbLpQEZP1Y/2.jpg", @width=120, @height=90, @time="00:01:34">, #<YouTubeIt::Model::Thumbnail:0xb6c6b248 @url="http://i.ytimg.com/vi/oSbLpQEZP1Y/1.jpg", @width=120, @height=90, @time="00:00:47">, #<YouTubeIt::Model::Thumbnail:0xb6c6a988 @url="http://i.ytimg.com/vi/oSbLpQEZP1Y/3.jpg", @width=120, @height=90, @time="00:02:21">, #<YouTubeIt::Model::Thumbnail:0xb6c69e34 @url="http://i.ytimg.com/vi/oSbLpQEZP1Y/0.jpg", @width=320, @height=240, @time="00:01:34">], @categories=[#<YouTubeIt::Model::Category:0xb6ca5d6c @term="Music", @label="Music">], @noembed=false, @racy=false, @favorite_count=7862, @duration=188, @author=#<YouTubeIt::Model::Author:0xb6c9942c @name="wili", @uri="http://gdata.youtube.com/feeds/api/users/wili">, @updated_at=Tue Nov 02 08:45:25 UTC 2010, @longitude=nil, @position=nil, @view_count=1682350, @html_content="penguin", @media_content=[#<YouTubeIt::Model::Content:0xb6c770d4 @url="http://www.youtube.com/v/oSbLpQEZP1Y?f=videos&app=youtube_gdata", @duration=188, @format=#<YouTubeIt::Model::Video::Format:0xb656d108 @name=:swf, @format_code=5>, @default=true, @mime_type="application/x-shockwave-flash">, #<YouTubeIt::Model::Content:0xb6c766d4 @url="rtsp://v5.cache3.c.youtube.com/CiILENy73wIaGQlWPxkBpcsmoRMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp", @duration=188, @format=#<YouTubeIt::Model::Video::Format:0xb656d11c @name=:rtsp, @format_code=1>, @default=false, @mime_type="video/3gpp">, #<YouTubeIt::Model::Content:0xb6c75d38 @url="rtsp://v8.cache3.c.youtube.com/CiILENy73wIaGQlWPxkBpcsmoRMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp", @duration=188, @format=#<YouTubeIt::Model::Video::Format:0xb656d0f4 @name=:three_gpp, @format_code=6>, @default=false, @mime_type="video/3gpp">], @description="penguin", @latitude=nil, @title="penguin", @published_at=Mon May 08 18:11:01 UTC 2006, @player_url="http://www.youtube.com/watch?v=oSbLpQEZP1Y&feature=youtube_gdata_player", @rating=#<YouTubeIt::Model::Rating:0xb6c5eb4c @min=1, @max=5, @average=4.676985, @rater_count=2746>, @keywords=["pigloo", "penguin"], @video_id="http://gdata.youtube.com/feeds/api/videos/oSbLpQEZP1Y", @where=nil>], @total_result_count=291282, @offset=1, @max_result_count=1> 

我想获取URL和缩略图的链接。有任何想法吗?

回答

3

我对这个特殊的宝石没有太多的了解,但你的答案至少应该接近这个。您可以直接通过videos访问器访问该对象,该访问器将为您提供视频对象,其中每个thumbnails都具有url。所以你可以做以下事情:

reply = client.videos_by(:query => "penguin", :max_results => 1) 
reply.videos.first.thumbnails.first.url # the thumbnail for the first video 
reply.videos.first.player_url # The website for the video 
reply.videos.first.media_content.first.url # direct embed url 

这可能是有用的搜索一些红宝石初学者指南,以帮助赶上你也加快。祝你好运!

+0

你摇滚。我无法确定使用您的示例调用回复的方法,但reply.videos.first像冠军一样工作。谢谢! – spectro 2010-11-03 19:52:38

1

威廉的答案是正确的,当你这样做

client.videos_by(:query => "penguin", :max_results => 1) 

这个返回数组称为视频,所以你只需要遍历它

client.videos.each do |video| 
    video.title 
    video.thumbnails 
    video.video_id 
end 

好运!