Class: YouTubeIt::Upload::VideoUpload

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

Instance Method Summary collapse

Methods included from Logging

#logger

Constructor Details

#initialize(*params) ⇒ VideoUpload

Returns a new instance of VideoUpload.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/youtube_it/request/video_upload.rb', line 6

def initialize *params
  if params.first.is_a?(Hash)
    hash_options = params.first
    @user                          = hash_options[:username]
    @password                      = hash_options[:password]
    @dev_key                       = hash_options[:dev_key]
    @access_token                  = hash_options[:access_token]
    @authsub_token                 = hash_options[:authsub_token]
    @client_id                     = hash_options[:client_id] || "youtube_it"
    @config_token                  = hash_options[:config_token]
  else
    puts "* warning: the method YouTubeIt::Upload::VideoUpload.new(username, password, dev_key) is depricated, use YouTubeIt::Upload::VideoUpload.new(:username => 'user', :password => 'passwd', :dev_key => 'dev_key')"
    @user                          = params.shift
    @password                      = params.shift
    @dev_key                       = params.shift
    @access_token                  = params.shift
    @authsub_token                 = params.shift
    @client_id                     = params.shift || "youtube_it"
    @config_token                  = params.shift
  end
end

Instance Method Details

#add_comment(video_id, comment) ⇒ Object



137
138
139
140
141
142
143
# File 'lib/youtube_it/request/video_upload.rb', line 137

def add_comment(video_id, comment)
  comment_body = video_xml_for(:comment => comment)
  comment_url  = "/feeds/api/videos/%s/comments" % video_id
  response     = yt_session.post(comment_url, comment_body)
  
  return {:code => response.status, :body => response.body}
end

#add_favorite(video_id) ⇒ Object



153
154
155
156
157
158
159
# File 'lib/youtube_it/request/video_upload.rb', line 153

def add_favorite(video_id)
  favorite_body = video_xml_for(:favorite => video_id)
  favorite_url  = "/feeds/api/users/default/favorites"
  response      = yt_session.post(favorite_url, favorite_body)
  
  return {:code => response.status, :body => response.body}
end

#add_playlist(options) ⇒ Object



192
193
194
195
196
197
198
# File 'lib/youtube_it/request/video_upload.rb', line 192

def add_playlist(options)
  playlist_body = video_xml_for_playlist(options)
  playlist_url  = "/feeds/api/users/default/playlists"
  response      = yt_session.post(playlist_url, playlist_body)
  
  return YouTubeIt::Parser::PlaylistFeedParser.new(response).parse
end

#add_video_to_playlist(playlist_id, video_id) ⇒ Object



200
201
202
203
204
205
206
# File 'lib/youtube_it/request/video_upload.rb', line 200

def add_video_to_playlist(playlist_id, video_id)
  playlist_body = video_xml_for(:playlist => video_id)
  playlist_url  = "/feeds/api/playlists/%s" % playlist_id
  response      = yt_session.post(playlist_url, playlist_body)
  
  return {:code => response.status, :body => response.body, :playlist_entry_id => playlist_entry_id_from_playlist(response.body)}
end

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



145
146
147
148
149
150
151
# File 'lib/youtube_it/request/video_upload.rb', line 145

def comments(video_id, opts = {})
  comment_url = "/feeds/api/videos/%s/comments?" % video_id
  comment_url << opts.collect { |k,p| [k,p].join '=' }.join('&')
  response    = yt_session.get(comment_url)
  
  return YouTubeIt::Parser::CommentsFeedParser.new(response).parse
end

#delete(video_id) ⇒ Object

Delete a video on YouTube



120
121
122
123
124
125
# File 'lib/youtube_it/request/video_upload.rb', line 120

def delete(video_id)
  delete_url = "/feeds/api/users/default/uploads/%s" % video_id
  response   = yt_session.delete(delete_url)

  return true
end

#delete_favorite(video_id) ⇒ Object



161
162
163
164
165
166
167
168
169
# File 'lib/youtube_it/request/video_upload.rb', line 161

def delete_favorite(video_id)
  favorite_header = {
    "GData-Version"  => "1",
  }
  favorite_url = "/feeds/api/users/default/favorites/%s" % video_id
  response     = yt_session.delete(favorite_url, favorite_header)
  
  return true
end

#delete_playlist(playlist_id) ⇒ Object



