youtube_it is developed by many contributors who are passionate about opensource projects and selflessly donate their time and effort. Following that spirit, your donations to this project will be donated to the Tuquito Libre Foundation(http://fundacion.tuquito.org.ar) for developing technology projects intended to close the digital gap in Latin America.
youtube_it is the most complete Ruby client for the YouTube GData API. It provides an easy way to access YouTube's video API. Compared to earlier YouTube interfaces, this new API and library offers much-improved flexibility around executing complex search queries to obtain well-targeted video search results. Also included is standard video management such as uploading, deleting, updating, liking, disliking, ratings and comments.
- Create a youtube account.
- Create a developer key here http://code.google.com/apis/youtube/dashboard.
- sudo gem install youtube_it
Note: youtube_it supports ClientLogin(YouTube account), OAuth, or AuthSub authentication methods.
An example of how to use youtube_it with Rails 3 can be found here.
You can see youtube_it in action here.
Important: The Account Authentication API for OAuth 1.0, AuthSub and Client Login has been officially deprecated as of April 20, 2012. It will continue to work as per our deprecation policy, but we encourage you to migrate to OAuth 2.0 authentication as soon as possible. If you are building a new application, you should use OAuth 2.0 authentication.
####Creating a Client
require 'youtube_it'
client = YouTubeIt::Client.new
####Client with Developer Key
client = YouTubeIt::Client.new(:dev_key => "developer_key")
####Client with YouTube Account and Developer Key
client = YouTubeIt::Client.new(:username => "youtube_username", :password => "youtube_password", :dev_key => "developer_key")
####Client with AuthSub
client = YouTubeIt::AuthSubClient.new(:token => "token" , :dev_key => "developer_key")
####Client with OAuth
client = YouTubeIt::OAuthClient.new("consumer_key", "consumer_secret", "youtube_username", "developer_key")
client.authorize_from_access("access_token", "access_secret")
####Client with OAuth2
client = YouTubeIt::OAuth2Client.new(client_access_token: "access_token", client_refresh_token: "refresh_token", client_id: "client_id", client_secret: "client_secret", dev_key: "dev_key", expires_at: "expiration time")
If your access token is still valid (be careful, access tokens may only be valid for about 1 hour), you can use the client directly. Refreshing the token is simple:
client.refresh_access_token!
You can see more about OAuth2 in the wiki.
You can use multiple profiles in the same account:
profiles = client.profiles(['username1','username2'])
profiles['username1'].username, "username1"
Note: Each type of client enables searching capabilities.
###Basic Queries
client.videos_by(:query => "penguin")
client.videos_by(:query => "penguin", :page => 2, :per_page => 15)
client.videos_by(:query => "penguin", :restriction => "DE")
client.videos_by(:tags => ['tiger', 'leopard'])
client.videos_by(:categories => [:news, :sports])
client.videos_by(:categories => [:news, :sports], :tags => ['soccer', 'football'])
client.videos_by(:user => 'liz')
client.videos_by(:favorites, :user => 'liz')
client.video_by("FQK1URcxmb4")
client.video_by("https://www.youtube.com/watch?v=QsbmrCtiEUU")
client.video_by_user("chebyte","FQK1URcxmb4")
###Standard Queries
client.videos_by(:most_viewed)
client.videos_by(:most_linked, :page => 3)
client.videos_by(:top_rated, :time => :today)
###Advanced Queries (with boolean operators OR (either), AND (include), NOT (exclude))
client.videos_by(:categories => { :either => [:news, :sports], :exclude => [:comedy] }, :tags => { :include => ['football'], :exclude => ['soccer'] })
###Custom Query Params
client.videos_by(:query => "penguin", :safe_search => "strict")
client.videos_by(:query => "penguin", :duration => "long")
client.videos_by(:query => "penguin", :hd => "true")
client.videos_by(:query => "penguin", :region => "AR")
You can see more options here.
####Fields Parameter(experimental features): Return videos more than 1000 views:
client.videos_by(:fields => {:view_count => "1000"})
####Filter by date
client.videos_by(:fields => {:published => ((Date.today)})
client.videos_by(:fields => {:recorded => ((Date.today)})
####Filter by date with range
client.videos_by(:fields => {:published => ((Date.today - 30)..(Date.today))})
client.videos_by(:fields => {:recorded => ((Date.today - 30)..(Date.today))})
Note: These queries do not find private videos! Use these methods instead:
client.get_my_video("FQK1URcxmb4")
client.get_my_videos(:query => "penguin")
##Video Management
Note: YouTube account, OAuth, or AuthSub enables video management.
####Upload Video
client.video_upload(File.open("test.mov"), :title => "test",:description => 'some description', :category => 'People',:keywords => %w[cool blah test])
####Upload Video With a Developer Tag
Note: the tags are not immediately available
client.video_upload(File.open("test.mov"), :title => "test",:description => 'some description', :category => 'People',:keywords => %w[cool blah test], :dev_tag => 'tagdev')
####Upload Private Video
client.video_upload(File.open("test.mov"), :title => "test",:description => 'some description', :category => 'People',:keywords => %w[cool blah test], :private => true)
####Update Video
client.video_update("FQK1URcxmb4", :title => "new test",:description => 'new description', :category => 'People',:keywords => %w[cool blah test])
####Update Video (partially)
client.video_partial_update("FQK1URcxmb4", :description => 'new description', :list => 'allowed')
####Delete Video
client.video_delete("FQK1URcxmb4")
####My Videos
client.my_videos
####My Video
client.my_video(video_id)
####Profile Details
client.profile(user) #default: current user
####List Comments
client.comments(video_id)
####Add a Comment
client.add_comment(video_id, "test comment!")
####Add a Reply Comment
client.add_comment(video_id, "test reply!", :reply_to => another_comment)
####Delete a Comment
client.delete_comment(video_id, comment_id)
####List Favorites
client.favorites(user) # default: current user
####Add Favorite
client.add_favorite(video_id)
####Delete Favorite
client.delete_favorite(favorite_entry_id)
####Like a Video
client.like_video(video_id)
####Dislike a Video
client.dislike_video(video_id)
####List Subscriptions
client.subscriptions(user) # default: current user
####Subscribe to a Channel
client.subscribe_channel(channel_name)
####Unsubscribe from a Channel
client.unsubscribe_channel(subscription_id)
####List New Subscription Videos
client.new_subscription_videos(user) # default: current user
####List Playlists
client.playlists(user, order_by) # default: current user, position
For example, you can get the videos of your playlist ordered by title
client.playlists(user, "title")
You can see more about options for order_by here.
####Select a Playlist
client.playlist(playlist_id)
####Select All Videos from a Playlist
playlist = client.playlist(playlist_id)
playlist.videos
####Create a Playlist
playlist = client.add_playlist(:title => "new playlist", :description => "playlist description")
####Delete a Playlist
client.delete_playlist(playlist_id)
####Add Video to a Playlist
client.add_video_to_playlist(playlist_id, video_id)
####Remove Video from a Playlist
client.delete_video_from_playlist(playlist_id, playlist_entry_id)
####Select All Videos from Your Watch Later Playlist
watch_later = client.watchlater(user) #default: current user
watch_later.videos
####Add Video to Watch Later Playlist
client.add_video_to_watchlater(video_id)
####Remove Video from Watch Later Playlist
client.delete_video_from_watchlater(watchlater_entry_id)
####List Related Videos
video = client.video_by("https://www.youtube.com/watch?v=QsbmrCtiEUU&feature=player_embedded")
video.related.videos
####Add a Response Video
video.add_response(original_video_id, response_video_id)
####Delete a Response Video
video.delete_response(original_video_id, response_video_id)
####List Response Videos
video = client.video_by("https://www.youtube.com/watch?v=QsbmrCtiEUU&feature=player_embedded")
video.responses.videos
You can give permissions in your videos, for example denied comments, rate, etc... More info here.
You have available the followings options:
- :rate, :comment, :commentVote, :videoRespond, :list, :embed, :syndicate
with just two values:
- allowed or denied
####Example (Upload Video with Denied Comments)
client = YouTubeIt::Client.new(:username => "youtube_username", :password => "youtube_password", :dev_key => "developer_key")
client.video_upload(File.open("test.mov"), :title => "test",:description => 'some description', :category => 'People',:keywords => %w[cool blah test], :comment => "denied")
You can get user activity with the followings params:
client.activity(user) #default current user
When uploading a video from your browser you need make a form upload with the followings params:
upload_token(params, nexturl)
####Example params
:title => "title", :description => "description", :category => "People", :tags => ["test"]
nexturl is the url to redirect to after upload is complete
Controller
def upload
@upload_info = YouTubeIt::Client.new.upload_token(params, videos_url)
end
View (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 %>
If the video has support for widescreen
video.embed_html_with_width(1280)
Note: you can specify width or just use the default of 1280.
Now you can embed videos without Flash using HTML5. Useful for mobile devices that do not support Flash.
You can specify these options:
video.embed_html5({:class => 'video-player', :id => 'my-video', :width => '425', :height => '350', :frameborder => '1', :url_params => {:option_one => "value", :option_two => "value"}})
or just use with default options:
video.embed_html5 #default: width: 425, height: 350, frameborder: 0
youtube_it passes all logs through the logger variable on the class itself. In Rails context, assign the Rails logger to that variable to collect the messages (don't forget to set the level to debug)
YouTubeIt.logger = RAILS_DEFAULT_LOGGER
RAILS_DEFAULT_LOGGER.level = Logger::DEBUG
- Kyle J. Ginavan.
- Mauro Torres - http://github.com/chebyte
- Marko Seppa - https://github.com/mseppae
- Walter Korman - https://github.com/shaper
- Shane Vitarana - https://github.com/shanev
MIT License
Copyright (c) 2010 Kyle J. Ginavan
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.