Class: YouTubeG::Upload::VideoUpload

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

Overview

Implements video uploads/updates/deletions

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

Constructor Details

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

Returns a new instance of VideoUpload.



19
20
21
# File 'lib/youtube_g/request/video_upload.rb', line 19

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

#delete(video_id) ⇒ Object

Delete a video on YouTube



103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/youtube_g/request/video_upload.rb', line 103

def delete(video_id)
  delete_header = authorization_headers.merge({
    "Content-Type"   => "application/atom+xml",
    "Content-Length" => "0",
  })
  
  delete_url = "/feeds/api/users/#{@user}/uploads/#{video_id}"
  
  Net::HTTP.start(base_url) do |session|
    response = session.delete(delete_url, delete_header)
    raise_on_faulty_response(response)
    return true
  end
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.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/youtube_g/request/video_upload.rb', line 82

def update(video_id, options)
  @opts = options
  
  update_body = video_xml
  
  update_header = authorization_headers.merge({
    "Content-Type"   => "application/atom+xml",
    "Content-Length" => "#{update_body.length}",
  })
  
  update_url = "/feeds/api/users/#{@user}/uploads/#{video_id}"
  
  Net::HTTP.start(base_url) do | session |
    response = session.put(update_url, update_body, update_header)
    raise_on_faulty_response(response)
    
    return YouTubeG::Parser::VideoFeedParser.new(response.body).parse
  end
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 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.



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
# File 'lib/youtube_g/request/video_upload.rb', line 41

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_headers = authorization_headers.merge({
      "Slug"           => "#{@opts[:filename]}",
      "Content-Type"   => "multipart/related; boundary=#{boundary}",
      "Content-Length" => "#{post_body_io.expected_length}", # required per YouTube spec
    # "Transfer-Encoding" => "chunked" # We will stream instead of posting at once
  })
  
  path = '/feeds/api/users/%s/uploads' % @user
  
  Net::HTTP.start(uploads_url) do | session |
    
    # Use the chained IO as body so that Net::HTTP reads into the socket for us
    post = Net::HTTP::Post.new(path, upload_headers)
    post.body_stream = post_body_io
    
    response = session.request(post)
    raise_on_faulty_response(response)
    
    return uploaded_video_id_from(response.body)
  end
end