class Tubeclip::Client

Public Class Methods

new(*params) click to toggle source

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

# File lib/tubeclip/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] || "tubeclip"
    @legacy_debug_flag = hash_options[:debug]
  elsif params.first
    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')"
    @user = params.shift
    @pass = params.shift
    @dev_key = params.shift
    @client_id = params.shift || "tubeclip"
    @legacy_debug_flag = params.shift
  end
end

Public Instance Methods

activity(user = nil, opts = {}) click to toggle source

Fetches a user's activity feed.

# File lib/tubeclip/client.rb, line 184
def activity(user = nil, opts = {})
  client.get_activity(user, opts)
end
add_comment(video_id, comment, opts = {}) click to toggle source
# File lib/tubeclip/client.rb, line 144
def add_comment(video_id, comment, opts = {})
  client.add_comment(video_id, comment, opts)
end
add_favorite(video_id) click to toggle source
# File lib/tubeclip/client.rb, line 159
def add_favorite(video_id)
  client.add_favorite(video_id)
end
add_playlist(options) click to toggle source
# File lib/tubeclip/client.rb, line 224
def add_playlist(options)
  client.add_playlist(options)
end
add_response(original_video_id, response_video_id) click to toggle source
# File lib/tubeclip/client.rb, line 272
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, position = nil) click to toggle source
# File lib/tubeclip/client.rb, line 232
def add_video_to_playlist(playlist_id, video_id, position = nil)
  client.add_video_to_playlist(playlist_id, video_id, position)
end
add_video_to_watchlater(video_id) click to toggle source
# File lib/tubeclip/client.rb, line 192
def add_video_to_watchlater(video_id)
  client.add_video_to_watchlater(video_id)
end
all_playlists(user = nil) click to toggle source

Fetches all playlists for a given user by repeatedly making requests for as many pages of playlists as the user has. Note that this can take a long time if the user has many playlists.

# File lib/tubeclip/client.rb, line 211
def all_playlists(user = nil)
  newest_playlists, all_playlists_for_user = [], []
  start_index, page_size = 1, 25

  begin
    newest_playlists = playlists(user, {'start-index' => start_index, 'max-results' => page_size})
    all_playlists_for_user += newest_playlists
    start_index += page_size
  end while newest_playlists && newest_playlists.size == page_size

  all_playlists_for_user
end
captions_update(video_id, data, opts = {}) click to toggle source
# File lib/tubeclip/client.rb, line 128
def captions_update(video_id, data, opts = {})
  client.captions_update(video_id, data, opts)
end
comments(video_id, opts = {}) click to toggle source

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

# File lib/tubeclip/client.rb, line 155
def comments(video_id, opts = {})
  client.comments(video_id, opts)
end
current_user() click to toggle source
# File lib/tubeclip/client.rb, line 280
def current_user
  client.get_current_user
end
delete_comment(video_id, comment_id) click to toggle source
# File lib/tubeclip/client.rb, line 148
def delete_comment(video_id, comment_id)
  client.delete_comment(video_id, comment_id)
end
delete_favorite(video_id) click to toggle source
# File lib/tubeclip/client.rb, line 163
def delete_favorite(video_id)
  client.delete_favorite(video_id)
end
delete_playlist(playlist_id) click to toggle source
# File lib/tubeclip/client.rb, line 244
def delete_playlist(playlist_id)
  client.delete_playlist(playlist_id)
end
delete_response(original_video_id, response_video_id) click to toggle source
# File lib/tubeclip/client.rb, line 276
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) click to toggle source
# File lib/tubeclip/client.rb, line 240
def delete_video_from_playlist(playlist_id, playlist_entry_id)
  client.delete_video_from_playlist(playlist_id, playlist_entry_id)
end
delete_video_from_watchlater(video_id) click to toggle source
# File lib/tubeclip/client.rb, line 196
def delete_video_from_watchlater(video_id)
  client.delete_video_from_watchlater(video_id)
