Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 4: integrating You Tube (youtube_it gem)

I am trying to incorporate you tube in my rails 4 app.

I want users to be able to upload a video file which will then be posted on my you tube channel.

I am using Rails 4 with Youtube_it gem. I have been trying to follow this tutorial: http://www.sitepoint.com/youtube-rails/

My file structure has project model and a video model. The associations are:

Project.rb

has_one :video
  accepts_nested_attributes_for :video

Video.rb

  belongs_to :project

My video table has a boolean attribute called :include_video.

    <%= f.collection_radio_buttons :include_video, [[true, ' Yes'], [false, ' No']], :first, :last, {:item_wrapper_class => 'fixradio'}, {:class => "radio-inline create-project response-project"} %>

If it's true, then I want to reveal the field where users add a link to the video (attribute is called :link).

    <%= f.file_field :link %>

The video form in which I ask these questions is a partial. The partial is inside the project form which has other questions. I also have a partial view which has the container for the you tube clip (as shown in the tutorial).

The view partial is included in my project show page as:

<% if @project.video.include_video? %>

          <%= render 'videos/particulars' %>
    <% end %>

My project controller has:

def new
    @project = Project.new
    @project.video = Video.new
end
 def show
    #authorise @project
    @project = Project.find(params[:id])
    @creator = User.find(@project.creator_id)
    @creator_profile = @creator.profile
    @project.video ||= Video.new

  end

My project controller also has white labelled attributes from my video table (as does my video controller.

 def project_params
      params.require(:project).permit(
      video_attributes: [:include_video, :link],

My video controller has:

  def show
    respond_with(@video)
  end


 def create

    @video = Video.new(video_params)
    @video.project_id = video_params[:project_id]

  end

   def video_params
      params[:video].permit(:include_video, :link, :project_id)
    end

My video.rb has:

class Video < ActiveRecord::Base

  # --------------- associations
  belongs_to :project
  belongs_to :program
  belongs_to :proposal
  belongs_to :profile
  belongs_to :user


  # --------------- scopes
  # --------------- validations

  YT_LINK_FORMAT = /\A.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*\z/i

  validates :link, presence: true, format: YT_LINK_FORMAT


  # --------------- class methods

  # --------------- callbacks

  before_create -> do
    uid = link.match(YT_LINK_FORMAT)
    self.uid = uid[2] if uid && uid[2]

    if self.uid.to_s.length != 11
      self.errors.add(:link, 'is invalid.')
      false
    elsif Video.where(uid: self.uid).any?
      self.errors.add(:link, 'is not unique.')
      false
    else
      get_additional_info
    end
  end

  # --------------- instance methods

  # --------------- private methods

  def get_additional_info
    begin
      client = YouTubeIt::OAuth2Client.new(dev_key: ENV['YT_developer_key'])
      video = client.video_by(uid)
      self.title = video.title
      self.duration = parse_duration(video.duration)
      self.author = video.author.name
      self.likes = video.rating.likes
    rescue
      self.title = '' ; self.duration = '00:00:00' ; self.author = '' ; self.likes = 0 ; self.dislikes = 0
    end
  end

  def parse_duration(d)
    hr = (d / 3600).floor
    min = ((d - (hr * 3600)) / 60).floor
    sec = (d - (hr * 3600) - (min * 60)).floor

    hr = '0' + hr.to_s if hr.to_i < 10
    min = '0' + min.to_s if min.to_i < 10
    sec = '0' + sec.to_s if sec.to_i < 10

    hr.to_s + ':' + min.to_s + ':' + sec.to_s
  end
end

I had a related problem and got help from this post: Rails 4 with You Tube

However, it's created a new problem and I don't know if it is advancing my progress toward a solution.

The bit that's new (from a suggestion made by a user on the other posted question) is to add a show line to my projects controller for the video. When I try this, I get this error:

Failed to save the new associated video.

When I try to make an entirely new project which has the video form partial within it, I get an error saving the form. It says the video link is not valid (I am selecting a .mov file from my hard drive).

I then changed the validation in my video.rb to:

 validates :link,  format: YT_LINK_FORMAT, :allow_blank => true, :allow_nil => true

(ie. removing: presence: true, and adding blank and nil, but I still get that error)

How do I approach figuring this out?

like image 773
Mel Avatar asked Jun 30 '15 12:06

Mel


1 Answers

I wouldn't use the youtube_it ruby gem because it uses v2 of the YouTube API (which has not been supported since April 20th, 2015). I would suggest using the yt ruby gem (which uses the latest version of the YouTube API).

A tutorial on the yt gem and why you shouldn't use youtube_it

Your issues are most likely related to trying to use youtube_it functions that aren't being supported by the YouTube API. It might seem like a lot of work to refactor your code, but I'm sure it'll save you a lot of headache down the road (especially since youtube_it doesn't seem very well maintained).

like image 79
David Ro Avatar answered Sep 29 '22 15:09

David Ro