pius-youtube-g 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ class YouTubeG
2
+ VERSION = '0.5.1'
3
+ end
@@ -0,0 +1,6 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'pp'
4
+ require File.dirname(__FILE__) + '/../lib/youtube_g'
5
+
6
+ YouTubeG.logger.level = Logger::ERROR
@@ -0,0 +1,63 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class TestChainIO < Test::Unit::TestCase
4
+ def setup
5
+ @klass = YouTubeG::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 = YouTubeG::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,263 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class TestClient < Test::Unit::TestCase
4
+ def setup
5
+ @client = YouTubeG::Client.new
6
+ end
7
+
8
+ def test_should_respond_to_a_basic_query
9
+ response = @client.videos_by(:query => "penguin")
10
+
11
+ assert_equal "http://gdata.youtube.com/feeds/api/videos", response.feed_id
12
+ assert_equal 25, response.max_result_count
13
+ assert_equal 25, response.videos.length
14
+ assert_equal 1, response.offset
15
+ assert(response.total_result_count > 100)
16
+ assert_instance_of Time, response.updated_at
17
+
18
+ response.videos.each { |v| assert_valid_video v }
19
+ end
20
+
21
+ def test_should_respond_to_a_basic_query_with_offset_and_max_results
22
+ response = @client.videos_by(:query => "penguin", :offset => 15, :max_results => 30)
23
+
24
+ assert_equal "http://gdata.youtube.com/feeds/api/videos", response.feed_id
25
+ assert_equal 30, response.max_result_count
26
+ assert_equal 30, response.videos.length
27
+ assert_equal 15, response.offset
28
+ assert(response.total_result_count > 100)
29
+ assert_instance_of Time, response.updated_at
30
+
31
+ response.videos.each { |v| assert_valid_video v }
32
+ end
33
+
34
+ def test_should_respond_to_a_basic_query_with_paging
35
+ response = @client.videos_by(:query => "penguin")
36
+ assert_equal "http://gdata.youtube.com/feeds/api/videos", response.feed_id
37
+ assert_equal 25, response.max_result_count
38
+ assert_equal 1, response.offset
39
+
40
+ response = @client.videos_by(:query => "penguin", :page => 2)
41
+ assert_equal "http://gdata.youtube.com/feeds/api/videos", response.feed_id
42
+ assert_equal 25, response.max_result_count
43
+ assert_equal 26, response.offset
44
+
45
+ response2 = @client.videos_by(:query => "penguin", :page => 3)
46
+ assert_equal "http://gdata.youtube.com/feeds/api/videos", response2.feed_id
47
+ assert_equal 25, response2.max_result_count
48
+ assert_equal 51, response2.offset
49
+ end
50
+
51
+ def test_should_get_videos_for_multiword_metasearch_query
52
+ response = @client.videos_by(:query => 'christina ricci')
53
+
54
+ assert_equal "http://gdata.youtube.com/feeds/api/videos", response.feed_id
55
+ assert_equal 25, response.max_result_count
56
+ assert_equal 25, response.videos.length
57
+ assert_equal 1, response.offset
58
+ assert(response.total_result_count > 100)
59
+ assert_instance_of Time, response.updated_at
60
+
61
+ response.videos.each { |v| assert_valid_video v }
62
+ end
63
+
64
+ def test_should_handle_video_not_yet_viewed
65
+ response = @client.videos_by(:query => "YnqHZDh_t2Q")
66
+
67
+ assert_equal 1, response.videos.length
68
+ response.videos.each { |v| assert_valid_video v }
69
+ end
70
+
71
+ # TODO: this doesn't work because the returned feed is in an unknown format
72
+ # def test_should_get_video_for_search_by_video_id
73
+ # response = @client.videos_by(:video_id => "T7YazwP8GtY")
74
+ # response.videos.each { |v| assert_valid_video v }
75
+ # end
76
+
77
+ def test_should_get_videos_for_one_tag
78
+ response = @client.videos_by(:tags => ['panther'])
79
+ response.videos.each { |v| assert_valid_video v }
80
+ end
81
+
82
+ def test_should_get_videos_for_multiple_tags
83
+ response = @client.videos_by(:tags => ['tiger', 'leopard'])
84
+ response.videos.each { |v| assert_valid_video v }
85
+ end
86
+
87
+ def test_should_get_videos_for_one_category
88
+ response = @client.videos_by(:categories => [:news])
89
+ response.videos.each { |v| assert_valid_video v }
90
+ end
91
+
92
+ def test_should_get_videos_for_multiple_categories
93
+ response = @client.videos_by(:categories => [:news, :sports])
94
+ response.videos.each { |v| assert_valid_video v }
95
+ end
96
+
97
+ # TODO: Need to do more specific checking in these tests
98
+ # Currently, if a URL is valid, and videos are found, the test passes regardless of search criteria
99
+ def test_should_get_videos_for_categories_and_tags
100
+ response = @client.videos_by(:categories => [:news, :sports], :tags => ['soccer', 'football'])
101
+ response.videos.each { |v| assert_valid_video v }
102
+ end
103
+
104
+ def test_should_get_most_viewed_videos
105
+ response = @client.videos_by(:most_viewed)
106
+ response.videos.each { |v| assert_valid_video v }
107
+ end
108
+
109
+ def test_should_get_top_rated_videos_for_today
110
+ response = @client.videos_by(:top_rated, :time => :today)
111
+ response.videos.each { |v| assert_valid_video v }
112
+ end
113
+
114
+ def test_should_get_videos_for_categories_and_tags_with_category_boolean_operators
115
+ response = @client.videos_by(:categories => { :either => [:news, :sports], :exclude => [:comedy] },
116
+ :tags => { :include => ['football'], :exclude => ['soccer'] })
117
+ response.videos.each { |v| assert_valid_video v }
118
+ end
119
+
120
+ def test_should_get_videos_for_categories_and_tags_with_tag_boolean_operators
121
+ response = @client.videos_by(:categories => { :either => [:news, :sports], :exclude => [:comedy] },
122
+ :tags => { :either => ['football', 'soccer', 'polo'] })
123
+ response.videos.each { |v| assert_valid_video v }
124
+ end
125
+
126
+ def test_should_get_videos_by_user
127
+ response = @client.videos_by(:user => 'liz')
128
+ response.videos.each { |v| assert_valid_video v }
129
+ end
130
+
131
+ def test_should_get_videos_by_user_with_pagination_and_ordering
132
+ response = @client.videos_by(:user => 'liz', :page => 2, :per_page => '2', :order_by => 'published')
133
+ response.videos.each { |v| assert_valid_video v }
134
+ assert_equal 3, response.offset
135
+ assert_equal 2, response.max_result_count
136
+ end
137
+
138
+ # HTTP 403 Error
139
+ # def test_should_get_favorite_videos_by_user
140
+ # response = @client.videos_by(:favorites, :user => 'liz')
141
+ # response.videos.each { |v| assert_valid_video v }
142
+ # end
143
+
144
+ def test_should_get_favorite_videos_by_user
145
+ response = @client.videos_by(:favorites, :user => 'drnicwilliams')
146
+ assert_equal "http://gdata.youtube.com/feeds/api/users/drnicwilliams/favorites", response.feed_id
147
+ response.videos.each { |v| assert_valid_video v }
148
+ end
149
+
150
+ def test_should_get_videos_for_query_search_with_categories_excluded
151
+ video = @client.video_by("EkF4JD2rO3Q")
152
+ assert_equal "<object width=\"425\" height=\"350\">\n <param name=\"movie\" value=\"http://www.youtube.com/v/EkF4JD2rO3Q\"></param>\n <param name=\"wmode\" value=\"transparent\"></param>\n <embed src=\"http://www.youtube.com/v/EkF4JD2rO3Q\" type=\"application/x-shockwave-flash\" \n wmode=\"transparent\" width=\"425\" height=\"350\"></embed>\n</object>\n", video.embed_html
153
+ assert_valid_video video
154
+ end
155
+
156
+ def test_should_always_return_a_logger
157
+ @client = YouTubeG::Client.new
158
+ assert_not_nil @client.logger
159
+ end
160
+
161
+ def test_should_not_bail_if_debug_is_true
162
+ assert_nothing_raised { YouTubeG::Client.new(true) }
163
+ end
164
+
165
+ def test_should_determine_if_nonembeddable_video_is_embeddable
166
+ response = @client.videos_by(:query => "avril lavigne girlfriend")
167
+
168
+ video = response.videos.first
169
+ assert !video.embeddable?
170
+ end
171
+
172
+ def test_should_determine_if_embeddable_video_is_embeddable
173
+ response = @client.videos_by(:query => "strongbad")
174
+
175
+ video = response.videos.first
176
+ assert video.embeddable?
177
+ end
178
+
179
+ def test_should_retrieve_video_by_id
180
+ video = @client.video_by("http://gdata.youtube.com/feeds/videos/EkF4JD2rO3Q")
181
+ assert_valid_video video
182
+
183
+ video = @client.video_by("EkF4JD2rO3Q")
184
+ assert_valid_video video
185
+ end
186
+
187
+ private
188
+
189
+ def assert_valid_video (video)
190
+ # pp video
191
+
192
+ # check general attributes
193
+ assert_instance_of YouTubeG::Model::Video, video
194
+ assert_instance_of Fixnum, video.duration
195
+ assert(video.duration > 0)
196
+ #assert_match(/^<div style=.*?<\/div>/m, video.html_content)
197
+ assert_instance_of String, video.html_content
198
+
199
+ # validate media content records
200
+ video.media_content.each do |media_content|
201
+ # http://www.youtube.com/v/IHVaXG1thXM
202
+ assert_valid_url media_content.url
203
+ assert(media_content.duration > 0)
204
+ assert_instance_of YouTubeG::Model::Video::Format, media_content.format
205
+ assert_instance_of String, media_content.mime_type
206
+ assert_match(/^[^\/]+\/[^\/]+$/, media_content.mime_type)
207
+ end
208
+
209
+ default_content = video.default_media_content
210
+ if default_content
211
+ assert_instance_of YouTubeG::Model::Content, default_content
212
+ assert default_content.is_default?
213
+ end
214
+
215
+ # validate keywords
216
+ video.keywords.each { |kw| assert_instance_of(String, kw) }
217
+
218
+ # http://www.youtube.com/watch?v=IHVaXG1thXM
219
+ assert_valid_url video.player_url
220
+ assert_instance_of Time, video.published_at
221
+
222
+ # validate optionally-present rating
223
+ if video.rating
224
+ assert_instance_of YouTubeG::Model::Rating, video.rating
225
+ assert_instance_of Float, video.rating.average
226
+ assert_instance_of Fixnum, video.rating.max
227
+ assert_instance_of Fixnum, video.rating.min
228
+ assert_instance_of Fixnum, video.rating.rater_count
229
+ end
230
+
231
+ # validate thumbnails
232
+ assert(video.thumbnails.size > 0)
233
+
234
+ assert_not_nil video.title
235
+ assert_instance_of String, video.title
236
+ assert(video.title.length > 0)
237
+
238
+ assert_instance_of Time, video.updated_at
239
+ # http://gdata.youtube.com/feeds/videos/IHVaXG1thXM
240
+ assert_valid_url video.video_id
241
+ assert_instance_of Fixnum, video.view_count
242
+
243
+ # validate author
244
+ assert_instance_of YouTubeG::Model::Author, video.author
245
+ assert_instance_of String, video.author.name
246
+ assert(video.author.name.length > 0)
247
+ assert_valid_url video.author.uri
248
+
249
+ # validate categories
250
+ video.categories.each do |cat|
251
+ assert_instance_of YouTubeG::Model::Category, cat
252
+ assert_instance_of String, cat.label
253
+ assert_instance_of String, cat.term
254
+ end
255
+ end
256
+
257
+ def assert_valid_url (url)
258
+ URI::parse(url)
259
+ return true
260
+ rescue
261
+ return false
262
+ end
263
+ end
@@ -0,0 +1,38 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class TestVideo < Test::Unit::TestCase
4
+ def test_should_extract_unique_id_from_video_id
5
+ video = YouTubeG::Model::Video.new(:video_id => "http://gdata.youtube.com/feeds/videos/ZTUVgYoeN_o")
6
+ assert_equal "ZTUVgYoeN_o", video.unique_id
7
+ end
8
+
9
+ def test_should_extract_unique_id_with_hypen_from_video_id
10
+ video = YouTubeG::Model::Video.new(:video_id => "http://gdata.youtube.com/feeds/videos/BDqs-OZWw9o")
11
+ assert_equal "BDqs-OZWw9o", video.unique_id
12
+ end
13
+
14
+ def test_should_have_related_videos
15
+ video = YouTubeG::Model::Video.new(:video_id => "http://gdata.youtube.com/feeds/videos/BDqs-OZWw9o")
16
+ response = video.related
17
+
18
+ assert_equal "http://gdata.youtube.com/feeds/api/videos/BDqs-OZWw9o/related", response.feed_id
19
+ assert_equal 25, response.max_result_count
20
+ assert_equal 25, response.videos.length
21
+ assert_equal 1, response.offset
22
+ assert(response.total_result_count > 0)
23
+ assert_instance_of Time, response.updated_at
24
+ end
25
+
26
+ def test_should_have_response_videos
27
+ video = YouTubeG::Model::Video.new(:video_id => "http://gdata.youtube.com/feeds/videos/BDqs-OZWw9o")
28
+ response = video.responses
29
+
30
+ assert_equal "http://gdata.youtube.com/feeds/api/videos/BDqs-OZWw9o/responses", response.feed_id
31
+ assert_equal 25, response.max_result_count
32
+ assert_equal 25, response.videos.length
33
+ assert_equal 1, response.offset
34
+ assert(response.total_result_count > 0)
35
+ assert_instance_of Time, response.updated_at
36
+ end
37
+
38
+ end
@@ -0,0 +1,134 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class TestVideoSearch < Test::Unit::TestCase
4
+
5
+ def test_should_build_basic_query_url
6
+ request = YouTubeG::Request::VideoSearch.new(:query => "penguin")
7
+ assert_equal "http://gdata.youtube.com/feeds/api/videos?vq=penguin", request.url
8
+ end
9
+
10
+ def test_should_build_multiword_metasearch_query_url
11
+ request = YouTubeG::Request::VideoSearch.new(:query => 'christina ricci')
12
+ assert_equal "http://gdata.youtube.com/feeds/api/videos?vq=christina+ricci", request.url
13
+ end
14
+
15
+ def test_should_build_video_id_url
16
+ request = YouTubeG::Request::VideoSearch.new(:video_id => 'T7YazwP8GtY')
17
+ assert_equal "http://gdata.youtube.com/feeds/api/videos/T7YazwP8GtY", request.url
18
+ end
19
+
20
+ def test_should_build_one_tag_querl_url
21
+ request = YouTubeG::Request::VideoSearch.new(:tags => ['panther'])
22
+ assert_equal "http://gdata.youtube.com/feeds/api/videos/-/panther/", request.url
23
+ end
24
+
25
+ def test_should_build_multiple_tags_query_url
26
+ request = YouTubeG::Request::VideoSearch.new(:tags => ['tiger', 'leopard'])
27
+ assert_equal "http://gdata.youtube.com/feeds/api/videos/-/tiger/leopard/", request.url
28
+ end
29
+
30
+ def test_should_build_one_category_query_url
31
+ request = YouTubeG::Request::VideoSearch.new(:categories => [:news])
32
+ assert_equal "http://gdata.youtube.com/feeds/api/videos/-/News/", request.url
33
+ end
34
+
35
+ def test_should_build_multiple_categories_query_url
36
+ request = YouTubeG::Request::VideoSearch.new(:categories => [:news, :sports])
37
+ assert_equal "http://gdata.youtube.com/feeds/api/videos/-/News/Sports/", request.url
38
+ end
39
+
40
+ def test_should_build_categories_and_tags_query_url
41
+ request = YouTubeG::Request::VideoSearch.new(:categories => [:news, :sports], :tags => ['soccer', 'football'])
42
+ assert_equal "http://gdata.youtube.com/feeds/api/videos/-/News/Sports/soccer/football/", request.url
43
+ end
44
+
45
+ def test_should_build_categories_and_tags_url_with_max_results
46
+ request = YouTubeG::Request::VideoSearch.new(:categories => [:music], :tags => ['classic', 'rock'], :max_results => 2)
47
+ assert_equal "http://gdata.youtube.com/feeds/api/videos/-/Music/classic/rock/?max-results=2", request.url
48
+ end
49
+
50
+ def test_should_build_author_query_url
51
+ request = YouTubeG::Request::VideoSearch.new(:author => "davidguetta")
52
+ assert_equal "http://gdata.youtube.com/feeds/api/videos?author=davidguetta", request.url
53
+ end
54
+ # -- Standard Feeds --------------------------------------------------------------------------------
55
+
56
+ def test_should_build_url_for_most_viewed
57
+ request = YouTubeG::Request::StandardSearch.new(:most_viewed)
58
+ assert_equal "http://gdata.youtube.com/feeds/api/standardfeeds/most_viewed", request.url
59
+ end
60
+
61
+ def test_should_build_url_for_top_rated_for_today
62
+ request = YouTubeG::Request::StandardSearch.new(:top_rated, :time => :today)
63
+ assert_equal "http://gdata.youtube.com/feeds/api/standardfeeds/top_rated?time=today", request.url
64
+ end
65
+
66
+ def test_should_build_url_for_most_viewed_offset_and_max_results_without_time
67
+ request = YouTubeG::Request::StandardSearch.new(:top_rated, :offset => 5, :max_results => 10)
68
+ assert_equal "http://gdata.youtube.com/feeds/api/standardfeeds/top_rated?max-results=10&start-index=5", request.url
69
+ end
70
+
71
+ def test_should_build_url_for_most_viewed_offset_and_max_results_with_time
72
+ request = YouTubeG::Request::StandardSearch.new(:top_rated, :offset => 5, :max_results => 10, :time => :today)
73
+ assert_equal "http://gdata.youtube.com/feeds/api/standardfeeds/top_rated?max-results=10&start-index=5&time=today", request.url
74
+ end
75
+
76
+ def test_should_raise_exception_for_invalid_type
77
+ assert_raise RuntimeError do
78
+ request = YouTubeG::Request::StandardSearch.new(:most_viewed_yo)
79
+ end
80
+ end
81
+
82
+ # -- Complex Video Queries -------------------------------------------------------------------------
83
+
84
+ def test_should_build_url_for_boolean_or_case_for_categories
85
+ request = YouTubeG::Request::VideoSearch.new(:categories => { :either => [:news, :sports] })
86
+ assert_equal "http://gdata.youtube.com/feeds/api/videos/-/News%7CSports/", request.url
87
+ end
88
+
89
+ def test_should_build_url_for_boolean_or_and_exclude_case_for_categories
90
+ request = YouTubeG::Request::VideoSearch.new(:categories => { :either => [:news, :sports], :exclude => [:comedy] })
91
+ assert_equal "http://gdata.youtube.com/feeds/api/videos/-/News%7CSports/-Comedy/", request.url
92
+ end
93
+
94
+ def test_should_build_url_for_exclude_case_for_tags
95
+ request = YouTubeG::Request::VideoSearch.new(:categories => { :either => [:news, :sports], :exclude => [:comedy] },
96
+ :tags => { :include => ['football'], :exclude => ['soccer'] })
97
+ assert_equal "http://gdata.youtube.com/feeds/api/videos/-/News%7CSports/-Comedy/football/-soccer/", request.url
98
+ end
99
+
100
+ def test_should_build_url_for_either_case_for_tags
101
+ request = YouTubeG::Request::VideoSearch.new(:categories => { :either => [:news, :sports], :exclude => [:comedy] },
102
+ :tags => { :either => ['soccer', 'football', 'donkey'] })
103
+ assert_equal "http://gdata.youtube.com/feeds/api/videos/-/News%7CSports/-Comedy/soccer%7Cfootball%7Cdonkey/", request.url
104
+ end
105
+
106
+ def test_should_build_url_for_query_search_with_categories_excluded
107
+ request = YouTubeG::Request::VideoSearch.new(:query => 'bench press',
108
+ :categories => { :exclude => [:comedy, :entertainment] },
109
+ :max_results => 10)
110
+ assert_equal "http://gdata.youtube.com/feeds/api/videos/-/-Comedy/-Entertainment/?max-results=10&vq=bench+press", request.url
111
+ end
112
+
113
+ # -- User Queries ---------------------------------------------------------------------------------
114
+
115
+ def test_should_build_url_for_videos_by_user
116
+ request = YouTubeG::Request::UserSearch.new(:user => 'liz')
117
+ assert_equal "http://gdata.youtube.com/feeds/api/users/liz/uploads", request.url
118
+ end
119
+
120
+ def test_should_build_url_for_videos_by_user_paginate_and_order
121
+ request = YouTubeG::Request::UserSearch.new(:user => 'liz', :offset => 20, :max_results => 10, :order_by => 'published')
122
+ assert_equal "http://gdata.youtube.com/feeds/api/users/liz/uploads?max-results=10&orderby=published&start-index=20", request.url
123
+ end
124
+
125
+ def test_should_build_url_for_favorite_videos_by_user
126
+ request = YouTubeG::Request::UserSearch.new(:favorites, :user => 'liz')
127
+ assert_equal "http://gdata.youtube.com/feeds/api/users/liz/favorites", request.url
128
+ end
129
+
130
+ def test_should_build_url_for_favorite_videos_by_user_paginate
131
+ request = YouTubeG::Request::UserSearch.new(:favorites, :user => 'liz', :offset => 20, :max_results => 10)
132
+ assert_equal "http://gdata.youtube.com/feeds/api/users/liz/favorites?max-results=10&start-index=20", request.url
133
+ end
134
+ end