diff --git a/lib/provider/youtube.rb b/lib/provider/youtube.rb index feab6d80..389b4872 100644 --- a/lib/provider/youtube.rb +++ b/lib/provider/youtube.rb @@ -6,14 +6,14 @@ class Youtube :duration, :date, :width, :height, :thumbnail_small, :thumbnail_large, :view_count - + def initialize(url) @video_id = url.gsub(/.*v=([^&]+).*$/i, '\1') get_info unless @video_id == url end - + private - + def get_info doc = Hpricot(open("http://gdata.youtube.com/feeds/api/videos/#{@video_id}")) @provider = "YouTube" @@ -23,15 +23,15 @@ def get_info @keywords = doc.search("media:keywords").inner_text @duration = doc.search("yt:duration").first[:seconds].to_i @date = Time.parse(doc.search("published").inner_text, Time.now.utc) - @thumbnail_small = doc.search("media:thumbnail").last[:url] - @thumbnail_large = doc.search("media:thumbnail").first[:url] + @thumbnail_small = doc.search("media:thumbnail").min { |a,b| a[:height].to_i * a[:width].to_i <=> b[:height].to_i * b[:width].to_i }[:url] + @thumbnail_large = doc.search("media:thumbnail").max { |a,b| a[:height].to_i * a[:width].to_i <=> b[:height].to_i * b[:width].to_i }[:url] # when your video still has no view, yt:statistics is not returned by Youtube # see: https://github.com/thibaudgg/video_info/issues#issue/2 if doc.search("yt:statistics").first @view_count = doc.search("yt:statistics").first[:viewcount].to_i else - @view_count = 0 + @view_count = 0 end end - + end \ No newline at end of file diff --git a/spec/fixtures/vcr_cassettes/vimeo/898029.yml b/spec/fixtures/vcr_cassettes/vimeo/898029.yml index 6e8c75e6..29f7bed9 100644 --- a/spec/fixtures/vcr_cassettes/vimeo/898029.yml +++ b/spec/fixtures/vcr_cassettes/vimeo/898029.yml @@ -13,17 +13,15 @@ x-powered-by: - PHP/5.3.3 expires: - - Wed, 12 Jan 2011 20:05:48 GMT + - Sat, 16 Apr 2011 23:26:17 GMT content-type: - text/xml;charset=UTF-8 server: - Apache date: - - Thu, 13 Jan 2011 08:05:48 GMT + - Sun, 17 Apr 2011 11:26:17 GMT content-length: - "1462" - x-server: - - 10.90.6.240 cache-control: - no-store, no-cache, must-revalidate - post-check=0, pre-check=0 @@ -48,7 +46,7 @@ http://b.vimeocdn.com/ps/146/987/1469877_100.jpg http://b.vimeocdn.com/ps/146/987/1469877_300.jpg 8 - 4159 + 4244 4 175 640 diff --git a/spec/fixtures/vcr_cassettes/youtube/JM9NgvjjVng.yml b/spec/fixtures/vcr_cassettes/youtube/JM9NgvjjVng.yml index ef87f179..a80d976e 100644 --- a/spec/fixtures/vcr_cassettes/youtube/JM9NgvjjVng.yml +++ b/spec/fixtures/vcr_cassettes/youtube/JM9NgvjjVng.yml @@ -15,15 +15,15 @@ x-frame-options: - SAMEORIGIN expires: - - Thu, 13 Jan 2011 08:05:47 GMT + - Sun, 17 Apr 2011 11:26:16 GMT last-modified: - - Wed, 12 Jan 2011 21:43:53 GMT + - Sat, 16 Apr 2011 15:49:59 GMT content-type: - application/atom+xml; charset=UTF-8 server: - GSE date: - - Thu, 13 Jan 2011 08:05:47 GMT + - Sun, 17 Apr 2011 11:26:16 GMT x-xss-protection: - 1; mode=block gdata-version: @@ -36,5 +36,5 @@ - "*" transfer-encoding: - chunked - body: http://gdata.youtube.com/feeds/api/videos/JM9NgvjjVng2008-04-21T15:34:35.000Z2011-01-12T21:43:53.000ZSyndication of this video was restricted by its owner.Amy Macdonald - Poison Prince - 2008 VersionSee the brand new video to Amy's new single 'Poison Prince' out May 19th 2008...amymacdonaldofficialhttp://gdata.youtube.com/feeds/api/users/amymacdonaldofficialMusicSee the brand new video to Amy's new single 'Poison Prince' out May 19th 2008...amy, macdonald, rock, roll, poison, princeDEAmy Macdonald - Poison Prince - 2008 Version + body: http://gdata.youtube.com/feeds/api/videos/JM9NgvjjVng2008-04-21T15:34:35.000Z2011-04-16T15:49:59.000ZSyndication of this video was restricted by its owner.Amy Macdonald - Poison Prince - 2008 VersionSee the brand new video to Amy's new single 'Poison Prince' out May 19th 2008...amymacdonaldofficialhttp://gdata.youtube.com/feeds/api/users/amymacdonaldofficialMusicSee the brand new video to Amy's new single 'Poison Prince' out May 19th 2008...amy, macdonald, rock, roll, poison, princeDEAmy Macdonald - Poison Prince - 2008 Version http_version: "1.1" diff --git a/spec/fixtures/vcr_cassettes/youtube/mZqGqE0D0n4.yml b/spec/fixtures/vcr_cassettes/youtube/mZqGqE0D0n4.yml index 760e2200..135bd5a7 100644 --- a/spec/fixtures/vcr_cassettes/youtube/mZqGqE0D0n4.yml +++ b/spec/fixtures/vcr_cassettes/youtube/mZqGqE0D0n4.yml @@ -15,7 +15,7 @@ x-frame-options: - SAMEORIGIN expires: - - Thu, 13 Jan 2011 08:05:47 GMT + - Sun, 17 Apr 2011 11:26:15 GMT last-modified: - Thu, 06 Jan 2011 20:35:33 GMT content-type: @@ -23,7 +23,7 @@ server: - GSE date: - - Thu, 13 Jan 2011 08:05:47 GMT + - Sun, 17 Apr 2011 11:26:15 GMT x-xss-protection: - 1; mode=block gdata-version: @@ -36,5 +36,5 @@ - "*" transfer-encoding: - chunked - body: http://gdata.youtube.com/feeds/api/videos/mZqGqE0D0n42008-04-12T22:25:35.000Z2011-01-06T20:35:33.000ZCherry Bloom - King Of The KnifeThe first video from the upcoming album Secret Sounds, to download in-stores April 14. Checkout http://www.cherrybloom.netcherrybloombandhttp://gdata.youtube.com/feeds/api/users/cherrybloombandMusicThe first video from the upcoming album Secret Sounds, to download in-stores April 14. Checkout http://www.cherrybloom.netcherry, bloom, king, of, the, knife, guitar, drum, clip, rock, alternative, tremplin, Paris-ForumCherry Bloom - King Of The Knife + body: http://gdata.youtube.com/feeds/api/videos/mZqGqE0D0n42008-04-12T22:25:35.000Z2011-01-06T20:35:33.000ZCherry Bloom - King Of The KnifeThe first video from the upcoming album Secret Sounds, to download in-stores April 14. Checkout http://www.cherrybloom.netcherrybloombandhttp://gdata.youtube.com/feeds/api/users/cherrybloombandMusicThe first video from the upcoming album Secret Sounds, to download in-stores April 14. Checkout http://www.cherrybloom.netcherry, bloom, king, of, the, knife, guitar, drum, clip, rock, alternative, tremplin, Paris-ForumCherry Bloom - King Of The Knife http_version: "1.1" diff --git a/spec/fixtures/vcr_cassettes/youtube/oQ49W_xKzKA.yml b/spec/fixtures/vcr_cassettes/youtube/oQ49W_xKzKA.yml index ff2e6562..27314e9a 100644 --- a/spec/fixtures/vcr_cassettes/youtube/oQ49W_xKzKA.yml +++ b/spec/fixtures/vcr_cassettes/youtube/oQ49W_xKzKA.yml @@ -15,7 +15,7 @@ x-frame-options: - SAMEORIGIN expires: - - Thu, 13 Jan 2011 08:05:47 GMT + - Sun, 17 Apr 2011 11:26:16 GMT last-modified: - Wed, 05 Jan 2011 21:29:03 GMT content-type: @@ -23,7 +23,7 @@ server: - GSE date: - - Thu, 13 Jan 2011 08:05:47 GMT + - Sun, 17 Apr 2011 11:26:16 GMT x-xss-protection: - 1; mode=block gdata-version: @@ -36,5 +36,5 @@ - "*" transfer-encoding: - chunked - body: http://gdata.youtube.com/feeds/api/videos/oQ49W_xKzKA2011-01-05T21:24:39.000Z2011-01-05T21:29:03.000Zyestest3juliend2http://gdata.youtube.com/feeds/api/users/juliend2Peopletest3test3 + body: http://gdata.youtube.com/feeds/api/videos/oQ49W_xKzKA2011-01-05T21:24:39.000Z2011-01-05T21:29:03.000Zyestest3juliend2http://gdata.youtube.com/feeds/api/users/juliend2Peopletest3test3 http_version: "1.1" diff --git a/spec/video_info_spec.rb b/spec/video_info_spec.rb index 7eb3e352..231212ea 100644 --- a/spec/video_info_spec.rb +++ b/spec/video_info_spec.rb @@ -1,12 +1,12 @@ require 'spec_helper' describe "VideoInfo" do - + context "from Youtube" do describe "Video mZqGqE0D0n4" do use_vcr_cassette "youtube/mZqGqE0D0n4" subject { VideoInfo.new('http://www.youtube.com/watch?v=mZqGqE0D0n4') } - + its(:provider) { should == 'YouTube' } its(:video_id) { should == 'mZqGqE0D0n4' } its(:url) { should == 'http://www.youtube.com/watch?v=mZqGqE0D0n4' } @@ -17,34 +17,34 @@ its(:width) { should be_nil } its(:height) { should be_nil } its(:date) { should == Time.parse('Sat Apr 12 22:25:35 UTC 2008', Time.now.utc) } - its(:thumbnail_small) { should == 'http://i.ytimg.com/vi/mZqGqE0D0n4/2.jpg' } + its(:thumbnail_small) { should == 'http://i.ytimg.com/vi/mZqGqE0D0n4/1.jpg' } its(:thumbnail_large) { should == 'http://i.ytimg.com/vi/mZqGqE0D0n4/0.jpg' } its(:view_count) { should be > 4000 } it { should be_valid } end - + describe "Video JM9NgvjjVng" do use_vcr_cassette "youtube/JM9NgvjjVng" subject { VideoInfo.new('http://www.youtube.com/watch?v=JM9NgvjjVng') } - + its(:provider) { should == 'YouTube' } its(:duration) { should == 217 } it { should be_valid } end - + describe "Video oQ49W_xKzKA" do use_vcr_cassette "youtube/oQ49W_xKzKA" subject { VideoInfo.new('http://www.youtube.com/watch?v=oQ49W_xKzKA') } - - its(:view_count) { should == 0 } + + its(:view_count) { should == 2 } end end - + context "from Vimeo" do describe "Video 898029" do use_vcr_cassette "vimeo/898029" subject { VideoInfo.new('http://www.vimeo.com/898029') } - + its(:provider) { should == 'Vimeo' } its(:video_id) { should == '898029' } its(:url) { should == 'http://vimeo.com/898029' } @@ -61,28 +61,28 @@ it { should be_valid } end end - - context "from" do + + context "from" do describe "misstaped url" do subject { VideoInfo.new('http://www.vimo.com/1') } - + it { should_not be_valid } end describe "bad url" do subject { VideoInfo.new('http://www.yasda.com/asdasd') } - + it { should_not be_valid } end describe "blank url" do subject { VideoInfo.new('') } - + it { should_not be_valid } end describe "nil url" do subject { VideoInfo.new(nil) } - + it { should_not be_valid } end end - + end \ No newline at end of file