nddrylliog_youtube_it 2.1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. data/Gemfile +5 -0
  2. data/Gemfile.lock +38 -0
  3. data/Manifest.txt +37 -0
  4. data/README.rdoc +296 -0
  5. data/Rakefile +21 -0
  6. data/VERSION +1 -0
  7. data/lib/youtube_it.rb +91 -0
  8. data/lib/youtube_it/chain_io.rb +76 -0
  9. data/lib/youtube_it/client.rb +469 -0
  10. data/lib/youtube_it/middleware/faraday_authheader.rb +24 -0
  11. data/lib/youtube_it/middleware/faraday_oauth.rb +21 -0
  12. data/lib/youtube_it/middleware/faraday_oauth2.rb +13 -0
  13. data/lib/youtube_it/middleware/faraday_youtubeit.rb +30 -0
  14. data/lib/youtube_it/model/activity.rb +17 -0
  15. data/lib/youtube_it/model/author.rb +13 -0
  16. data/lib/youtube_it/model/category.rb +11 -0
  17. data/lib/youtube_it/model/comment.rb +16 -0
  18. data/lib/youtube_it/model/contact.rb +19 -0
  19. data/lib/youtube_it/model/content.rb +18 -0
  20. data/lib/youtube_it/model/message.rb +12 -0
  21. data/lib/youtube_it/model/playlist.rb +11 -0
  22. data/lib/youtube_it/model/rating.rb +23 -0
  23. data/lib/youtube_it/model/subscription.rb +7 -0
  24. data/lib/youtube_it/model/thumbnail.rb +17 -0
  25. data/lib/youtube_it/model/user.rb +28 -0
  26. data/lib/youtube_it/model/video.rb +243 -0
  27. data/lib/youtube_it/parser.rb +543 -0
  28. data/lib/youtube_it/record.rb +12 -0
  29. data/lib/youtube_it/request/base_search.rb +76 -0
  30. data/lib/youtube_it/request/error.rb +15 -0
  31. data/lib/youtube_it/request/standard_search.rb +43 -0
  32. data/lib/youtube_it/request/user_search.rb +47 -0
  33. data/lib/youtube_it/request/video_search.rb +105 -0
  34. data/lib/youtube_it/request/video_upload.rb +552 -0
  35. data/lib/youtube_it/response/video_search.rb +41 -0
  36. data/lib/youtube_it/version.rb +4 -0
  37. data/test/files/recorded_response.xml +1 -0
  38. data/test/files/youtube_video_response.xml +53 -0
  39. data/test/helper.rb +9 -0
  40. data/test/test.mov +0 -0
  41. data/test/test_chain_io.rb +63 -0
  42. data/test/test_client.rb +504 -0
  43. data/test/test_field_search.rb +48 -0
  44. data/test/test_video.rb +48 -0
  45. data/test/test_video_feed_parser.rb +264 -0
  46. data/test/test_video_search.rb +147 -0
  47. data/youtube_it.gemspec +95 -0
  48. metadata +149 -0
