tmm1-youtube-g 0.4.5

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,23 @@
1
+ class YouTubeG
2
+ module Response
3
+ class VideoSearch < YouTubeG::Record
4
+ # the unique feed identifying url
5
+ attr_reader :feed_id
6
+
7
+ # the number of results per page
8
+ attr_reader :max_result_count
9
+
10
+ # the 1-based offset index into the full result set
11
+ attr_reader :offset
12
+
13
+ # the total number of results available for the original request
14
+ attr_reader :total_result_count
15
+
16
+ # the date and time at which the feed was last updated
17
+ attr_reader :updated_at
18
+
19
+ # the list of Video records
20
+ attr_reader :videos
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,226 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'pp'
4
+
5
+ require 'youtube_g'
6
+
7
+ class TestClient < Test::Unit::TestCase
8
+ def setup
9
+ @client = YouTubeG::Client.new
10
+ end
11
+
12
+ def test_should_respond_to_a_basic_query
13
+ response = @client.videos_by(:query => "penguin")
14
+
15
+ assert_equal "http://gdata.youtube.com/feeds/api/videos?start-index=1&max-results=25&vq=penguin", response.feed_id
16
+ assert_equal 25, response.max_result_count
17
+ assert_equal 25, response.videos.length
18
+ assert_equal 1, response.offset
19
+ assert(response.total_result_count > 100)
20
+ assert_instance_of Time, response.updated_at
21
+
22
+ response.videos.each { |v| assert_valid_video v }
23
+ end
24
+
25
+ def test_should_get_videos_for_multiword_metasearch_query
26
+ response = @client.videos_by(:query => 'christina ricci')
27
+
28
+ assert_equal "http://gdata.youtube.com/feeds/api/videos?start-index=1&max-results=25&vq=christina+ricci", response.feed_id
29
+ assert_equal 25, response.max_result_count
30
+ assert_equal 25, response.videos.length
31
+ assert_equal 1, response.offset
32
+ assert(response.total_result_count > 100)
33
+ assert_instance_of Time, response.updated_at
34
+
35
+ response.videos.each { |v| assert_valid_video v }
36
+ end
37
+
38
+ def test_should_handle_video_not_yet_viewed
39
+ response = @client.videos_by(:query => "YnqHZDh_t2Q")
40
+
41
+ assert_equal 1, response.videos.length
42
+ response.videos.each { |v| assert_valid_video v }
43
+ end
44
+
45
+ # TODO: this doesn't work because the returned feed is in an unknown format
46
+ # def test_should_get_video_for_search_by_video_id
47
+ # response = @client.videos_by(:video_id => "T7YazwP8GtY")
48
+ # response.videos.each { |v| assert_valid_video v }
49
+ # end
50
+
51
+ def test_should_get_videos_for_one_tag
52
+ response = @client.videos_by(:tags => ['panther'])
53
+ response.videos.each { |v| assert_valid_video v }
54
+ end
55
+
56
+ def test_should_get_videos_for_multiple_tags
57
+ response = @client.videos_by(:tags => ['tiger', 'leopard'])
58
+ response.videos.each { |v| assert_valid_video v }
59
+ end
60
+
61
+ def test_should_get_videos_for_one_category
62
+ response = @client.videos_by(:categories => [:news])
63
+ response.videos.each { |v| assert_valid_video v }
64
+ end
65
+
66
+ def test_should_get_videos_for_multiple_categories
67
+ response = @client.videos_by(:categories => [:news, :sports])
68
+ response.videos.each { |v| assert_valid_video v }
69
+ end
70
+
71
+ # TODO: Need to do more specific checking in these tests
72
+ # Currently, if a URL is valid, and videos are found, the test passes regardless of search criteria
73
+ def test_should_get_videos_for_categories_and_tags
74
+ response = @client.videos_by(:categories => [:news, :sports], :tags => ['soccer', 'football'])
75
+ response.videos.each { |v| assert_valid_video v }
76
+ end
77
+
78
+ def test_should_get_most_viewed_videos
79
+ response = @client.videos_by(:most_viewed)
80
+ response.videos.each { |v| assert_valid_video v }
81
+ end
82
+
83
+ def test_should_get_top_rated_videos_for_today
84
+ response = @client.videos_by(:top_rated, :time => :today)
85
+ response.videos.each { |v| assert_valid_video v }
86
+ end
87
+
88
+ def test_should_get_videos_for_categories_and_tags_with_category_boolean_operators
89
+ response = @client.videos_by(:categories => { :either => [:news, :sports], :exclude => [:comedy] },
90
+ :tags => { :include => ['football'], :exclude => ['soccer'] })
91
+ response.videos.each { |v| assert_valid_video v }
92
+ end
93
+
94
+ def test_should_get_videos_for_categories_and_tags_with_tag_boolean_operators
95
+ response = @client.videos_by(:categories => { :either => [:news, :sports], :exclude => [:comedy] },
96
+ :tags => { :either => ['football', 'soccer', 'polo'] })
97
+ response.videos.each { |v| assert_valid_video v }
98
+ end
99
+
100
+ def test_should_get_videos_by_user
101
+ response = @client.videos_by(:user => 'liz')
102
+ response.videos.each { |v| assert_valid_video v }
103
+ end
104
+
105
+ # HTTP 403 Error
106
+ # def test_should_get_favorite_videos_by_user
107
+ # response = @client.videos_by(:favorites, :user => 'liz')
108
+ # response.videos.each { |v| assert_valid_video v }
109
+ # end
110
+
111
+ def test_should_get_videos_for_query_search_with_categories_excluded
112
+ response = @client.videos_by(:query => 'bench press', :categories => { :exclude => [:comedy, :entertainment] },
113
+ :max_results => 10)
114
+ assert_equal "<object width=\"425\" height=\"350\">\n <param name=\"movie\" value=\"http://www.youtube.com/v/BlDWdfTAx8o\"></param>\n <param name=\"wmode\" value=\"transparent\"></param>\n <embed src=\"http://www.youtube.com/v/BlDWdfTAx8o\" type=\"application/x-shockwave-flash\" \n wmode=\"transparent\" width=\"425\" height=\"350\"></embed>\n</object>\n", response.videos.first.embed_html
115
+ response.videos.each { |v| assert_valid_video v }
116
+ end
117
+
118
+ def test_should_be_able_to_pass_in_logger
119
+ @client = YouTubeG::Client.new(Logger.new(STDOUT))
120
+ assert_not_nil @client.logger
121
+ end
122
+
123
+ def test_should_create_logger_if_not_passed_in
124
+ @client = YouTubeG::Client.new
125
+ assert_not_nil @client.logger
126
+ end
127
+
128
+ def test_should_determine_if_nonembeddable_video_is_embeddable
129
+ response = @client.videos_by(:query => "avril lavigne girlfriend")
130
+
131
+ video = response.videos.first
132
+ assert !video.can_embed?
133
+ end
134
+
135
+ def test_should_determine_if_embeddable_video_is_embeddable
136
+ response = @client.videos_by(:query => "strongbad")
137
+
138
+ video = response.videos.first
139
+ assert video.can_embed?
140
+ end
141
+
142
+ def test_should_retrieve_video_by_id
143
+ video = @client.video_by("http://gdata.youtube.com/feeds/videos/EkF4JD2rO3Q")
144
+ assert_valid_video video
145
+
146
+ video = @client.video_by("EkF4JD2rO3Q")
147
+ assert_valid_video video
148
+ end
149
+
150
+ private
151
+
152
+ def assert_valid_video (video)
153
+ # pp video
154
+
155
+ # check general attributes
156
+ assert_instance_of YouTubeG::Model::Video, video
157
+ assert_instance_of Fixnum, video.duration
158
+ assert(video.duration > 0)
159
+ #assert_match(/^<div style=.*?<\/div>/m, video.html_content)
160
+ assert_instance_of String, video.html_content
161
+
162
+ # validate media content records
163
+ video.media_content.each do |media_content|
164
+ # http://www.youtube.com/v/IHVaXG1thXM
165
+ assert_valid_url media_content.url
166
+ assert(media_content.duration > 0)
167
+ assert_instance_of YouTubeG::Model::Video::Format, media_content.format
168
+ assert_instance_of String, media_content.mime_type
169
+ assert_match(/^[^\/]+\/[^\/]+$/, media_content.mime_type)
170
+ end
171
+
172
+ default_content = video.default_media_content
173
+ if default_content
174
+ assert_instance_of YouTubeG::Model::Content, default_content
175
+ assert default_content.is_default?
176
+ end
177
+
178
+ # validate keywords
179
+ video.keywords.each { |kw| assert_instance_of(String, kw) }
180
+
181
+ # http://www.youtube.com/watch?v=IHVaXG1thXM
182
+ assert_valid_url video.player_url
183
+ assert_instance_of Time, video.published_at
184
+
185
+ # validate optionally-present rating
186
+ if video.rating
187
+ assert_instance_of YouTubeG::Model::Rating, video.rating
188
+ assert_instance_of Float, video.rating.average
189
+ assert_instance_of Fixnum, video.rating.max
190
+ assert_instance_of Fixnum, video.rating.min
191
+ assert_instance_of Fixnum, video.rating.rater_count
192
+ end
193
+
194
+ # validate thumbnails
195
+ assert(video.thumbnails.size > 0)
196
+
197
+ assert_not_nil video.title
198
+ assert_instance_of String, video.title
199
+ assert(video.title.length > 0)
200
+
201
+ assert_instance_of Time, video.updated_at
202
+ # http://gdata.youtube.com/feeds/videos/IHVaXG1thXM
203
+ assert_valid_url video.video_id
204
+ assert_instance_of Fixnum, video.view_count
205
+
206
+ # validate author
207
+ assert_instance_of YouTubeG::Model::Author, video.author
208
+ assert_instance_of String, video.author.name
209
+ assert(video.author.name.length > 0)
210
+ assert_valid_url video.author.uri
211
+
212
+ # validate categories
213
+ video.categories.each do |cat|
214
+ assert_instance_of YouTubeG::Model::Category, cat
215
+ assert_instance_of String, cat.label
216
+ assert_instance_of String, cat.term
217
+ end
218
+ end
219
+
220
+ def assert_valid_url (url)
221
+ URI::parse(url)
222
+ return true
223
+ rescue
224
+ return false
225
+ end
226
+ end
@@ -0,0 +1,30 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'pp'
4
+
5
+ require 'youtube_g'
6
+
7
+ class TestVideo < Test::Unit::TestCase
8
+ def test_should_extract_unique_id_from_video_id
9
+ video = YouTubeG::Model::Video.new(:video_id => "http://gdata.youtube.com/feeds/videos/ZTUVgYoeN_o")
10
+ assert_equal "ZTUVgYoeN_o", video.unique_id
11
+ end
12
+
13
+ def test_should_extract_unique_id_with_hypen_from_video_id
14
+ video = YouTubeG::Model::Video.new(:video_id => "http://gdata.youtube.com/feeds/videos/BDqs-OZWw9o")
15
+ assert_equal "BDqs-OZWw9o", video.unique_id
16
+ end
17
+
18
+ def test_should_have_related_videos
19
+ video = YouTubeG::Model::Video.new(:video_id => "http://gdata.youtube.com/feeds/videos/BDqs-OZWw9o")
20
+ response = video.related
21
+
22
+ assert_equal "http://gdata.youtube.com/feeds/api/videos/BDqs-OZWw9o/related", response.feed_id
23
+ assert_equal 25, response.max_result_count
24
+ assert_equal 25, response.videos.length
25
+ assert_equal 1, response.offset
26
+ puts response.total_result_count
27
+ assert(response.total_result_count > 0)
28
+ assert_instance_of Time, response.updated_at
29
+ end
30
+ end
@@ -0,0 +1,118 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'pp'
4
+
5
+ require 'youtube_g'
6
+
7
+ class TestVideoSearch < Test::Unit::TestCase
8
+
9
+ def test_should_build_basic_query_url
10
+ request = YouTubeG::Request::VideoSearch.new(:query => "penguin")
11
+ assert_equal "http://gdata.youtube.com/feeds/api/videos?vq=penguin", request.url
12
+ end
13
+
14
+ def test_should_build_multiword_metasearch_query_url
15
+ request = YouTubeG::Request::VideoSearch.new(:query => 'christina ricci')
16
+ assert_equal "http://gdata.youtube.com/feeds/api/videos?vq=christina+ricci", request.url
17
+ end
18
+
19
+ def test_should_build_video_id_url
20
+ request = YouTubeG::Request::VideoSearch.new(:video_id => 'T7YazwP8GtY')
21
+ assert_equal "http://gdata.youtube.com/feeds/api/videos/T7YazwP8GtY", request.url
22
+ end
23
+
24
+ def test_should_build_one_tag_querl_url
25
+ request = YouTubeG::Request::VideoSearch.new(:tags => ['panther'])
26
+ assert_equal "http://gdata.youtube.com/feeds/api/videos/-/panther/", request.url
27
+ end
28
+
29
+ def test_should_build_multiple_tags_query_url
30
+ request = YouTubeG::Request::VideoSearch.new(:tags => ['tiger', 'leopard'])
31
+ assert_equal "http://gdata.youtube.com/feeds/api/videos/-/tiger/leopard/", request.url
32
+ end
33
+
34
+ def test_should_build_one_category_query_url
35
+ request = YouTubeG::Request::VideoSearch.new(:categories => [:news])
36
+ assert_equal "http://gdata.youtube.com/feeds/api/videos/-/News/", request.url
37
+ end
38
+
39
+ def test_should_build_multiple_categories_query_url
40
+ request = YouTubeG::Request::VideoSearch.new(:categories => [:news, :sports])
41
+ assert_equal "http://gdata.youtube.com/feeds/api/videos/-/News/Sports/", request.url
42
+ end
43
+
44
+ def test_should_build_categories_and_tags_query_url
45
+ request = YouTubeG::Request::VideoSearch.new(:categories => [:news, :sports], :tags => ['soccer', 'football'])
46
+ assert_equal "http://gdata.youtube.com/feeds/api/videos/-/News/Sports/soccer/football/", request.url
47
+ end
48
+
49
+ def test_should_build_categories_and_tags_url_with_max_results
50
+ request = YouTubeG::Request::VideoSearch.new(:categories => [:music], :tags => ['classic', 'rock'], :max_results => 2)
51
+ assert_equal "http://gdata.youtube.com/feeds/api/videos/-/Music/classic/rock/?max-results=2", request.url
52
+ end
53
+
54
+ def test_should_build_author_query_url
55
+ request = YouTubeG::Request::VideoSearch.new(:author => "davidguetta")
56
+ assert_equal "http://gdata.youtube.com/feeds/api/videos?author=davidguetta", request.url
57
+ end
58
+ # -- Standard Feeds --------------------------------------------------------------------------------
59
+
60
+ def test_should_build_url_for_most_viewed
61
+ request = YouTubeG::Request::StandardSearch.new(:most_viewed)
62
+ assert_equal "http://gdata.youtube.com/feeds/api/standardfeeds/most_viewed", request.url
63
+ end
64
+
65
+ def test_should_raise_exception_for_invalid_type
66
+ assert_raise RuntimeError do
67
+ request = YouTubeG::Request::StandardSearch.new(:most_viewed_yo)
68
+ end
69
+ end
70
+
71
+ def test_should_build_url_for_top_rated_for_today
72
+ request = YouTubeG::Request::StandardSearch.new(:top_rated, :time => :today)
73
+ assert_equal "http://gdata.youtube.com/feeds/api/standardfeeds/top_rated?time=today", request.url
74
+ end
75
+
76
+ # -- Complex Video Queries -------------------------------------------------------------------------
77
+
78
+ def test_should_build_url_for_boolean_or_case_for_categories
79
+ request = YouTubeG::Request::VideoSearch.new(:categories => { :either => [:news, :sports] })
80
+ assert_equal "http://gdata.youtube.com/feeds/api/videos/-/News%7CSports/", request.url
81
+ end
82
+
83
+ def test_should_build_url_for_boolean_or_and_exclude_case_for_categories
84
+ request = YouTubeG::Request::VideoSearch.new(:categories => { :either => [:news, :sports], :exclude => [:comedy] })
85
+ assert_equal "http://gdata.youtube.com/feeds/api/videos/-/News%7CSports/-Comedy/", request.url
86
+ end
87
+
88
+ def test_should_build_url_for_exclude_case_for_tags
89
+ request = YouTubeG::Request::VideoSearch.new(:categories => { :either => [:news, :sports], :exclude => [:comedy] },
90
+ :tags => { :include => ['football'], :exclude => ['soccer'] })
91
+ assert_equal "http://gdata.youtube.com/feeds/api/videos/-/News%7CSports/-Comedy/football/-soccer/", request.url
92
+ end
93
+
94
+ def test_should_build_url_for_either_case_for_tags
95
+ request = YouTubeG::Request::VideoSearch.new(:categories => { :either => [:news, :sports], :exclude => [:comedy] },
96
+ :tags => { :either => ['soccer', 'football', 'donkey'] })
97
+ assert_equal "http://gdata.youtube.com/feeds/api/videos/-/News%7CSports/-Comedy/soccer%7Cfootball%7Cdonkey/", request.url
98
+ end
99
+
100
+ def test_should_build_url_for_query_search_with_categories_excluded
101
+ request = YouTubeG::Request::VideoSearch.new(:query => 'bench press',
102
+ :categories => { :exclude => [:comedy, :entertainment] },
103
+ :max_results => 10)
104
+ assert_equal "http://gdata.youtube.com/feeds/api/videos/-/-Comedy/-Entertainment/?vq=bench+press&max-results=10", request.url
105
+ end
106
+
107
+ # -- User Queries ---------------------------------------------------------------------------------
108
+
109
+ def test_should_build_url_for_videos_by_user
110
+ request = YouTubeG::Request::UserSearch.new(:user => 'liz')
111
+ assert_equal "http://gdata.youtube.com/feeds/api/users/liz/uploads", request.url
112
+ end
113
+
114
+ def test_should_build_url_for_favorite_videos_by_user
115
+ request = YouTubeG::Request::UserSearch.new(:favorites, :user => 'liz')
116
+ assert_equal "http://gdata.youtube.com/feeds/api/users/liz/favorites", request.url
117
+ end
118
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tmm1-youtube-g
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.5
5
+ platform: ruby
6
+ authors:
7
+ - Shane Vitarana
8
+ - Walter Korman
9
+ - Aman Gupta
10
+ - Filip H.F. Slagter
11
+ autorequire:
12
+ bindir: bin
13
+ cert_chain: []
14
+
15
+ date: 2008-06-08 00:00:00 -07:00
16
+ default_executable:
17
+ dependencies: []
18
+
19
+ description: An object-oriented Ruby wrapper for the YouTube GData API
20
+ email: ruby-youtube-library@googlegroups.com
21
+ executables: []
22
+
23
+ extensions: []
24
+
25
+ extra_rdoc_files:
26
+ - History.txt
27
+ - Manifest.txt
28
+ - README.txt
29
+ files:
30
+ - History.txt
31
+ - Manifest.txt
32
+ - README.txt
33
+ - Rakefile
34
+ - TODO.txt
35
+ - lib/youtube_g.rb
36
+ - lib/youtube_g/client.rb
37
+ - lib/youtube_g/logger.rb
38
+ - lib/youtube_g/model/author.rb
39
+ - lib/youtube_g/model/category.rb
40
+ - lib/youtube_g/model/contact.rb
41
+ - lib/youtube_g/model/content.rb
42
+ - lib/youtube_g/model/playlist.rb
43
+ - lib/youtube_g/model/rating.rb
44
+ - lib/youtube_g/model/thumbnail.rb
45
+ - lib/youtube_g/model/user.rb
46
+ - lib/youtube_g/model/video.rb
47
+ - lib/youtube_g/parser.rb
48
+ - lib/youtube_g/record.rb
49
+ - lib/youtube_g/request/video_search.rb
50
+ - lib/youtube_g/request/video_upload.rb
51
+ - lib/youtube_g/response/video_search.rb
52
+ - test/test_client.rb
53
+ - test/test_video.rb
54
+ - test/test_video_search.rb
55
+ has_rdoc: true
56
+ homepage: http://youtube-g.rubyforge.org/
57
+ post_install_message:
58
+ rdoc_options:
59
+ - --main
60
+ - README.txt
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ version:
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: "0"
74
+ version:
75
+ requirements: []
76
+
77
+ rubyforge_project:
78
+ rubygems_version: 1.2.0
79
+ signing_key:
80
+ specification_version: 2
81
+ summary: An object-oriented Ruby wrapper for the YouTube GData API
82
+ test_files:
83
+ - test/test_client.rb
84
+ - test/test_video.rb
85
+ - test/test_video_search.rb