223
224
225
226
227
228
# File 'lib/youtube_it/request/video_upload.rb', line 223

def delete_playlist(playlist_id)
  playlist_url = "/feeds/api/users/default/playlists/%s" % playlist_id
  response     = yt_session.delete(playlist_url)
  
  return true
end

#delete_video_from_playlist(playlist_id, playlist_entry_id) ⇒ Object



216
217
218
219
220
221
# File 'lib/youtube_it/request/video_upload.rb', line 216

def delete_video_from_playlist(playlist_id, playlist_entry_id)
  playlist_url = "/feeds/api/playlists/%s/%s" % [playlist_id, playlist_entry_id]
  response     = yt_session.delete(playlist_url)
  
  return true
end

#enable_http_debuggingObject



29
30
31
# File 'lib/youtube_it/request/video_upload.rb', line 29

def enable_http_debugging
  @http_debugging = true
end

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



260
261
262
263
264
265
# File 'lib/youtube_it/request/video_upload.rb', line 260

def favorites(user, opts = {})
  favorite_url = "/feeds/api/users/%s/favorites#{opts.empty? ? '' : '?#{opts.to_param}'}" % (user ? user : "default")
  response     = yt_session.get(favorite_url)

  return YouTubeIt::Parser::VideosFeedParser.new(response.body).parse
end

#get_current_userObject



267
268
269
270
271
272
# File 'lib/youtube_it/request/video_upload.rb', line 267

def get_current_user
  current_user_url = "/feeds/api/users/default"
  response         = yt_session.get(current_user_url, authorization_headers)

  return REXML::Document.new(response.body).elements["entry"].elements['author'].elements['name'].text
end

#get_my_video(video_id) ⇒ Object

Fetches the data of a video, which may be private. The video must be owned by this user. When the authentication credentials are incorrect, an AuthenticationError will be raised.



101
102
103
104
105
106
# File 'lib/youtube_it/request/video_upload.rb', line 101

def get_my_video(video_id)
  get_url  = "/feeds/api/users/default/uploads/%s" % video_id
  response = yt_session.get(get_url)
  
  return YouTubeIt::Parser::VideoFeedParser.new(response.body).parse
end

#get_my_videos(opts) ⇒ Object

Fetches the data of the videos of the current user, which may be private. When the authentication credentials are incorrect, an AuthenticationError will be raised.



110
111
112
113
114
115
116
117
# File 'lib/youtube_it/request/video_upload.rb', line 110

def get_my_videos(opts)
  max_results = opts[:per_page] || 50
  start_index = ((opts[:page] || 1) -1) * max_results +1
  get_url     = "/feeds/api/users/default/uploads?max-results=#{max_results}&start-index=#{start_index}"
  response    = yt_session.get(get_url)

  return YouTubeIt::Parser::VideosFeedParser.new(response.body).parse
end

#get_upload_token(options, nexturl) ⇒ Object



127
128
129
130
131
132
133
134
135
# File 'lib/youtube_it/request/video_upload.rb', line 127

def get_upload_token(options, nexturl)
  @opts      = options
  token_body = video_xml
  token_url  = "/action/GetUploadToken"
  response   = yt_session.post(token_url, token_body)
  
  return {:url    => "#{response.body[/<url>(.+)<\/url>/, 1]}?nexturl=#{nexturl}",
          :token  => response.body[/<token>(.+)<\/token>/, 1]}
end

#playlist(playlist_id) ⇒ Object



178
179
180
181
182
183
# File 'lib/youtube_it/request/video_upload.rb', line 178

def playlist(playlist_id)
  playlist_url = "/feeds/api/playlists/%s?v=2" % playlist_id
  response     = yt_session.get(playlist_url)
  
  return YouTubeIt::Parser::PlaylistFeedParser.new(response).parse
end

#playlists(user) ⇒ Object



185
186
187
188
189
190
# File 'lib/youtube_it/request/video_upload.rb', line 185

def playlists(user)
  playlist_url = "/feeds/api/users/%s/playlists?v=2" % (user ? user : "default")
  response     = yt_session.get(playlist_url)
  
  return YouTubeIt::Parser::PlaylistsFeedParser.new(response).parse
end

#profile(user) ⇒ Object



171
172
173
174
175
176
# File 'lib/youtube_it/request/video_upload.rb', line 171

