Class: YouTubeG::Upload::VideoUpload
- Inherits:
-
Object
- Object
- YouTubeG::Upload::VideoUpload
- 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 Method Summary collapse
-
#initialize(user, pass, dev_key, client_id = 'youtube_g') ⇒ VideoUpload
constructor
A new instance of VideoUpload.
-
#upload(data, opts = {}) ⇒ Object
Upload “data” to youtube, where data is either an IO object or raw file data.
Constructor Details
#initialize(user, pass, dev_key, client_id = 'youtube_g') ⇒ VideoUpload
Returns a new instance of VideoUpload.
17 18 19 |
# 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 |
Instance Method Details
#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.
39 40 41 42 43 44 45 46 47 48 49 50 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 77 78 |
# 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 |