end
dislike_video(video_id) click to toggle source
# File lib/tubeclip/client.rb, line 252
def dislike_video(video_id)
  client.rate_video(video_id, 'dislike')
end
enable_http_debugging() click to toggle source
# File lib/tubeclip/client.rb, line 268
def enable_http_debugging
  client.enable_http_debugging
end
favorites(user = nil, opts = {}) click to toggle source
# File lib/tubeclip/client.rb, line 167
def favorites(user = nil, opts = {})
  client.favorites(user, opts)
end
get_all_videos(opts) click to toggle source
# File lib/tubeclip/client.rb, line 105
def get_all_videos(opts)
  page = videos_by(opts.merge({:page => 1}))
  videos = page.videos

  while page.next_page && (page = videos_by(opts.merge({:page => page.next_page})) || true)
   videos += page.videos
  end

  videos
end
like_video(video_id) click to toggle source
# File lib/tubeclip/client.rb, line 248
def like_video(video_id)
  client.rate_video(video_id, 'like')
end
message_delete(message_id) click to toggle source
# File lib/tubeclip/client.rb, line 136
def message_delete(message_id)
  client.delete_message(message_id)
end
my_contacts(opts = {}) click to toggle source

Gets all of the user's contacts/friends.

# File lib/tubeclip/client.rb, line 295
def my_contacts(opts = {})
  client.get_my_contacts(opts)
end
my_messages(opts = {}) click to toggle source

Gets all of the user's messages/inbox.

# File lib/tubeclip/client.rb, line 305
def my_messages(opts = {})
  client.get_my_messages(opts)
end
my_video(video_id) click to toggle source

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

# File lib/tubeclip/client.rb, line 285
def my_video(video_id)
  client.get_my_video(video_id)
end
my_videos(opts = {}) click to toggle source

Gets all videos

# File lib/tubeclip/client.rb, line 290
def my_videos(opts = {})
  client.get_my_videos(opts)
end
new_subscription_videos(user_id = nil) click to toggle source

Gets new subscription videos

# File lib/tubeclip/client.rb, line 315
def new_subscription_videos(user_id = nil)
  client.new_subscription_videos(user_id)
end
playlist(playlist_id, opts = {}) click to toggle source
# File lib/tubeclip/client.rb, line 200
def playlist(playlist_id, opts = {})
  client.playlist(playlist_id, opts)
end
playlists(user = nil, opts = nil) click to toggle source
# File lib/tubeclip/client.rb, line 204
def playlists(user = nil, opts = nil)
  client.playlists(user, opts)
end
profile(user = nil) click to toggle source
# File lib/tubeclip/client.rb, line 171
def profile(user = nil)
  client.profile(user)
end
profiles(*users) click to toggle source
# File lib/tubeclip/client.rb, line 175
def profiles(*users)
  client.profiles(*users)
end
send_message(opts = {}) click to toggle source

Send video message

# File lib/tubeclip/client.rb, line 300
def send_message(opts = {})
  client.send_message(opts)
end
subscribe_channel(channel_name) click to toggle source
# File lib/tubeclip/client.rb, line 256
def subscribe_channel(channel_name)
  client.subscribe_channel(channel_name)
end
subscriptions(user_id = nil) click to toggle source
# File lib/tubeclip/client.rb, line 264
def subscriptions(user_id = nil)
  client.subscriptions(user_id)
end
unsubscribe_channel(subscription_id) click to toggle source
# File lib/tubeclip/client.rb, line 260
def unsubscribe_channel(subscription_id)
  client.unsubscribe_channel(subscription_id)
end
update_playlist(playlist_id, options) click to toggle source
# File lib/tubeclip/client.rb, line 228
def update_playlist(playlist_id, options)
  client.update_playlist(playlist_id, options)
end
update_position_video_from_playlist(playlist_id, playlist_entry_id, position = nil) click to toggle source
# File lib/tubeclip/client.rb, line 236
def update_position_video_from_playlist(playlist_id, playlist_entry_id, position = nil)
  client.update_position_video_from_playlist(playlist_id, playlist_entry_id, position)
