Class: YouTubeG::Upload::VideoUpload

Inherits:
Object
  • Object
show all
Defined in:
lib/youtube_g/request/video_upload.rb

Overview

require ‘youtube_g’

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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, pass, dev_key, client_id = 'youtube_g', auth_sub_token = nil) ⇒ VideoUpload

Returns a new instance of VideoUpload.



23
24
25
26
27
# File 'lib/youtube_g/request/video_upload.rb', line 23

def initialize user, pass, dev_key, client_id = 'youtube_g', auth_sub_token = nil
  @user, @pass, @dev_key, @client_id, @auth_sub_token = user, pass, dev_key, client_id, auth_sub_token
  @auth_token = @auth_sub_token != nil ? @auth_sub_token : nil
  @logger = init_logger
end

Instance Attribute Details

#auth_tokenObject

Returns the value of attribute auth_token.



21
22
23
# File 'lib/youtube_g/request/video_upload.rb', line 21

def auth_token
  @auth_token
end

Instance Method Details

#get_auth_headerObject



105
106
107
108
109
110
111
112
# File 'lib/youtube_g/request/video_upload.rb', line 105

def get_auth_header 
  logger.debug("@auth_sub_token [#{@auth_sub_token}]") 
  if @auth_sub_token
    return YouTubeG::Upload::AUTH_SUB_HEADER+derive_auth_token
  else
    return YouTubeG::Upload::CLIENT_LOGIN_HEADER+derive_auth_token
  end 
end

#get_rails_default_logger_nameObject



42
43
44
# File 'lib/youtube_g/request/video_upload.rb', line 42

def get_rails_default_logger_name
   "RAILS_DEFAULT_LOGGER"
end

#init_loggerObject

TODO merge this in with logger.rb or replace logger.rb



30
31
32
33
34
35
36
# File 'lib/youtube_g/request/video_upload.rb', line 30

def init_logger      
  if not Object.const_defined?(get_rails_default_logger_name)
     Logger.new(STDOUT)
  else
     eval(get_rails_default_logger_name)
  end                                                                                                             
end

#loggerObject



38
39
40
# File 'lib/youtube_g/request/video_upload.rb', line 38

def logger
  @logger        
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

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.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/youtube_g/request/video_upload.rb', line 64

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)

  upload_body = generate_upload_body(boundary, video_xml, data)

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

  direct_upload_url = "/feeds/api/users/#{@user}/uploads"
  logger.debug("upload_header [#{upload_header}]")

  Net::HTTP.start(base_url) do |upload|
    response = upload.post(direct_upload_url, upload_body, upload_header)
    # todo parse this out also
    if response.code.to_i == 403                      
      logger.error("ERROR: #{response.code}")
      raise AuthenticationError, response.body[/<TITLE>(.+)<\/TITLE>/, 1]
    elsif response.code.to_i != 201               
      logger.error("ERROR: #{response.code}")
      logger.debug("response: #{response.body}")
      upload_errors = YouTubeG::Parser::UploadErrorParser.new(response.body).parse
      logger.debug("upload_errors: #{upload_errors}")
      raise UploadError, upload_errors.inspect
    end 
    return YouTubeG::Parser::VideoFeedParser.new(response.body).parse
  end

end