Class: YouTubeIt::Client

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/youtube_it/client.rb

Direct Known Subclasses

AuthSubClient, OAuth2Client, OAuthClient

Instance Method Summary collapse

Methods included from Logging

#logger

Constructor Details

#initialize(*params) ⇒ Client

Previously this was a logger instance but we now do it globally



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/youtube_it/client.rb', line 6

def initialize *params
  if params.first.is_a?(Hash)
    hash_options = params.first
    @user                  = hash_options[:username]
    @pass                  = hash_options[:password]
    @dev_key               = hash_options[:dev_key]
    @client_id             = hash_options[:client_id] || "youtube_it"
    @legacy_debug_flag     = hash_options[:debug]
  elsif params.first
    puts "* warning: the method YouTubeIt::Client.new(user, passwd, dev_key) is deprecated, use YouTubeIt::Client.new(:username => 'user', :password => 'passwd', :dev_key => 'dev_key')"
    @user               = params.shift
    @pass               = params.shift
    @dev_key            = params.shift
    @client_id          = params.shift || "youtube_it"
    @legacy_debug_flag  = params.shift
  end
end

Instance Method Details

#activity(user = nil, opts = {}) ⇒ Object

Fetches a user's activity feed.



153
154
155
# File 'lib/youtube_it/client.rb', line 153

def activity(user = nil, opts = {})
  client.get_activity(user, opts)
end

#add_comment(video_id, comment) ⇒ Object



125
126
127
# File 'lib/youtube_it/client.rb', line 125

def add_comment(video_id, comment)
  client.add_comment(video_id, comment)
end

#add_favorite(video_id) ⇒ Object



136
137
138
# File 'lib/youtube_it/client.rb', line 136

def add_favorite(video_id)
  client.add_favorite(video_id)
end

#add_playlist(options) ⇒ Object



165
166
167
# File 'lib/youtube_it/client.rb', line 165

def add_playlist(options)
  client.add_playlist(options)
end

#add_response(original_video_id, response_video_id) ⇒ Object



209
210
211
# File 'lib/youtube_it/client.rb', line 209

def add_response(original_video_id, response_video_id)
  client.add_response(original_video_id, response_video_id)
end

#add_video_to_playlist(playlist_id, video_id) ⇒ Object



173
174
175
# File 'lib/youtube_it/client.rb', line 173

def add_video_to_playlist(playlist_id, video_id)
  client.add_video_to_playlist(playlist_id, video_id)
end

#comments(video_id, opts = {}) ⇒ Object

opts is converted to get params and appended to comments gdata api url eg opts = { 'max-results' => 10, 'start-index' => 20 } hash does not play nice with symbols



132
133
134
# File 'lib/youtube_it/client.rb', line 132

def comments(video_id, opts = {})
  client.comments(video_id, opts)
end

#current_userObject



217
218
219
# File 'lib/youtube_it/client.rb', line 217

def current_user
  client.get_current_user
end

#delete_favorite(video_id) ⇒ Object



140
141
142
# File 'lib/youtube_it/client.rb', line 140

def delete_favorite(video_id)
  client.delete_favorite(video_id)
end

#delete_playlist(playlist_id) ⇒ Object



181
182
183
# File 'lib/youtube_it/client.rb', line 181

def delete_playlist(playlist_id)
  client.delete_playlist(playlist_id)
end

#delete_response(original_video_id, response_video_id) ⇒ Object



213
214
215
# File 'lib/youtube_it/client.rb', line 213

def delete_response(original_video_id, response_video_id)
  client.delete_response(original_video_id, response_video_id)
end

#delete_video_from_playlist(playlist_id, playlist_entry_id) ⇒ Object



177
178
179
# File 'lib/youtube_it/client.rb', line 177

def delete_video_from_playlist(playlist_id, playlist_entry_id)
  client.delete_video_from_playlist(playlist_id, playlist_entry_id)
end

#dislike_video(video_id) ⇒ Object



189
190
191
# File 'lib/youtube_it/client.rb', line 189