def profile(user)
  profile_url = "/feeds/api/users/%s?v=2" % (user ? user : "default")
  response    = yt_session.get(profile_url)

  return YouTubeIt::Parser::ProfileFeedParser.new(response).parse
end

#rate_video(video_id, rating) ⇒ Object



230
231
232
233
234
235
236
# File 'lib/youtube_it/request/video_upload.rb', line 230

def rate_video(video_id, rating)
  rating_body = video_xml_for(:rating => rating)
  rating_url  = "/feeds/api/videos/#{video_id}/ratings"
  response    = yt_session.post(rating_url, rating_body)
  
  return {:code => response.status, :body => response.body}
end

#subscribe_channel(channel_name) ⇒ Object



245
246
247
248
249
250
251
# File 'lib/youtube_it/request/video_upload.rb', line 245

def subscribe_channel(channel_name)
  subscribe_body = video_xml_for(:subscribe => channel_name)
  subscribe_url  = "/feeds/api/users/default/subscriptions"
  response       = yt_session.post(subscribe_url, subscribe_body)
   
  return {:code => response.status, :body => response.body}
end

#subscriptions(user) ⇒ Object



238
239
240
241
242
243
# File 'lib/youtube_it/request/video_upload.rb', line 238

def subscriptions(user)
  subscription_url = "/feeds/api/users/%s/subscriptions?v=2" % (user ? user : "default")
  response         = yt_session.get(subscription_url)
  
  return YouTubeIt::Parser::SubscriptionFeedParser.new(response).parse
end

#unsubscribe_channel(subscription_id) ⇒ Object



253
254
255
256
257
258
# File 'lib/youtube_it/request/video_upload.rb', line 253

def unsubscribe_channel(subscription_id)
  unsubscribe_url = "/feeds/api/users/default/subscriptions/%s" % subscription_id
  response        = yt_session.delete(unsubscribe_url)
     
  return {:code => response.status, :body => response.body}
end

#update(video_id, options) ⇒ Object

Updates a video in YouTube. Requires:

:title
:description
:category
:keywords

The following are optional attributes:

:private

When the authentication credentials are incorrect, an AuthenticationError will be raised.



90
91
92
93
94
95
96
97
# File 'lib/youtube_it/request/video_upload.rb', line 90

def update(video_id, options)
  @opts       = options
  update_body = video_xml
  update_url  = "/feeds/api/users/default/uploads/%s" % video_id
  response    = yt_session.put(update_url, update_body)
  
  return YouTubeIt::Parser::VideoFeedParser.new(response.body).parse
end

#update_playlist(playlist_id, options) ⇒ Object



208
209
210
211
212
213
214
# File 'lib/youtube_it/request/video_upload.rb', line 208

def update_playlist(playlist_id, options)
  playlist_body = video_xml_for_playlist(options)
  playlist_url  = "/feeds/api/users/default/playlists/%s" % playlist_id
  response      = yt_session.put(playlist_url, playlist_body)
  
  return YouTubeIt::Parser::PlaylistFeedParser.new(response).parse
end

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

Upload “data” to youtube, where data is either an IO object or raw file data. The hash keys for opts (which specify video info) are as follows:

:mime_type
:filename
:title
:description
:category
:keywords
:private

New V2 api hash keys for accessControl:

:rate
:comment
:commentVote
:videoRespond
:list
:embed
:syndicate

Specifying :private will make the video private, otherwise it will be public.

When one of the fields is invalid according to YouTube, an UploadError will be raised. Its message contains a list of newline separated errors, containing the key and its error code.

When the authentication credentials are incorrect, an AuthenticationError will be raised.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/youtube_it/request/video_upload.rb', line 59

def upload(data, opts = {})
  @opts    = { :mime_type => 'video/mp4',
               :title => '',
               :description => '',
               :category => '',
               :keywords => [] }.merge(opts)

  @opts[:filename] ||= generate_uniq_filename_from(data)

  post_body_io = generate_upload_io(video_xml, data)

  upload_header = {
      "Slug"           => "#{@opts[:filename]}",
      "Content-Type"   => "multipart/related; boundary=#{boundary}",
      "Content-Length" => "#{post_body_io.expected_length}",
  }

  upload_url = "/feeds/api/users/default/uploads"
  response = yt_session(uploads_url).post(upload_url, post_body_io, upload_header)

  return YouTubeIt::Parser::VideoFeedParser.new(response.body).parse
end