tubeclip 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +9 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +5 -0
  5. data/CODE_OF_CONDUCT.md +74 -0
  6. data/Gemfile +4 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +41 -0
  9. data/Rakefile +6 -0
  10. data/bin/console +14 -0
  11. data/bin/setup +8 -0
  12. data/lib/tubeclip/chain_io.rb +86 -0
  13. data/lib/tubeclip/client.rb +541 -0
  14. data/lib/tubeclip/middleware/faraday_authheader.rb +24 -0
  15. data/lib/tubeclip/middleware/faraday_oauth.rb +21 -0
  16. data/lib/tubeclip/middleware/faraday_oauth2.rb +13 -0
  17. data/lib/tubeclip/middleware/faraday_tubeclip.rb +38 -0
  18. data/lib/tubeclip/model/activity.rb +17 -0
  19. data/lib/tubeclip/model/author.rb +13 -0
  20. data/lib/tubeclip/model/caption.rb +7 -0
  21. data/lib/tubeclip/model/category.rb +11 -0
  22. data/lib/tubeclip/model/comment.rb +18 -0
  23. data/lib/tubeclip/model/contact.rb +19 -0
  24. data/lib/tubeclip/model/content.rb +18 -0
  25. data/lib/tubeclip/model/message.rb +12 -0
  26. data/lib/tubeclip/model/playlist.rb +11 -0
  27. data/lib/tubeclip/model/rating.rb +23 -0
  28. data/lib/tubeclip/model/subscription.rb +7 -0
  29. data/lib/tubeclip/model/thumbnail.rb +20 -0
  30. data/lib/tubeclip/model/user.rb +35 -0
  31. data/lib/tubeclip/model/video.rb +302 -0
  32. data/lib/tubeclip/parser.rb +643 -0
  33. data/lib/tubeclip/record.rb +12 -0
  34. data/lib/tubeclip/request/base_search.rb +76 -0
  35. data/lib/tubeclip/request/error.rb +21 -0
  36. data/lib/tubeclip/request/remote_file.rb +70 -0
  37. data/lib/tubeclip/request/standard_search.rb +49 -0
  38. data/lib/tubeclip/request/user_search.rb +47 -0
  39. data/lib/tubeclip/request/video_search.rb +125 -0
  40. data/lib/tubeclip/request/video_upload.rb +762 -0
  41. data/lib/tubeclip/response/video_search.rb +41 -0
  42. data/lib/tubeclip/version.rb +3 -0
  43. data/lib/tubeclip.rb +85 -0
  44. data/tubeclip.gemspec +44 -0
  45. metadata +259 -0
