Finding out the destination YouTube URL when uploading videos to YouTube via youtube_it gem with Rails
I've gotten the 'youtube_it' working great in conjunction with Paperclip to handle video uploads through the browser using the following code:
videos_controller.rb
def create
@video = Video.new(params[:video])
if @video.save
uploader = YouTubeIt::Upload::VideoUpload.new( :username => AppConfig[:youtube_user],
:password => AppConfig[:youtube_pass],
:dev_key => AppConfig[:youtube_api_key])
uploader.upload open( params[:video][:attachment] ), :title => @video.title,
:description => @video.description,
:category => 'some category',
:keywords => ['some keyword 1', "some keyword 2"]
@video.deliver_video_notification
flash[:notice] = 'Your video is under review for approval.<br/> Please check back in 48 hours.'
redirect_to videos_url
else
@errors = @video.errors
@current_video = params[:v].blank? ? Video.newest : Video.find(params[:v])
render :action => :index
end
end开发者_运维知识库
However once the video is uploaded, I have no idea what URL YouTube created for the video without manually logging into the YouTube channel and looking it up. I didn't see any callback in the logs or the response.body that revealed the destination. I'd like to programatically save the destination in some kind of after_save method. As it works right now, it is saving the video object, and after save it is uploading the video to youtube.
The upload
method returns a YouTubeIt::Model::Video
, which has a player_url
attribute. So just catch the return value from the upload
method, and call player_url
on that, and voilà, you have the URL to the video. For a list of the other attributes that model has, check the source code.
Example:
def create
@video = Video.new(params[:video])
if @video.save
uploader = YouTubeIt::Upload::VideoUpload.new( :username => AppConfig[:youtube_user],
:password => AppConfig[:youtube_pass],
:dev_key => AppConfig[:youtube_api_key])
uploaded_video = uploader.upload open( params[:video][:attachment] ), :title => @video.title,
:description => @video.description,
:category => 'some category',
:keywords => ['some keyword 1', "some keyword 2"]
puts uploaded_video.player_url # This will print the URL to the log
@video.deliver_video_notification
flash[:notice] = 'Your video is under review for approval.<br/> Please check back in 48 hours.'
redirect_to videos_url
else
@errors = @video.errors
@current_video = params[:v].blank? ? Video.newest : Video.find(params[:v])
render :action => :index
end
end
How about getting the list of videos uploaded by the user and choosing the latest one?
$ client.my_videos
https://github.com/kylejginavan/youtube_it
精彩评论