@@ -0,0 +1,41 @@
1
+ class YouTubeIt
2
+ module Response
3
+ class VideoSearch < YouTubeIt::Record
4
+ # *String*:: Unique feed identifying url.
5
+ attr_reader :feed_id
6
+
7
+ # *Fixnum*:: Number of results per page.
8
+ attr_reader :max_result_count
9
+
10
+ # *Fixnum*:: 1-based offset index into the full result set.
11
+ attr_reader :offset
12
+
13
+ # *Fixnum*:: Total number of results available for the original request.
14
+ attr_reader :total_result_count
15
+
16
+ # *Time*:: Date and time at which the feed was last updated
17
+ attr_reader :updated_at
18
+
19
+ # *Array*:: Array of YouTubeIt::Model::Video records
20
+ attr_reader :videos
21
+
22
+ def current_page
23
+ ((offset - 1) / max_result_count) + 1
24
+ end
25
+
26
+ # current_page + 1 or nil if there is no next page
27
+ def next_page
28
+ current_page < total_pages ? (current_page + 1) : nil
29
+ end
30
+
31
+ # current_page - 1 or nil if there is no previous page
32
+ def previous_page
33
+ current_page > 1 ? (current_page - 1) : nil
34
+ end
35
+
36
+ def total_pages
37
+ (total_result_count / max_result_count.to_f).ceil
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,4 @@
1
+ class YouTubeIt
2
+ VERSION = '2.1.4'
3
+ end
4
+
@@ -0,0 +1 @@
1
+ <?xml version='1.0' encoding='UTF-8'?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:app='http://www.w3.org/2007/app' xmlns:media='http://search.yahoo.com/mrss/' xmlns:gd='http://schemas.google.com/g/2005' xmlns:yt='http://gdata.youtube.com/schemas/2007'><entry gd:etag='W/&quot;C08NSH47eCp7ImA9WhZXGUU.&quot;'><id>tag:youtube.com,2008:video:z8zIecfFRjM</id><published>2011-01-28T20:32:47.000Z</published><updated>2011-05-09T22:58:19.000Z</updated><category scheme='http://schemas.google.com/g/2005#kind' term='http://gdata.youtube.com/schemas/2007#video'/><category scheme='http://gdata.youtube.com/schemas/2007/categories.cat' term='Music' label='Music'/><category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='School Jam USA'/><category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='lennon bus live'/><category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='lennon bus'/><category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='lennon'/><category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='john lennon'/><category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='ac/dc'/><category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='motley crue'/><category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='namm 2011'/><category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='namm'/><title>Lennon Bus Live: Peligro - "Hollywood Red"</title><content type='application/x-shockwave-flash' src='http://www.youtube.com/v/z8zIecfFRjM?f=videos&amp;app=youtube_gdata'/><link rel='alternate' type='text/html' href='http://www.youtube.com/watch?v=z8zIecfFRjM&amp;feature=youtube_gdata'/><link rel='http://gdata.youtube.com/schemas/2007#video.responses' type='application/atom+xml' href='http://gdata.youtube.com/feeds/api/videos/z8zIecfFRjM/responses?v=2'/><link rel='http://gdata.youtube.com/schemas/2007#video.related' type='application/atom+xml' href='http://gdata.youtube.com/feeds/api/videos/z8zIecfFRjM/related?v=2'/><link rel='http://gdata.youtube.com/schemas/2007#mobile' type='text/html' href='http://m.youtube.com/details?v=z8zIecfFRjM'/><link rel='self' type='application/atom+xml' href='http://gdata.youtube.com/feeds/api/videos/z8zIecfFRjM?v=2'/><author><name>johnlennonbus</name><uri>http://gdata.youtube.com/feeds/api/users/johnlennonbus</uri></author><yt:accessControl action='comment' permission='allowed'/><yt:accessControl action='commentVote' permission='allowed'/><yt:accessControl action='videoRespond' permission='moderated'/><yt:accessControl action='rate' permission='allowed'/><yt:accessControl action='embed' permission='allowed'/><yt:accessControl action='list' permission='allowed'/><yt:accessControl action='syndicate' permission='allowed'/><gd:comments><gd:feedLink href='http://gdata.youtube.com/feeds/api/videos/z8zIecfFRjM/comments?v=2' countHint='28'/></gd:comments><yt:location>Anaheim, Ca</yt:location><media:group><media:category label='Music' scheme='http://gdata.youtube.com/schemas/2007/categories.cat'>Music</media:category><media:content url='http://www.youtube.com/v/z8zIecfFRjM?f=videos&amp;app=youtube_gdata' type='application/x-shockwave-flash' medium='video' isDefault='true' expression='full' duration='192' yt:format='5'/><media:content url='rtsp://v6.cache2.c.youtube.com/CiILENy73wIaGQkzRsXHecjMzxMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp' type='video/3gpp' medium='video' expression='full' duration='192' yt:format='1'/><media:content url='rtsp://v3.cache5.c.youtube.com/CiILENy73wIaGQkzRsXHecjMzxMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp' type='video/3gpp' medium='video' expression='full' duration='192' yt:format='6'/><media:credit role='uploader' scheme='urn:youtube'>johnlennonbus</media:credit><media:description type='plain'>At NAMM 2011, School Jam USA winners Peligro had the opportunity to come aboard the John Lennon Educational Tour Bus to record a song and shoot a music video. These young rockers would not seem out of place alongside AC/DC or Mötley Crüe. Be Sure to check them out as they rock out during a Lennon Bus Live Session, performing their hit, Hollywood Red</media:description><media:keywords>School Jam USA, lennon bus live, lennon bus, lennon, john lennon, ac/dc, motley crue, namm 2011, namm</media:keywords><media:player url='http://www.youtube.com/watch?v=z8zIecfFRjM&amp;feature=youtube_gdata_player'/><media:thumbnail url='http://i.ytimg.com/vi/z8zIecfFRjM/default.jpg' height='90' width='120' time='00:01:36' yt:name='default'/><media:thumbnail url='http://i.ytimg.com/vi/z8zIecfFRjM/hqdefault.jpg' height='360' width='480' yt:name='hqdefault'/><media:thumbnail url='http://i.ytimg.com/vi/z8zIecfFRjM/1.jpg' height='90' width='120' time='00:00:48' yt:name='start'/><media:thumbnail url='http://i.ytimg.com/vi/z8zIecfFRjM/2.jpg' height='90' width='120' time='00:01:36' yt:name='middle'/><media:thumbnail url='http://i.ytimg.com/vi/z8zIecfFRjM/3.jpg' height='90' width='120' time='00:02:24' yt:name='end'/><media:title type='plain'>Lennon Bus Live: Peligro - "Hollywood Red"</media:title><yt:aspectRatio>widescreen</yt:aspectRatio><yt:duration seconds='192'/><yt:uploaded>2011-01-28T20:32:47.000Z</yt:uploaded><yt:videoid>z8zIecfFRjM</yt:videoid></media:group><gd:rating average='4.894737' max='5' min='1' numRaters='38' rel='http://schemas.google.com/g/2005#overall'/><yt:recorded>2011-01-16</yt:recorded><yt:statistics favoriteCount='21' viewCount='3648'/><yt:rating numDislikes='1' numLikes='37'/></entry></feed>
@@ -0,0 +1,53 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <entry xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xmlns:gd="http://schemas.google.com/g/2005" xmlns:yt="http://gdata.youtube.com/schemas/2007" gd:etag="W/&quot;A0UBR347eCp7ImA9Wx9bFEs.&quot;">
3
+ <id>tag:youtube.com,2008:video:AbC123DeFgH</id>
4
+ <published>2010-12-29T13:57:49.000Z</published>
5
+ <updated>2011-02-23T13:54:16.000Z</updated>
6
+ <category scheme="http://schemas.google.com/g/2005#kind" term="http://gdata.youtube.com/schemas/2007#video"/>
7
+ <category scheme="http://gdata.youtube.com/schemas/2007/categories.cat" term="Test" label="Test"/>
8
+ <category scheme="http://gdata.youtube.com/schemas/2007/keywords.cat" term="test"/>
9
+ <title>YouTube Test Video</title>
10
+ <content type="application/x-shockwave-flash" src="http://www.youtube.com/v/YTAbC123DeF?f=videos&amp;app=youtube_gdata"/>
11
+ <link rel="alternate" type="text/html" href="http://www.youtube.com/watch?v=AbC123DeFgH&amp;feature=youtube_gdata"/>
12
+ <link rel="http://gdata.youtube.com/schemas/2007#video.responses" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/videos/AbC123DeFgH/responses?v=2"/>
13
+ <link rel="http://gdata.youtube.com/schemas/2007#video.related" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/videos/AbC123DeFgH/related?v=2"/>
14
+ <link rel="http://gdata.youtube.com/schemas/2007#mobile" type="text/html" href="http://m.youtube.com/details?v=AbC123DeFgH"/>
15
+ <link rel="self" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/videos/AbC123DeFgH?v=2"/>
16
+ <author>
17
+ <name>Test user</name>
18
+ <uri>http://gdata.youtube.com/feeds/api/users/test_user</uri>
19
+ </author>
20
+ <yt:accessControl action="comment" permission="allowed"/>
21
+ <yt:accessControl action="commentVote" permission="allowed"/>
22
+ <yt:accessControl action="videoRespond" permission="moderated"/>
23
+ <yt:accessControl action="rate" permission="allowed"/>
24
+ <yt:accessControl action="embed" permission="allowed"/>
25
+ <yt:accessControl action="list" permission="allowed"/>
26
+ <yt:accessControl action="syndicate" permission="allowed"/>
27
+ <gd:comments>
28
+ <gd:feedLink href="http://gdata.youtube.com/feeds/api/videos/AbC123DeFgH/comments?v=2" countHint="1000"/>
29
+ </gd:comments>
30
+ <media:group>
31
+ <media:category label="Sports" scheme="http://gdata.youtube.com/schemas/2007/categories.cat">Test</media:category>
32
+ <media:content url="http://www.youtube.com/v/AbC123DeFgH?f=videos&amp;app=youtube_gdata" type="application/x-shockwave-flash" medium="video" isDefault="true" expression="full" duration="356" yt:format="5"/>
33
+ <media:content url="rtsp://v7.cache6.c.youtube.com/TESTCACHE/0/0/0/video.3gp" type="video/3gpp" medium="video" expression="full" duration="356" yt:format="1"/>
34
+ <media:content url="rtsp://v8.cache7.c.youtube.com/TESTCACHE/0/0/0/video.3gp" type="video/3gpp" medium="video" expression="full" duration="356" yt:format="6"/>
35
+ <media:credit role="uploader" scheme="urn:youtube" yt:type="partner">Test user</media:credit>
36
+ <media:description type="plain">Youtube Test Video</media:description>
37
+ <media:keywords>Test, Youtube, Youtube_it, Ruby</media:keywords>
38
+ <media:player url="http://www.youtube.com/watch?v=AbC123DeFgH&amp;feature=youtube_gdata_player"/>
39
+ <media:thumbnail url="http://i.ytimg.com/vi/AbC123DeFgH/default.jpg" height="90" width="120" time="00:02:58" yt:name="default"/>
40
+ <media:thumbnail url="http://i.ytimg.com/vi/AbC123DeFgH/hqdefault.jpg" height="360" width="480" yt:name="hqdefault"/>
41
+ <media:thumbnail url="http://i.ytimg.com/vi/AbC123DeFgH/1.jpg" height="90" width="120" time="00:01:29" yt:name="start"/>
42
+ <media:thumbnail url="http://i.ytimg.com/vi/AbC123DeFgH/2.jpg" height="90" width="120" time="00:02:58" yt:name="middle"/>
43
+ <media:thumbnail url="http://i.ytimg.com/vi/AbC123DeFgH/3.jpg" height="90" width="120" time="00:04:27" yt:name="end"/>
44
+ <media:title type="plain">Ich sterbe für YouTube</media:title>
45
+ <yt:aspectRatio>widescreen</yt:aspectRatio>
46
+ <yt:duration seconds="356"/>
47
+ <yt:uploaded>2010-12-29T13:57:49.000Z</yt:uploaded>
48
+ <yt:videoid>AbC123DeFgH</yt:videoid>
49
+ </media:group>
50
+ <gd:rating average="4.305027" max="5" min="1" numRaters="2049" rel="http://schemas.google.com/g/2005#overall"/>
51
+ <yt:statistics favoriteCount="200" viewCount="240000"/>
52
+ <yt:rating numDislikes="350" numLikes="1700"/>
53
+ </entry>
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'date'
3
+ require 'test/unit'
4
+ require 'pp'
5
+ require 'open-uri'
6
+ require File.dirname(__FILE__) + '/../lib/youtube_it'
7
+
8
+ YouTubeIt.logger.level = Logger::ERROR
9
+
Binary file
@@ -0,0 +1,63 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/helper')
2
+
3
+ class TestChainIO < Test::Unit::TestCase
4
+ def setup
5
+ @klass = YouTubeIt::ChainIO # save typing
6
+ end
7
+
8
+ def test_should_support_read_from_one_io
9
+ io = @klass.new "abcd"
10
+ assert io.respond_to?(:read)
11
+ assert_equal "ab", io.read(2)
12
+ assert_equal "cd", io.read(2)
13
+ assert_equal false, io.read(2)
14
+ end
15
+
16
+ def test_should_skip_over_depleted_streams
17
+ io = @klass.new '', '', '', '', 'ab'
18
+ assert_equal 'ab', io.read(2)
19
+ end
20
+
21
+ def test_should_read_across_nultiple_streams_with_large_offset
22
+ io = @klass.new 'abc', '', 'def', '', 'ghij'
23
+ assert_equal 'abcdefgh', io.read(8)
24
+ end
25
+
26
+ def test_should_return_false_for_empty_items
27
+ io = @klass.new '', '', '', '', ''
28
+ assert_equal false, io.read(8)
29
+ end
30
+
31
+ def test_should_support_overzealous_read
32
+ io = @klass.new "ab"
33
+ assert_equal "ab", io.read(5000)
34
+ end
35
+
36
+ def test_should_predict_expected_length
37
+ io = @klass.new "ab", "cde"
38
+ assert_equal 5, io.expected_length
39
+ end
40
+
41
+ def test_should_predict_expected_length_with_prepositioned_io
42
+ first_buf = StringIO.new("ab")
43
+ first_buf.read(1)
44
+
45
+ io = @klass.new first_buf, "cde"
46
+ assert_equal 4, io.expected_length
47
+ end
48
+
49
+ def test_should_predict_expected_length_with_file_handle
50
+ test_size = File.size(__FILE__)
51
+ first_buf = StringIO.new("ab")
52
+ first_buf.read(1)
53
+
54
+ io = @klass.new File.open(__FILE__), first_buf
55
+ assert_equal test_size + 1, io.expected_length
56
+ end
57
+
58
+ def test_greedy
59
+ io = YouTubeIt::GreedyChainIO.new("a" * (1024 * 513))
60
+ chunk = io.read(123)
61
+ assert_equal 1024 * 512, chunk.length, "Should have read the first 512 KB chunk at once instead"
62
+ end
63
+ end
@@ -0,0 +1,504 @@
1
+ #encoding: utf-8
2
+
3
+ require File.expand_path(File.dirname(__FILE__) + '/helper')
4
+
5
+ class TestClient < Test::Unit::TestCase
6
+
7
+ OPTIONS = {:title => "test title",
8
+ :description => "test description",
9
+ :category => 'People',
10
+ :keywords => %w[test]}
11
+ ACCOUNT = {:user => "tubeit20101", :passwd => "youtube_it", :dev_key => "AI39si411VBmO4Im9l0rfRsORXDI6F5AX5NlTIA4uHSWqa-Cgf-jUQG-6osUBB3PTLawLHlkKXPLr3B0pNcGU9wkNd11gIgdPg" }
12
+ RAILS_ENV = "test"
13
+
14
+ #oauth
15
+ KEY = "youtube-it.heroku.com"
16
+ SECRET = "6dghuose3hl-oC_04BKPXCej"
17
+
18
+ def setup
19
+ #clientlogin
20
+ #@client = YouTubeIt::Client.new(:username => ACCOUNT[:user], :password => ACCOUNT[:passwd] , :dev_key => ACCOUNT[:dev_key])
21
+ #authsub
22
+ #@client = YouTubeIt::AuthSubClient.new(:token => "1/vqYlJytmn4eWRjJnORHT94mENNfZzZsLutMOrvvygB4" , :dev_key => ACCOUNT[:dev_key])
23
+ #oauth
24
+ # @client = YouTubeIt::OAuthClient.new(:consumer_key => KEY, :consumer_secret => SECRET, :dev_key => "AI39si7WuZZxAkYebKSyrlJR7hIFktt6OoPycEOeOT_yHkZgr6QsGbZgmhKvbS4bsSAv0utgrfhNfXQBITu1wX_z3VsZE02giQ")
25
+ # @client.authorize_from_access("1/cLF_PRBYpyEY5KBhlsECv-g_wfC2MjHluPMi1c12ChI","DCyZxb1hHPd5jQCZUUZ6WHcz")
26
+ #oauth2
27
+ @client = YouTubeIt::OAuth2Client.new(:client_access_token => "ya29.AHES6ZScTtSAYx3xMRpF0DdKO6sJU2tnJBYa58P-FG-IhIpjloowYw", :client_id => "68330730158.apps.googleusercontent.com", :client_secret => "Npj4rmtme7q6INPPQjpQFuCZ", :client_refresh_token => "1/HzXbuoUO9iK-9kwJc7pAk54nxOCQiuCWre95YgLi1Dc", :dev_key => ACCOUNT[:dev_key])
28
+ @client.refresh_access_token!
29
+ end
30
+
31
+ def test_should_respond_to_a_basic_query
32
+ response = @client.videos_by(:query => "penguin")
33
+ assert_equal "tag:youtube.com,2008:videos", response.feed_id
34
+ assert_equal 25, response.max_result_count
35
+ assert_equal 25, response.videos.length
36
+ assert_equal 1, response.offset
37
+ assert(response.total_result_count > 100)
38
+ assert_instance_of Time, response.updated_at
39
+
40
+ response.videos.each { |v| assert_valid_video v }
41
+ end
42
+
43
+ def test_should_respond_to_a_basic_query_with_offset_and_max_results
44
+ response = @client.videos_by(:query => "penguin", :offset => 15, :max_results => 30)
45
+
46
+ assert_equal "tag:youtube.com,2008:videos", response.feed_id
47
+ assert_equal 30, response.max_result_count
48
+ assert_equal 30, response.videos.length
49
+ assert_equal 15, response.offset
50
+ assert(response.total_result_count > 100)
51
+ assert_instance_of Time, response.updated_at
52
+
53
+ response.videos.each { |v| assert_valid_video v }
54
+ end
55
+
56
+ def test_should_respond_to_a_basic_query_with_paging
57
+ response = @client.videos_by(:query => "penguin")
58
+ assert_equal "tag:youtube.com,2008:videos", response.feed_id
59
+ assert_equal 25, response.max_result_count
60
+ assert_equal 1, response.offset
61
+
62
+ response = @client.videos_by(:query => "penguin", :page => 2)
63
+ assert_equal "tag:youtube.com,2008:videos", response.feed_id
64
+ assert_equal 25, response.max_result_count
65
+ assert_equal 26, response.offset
66
+
67
+ response2 = @client.videos_by(:query => "penguin", :page => 3)
68
+ assert_equal "tag:youtube.com,2008:videos", response2.feed_id
69
+ assert_equal 25, response2.max_result_count
70
+ assert_equal 51, response2.offset
71
+ end
72
+
73
+ def test_should_get_videos_for_multiword_metasearch_query
74
+ response = @client.videos_by(:query => 'christina ricci')
75
+
76
+ assert_equal "tag:youtube.com,2008:videos", response.feed_id
77
+ assert_equal 25, response.max_result_count
78
+ assert_equal 25, response.videos.length
79
+ assert_equal 1, response.offset
80
+ assert(response.total_result_count > 100)
81
+ assert_instance_of Time, response.updated_at
82
+
83
+ response.videos.each { |v| assert_valid_video v }
84
+ end
85
+
86
+ def test_should_handle_video_not_yet_viewed
87
+ response = @client.videos_by(:query => "CE62FSEoY28")
88
+
89
+ assert_equal 1, response.videos.length
90
+ response.videos.each { |v| assert_valid_video v }
91
+ end
92
+
93
+ def test_should_get_videos_for_one_tag
94
+ response = @client.videos_by(:tags => ['panther'])
95
+ response.videos.each { |v| assert_valid_video v }
96
+ end
97
+
98
+ def test_should_get_videos_for_multiple_tags
99
+ response = @client.videos_by(:tags => ['tiger', 'leopard'])
100
+ response.videos.each { |v| assert_valid_video v }
101
+ end
102
+
103
+ def test_should_get_videos_for_one_category
104
+ response = @client.videos_by(:categories => [:news])
105
+ response.videos.each { |v| assert_valid_video v }
106
+ end
107
+
108
+ def test_should_get_videos_for_multiple_categories
109
+ response = @client.videos_by(:categories => [:news, :sports])
110
+ response.videos.each { |v| assert_valid_video v }
111
+ end
112
+
113
+ # TODO: Need to do more specific checking in these tests
114
+ # Currently, if a URL is valid, and videos are found, the test passes regardless of search criteria
115
+ def test_should_get_videos_for_categories_and_tags
116
+ response = @client.videos_by(:categories => [:news, :sports], :tags => ['soccer', 'football'])
117
+ response.videos.each { |v| assert_valid_video v }
118
+ end
119
+
120
+ def test_should_get_most_viewed_videos
121
+ response = @client.videos_by(:most_viewed)
122
+ response.videos.each { |v| assert_valid_video v }
123
+ end
124
+
125
+ def test_should_get_top_rated_videos_for_today
126
+ response = @client.videos_by(:top_rated, :time => :today)
127
+ response.videos.each { |v| assert_valid_video v }
128
+ end
129
+
130
+ def test_should_get_videos_for_categories_and_tags_with_category_boolean_operators
131
+ response = @client.videos_by(:categories => { :either => [:news, :sports], :exclude => [:comedy] },
132
+ :tags => { :include => ['football'], :exclude => ['soccer'] })
133
+ response.videos.each { |v| assert_valid_video v }
134
+ end
135
+
136
+ def test_should_get_videos_for_categories_and_tags_with_tag_boolean_operators
137
+ response = @client.videos_by(:categories => { :either => [:news, :sports], :exclude => [:comedy] },
138
+ :tags => { :either => ['football', 'soccer', 'polo'] })
139
+ response.videos.each { |v| assert_valid_video v }
140
+ end
141
+
142
+ def test_should_get_videos_by_user
143
+ response = @client.videos_by(:user => 'liz')
144
+ response.videos.each { |v| assert_valid_video v }
145
+ end
146
+
147
+ def test_should_get_videos_by_user_with_pagination_and_ordering
148
+ response = @client.videos_by(:user => 'liz', :page => 2, :per_page => '2', :order_by => 'published')
149
+ response.videos.each { |v| assert_valid_video v }
150
+ assert_equal 3, response.offset
151
+ assert_equal 2, response.max_result_count
152
+ end
153
+
154
+
155
+ def test_should_get_favorite_videos_by_user
156
+ response = @client.videos_by(:favorites, :user => 'drnicwilliams')
157
+ assert_equal "tag:youtube.com,2008:user:drnicwilliams:favorites", response.feed_id
158
+ assert_valid_video response.videos.first
159
+ end
160
+
161
+ def test_should_get_videos_for_query_search_with_categories_excluded
162
+ video = @client.video_by("EkF4JD2rO3Q")
163
+ assert_equal "<object width=\"425\" height=\"350\">\n <param name=\"movie\" value=\"http://www.youtube.com/v/EkF4JD2rO3Q&feature=youtube_gdata_player\"></param>\n <param name=\"wmode\" value=\"transparent\"></param>\n <embed src=\"http://www.youtube.com/v/EkF4JD2rO3Q&feature=youtube_gdata_player\" type=\"application/x-shockwave-flash\"\n wmode=\"transparent\" width=\"425\" height=\"350\"></embed>\n</object>\n", video.embed_html
164
+ assert_valid_video video
165
+ end
166
+
167
+ def test_should_get_video_from_user
168
+ video = @client.video_by_user("chebyte","FQK1URcxmb4")
169
+ assert_equal "<object width=\"425\" height=\"350\">\n <param name=\"movie\" value=\"http://www.youtube.com/v/FQK1URcxmb4&feature=youtube_gdata_player\"></param>\n <param name=\"wmode\" value=\"transparent\"></param>\n <embed src=\"http://www.youtube.com/v/FQK1URcxmb4&feature=youtube_gdata_player\" type=\"application/x-shockwave-flash\"\n wmode=\"transparent\" width=\"425\" height=\"350\"></embed>\n</object>\n", video.embed_html
170
+ assert_valid_video video
171
+ end
172
+
173
+ def test_should_get_embed_video_for_html5
174
+ video = @client.video_by_user("chebyte","FQK1URcxmb4")
175
+ embed_html5 = video.embed_html5({:class => 'video-player', :id => 'my-video', :width => '425', :height => '350', :frameborder => '1', :url_params => {:option => "value"}})
176
+ assert_equal "<iframe class=\"video-player\" id=\"my-video\" type=\"text/html\" width=\"425\" height=\"350\" src=\"http://www.youtube.com/embed/FQK1URcxmb4?option=value\" frameborder=\"1\"></iframe>\n", embed_html5
177
+ end
178
+
179
+
180
+ def test_should_always_return_a_logger
181
+ @client = YouTubeIt::Client.new
182
+ assert_not_nil @client.logger
183
+ end
184
+
185
+ def test_should_not_bail_if_debug_is_true
186
+ assert_nothing_raised { YouTubeIt::Client.new(:debug => true) }
187
+ end
188
+
189
+ def test_should_determine_if_embeddable_video_is_embeddable
190
+ response = @client.videos_by(:query => "strongbad")
191
+
192
+ video = response.videos.first
193
+ assert video.embeddable?
194
+ end
195
+
196
+ def test_should_retrieve_video_by_id
197
+ video = @client.video_by("http://gdata.youtube.com/feeds/videos/EkF4JD2rO3Q")
198
+ assert_valid_video video
199
+
200
+ video = @client.video_by("EkF4JD2rO3Q")
201
+ assert_valid_video video
202
+ end
203
+
204
+ def test_return_upload_info_for_upload_from_browser
205
+ response = @client.upload_token(OPTIONS)
206
+ assert response.kind_of?(Hash)
207
+ assert_equal response.size, 2
208
+ response.each do |k,v|
209
+ assert v
210
+ end
211
+ end
212
+
213
+ def test_should_upload_a_video
214
+ video = @client.video_upload(File.open("test/test.mov"), OPTIONS)
215
+ assert_valid_video video
216
+ @client.video_delete(video.unique_id)
217
+ end
218
+
219
+ def test_should_update_a_video
220
+ OPTIONS[:title] = "title changed"
221
+ video = @client.video_upload(File.open("test/test.mov"), OPTIONS)
222
+ updated_video = @client.video_update(video.unique_id, OPTIONS)
223
+ assert updated_video.title == "title changed"
224
+ @client.video_delete(video.unique_id)
225
+ end
226
+
227
+ def test_should_delete_video
228
+ video = @client.video_upload(File.open("test/test.mov"), OPTIONS)
229
+ assert_valid_video video
230
+ assert @client.video_delete(video.unique_id)
231
+ end
232
+
233
+ def test_should_denied_comments
234
+ video = @client.video_upload(File.open("test/test.mov"), OPTIONS.merge(:comment => "denied"))
235
+ assert_valid_video video
236
+ doc = open("http://www.youtube.com/watch?v=#{video.unique_id}").read
237
+ assert "Adding comments has been disabled for this video.", doc.match("Adding comments has been disabled for this video.")[0]
238
+ @client.video_delete(video.unique_id)
239
+ end
240
+
241
+ def test_should_denied_rate
242
+ video = @client.video_upload(File.open("test/test.mov"), OPTIONS.merge(:rate => "denied"))
243
+ assert_valid_video video
244
+ doc = open("http://www.youtube.com/watch?v=#{video.unique_id}").read
245
+ assert "Ratings have been disabled for this video.", doc.match("Ratings have been disabled for this video.")[0]
246
+ @client.video_delete(video.unique_id)
247
+ end
248
+
249
+ def test_should_denied_embed
250
+ video = @client.video_upload(File.open("test/test.mov"), OPTIONS.merge(:embed => "denied"))
251
+ assert video.noembed
252
+ @client.video_delete(video.unique_id)
253
+ end
254
+
255
+
256
+ def test_should_add_new_comment
257
+ video = @client.video_upload(File.open("test/test.mov"), OPTIONS)
258
+ @client.add_comment(video.unique_id, "test comment")
259
+ comment = @client.comments(video.unique_id).first.content
260
+ assert comment, "test comment"
261
+ @client.video_delete(video.unique_id)
262
+ end
263
+
264
+ def test_should_add_and_delete_video_from_playlist
265
+ begin
266
+ playlist = @client.add_playlist(:title => "youtube_it test!", :description => "test playlist")
267
+ rescue
268
+ @client.playlists.each{|p| @client.delete_playlist(p.playlist_id)}
269
+ playlist = @client.add_playlist(:title => "youtube_it test!", :description => "test playlist")
270
+ end
271
+ video = @client.add_video_to_playlist(playlist.playlist_id,"CE62FSEoY28")
272
+ assert_equal video[:code].to_i, 201
273
+ assert @client.delete_video_from_playlist(playlist.playlist_id, video[:playlist_entry_id])
274
+ assert @client.delete_playlist(playlist.playlist_id)
275
+ end
276
+
277
+ def test_should_return_unique_id_from_playlist
278
+ begin
279
+ playlist = @client.add_playlist(:title => "youtube_it test!", :description => "test playlist")
280
+ rescue
281
+ @client.playlists.each{|p| @client.delete_playlist(p.playlist_id)}
282
+ playlist = @client.add_playlist(:title => "youtube_it test!", :description => "test playlist")
283
+ end
284
+ video = @client.add_video_to_playlist(playlist.playlist_id,"CE62FSEoY28")
285
+
286
+ assert_equal "CE62FSEoY28", playlist.videos.last.unique_id
287
+
288
+ assert @client.delete_video_from_playlist(playlist.playlist_id, video[:playlist_entry_id])
289
+ assert @client.delete_playlist(playlist.playlist_id)
290
+ end
291
+
292
+ def test_should_add_and_delete_new_playlist
293
+ result = @client.add_playlist(:title => "youtube_it test4!", :description => "test playlist")
294
+ assert result.title, "youtube_it test!"
295
+ sleep 4
296
+ assert @client.delete_playlist(result.playlist_id)
297
+ end
298
+
299
+ def test_should_update_playlist
300
+ begin
301
+ playlist = @client.add_playlist(:title => "youtube_it test!", :description => "test playlist")
302
+ rescue
303
+ @client.playlists.each{|p| @client.delete_playlist(p.playlist_id)}
304
+ playlist = @client.add_playlist(:title => "youtube_it test!", :description => "test playlist")
305
+ end
306
+ playlist_updated = @client.update_playlist(playlist.playlist_id, :title => "title changed")
307
+ assert_equal playlist_updated.title, "title changed"
308
+ assert @client.delete_playlist(playlist.playlist_id)
309
+ end
310
+
311
+ def test_should_list_playlist_for_user
312
+ result = @client.playlists('chebyte')
313
+ assert_equal "rock", result.last.title
314
+ end
315
+
316
+ def test_should_determine_if_widescreen_video_is_widescreen
317
+ widescreen_id = 'QqQVll-MP3I'
318
+
319
+ video = @client.video_by(widescreen_id)
320
+ assert video.widescreen?
321
+ end
322
+
323
+ def test_get_current_user
324
+ assert_equal 'tubeit20101', @client.current_user
325
+ end
326
+
327
+ def test_should_get_my_videos
328
+ video = @client.video_upload(File.open("test/test.mov"), OPTIONS)
329
+ assert_valid_video video
330
+ result = @client.my_videos
331
+ assert_equal result.videos.first.unique_id, video.unique_id
332
+ assert_not_nil result.videos.first.insight_uri, 'insight data not present'
333
+ ensure
334
+ @client.video_delete(video.unique_id)
335
+ end
336
+
337
+ def test_should_get_my_video
338
+ video = @client.video_upload(File.open("test/test.mov"), OPTIONS)
339
+ assert_valid_video video
340
+ result = @client.my_video(video.unique_id)
341
+ assert_equal result.unique_id, video.unique_id
342
+ @client.video_delete(video.unique_id)
343
+ end
344
+
345
+ def test_should_add_like_to_video
346
+ r = @client.like_video("CE62FSEoY28")
347
+ assert_equal r[:code], 201
348
+ @client.dislike_video("CE62FSEoY28")
349
+ end
350
+
351
+ def test_should_dislike_to_video
352
+ @client.like_video("CE62FSEoY28")
353
+ r = @client.dislike_video("CE62FSEoY28")
354
+ assert_equal r[:code], 201
355
+ end
356
+
357
+
358
+ def test_should_subscribe_to_channel
359
+ r = @client.subscribe_channel("TheWoWArthas")
360
+ sleep(4)
361
+ assert_equal r[:code], 201
362
+ assert_equal @client.subscriptions.first.title, "Videos published by: TheWoWArthas"
363
+ @client.unsubscribe_channel(@client.subscriptions.first.id)
364
+ end
365
+
366
+ def test_should_unsubscribe_to_channel
367
+ @client.subscribe_channel("TheWoWArthas")
368
+ sleep(4)
369
+ r = @client.unsubscribe_channel(@client.subscriptions.first.id)
370
+ assert_equal r[:code], 200
371
+ end
372
+
373
+ def test_should_list_subscriptions
374
+ @client.subscribe_channel("TheWoWArthas")
375
+ sleep(4)
376
+ assert @client.subscriptions.count == 1
377
+ assert_equal @client.subscriptions.first.title, "Videos published by: TheWoWArthas"
378
+ @client.unsubscribe_channel(@client.subscriptions.first.id)
379
+ end
380
+
381
+ def test_should_get_profile
382
+ profile = @client.profile
383
+ assert_equal profile.username, "tubeit20101"
384
+ assert_not_nil profile.insight_uri, 'user insight_uri nil'
385
+ end
386
+
387
+ def test_should_add_and_delete_video_to_favorite
388
+ video_id ="j5raG94IGCc"
389
+ begin
390
+ result = @client.add_favorite(video_id)
391
+ rescue
392
+ @client.delete_favorite(video_id)
393
+ result = @client.add_favorite(video_id)
394
+ end
395
+ assert_equal result[:code], 201
396
+ sleep 4
397
+ assert @client.delete_favorite(video_id)
398
+ end
399
+
400
+ def test_esc
401
+ result = YouTubeIt.esc("спят усталые игрушки")
402
+ assert_equal "%D1%81%D0%BF%D1%8F%D1%82+%D1%83%D1%81%D1%82%D0%B0%D0%BB%D1%8B%D0%B5+%D0%B8%D0%B3%D1%80%D1%83%D1%88%D0%BA%D0%B8", result
403
+ end
404
+
405
+ def test_should_encode_ampersand
406
+ result = YouTubeIt.esc("such & such")
407
+ assert_equal "such+%26+such", result
408
+ end
409
+
410
+ def test_unicode_query
411
+ videos = @client.videos_by(:query => 'спят усталые игрушки').videos
412
+ assert videos.map(&:unique_id).include?("w-7BT2CFYNU")
413
+ end
414
+
415
+ def test_return_video_by_url
416
+ video = @client.video_by("https://www.youtube.com/watch?v=EkF4JD2rO3Q")
417
+ assert_valid_video video
418
+ end
419
+
420
+ def test_configure_faraday_adapter
421
+ assert YouTubeIt.adapter == Faraday.default_adapter
422
+ YouTubeIt.adapter = :net_http
423
+ assert YouTubeIt.adapter == :net_http
424
+ end
425
+
426
+ def test_safe_search_params
427
+ @videos = @client.videos_by(:query => "porno", :safe_search => 'none').videos
428
+ assert_equal @videos.count, 25
429
+ @videos = @client.videos_by(:query => "porno", :safe_search => 'strict').videos
430
+ assert_equal @videos.count, 0
431
+ end
432
+
433
+ private
434
+
435
+ def assert_valid_video (video)
436
+ # check general attributes
437
+ assert_instance_of YouTubeIt::Model::Video, video
438
+ assert_instance_of Fixnum, video.duration
439
+ assert_instance_of String, video.html_content if video.html_content
440
+
441
+ # validate media content records
442
+ video.media_content.each do |media_content|
443
+ assert_valid_url media_content.url
444
+ assert_instance_of YouTubeIt::Model::Video::Format, media_content.format
445
+ assert_instance_of String, media_content.mime_type
446
+ assert_match(/^[^\/]+\/[^\/]+$/, media_content.mime_type)
447
+ end
448
+
449
+ default_content = video.default_media_content
450
+ if default_content
451
+ assert_instance_of YouTubeIt::Model::Content, default_content
452
+ assert default_content.is_default?
453
+ end
454
+
455
+ # validate keywords
456
+ video.keywords.each { |kw| assert_instance_of(String, kw) }
457
+
458
+ # http://www.youtube.com/watch?v=IHVaXG1thXM
459
+ assert_valid_url video.player_url
460
+ assert_instance_of Time, video.published_at
461
+
462
+ # validate optionally-present rating
463
+ if video.rating
464
+ assert_instance_of YouTubeIt::Model::Rating, video.rating
465
+ assert_instance_of Float, video.rating.average
466
+ assert_instance_of Fixnum, video.rating.max
467
+ assert_instance_of Fixnum, video.rating.min
468
+ assert_instance_of Fixnum, video.rating.rater_count
469
+ end
470
+
471
+ # validate thumbnails
472
+ assert(video.thumbnails.size > 0)
473
+
474
+ assert_not_nil video.title
475
+ assert_instance_of String, video.title
476
+ assert(video.title.length > 0)
477
+
478
+ assert_instance_of Time, video.updated_at
479
+ # http://gdata.youtube.com/feeds/videos/IHVaXG1thXM
480
+ assert_valid_url video.unique_id
481
+ assert_instance_of Fixnum, video.view_count
482
+ assert_instance_of Fixnum, video.favorite_count
483
+
484
+ # validate author
485
+ assert_instance_of YouTubeIt::Model::Author, video.author
486
+ assert_instance_of String, video.author.name
487
+ assert(video.author.name.length > 0)
488
+ assert_valid_url video.author.uri
489
+
490
+ # validate categories
491
+ video.categories.each do |cat|
492
+ assert_instance_of YouTubeIt::Model::Category, cat
493
+ assert_instance_of String, cat.label
494
+ assert_instance_of String, cat.term
495
+ end
496
+ end
497
+
498
+ def assert_valid_url (url)
499
+ URI::parse(url)
500
+ return true
501
+ rescue
502
+ return false
503
+ end
504
+ end