Skip to content

vibha/youtube-model

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

YouTube Model

This plugin allows you to interact with the new YouTube API (yes, that means uploading is now featured)
through a simple ActiveResource model. The uploaded video can also be updated and deleted. The changes will be reflected at the youtube.

Repository

Find it at http://github.com/vibha/youtube-model

Documentation

Installing

Simply as:

./script/plugin install git://github.com/vibha/youtube-model.git

Generating the system

The plugin includes a generator to create a model based on ActiveResource

./script/generate youtube_model

This generator will create four files:

  • An ActiveResource based model under app/models directory.
  • A yaml configuration file under config directory.
  • An initializer that loads the configuration file, under config/initializers directory.

And that’s all, but be sure to check the configuration file to set up your Developer and Client Keys.
You can get these keys at the YouTube APIs and Tools page.

AuthSub for web applications

A link is provided to authorise the website to access to the logged in user’s youtube account.

The token obtained after doing authorisation process will become a session token. So we will set the session parameter to 1 in yaml configuration file. This token is used to upload, edit and delete video.

#app/views/videos/index.html.erb

Please Click <%= link_to ‘here’, youtube_auth_url(authorise_videos_url) %> first to authorise access to your youtube account.

An authorise method will be added to make the single use token to a session token.


#VideosController
def authorise
client = GData::Client::DocList.new
if params[:token]
client.authsub_token = params[:token] # extract the single-use token from the URL query params
session[:token] = client.auth_handler.upgrade()
client.authsub_token = session[:token] if session[:token]
end
redirect_to videos_path
end

Also add ‘include GData’ in the videos controller, which includes the module GData for authorisation process.


  #config/environment.rb
  config.gem "hpricot", :version => '0.6', :source => "http://code.whytheluckystiff.net"
  config.gem 'gdata', :lib => 'gdata'
  

Listing videos

Example

To get all the videos uploaded by a user you can use the method uploaded_by_user passing the session[:token] we saved during authorisation process

YouTube.uploaded_by_user(session[:token])

#VideosController
def index
@youtube =YouTube.uploaded_by_user(session[:token]) if session[:token]
end

def show
@video = YouTube.find_by_id(params[:id]) rescue nil
flash[:message] = “Sorry the video is not found at Youtube” and redirect_to videos_path unless @video
end

#app/views/videos/index.html.erb

<% if session[:token] %> <%= image_tag @youtube.logo %>

<%= @youtube.title %>

<%= “Displaying #{@youtube.startIndex} – #{@youtube.itemsPerPage} of #{@youtube.totalResults}” %> <% for video in @youtube.videos %>

<%= video.title %>

in <%= video.group.category >
by <
= link_to video.author.name, “http://youtube.com/#{video.author.name}” %>

<%= simple_format truncate(video.content, :length => 100) %> <%= link_to image_tag(video.group.thumbnail.first.url, :alt => video.title), video_path(video.id) %> <%= link_to ‘Watch on youtube’, video.link.first.href %> <%= link_to ‘Edit/Update’, edit_video_path(video) %>

<%= link_to ‘Delete’, video_path(video), :method => “delete” >
<
end >
<
end %>

#app/views/videos/show.html.erb

<%= @video.title %>

by <%= link_to video.author.name, "http://youtube.com/#{video.author.name}" >
<
= youtube_embed @video %>

<%= link_to ‘Back’, videos_path %> <%= link_to ‘Edit/Update’, edit_video_path(@video) %> <%= link_to ‘Delete’, video_path(@video), :method => “delete” %>

As you can see, the youtube_embed method is used to display a video.

Uploading videos

As seen in the index example above, there is a helper named youtube_auth_url wich generates an url to the
YouTube authentication page. Pass another url as param to wich be redirected after authentication.
This helper method takes the session and secure options from the configuration file. See the details of this options
at the YouTube API

Upload process has two steps:

  1. Meta-info for the video.
  2. File choose.

So basically we have two actions in our controller to upload a video. Here’s a quick code example:


#routes.rb
map.resources :videos, :new => {:upload => :post}, :collection => {:authorise => :get }

#VideosController
def new
@categories ||= YouTube.video_categories
end

def upload
@upload_info = YouTube.get_upload_url(params[:video])
end

Also add a link to upload the video at the index page.


#app/views/videos/index.html.erb
<%= link_to “Upload Video”, new_video_url %>
#app/views/videos/new.html.erb <% form_for ‘video’, :url => upload_new_video_path do |f| %> <%= f.hidden_field :auth_sub, :value => session[:token] %> <%= f.label :title %> <%= f.text_field :title %> <%= f.label :description %> <%= f.text_area :content, :rows => 10 %> <%= f.label :category %> <%= f.select :category, @categories %> <%= f.label :keywords %> <%= f.text_field :keywords %> <%= f.submit ‘Continue to Step 2’ %> <% end %> #app/views/videos/upload.html.erb <% form_tag @upload_info[:url], :multipart => true do %> <%= hidden_field_tag :token, @upload_info[:token] %> <%= label_tag :file %> <%= file_field_tag :file %> <%= submit_tag ‘Upload video’ %> <% end %>

Updating Videos

To get the videos updated by a user you can use the method update_video passing the id of that video, session[:token] and params hash

YouTube.update_video(params[:id], session[:token], params[:you_tube_entry])


#VideosController
before_filter :find_video, :only => [:edit, :update]
def edit
@youtube_videos =YouTube.uploaded_by_user(session[:token])
@video = @youtube_videos.videos.select { |video| video.id == params[:id] }
@youtube = @video.first
@youtube.keywords = @youtube.group.keywords
@youtube.category = @youtube.group.category
@categories ||= YouTube.video_categories
end

def update
if u = YouTube.update_video(params[:id], session[:token], params[:you_tube_entry]) rescue nil
flash[:message] = “Video has been Updated Successfully.”
else
flash[:message] = “Video has not been Updated Successfully.”
end
redirect_to videos_path
end

private def find_video flash[:message] = “Sorry the video is not found at Youtube” and redirect_to videos_path unless @video = YouTube.find_by_id(params[:id]) rescue nil end

#app/views/videos/edit.html.erb
<% form_for @youtube, :url => video_url, :html => { :multipart => true } do |f| >
<
= f.label :title >
<
= f.text_field :title %>

<%= f.label :description %> <%= f.text_area :content, :rows => 10 %> <%= f.label :category %> <%= f.select :category, @categories %> <%= f.label :keywords %>

<%= f.text_field :keywords %>

<%= f.submit ‘Update video’ %> <% end %> <%= link_to ‘Back’, videos_path %>

h3. Deleting Videos

To delete the videos of any user you can use the method delete_video passing the id of that video and session[:token]

YouTube.delete_video(params[:id], session[:token])

#VideosController
before_filter :find_video, :only => [:show, :edit, :update, :destroy]
def destroy
yt = YouTube.delete_video(params[:id], session[:token]) rescue nil
if yt.msg == “OK”
flash[:message] = “Video has been sucessfully deleted.”
else
flash[:message] = “Sorry the video has not been deleted.”
end
redirect_to videos_path
end

The filter is applied on show method so that method can now be removed.

Also don’t forget to set :multipart => true in the second step.

And for a more specific speech… the class method get_upload_url receives a hash containing
the meta-info of the first step, and returns a hash with the upload url and token.

Feedback

I’ll really appreciate your feedback, please contact me at vibha.dei@gmail.com

License

This code is released under Creative Commons Attribution-Share Alike 3.0 license.

About

Allows you to generate an ActiveResource model ready to interact with the You Tube API.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Ruby 100.0%