def dislike_video(video_id)
  client.rate_video(video_id, 'dislike')
end

#enable_http_debuggingObject



205
206
207
# File 'lib/youtube_it/client.rb', line 205

def enable_http_debugging
  client.enable_http_debugging
end

#favorites(user = nil, opts = {}) ⇒ Object



144
145
146
# File 'lib/youtube_it/client.rb', line 144

def favorites(user = nil, opts = {})
  client.favorites(user, opts)
end

#like_video(video_id) ⇒ Object



185
186
187
# File 'lib/youtube_it/client.rb', line 185

def like_video(video_id)
  client.rate_video(video_id, 'like')
end

#message_delete(message_id) ⇒ Object



117
118
119
# File 'lib/youtube_it/client.rb', line 117

def message_delete(message_id)
  client.delete_message(message_id)
end

#my_contacts(opts = {}) ⇒ Object

Gets all of the user's contacts/friends.



232
233
234
# File 'lib/youtube_it/client.rb', line 232

def my_contacts(opts = {})
  client.get_my_contacts(opts)
end

#my_messages(opts = {}) ⇒ Object

Gets all of the user's messages/inbox.



242
243
244
# File 'lib/youtube_it/client.rb', line 242

def my_messages(opts = {})
  client.get_my_messages(opts)
end

#my_video(video_id) ⇒ Object

Gets the authenticated users video with the given ID. It may be private.



222
223
224
# File 'lib/youtube_it/client.rb', line 222

def my_video(video_id)
  client.get_my_video(video_id)
end

#my_videos(opts = {}) ⇒ Object

Gets all videos



227
228
229
# File 'lib/youtube_it/client.rb', line 227

def my_videos(opts = {})
  client.get_my_videos(opts)
end

#new_subscription_videos(user_id = nil) ⇒ Object

Gets new subscription videos



252
253
254
# File 'lib/youtube_it/client.rb', line 252

def new_subscription_videos(user_id = nil)
  client.new_subscription_videos(user_id)
end

#playlist(playlist_id) ⇒ Object



157
158
159
# File 'lib/youtube_it/client.rb', line 157

def playlist(playlist_id)
  client.playlist playlist_id
end

#playlists(user = nil) ⇒ Object



161
162
163
# File 'lib/youtube_it/client.rb', line 161

def playlists(user = nil)
  client.playlists(user)
end

#profile(user = nil) ⇒ Object



148
149
150
# File 'lib/youtube_it/client.rb', line 148

def profile(user = nil)
  client.profile(user)
end

#send_message(opts = {}) ⇒ Object

Send video message



237
238
239
# File 'lib/youtube_it/client.rb', line 237

def send_message(opts = {})
  client.send_message(opts)
end

#subscribe_channel(channel_name) ⇒ Object



193
194
195
# File 'lib/youtube_it/client.rb', line 193

def subscribe_channel(channel_name)
  client.subscribe_channel(channel_name)
end

#subscriptions(user_id = nil) ⇒ Object



201
202
203
# File 'lib/youtube_it/client.rb', line 201

def subscriptions(user_id = nil)
  client.subscriptions(user_id)
end

#unsubscribe_channel(subscription_id) ⇒ Object



197
198
199
# File 'lib/youtube_it/client.rb', line 197

def unsubscribe_channel(subscription_id)
  client.unsubscribe_channel(subscription_id)
end

#update_playlist(playlist_id, options) ⇒ Object



169
170
171
# File 'lib/youtube_it/client.rb', line 169

def update_playlist(playlist_id, options)
  client.update_playlist(playlist_id, options)
end

#upload_token(options, nexturl = "http://www.youtube.com/my_videos") ⇒ Object



121
122
123
# File 'lib/youtube_it/client.rb', line 121

def upload_token(options, nexturl = "http://www.youtube.com/my_videos")
  client.get_upload_token(options, nexturl)
end

#video_by(video) ⇒ Object

Retrieves a single YouTube video.

Parameters

vid

The ID or URL of the video that you'd like to retrieve.

user

