使用Rails通过youtube_it gem将视频上传到YouTube时,找出目标YouTube网址

时间:2011-05-11 20:23:41

标签: ruby-on-rails upload youtube-api

我已经让'youtube_it'与Paperclip配合使用,可以使用以下代码通过浏览器处理视频上传:

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

然而,一旦视频上传,我不知道YouTube为视频创建的网址,而无需手动登录YouTube频道并进行查找。我没有看到日志中的任何回调或者显示目的地的response.body。我想以某种post_save方法以编程方式保存目标。由于它现在正常工作,它正在保存视频对象,保存后将视频上传到youtube。

2 个答案:

答案 0 :(得分:3)

upload方法返回YouTubeIt::Model::Video,其player_url属性。因此,只需从upload方法中捕获返回值,然后在其上调用player_url,即可获得视频的网址。有关模型具有的其他属性的列表,请查看source code

示例:

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

答案 1 :(得分:0)

如何获取用户上传的视频列表并选择最新的视频?

$ client.my_videos

https://github.com/kylejginavan/youtube_it