class YouTubeG::Upload::VideoUpload

require ‘youtube_g’

uploader = ::new(“user”, “pass”, “dev-key”) uploader.upload File.open(“test.m4v”), :title => ‘test’,

:description => 'cool vid d00d',
:category => 'People',
:keywords => %w[cool blah test]

Public Instance Methods

upload(data, opts = {}) click to toggle source

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

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 returned. 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.

# File lib/youtube_g/request/video_upload.rb, line 39
def upload data, opts = {}
  data = data.respond_to?(:read) ? data.read : data
  @opts = { :mime_type => 'video/mp4',
            :filename => Digest::MD5.hexdigest(data),
            :title => '',
            :description => '',
            :category => '',
            :keywords => [] }.merge(opts)

  uploadBody = generate_upload_body(boundary, video_xml, data)

  uploadHeader = {
    "Authorization"  => "GoogleLogin auth=#{auth_token}",
    "X-GData-Client" => "#{@client_id}",
    "X-GData-Key"    => "key=#{@dev_key}",
    "Slug"           => "#{@opts[:filename]}",
    "Content-Type"   => "multipart/related; boundary=#{boundary}",
    "Content-Length" => "#{uploadBody.length}"
  }

  Net::HTTP.start(base_url) do |upload|
    response = upload.post('/feeds/api/users/' << @user << '/uploads', uploadBody, uploadHeader)
    if response.code.to_i == 403
      raise AuthenticationError, response.body[/<TITLE>(.+)<\/TITLE>/, 1]
    elsif response.code.to_i != 201
      upload_error = ''
      xml = REXML::Document.new(response.body)
      errors = xml.elements["//errors"]
      errors.each do |error|
        location = error.elements["location"].text[/media:group\/media:(.*)\/text\(\)/,1]
        code = error.elements["code"].text
        upload_error << sprintf("%s: %s\r\n", location, code)
      end
      raise UploadError, upload_error
    end
    xml = REXML::Document.new(response.body)
    return xml.elements["//id"].text[/videos\/(.+)/, 1]
  end

end

Public Class Methods

new(user, pass, dev_key, client_id = 'youtube_g') click to toggle source
# File lib/youtube_g/request/video_upload.rb, line 17
def initialize user, pass, dev_key, client_id = 'youtube_g'
  @user, @pass, @dev_key, @client_id = user, pass, dev_key, client_id
end