The user that uploaded the video that you'd like to retrieve.

Returns

YouTubeIt::Model::Video



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/youtube_it/client.rb', line 86

def video_by(video)
  vid = nil
  vid_regex = /(?:youtube.com|youtu.be).*(?:\/|v=)([\w-]+)/
  if video =~ vid_regex
    vid = $1
  else
    vid = video
  end
  video_id ="http://gdata.youtube.com/feeds/api/videos/#{vid}?v=2#{@dev_key ? '&key='+@dev_key : ''}"
  parser = YouTubeIt::Parser::VideoFeedParser.new(video_id)
  parser.parse
end

#video_by_user(user, vid) ⇒ Object



99
100
101
102
103
# File 'lib/youtube_it/client.rb', line 99

def video_by_user(user, vid)
  video_id = "http://gdata.youtube.com/feeds/api/users/#{user}/uploads/#{vid}?v=2#{@dev_key ? '&key='+@dev_key : ''}"
  parser = YouTubeIt::Parser::VideoFeedParser.new(video_id)
  parser.parse
end

#video_delete(video_id) ⇒ Object



113
114
115
# File 'lib/youtube_it/client.rb', line 113

def video_delete(video_id)
  client.delete(video_id)
end

#video_update(video_id, opts = {}) ⇒ Object



109
110
111
# File 'lib/youtube_it/client.rb', line 109

def video_update(video_id, opts = {})
  client.update(video_id, opts)
end

#video_upload(data, opts = {}) ⇒ Object



105
106
107
# File 'lib/youtube_it/client.rb', line 105

def video_upload(data, opts = {})
  client.upload(data, opts)
end

#videos_by(params, options = {}) ⇒ Object

Retrieves an array of standard feed, custom query, or user videos.

Parameters

If fetching videos for a standard feed:

params<Symbol>:: Accepts a symbol of :top_rated, :top_favorites, :most_viewed,
               :most_popular, :most_recent, :most_discussed, :most_linked,
               :most_responded, :recently_featured, and :watch_on_mobile.

You can find out more specific information about what each standard feed provides
by visiting: http://code.google.com/apis/youtube/reference.html#Standard_feeds

options<Hash> (optional)::  Accepts the options of :time, :page (default is 1),
                          and :per_page (default is 25). :offset and :max_results
                          can also be passed for a custom offset.

If fetching videos by tags, categories, query:

params<Hash>:: Accepts the keys :tags, :categories, :query, :order_by,
             :author, :safe_search, :response_format, :video_format, :page (default is 1),
             and :per_page(default is 25)

options<Hash>:: Not used. (Optional)

If fetching videos for a particular user:

params<Hash>:: Key of :user with a value of the username.
options<Hash>:: Not used. (Optional)

Returns

YouTubeIt::Response::VideoSearch



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/youtube_it/client.rb', line 51

def videos_by(params, options={})
  request_params = params.respond_to?(:to_hash) ? params : options
  request_params[:page] = integer_or_default(request_params[:page], 1)

  request_params[:dev_key] = @dev_key if @dev_key

  unless request_params[:max_results]
    request_params[:max_results] = integer_or_default(request_params[:per_page], 25)
  end

  unless request_params[:offset]
    request_params[:offset] = calculate_offset(request_params[:page], request_params[:max_results] )
  end

  if params.respond_to?(:to_hash) and not params[:user]
    request = YouTubeIt::Request::VideoSearch.new(request_params)
  elsif (params.respond_to?(:to_hash) && params[:user]) || (params == :favorites)
    request = YouTubeIt::Request::UserSearch.new(params, request_params)
  else
    request = YouTubeIt::Request::StandardSearch.new(params, request_params)
  end

  logger.debug "Submitting request [url=#{request.url}]." if @legacy_debug_flag
  parser = YouTubeIt::Parser::VideosFeedParser.new(request.url)
  parser.parse
end

#watch_historyObject

Gets the user's watch history



247
248
249
# File 'lib/youtube_it/client.rb', line 247

def watch_history
  client.get_watch_history
end