end
upload_token(options, nexturl = "http://www.youtube.com/my_videos") click to toggle source
# File lib/tubeclip/client.rb, line 140
def upload_token(options, nexturl = "http://www.youtube.com/my_videos")
  client.get_upload_token(options, nexturl)
end
video_by(video) click to toggle source

Retrieves a single YouTube video.

Parameters

vid<String>:: The ID or URL of the video that you'd like to retrieve.
user<String>:: The user that uploaded the video that you'd like to retrieve.

Returns

Tubeclip::Model::Video

# File lib/tubeclip/client.rb, line 86
def video_by(video)
  vid = nil
  vid_regex = /(?:youtube.com|youtu.be).*(?:\/|v=)([a-zA-Z0-9_-]+)/
  if video =~ vid_regex
    vid = $1
  else
    vid = video
  end
  video_id ="http://gdata.youtube.com/feeds/api/videos/#{vid}?v=#{Tubeclip::API_VERSION}#{@dev_key ? '&key='+@dev_key : ''}"
  parser = Tubeclip::Parser::VideoFeedParser.new(video_id)
  parser.parse
end
video_by_user(user, vid) click to toggle source
# File lib/tubeclip/client.rb, line 99
def video_by_user(user, vid)
  video_id = "http://gdata.youtube.com/feeds/api/users/#{user}/uploads/#{vid}?v=#{Tubeclip::API_VERSION}#{@dev_key ? '&key='+@dev_key : ''}"
  parser = Tubeclip::Parser::VideoFeedParser.new(video_id)
  parser.parse
end
video_delete(video_id) click to toggle source
# File lib/tubeclip/client.rb, line 132
def video_delete(video_id)
  client.delete(video_id)
end
video_partial_update(video_id, opts = {}) click to toggle source
# File lib/tubeclip/client.rb, line 124
def video_partial_update(video_id, opts = {})
  client.partial_update(video_id, opts)
end
video_update(video_id, opts = {}) click to toggle source
# File lib/tubeclip/client.rb, line 120
def video_update(video_id, opts = {})
  client.update(video_id, opts)
end
video_upload(data, opts = {}) click to toggle source
# File lib/tubeclip/client.rb, line 116
def video_upload(data, opts = {})
  client.upload(data, opts)
end
videos(*idxes) click to toggle source
# File lib/tubeclip/client.rb, line 179
def videos(*idxes)
  client.videos(*idxes)
end
videos_by(params, options={}) click to toggle source

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

Tubeclip::Response::VideoSearch

# File lib/tubeclip/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 = Tubeclip::Request::VideoSearch.new(request_params)
  elsif (params.respond_to?(:to_hash) && params[:user]) || (params == :favorites)
    request = Tubeclip::Request::UserSearch.new(params, request_params)
  else
    request = Tubeclip::Request::StandardSearch.new(params, request_params)
  end

  logger.debug "Submitting request [url=#{request.url}]." if @legacy_debug_flag
  parser = Tubeclip::Parser::VideosFeedParser.new(request.url)
  parser.parse
end
watch_history() click to toggle source

Gets the user's watch history

# File lib/tubeclip/client.rb, line 310
def watch_history
  client.get_watch_history
end
watchlater(user = nil) click to toggle source
# File lib/tubeclip/client.rb, line 188
def watchlater(user = nil)
  client.watchlater(user)
end

Private Instance Methods

calculate_offset(page, per_page) click to toggle source
# File lib/tubeclip/client.rb, line 325
def calculate_offset(page, per_page)
  page == 1 ? 1 : ((per_page * page) - per_page + 1)
end
client() click to toggle source
# File lib/tubeclip/client.rb, line 321
def client
  @client ||= Tubeclip::Upload::VideoUpload.new(:username => @user, :password => @pass, :dev_key => @dev_key)
end
integer_or_default(value, default) click to toggle source
# File lib/tubeclip/client.rb, line 329
def integer_or_default(value, default)
  value = value.to_i
  value > 0 ? value : default
end