Skip to content

Latest commit

 

History

History
221 lines (160 loc) · 7.17 KB

README.textile

File metadata and controls

221 lines (160 loc) · 7.17 KB

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.

UPDATE: This fork allow youtube model to work as an active resource like. Allowing validation, callback, etc..
The project is steel on going on my side, some things may still not work properly, but the main functionality (upload, update, remote video collection retrievals) should work properly)

Documentation

Installing

rails plugin install git://github.com/itkin/youtube-model.git

Generating the system

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

rails g youtube_model ModelName

This generator will create four files:

  • An YouTubeModel inherited from ActiveResource::Base under app/models directory.
  • A yaml configuration file under config 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

All the api finders (except find_by_id) render collections of youtube_model resources. The collection are basically an aray of objects, extended with the following 3 accessors:
- start_index
- items_per_page
- total_results
You can pass any youtube argument to the api finders, or set them as default in your youtube model class


class Video < YouTubeModel::Base
self.default_youtube_options = { :itemPerPage => 10 }
end
Video.top_rated(:startIndex => 10, :itemPerPage => 30)
Video.uploaded_by(‘nicolas’)

uploaded_by_user requires authentication and find_by_id needs it optionally; To use them you have to pass the session[:token] we saved during authorisation process
Accessing the API with theses finders gives you private information about video status (also accessible via @video.status(:token => session[:token])


Video.uploaded_by_user(:token => session[:token])
Video.find_by_id(video_id,:token => session[:token])

#VideosController
def index
@videos= Video.uploaded_by_user(:token => session[:token])
end

def show
@video = Video.find_by_id(params[:id], :token => session[:token])
flash[:message] = “Sorry the video is not found at Youtube” and redirect_to videos_path unless @video
end

#app/views/videos/index.html.erb <%= “Displaying #{@videos.startIndex} – #{@videos.itemsPerPage} of #{@videos.totalResults}” %> <% for video in @videos %> <%= video.title > in <= video.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.thumbnail.first.url, :alt => video.title), video_path(video) %> <%= 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}" >
<
= raw 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

To upload a new video to youtube, just instanciate the generated class you inherited from youtube model with a token param and save it


#routes.rb
map.resources :videos, :collection => {:authorise => :get }
#VideosController
def new
@categories ||= YouTube.video_categories
@video = Video.new(:token => session[:token])
end
def create
@video = video.new(params[:video])
unless @video.save
render :action => :new
end
end

Updating Videos

To get the videos updated by a user, find it with find_by_id. You must pass a token option with the google api authsub token
in order retrieve a video event it hasn’t been processed or accepted by youtube (have a look here http://code.google.com/intl/fr/apis/youtube/2.0/developers_guide_protocol_video_entries.html)


#VideosController
def edit
@video = Video.find_by_id(params[:id], :token => session[:token])
end
def update
@video.find_by_id(params[:id
if @video.update_attributes(params[:video])
render :action => :update
else
render :action => :edit
end
end
#app/views/videos/edit.html.erb <% form_for @youtube, :url => video_url do |f| %> <%= f.label :title %> <%= f.text_field :title %> <%= f.label :description %> <%= f.text_area :description, :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 destroy method on a video instance

video.destroy

#VideosController
def destroy
@video = Video.find_by_id(params[:id], :token => session[:token])
if @video.destroy
flash[:message] = “Video has been sucessfully deleted.”
else
flash[:message] = “Sorry the video has not been deleted.”
end
redirect_to videos_path
end

Feedback

I’ll really appreciate your feedback, please contact me at vibha[at]vinsol.com

License

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