@@ -0,0 +1,541 @@
1
+ class Tubeclip
2
+ class Client
3
+ include Tubeclip::Logging
4
+ # Previously this was a logger instance but we now do it globally
5
+
6
+ def initialize *params
7
+ if params.first.is_a?(Hash)
8
+ hash_options = params.first
9
+ @user = hash_options[:username]
10
+ @pass = hash_options[:password]
11
+ @dev_key = hash_options[:dev_key]
12
+ @client_id = hash_options[:client_id] || "youtube_it"
13
+ @legacy_debug_flag = hash_options[:debug]
14
+ elsif params.first
15
+ puts "* warning: the method Tubeclip::Client.new(user, passwd, dev_key) is deprecated, use Tubeclip::Client.new(:username => 'user', :password => 'passwd', :dev_key => 'dev_key')"
16
+ @user = params.shift
17
+ @pass = params.shift
18
+ @dev_key = params.shift
19
+ @client_id = params.shift || "youtube_it"
20
+ @legacy_debug_flag = params.shift
21
+ end
22
+ end
23
+
24
+ # Retrieves an array of standard feed, custom query, or user videos.
25
+ #
26
+ # === Parameters
27
+ # If fetching videos for a standard feed:
28
+ # params<Symbol>:: Accepts a symbol of :top_rated, :top_favorites, :most_viewed,
29
+ # :most_popular, :most_recent, :most_discussed, :most_linked,
30
+ # :most_responded, :recently_featured, and :watch_on_mobile.
31
+ #
32
+ # You can find out more specific information about what each standard feed provides
33
+ # by visiting: http://code.google.com/apis/youtube/reference.html#Standard_feeds
34
+ #
35
+ # options<Hash> (optional):: Accepts the options of :time, :page (default is 1),
36
+ # and :per_page (default is 25). :offset and :max_results
37
+ # can also be passed for a custom offset.
38
+ #
39
+ # If fetching videos by tags, categories, query:
40
+ # params<Hash>:: Accepts the keys :tags, :categories, :query, :order_by,
41
+ # :author, :safe_search, :response_format, :video_format, :page (default is 1),
42
+ # and :per_page(default is 25)
43
+ #
44
+ # options<Hash>:: Not used. (Optional)
45
+ #
46
+ # If fetching videos for a particular user:
47
+ # params<Hash>:: Key of :user with a value of the username.
48
+ # options<Hash>:: Not used. (Optional)
49
+ # === Returns
50
+ # Tubeclip::Response::VideoSearch
51
+ def videos_by(params, options={})
52
+ request_params = params.respond_to?(:to_hash) ? params : options
53
+ request_params[:page] = integer_or_default(request_params[:page], 1)
54
+
55
+ request_params[:dev_key] = @dev_key if @dev_key
56
+
57
+ unless request_params[:max_results]
58
+ request_params[:max_results] = integer_or_default(request_params[:per_page], 25)
59
+ end
60
+
61
+ unless request_params[:offset]
62
+ request_params[:offset] = calculate_offset(request_params[:page], request_params[:max_results])
63
+ end
64
+
65
+ if params.respond_to?(:to_hash) and not params[:user]
66
+ request = Tubeclip::Request::VideoSearch.new(request_params)
67
+ elsif (params.respond_to?(:to_hash) && params[:user]) || (params == :favorites)
68
+ request = Tubeclip::Request::UserSearch.new(params, request_params)
69
+ else
70
+ request = Tubeclip::Request::StandardSearch.new(params, request_params)
71
+ end
72
+
73
+ logger.debug "Submitting request [url=#{request.url}]." if @legacy_debug_flag
74
+ parser = Tubeclip::Parser::VideosFeedParser.new(request.url)
75
+ parser.parse
76
+ end
77
+
78
+ # Retrieves a single YouTube video.
79
+ #
80
+ # === Parameters
81
+ # vid<String>:: The ID or URL of the video that you'd like to retrieve.
82
+ # user<String>:: The user that uploaded the video that you'd like to retrieve.
83
+ #
84
+ # === Returns
85
+ # Tubeclip::Model::Video
86
+ def video_by(video)
87
+ vid = nil
88
+ vid_regex = /(?:youtube.com|youtu.be).*(?:\/|v=)([a-zA-Z0-9_-]+)/
89
+ if video =~ vid_regex
90
+ vid = $1
91
+ else
92
+ vid = video
93
+ end
94
+ video_id ="http://gdata.youtube.com/feeds/api/videos/#{vid}?v=#{Tubeclip::API_VERSION}#{@dev_key ? '&key='+@dev_key : ''}"
95
+ parser = Tubeclip::Parser::VideoFeedParser.new(video_id)
96
+ parser.parse
97
+ end
98
+
99
+ def video_by_user(user, vid)
100
+ video_id = "http://gdata.youtube.com/feeds/api/users/#{user}/uploads/#{vid}?v=#{Tubeclip::API_VERSION}#{@dev_key ? '&key='+@dev_key : ''}"
101
+ parser = Tubeclip::Parser::VideoFeedParser.new(video_id)
102
+ parser.parse
103
+ end
104
+
105
+ def get_all_videos(opts)
106
+ page = videos_by(opts.merge({:page => 1}))
107
+ videos = page.videos
108
+
109
+ while page.next_page && (page = videos_by(opts.merge({:page => page.next_page})) || true)
110
+ videos += page.videos
111
+ end
112
+
113
+ videos
114
+ end
115
+
116
+ def video_upload(data, opts = {})
117
+ client.upload(data, opts)
118
+ end
119
+
120
+ def video_update(video_id, opts = {})
121
+ client.update(video_id, opts)
122
+ end
123
+
124
+ def video_partial_update(video_id, opts = {})
125
+ client.partial_update(video_id, opts)
126
+ end
127
+
128
+ def captions_update(video_id, data, opts = {})
129
+ client.captions_update(video_id, data, opts)
130
+ end
131
+
132
+ def video_delete(video_id)
133
+ client.delete(video_id)
134
+ end
135
+
136
+ def message_delete(message_id)
137
+ client.delete_message(message_id)
138
+ end
139
+
140
+ def upload_token(options, nexturl = "http://www.youtube.com/my_videos")
141
+ client.get_upload_token(options, nexturl)
142
+ end
143
+
144
+ def add_comment(video_id, comment, opts = {})
145
+ client.add_comment(video_id, comment, opts)
146
+ end
147
+
148
+ def delete_comment(video_id, comment_id)
149
+ client.delete_comment(video_id, comment_id)
150
+ end
151
+
152
+ # opts is converted to get params and appended to comments gdata api url
153
+ # eg opts = { 'max-results' => 10, 'start-index' => 20 }
154
+ # hash does _not_ play nice with symbols
155
+ def comments(video_id, opts = {})
156
+ client.comments(video_id, opts)
157
+ end
158
+
159
+ def add_favorite(video_id)
160
+ client.add_favorite(video_id)
161
+ end
162
+
163
+ def delete_favorite(video_id)
164
+ client.delete_favorite(video_id)
165
+ end
166
+
167
+ def favorites(user = nil, opts = {})
168
+ client.favorites(user, opts)
169
+ end
170
+
171
+ def profile(user = nil)
172
+ client.profile(user)
173
+ end
174
+
175
+ def profiles(*users)
176
+ client.profiles(*users)
177
+ end
178
+
179
+ def videos(*idxes)
180
+ client.videos(*idxes)
181
+ end
182
+
183
+ # Fetches a user's activity feed.
184
+ def activity(user = nil, opts = {})
185
+ client.get_activity(user, opts)
186
+ end
187
+
188
+ def watchlater(user = nil)
189
+ client.watchlater(user)
190
+ end
191
+
192
+ def add_video_to_watchlater(video_id)
193
+ client.add_video_to_watchlater(video_id)
194
+ end
195
+
196
+ def delete_video_from_watchlater(video_id)
197
+ client.delete_video_from_watchlater(video_id)
198
+ end
199
+
200
+ def playlist(playlist_id, opts = {})
201
+ client.playlist(playlist_id, opts)
202
+ end
203
+
204
+ def playlists(user = nil, opts = nil)
205
+ client.playlists(user, opts)
206
+ end
207
+
208
+ # Fetches all playlists for a given user by repeatedly making requests for
209
+ # as many pages of playlists as the user has. Note that this can take a
210
+ # long time if the user has many playlists.
211
+ def all_playlists(user = nil)
212
+ newest_playlists, all_playlists_for_user = [], []
213
+ start_index, page_size = 1, 25
214
+
215
+ begin
216
+ newest_playlists = playlists(user, {'start-index' => start_index, 'max-results' => page_size})
217
+ all_playlists_for_user += newest_playlists
218
+ start_index += page_size
219
+ end while newest_playlists && newest_playlists.size == page_size
220
+
221
+ all_playlists_for_user
222
+ end
223
+
224
+ def add_playlist(options)
225
+ client.add_playlist(options)
226
+ end
227
+
228
+ def update_playlist(playlist_id, options)
229
+ client.update_playlist(playlist_id, options)
230
+ end
231
+
232
+ def add_video_to_playlist(playlist_id, video_id, position = nil)
233
+ client.add_video_to_playlist(playlist_id, video_id, position)
234
+ end
235
+
236
+ def update_position_video_from_playlist(playlist_id, playlist_entry_id, position = nil)
237
+ client.update_position_video_from_playlist(playlist_id, playlist_entry_id, position)
238
+ end
239
+
240
+ def delete_video_from_playlist(playlist_id, playlist_entry_id)
241
+ client.delete_video_from_playlist(playlist_id, playlist_entry_id)
242
+ end
243
+
244
+ def delete_playlist(playlist_id)
245
+ client.delete_playlist(playlist_id)
246
+ end
247
+
248
+ def like_video(video_id)
249
+ client.rate_video(video_id, 'like')
250
+ end
251
+
252
+ def dislike_video(video_id)
253
+ client.rate_video(video_id, 'dislike')
254
+ end
255
+
256
+ def subscribe_channel(channel_name)
257
+ client.subscribe_channel(channel_name)
258
+ end
259
+
260
+ def unsubscribe_channel(subscription_id)
261
+ client.unsubscribe_channel(subscription_id)
262
+ end
263
+
264
+ def subscriptions(user_id = nil)
265
+ client.subscriptions(user_id)
266
+ end
267
+
268
+ def enable_http_debugging
269
+ client.enable_http_debugging
270
+ end
271
+
272
+ def add_response(original_video_id, response_video_id)
273
+ client.add_response(original_video_id, response_video_id)
274
+ end
275
+
276
+ def delete_response(original_video_id, response_video_id)
277
+ client.delete_response(original_video_id, response_video_id)
278
+ end
279
+
280
+ def current_user
281
+ client.get_current_user
282
+ end
283
+
284
+ # Gets the authenticated users video with the given ID. It may be private.
285
+ def my_video(video_id)
286
+ client.get_my_video(video_id)
287
+ end
288
+
289
+ # Gets all videos
290
+ def my_videos(opts = {})
291
+ client.get_my_videos(opts)
292
+ end
293
+
294
+ # Gets all of the user's contacts/friends.
295
+ def my_contacts(opts = {})
296
+ client.get_my_contacts(opts)
297
+ end
298
+
299
+ # Send video message
300
+ def send_message(opts = {})
301
+ client.send_message(opts)
302
+ end
303
+
304
+ # Gets all of the user's messages/inbox.
305
+ def my_messages(opts = {})
306
+ client.get_my_messages(opts)
307
+ end
308
+
309
+ # Gets the user's watch history
310
+ def watch_history
311
+ client.get_watch_history
312
+ end
313
+
314
+ # Gets new subscription videos
315
+ def new_subscription_videos(user_id = nil)
316
+ client.new_subscription_videos(user_id)
317
+ end
318
+
319
+ private
320
+
321
+ def client
322
+ @client ||= Tubeclip::Upload::VideoUpload.new(:username => @user, :password => @pass, :dev_key => @dev_key)
323
+ end
324
+
325
+ def calculate_offset(page, per_page)
326
+ page == 1 ? 1 : ((per_page * page) - per_page + 1)
327
+ end
328
+
329
+ def integer_or_default(value, default)
330
+ value = value.to_i
331
+ value > 0 ? value : default
332
+ end
333
+ end
334
+
335
+ class AuthSubClient < Client
336
+ def initialize *params
337
+ puts "* AuthSubClient will be deprecated. Use OAuth2 Client."
338
+ if params.first.is_a?(Hash)
339
+ hash_options = params.first
340
+ @authsub_token = hash_options[:token]
341
+ @dev_key = hash_options[:dev_key]
342
+ @client_id = hash_options[:client_id] || "youtube_it"
343
+ @legacy_debug_flag = hash_options[:debug]
344
+ else
345
+ puts "* warning: the method Tubeclip::AuthSubClient.new(token, dev_key) is deprecated, use Tubeclip::AuthSubClient.new(:token => 'token', :dev_key => 'dev_key')"
346
+ @authsub_token = params.shift
347
+ @dev_key = params.shift
348
+ @client_id = params.shift || "youtube_it"
349
+ @legacy_debug_flag = params.shift
350
+ end
351
+ end
352
+
353
+ def create_session_token
354
+ response = nil
355
+ session_token_url = "/accounts/AuthSubSessionToken"
356
+
357
+ http_connection do |session|
358
+ response = session.get2('https://%s' % session_token_url, session_token_header).body
359
+ end
360
+ @authsub_token = response.sub('Token=', '')
361
+ end
362
+
363
+ def revoke_session_token
364
+ response = nil
365
+ session_token_url = "/accounts/AuthSubRevokeToken"
366
+
367
+ http_connection do |session|
368
+ response = session.get2('https://%s' % session_token_url, session_token_header).code
369
+ end
370
+ response.to_s == '200' ? true : false
371
+ end
372
+
373
+ def session_token_info
374
+ response = nil
375
+ session_token_url = "/accounts/AuthSubTokenInfo"
376
+
377
+ http_connection do |session|
378
+ response = session.get2('https://%s' % session_token_url, session_token_header)
379
+ end
380
+ {:code => response.code, :body => response.body}
381
+ end
382
+
383
+ private
384
+ def client
385
+ @client ||= Tubeclip::Upload::VideoUpload.new(:dev_key => @dev_key, :authsub_token => @authsub_token)
386
+ end
387
+
388
+ def session_token_header
389
+ {
390
+ "Content-Type" => "application/x-www-form-urlencoded",
391
+ "Authorization" => "AuthSub token=#{@authsub_token}"
392
+ }
393
+ end
394
+
395
+ def http_connection
396
+ http = Net::HTTP.new("www.google.com")
397
+ http.set_debug_output(logger) if @http_debugging
398
+ http.start do |session|
399
+ yield(session)
400
+ end
401
+ end
402
+ end
403
+
404
+ class OAuthClient < Client
405
+ def initialize *params
406
+ puts "* OAuth 1.0 Client will be deprecated. Use OAuth2 Client."
407
+ if params.first.is_a?(Hash)
408
+ hash_options = params.first
409
+ @consumer_key = hash_options[:consumer_key]
410
+ @consumer_secret = hash_options[:consumer_secret]
411
+ @user = hash_options[:username]
412
+ @dev_key = hash_options[:dev_key]
413
+ @client_id = hash_options[:client_id] || "youtube_it"
414
+ @legacy_debug_flag = hash_options[:debug]
415
+ else
416
+ puts "* warning: the method Tubeclip::OAuthClient.new(consumer_key, consumer_secrect, dev_key) is depricated, use Tubeclip::OAuthClient.new(:consumer_key => 'consumer key', :consumer_secret => 'consumer secret', :dev_key => 'dev_key')"
417
+ @consumer_key = params.shift
418
+ @consumer_secret = params.shift
419
+ @dev_key = params.shift
420
+ @user = params.shift
421
+ @client_id = params.shift || "youtube_it"
422
+ @legacy_debug_flag = params.shift
423
+ end
424
+ end
425
+
426
+ def consumer
427
+ @consumer ||= ::OAuth::Consumer.new(@consumer_key, @consumer_secret, {
428
+ :site => "https://www.google.com",
429
+ :request_token_path => "/accounts/OAuthGetRequestToken",
430
+ :authorize_path => "/accounts/OAuthAuthorizeToken",
431
+ :access_token_path => "/accounts/OAuthGetAccessToken"})
432
+ end
433
+
434
+ def request_token(callback)
435
+ @request_token = consumer.get_request_token({:oauth_callback => callback}, {:scope => "http://gdata.youtube.com"})
436
+ end
437
+
438
+ def access_token
439
+ @access_token = ::OAuth::AccessToken.new(consumer, @atoken, @asecret)
440
+ end
441
+
442
+ def config_token
443
+ {
444
+ :consumer_key => @consumer_key,
445
+ :consumer_secret => @consumer_secret,
446
+ :token => @atoken,
447
+ :token_secret => @asecret
448
+ }
449
+ end
450
+
451
+ def authorize_from_request(rtoken, rsecret, verifier)
452
+ request_token = ::OAuth::RequestToken.new(consumer, rtoken, rsecret)
453
+ access_token = request_token.get_access_token({:oauth_verifier => verifier})
454
+ @atoken, @asecret = access_token.token, access_token.secret
455
+ end
456
+
457
+ def authorize_from_access(atoken, asecret)
458
+ @atoken, @asecret = atoken, asecret
459
+ end
460
+
461
+ def current_user
462
+ profile = access_token.get("http://gdata.youtube.com/feeds/api/users/default")
463
+ response_code = profile.code.to_i
464
+
465
+ if (response_code / 10).to_i == 20 # success
466
+ Nokogiri::XML(profile.body).at("//yt:username").text
467
+ elsif response_code == 403 || response_code == 401 # auth failure
468
+ raise AuthenticationError.new(profile.inspect, response_code)
469
+ else
470
+ raise UploadError.new(profile.inspect, response_code)
471
+ end
472
+ end
473
+
474
+ private
475
+
476
+ def client
477
+ # IMPORTANT: make sure authorize_from_access is called before client is fetched
478
+ @client ||= Tubeclip::Upload::VideoUpload.new(:username => current_user, :dev_key => @dev_key, :access_token => access_token, :config_token => config_token)
479
+ end
480
+
481
+ end
482
+
483
+ class OAuth2Client < Client
484
+ def initialize(options)
485
+ @client_id = options[:client_id]
486
+ @client_secret = options[:client_secret]
487
+ @client_access_token = options[:client_access_token]
488
+ @client_refresh_token = options[:client_refresh_token]
489
+ @client_token_expires_at = options[:client_token_expires_at]
490
+ @dev_key = options[:dev_key]
491
+ @legacy_debug_flag = options[:debug]
492
+ end
493
+
494
+ def oauth_client
495
+ options = {:site => "https://accounts.google.com",
496
+ :authorize_url => '/o/oauth2/auth',
497
+ :token_url => '/o/oauth2/token'
498
+ }
499
+ options.merge(:connection_opts => @connection_opts) if @connection_opts
500
+ @oauth_client ||= ::OAuth2::Client.new(@client_id, @client_secret, options)
501
+ end
502
+
503
+ def access_token
504
+ @access_token ||= ::OAuth2::AccessToken.new(oauth_client, @client_access_token, :refresh_token => @client_refresh_token, :expires_at => @client_token_expires_at)
505
+ end
506
+
507
+ def refresh_access_token!
508
+ new_access_token = access_token.refresh!
509
+ require 'thread' unless Thread.respond_to?(:exclusive)
510
+ Thread.exclusive do
511
+ @access_token = new_access_token
512
+ @client = nil
513
+ end
514
+ @access_token
515
+ end
516
+
517
+ def session_token_info
518
+ response = Faraday.get("https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=#{@client_access_token}")
519
+ {:code => response.status, :body => response.body }
520
+ end
521
+
522
+ def current_user
523
+ profile = access_token.get("http://gdata.youtube.com/feeds/api/users/default")
524
+ response_code = profile.status
525
+
526
+ if (response_code / 10).to_i == 20 # success
527
+ Nokogiri::XML(profile.body).at("//yt:username").text
528
+ elsif response_code == 403 || response_code == 401 # auth failure
529
+ raise AuthenticationError.new(profile.inspect, response_code)
530
+ else
531
+ raise UploadError.new(profile.inspect, response_code)
532
+ end
533
+ end
534
+
535
+ private
536
+
537
+ def client
538
+ @client ||= Tubeclip::Upload::VideoUpload.new(:username => current_user, :access_token => access_token, :dev_key => @dev_key)
539
+ end
540
+ end
541
+ end
@@ -0,0 +1,24 @@
1
+ module FaradayMiddleware
2
+ class YoutubeAuthHeader < Faraday::Middleware
3
+
4
+ def call(env)
5
+ req_headers = env[:request_headers]
6
+ req_headers.merge!(@headers)
7
+ unless req_headers.include?("GData-Version")
8
+ req_headers.merge!("GData-Version" => "2.1")
9
+ end
10
+ unless req_headers.include?("Content-Type")
11
+ req_headers.merge!("Content-Type" => "application/atom+xml; charset=UTF-8")
12
+ end
13
+ unless req_headers.include?("Content-Length")
14
+ req_headers.merge!("Content-Length" => env[:body] ? "#{env[:body].length}" : "0")
15
+ end
16
+
17
+ @app.call(env)
18
+ end
19
+
20
+ def initialize(app, headers)
21
+ @app, @headers = app, headers
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,21 @@
1
+ module FaradayMiddleware
2
+ class YoutubeOAuth < Faraday::Middleware
3
+ dependency 'simple_oauth'
4
+
5
+ def call(env)
6
+ params = env[:body].is_a?(Hash) ? env[:body] : {}
7
+
8
+ signature_params = params.reject{ |k,v| v.respond_to?(:content_type) }
9
+
10
+ header = SimpleOAuth::Header.new(env[:method], env[:url], signature_params, @options)
11
+
12
+ env[:request_headers]['Authorization'] = header.to_s
13
+
14
+ @app.call(env)
15
+ end
16
+
17
+ def initialize(app, options)
18
+ @app, @options = app, options
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,13 @@
1
+ module FaradayMiddleware
2
+ class YoutubeOAuth2 < Faraday::Middleware
3
+ def call(env)
4
+ env[:request_headers]['Authorization'] = "Bearer #{@access_token.token}"
5
+
6
+ @app.call(env)
7
+ end
8
+
9
+ def initialize(app, access_token)
10
+ @app, @access_token = app, access_token
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,38 @@
1
+ module Faraday
2
+ class Response::Tubeclip < Response::Middleware
3
+ def on_complete(env) #this method is called after finish request
4
+ msg = parse_error_from(env[:body])
5
+ if env[:status] == 404
6
+ raise ::Tubeclip::ResourceNotFoundError.new(msg)
7
+ elsif env[:status] == 403 || env[:status] == 401
8
+ raise ::Tubeclip::AuthenticationError.new(msg, env[:status])
9
+ elsif (env[:status] / 10).to_i != 20
10
+ raise ::Tubeclip::UploadError.new(msg, env[:status])
11
+ end
12
+ end
13
+
14
+ private
15
+ def parse_error_from(string)
16
+ return "" unless string
17
+
18
+ string.gsub!("\n", "")
19
+
20
+ xml = Nokogiri::XML(string).at('errors')
21
+ if xml
22
+ xml.css("error").inject('') do |all_faults, error|
23
+ if error.at("internalReason")
24
+ msg_error = error.at("internalReason").text
25
+ elsif error.at("location")
26
+ msg_error = error.at("location").text[/media:group\/media:(.*)\/text\(\)/,1]
27
+ else
28
+ msg_error = "Unspecified error"
29
+ end
30
+ code = error.at("code").text if error.at("code")
31
+ all_faults + sprintf("%s: %s\n", msg_error, code)
32
+ end
33
+ else
34
+ string[/<TITLE>(.+)<\/TITLE>/, 1] || string
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,17 @@
1
+ class Tubeclip
2
+ module Model
3
+ class Activity < Tubeclip::Record
4
+ # Attributes common to multiple activity types
5
+ attr_reader :type, :author, :videos, :video_id, :time
6
+
7
+ # video_rated
8
+ attr_reader :user_rating, :video_rating
9
+
10
+ # video_commented
11
+ attr_reader :comment_thread_url, :video_url
12
+
13
+ # friend_added and user_subscription_added
14
+ attr_reader :username
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,13 @@
1
+ class Tubeclip
2
+ module Model
3
+ class Author < Tubeclip::Record
4
+ # *String*: Author's YouTube username.
5
+ attr_reader :name
6
+
7
+ # *String*: Feed URL of the author.
8
+ attr_reader :uri
9
+
10
+ attr_reader :thumbnail_url
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,7 @@
1
+ class Tubeclip
2
+ module Model
3
+ class Caption < Tubeclip::Record
4
+ attr_reader :id, :title, :published
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,11 @@
1
+ class Tubeclip
2
+ module Model
3
+ class Category < Tubeclip::Record
4
+ # *String*:: Name of the YouTube category
5
+ attr_reader :label
6
+
7
+ # *String*:: Identifies the type of item described.
8
+ attr_reader :term
9
+ end
10
+ end
11
+ end