The reliable YouTube API Ruby client
Yt helps you write apps that need to interact with YouTube.
The source code is available on GitHub and the documentation on RubyDoc. After registering your app, you can run commands like:
channel = Yt::Channel.new id: 'UCxO1tY8h1AhOz0T4ENwmpow'
channel.title #=> "Fullscreen"
channel.public? #=> true
channel.videos.count #=> 12video = Yt::Video.new id: 'jNQXAC9IVRw'
video.title #=> "Fullscreen Creator Platform"
video.comment_count #=> 308
video.hd? #=> true
video.annotations.count #=> 1
video.comment_threads #=> #
# Use #take to limit the number of pages need to fetch from server
video.comment_threads.take(99).map(&:author_display_name) #=> ["Paul", "Tommy", ...]The full documentation is available at rubydoc.info.
To install on your system, run
gem install ytTo use inside a bundled Ruby project, add this line to the Gemfile:
gem 'yt', '~> 0.34.0'Since the gem follows Semantic Versioning,
indicating the full version in your Gemfile (~> major.minor.patch)
guarantees that your project won’t occur in any error when you bundle update
and a new version of Yt is released.
Check claudiob.github.io/yt for the list of methods available for Yt::Account.
Use Yt::ContentOwner to:
…All the above methods require authentication (see below).
Check claudiob.github.io/yt for the list of methods available for Yt::Channel.
Check claudiob.github.io/yt for the list of methods available for Yt::Video.
Check claudiob.github.io/yt for the list of methods available for Yt::Playlist.
Check claudiob.github.io/yt for the list of methods available for Yt::PlaylistItem.
Use Yt::CommentThread to:
Yt::CommentThread.new id: 'z13vsnnbwtv4sbnug232erczcmi3wzaug'
comment_thread.video_id #=> "1234"
comment_thread.total_reply_count #=> 1
comment_thread.can_reply? #=> true
comment_thread.public? #=> true
comment_thread.top_level_comment #=> #
comment_thread.text_display #=> "funny video!"
comment_thread.like_count #=> 9
comment_thread.updated_at #=> 2016-03-22 12:56:56 UTC
comment_thread.author_display_name #=> "Joe"Use Yt::Comment to:
Yt::Comment.new id: 'z13vsnnbwtv4sbnug232erczcmi3wzaug'
comment.text_display #=> "awesome"
comment.author_display_name #=> "Jack"
comment.like_count #=> 1
comment.updated_at #=> 2016-03-22 12:56:56 UTC
comment.parent_id #=> "abc1234" (return nil if the comment is not a reply)Use Yt::BulkReportJob to:
content_owner = Yt::ContentOwner.new owner_name: 'CMSname', access_token: 'ya29.1.ABCDEFGHIJ'
bulk_report_job = content_owner.bulk_report_jobs.first
bulk_report_job.report_type_id #=> "content_owner_demographics_a1"Use Yt::BulkReport to:
content_owner = Yt::ContentOwner.new owner_name: 'CMSname', access_token: 'ya29.1.ABCDEFGHIJ'
bulk_report_job = content_owner.bulk_report_jobs.first
bulk_report = bulk_report_job.bulk_reports.first
bulk_report.start_time #=> 2017-08-11 07:00:00 UTC
bulk_report.end_time #=> 2017-08-12 07:00:00 UTC
bulk_report.download_url #=> "https://youtubereporting.googleapis.com/v1/..."Use Yt::Collections::Videos to:
videos = Yt::Collections::Videos.new
videos.where(order: 'viewCount').first.title #=> "PSY - GANGNAM STYLE"
videos.where(q: 'Fullscreen CreatorPlatform', safe_search: 'none').size #=> 324
videos.where(chart: 'mostPopular', video_category_id: 44).first.title #=> "SINISTER - Trailer"
videos.where(id: 'jNQXAC9IVRw,invalid').map(&:title) #=> ["Fullscreen Creator Platform"]The methods above do not require authentication.
Check claudiob.github.io/yt for the list of methods available for Yt::Annotation.
Use Yt::MatchPolicy to:
content_owner = Yt::ContentOwner.new owner_name: 'CMSname', access_token: 'ya29.1.ABCDEFGHIJ'
match_policy = Yt::MatchPolicy.new asset_id: 'ABCD12345678', auth: content_owner
match_policy.update policy_id: 'aBcdEF6g-HJ' #=> trueUse Yt::Asset to:
content_owner = Yt::ContentOwner.new owner_name: 'CMSname', access_token: 'ya29.1.ABCDEFGHIJ'
asset = Yt::Asset.new id: 'ABCD12345678', auth: content_owner
asset.ownership #=> #
asset.ownership.obtain! #=> true
asset.general_owners.first.owner #=> 'CMSname'
asset.general_owners.first.everywhere? #=> true
asset.ownership.release! #=> true
asset.update metadata_mine: {notes: 'Some notes'} #=> truecontent_owner = Yt::ContentOwner.new(...)
asset = content_owner.assets.where(id: 'A969176766549462', fetch_metadata: 'mine').first
asset.metadata_mine.title #=> "Master Final Neu La Anh Fix"
asset = content_owner.assets.where(id: 'A969176766549462', fetch_metadata: 'effective').first
asset.metadata_effective.title #=> "Neu la anh" (different due to ownership conflicts)asset = content_owner.assets.where(id: 'A125058570526569', fetch_ownership: 'effective').first
asset.ownership_effective.general_owners.first.owner # => "XOuN81q-MeEUVrsiZeK1lQ"content_owner.assets.where(labels: "campaign:cpiuwdz-8oc").size #=> 417
content_owner.assets.where(labels: "campaign:cpiuwdz-8oc").first.title #=> "Whoomp! (Supadupafly) (Xxl Hip House Mix)"Use Yt::Claim to:
…The methods above require to be authenticated as the video’s content owner (see below).
Use Yt::Ownership to:
content_owner = Yt::ContentOwner.new owner_name: 'CMSname', access_token: 'ya29.1.ABCDEFGHIJ'
ownership = Yt::Ownership.new asset_id: 'ABCD12345678', auth: $content_owner
new_general_owner_attrs = {ratio: 100, owner: 'CMSname', type: 'include', territories: ['US', 'CA']}
ownership.update general: [new_general_owner_attrs]The methods above require to be authenticated as the video’s content owner (see below).
Use Yt::AdvertisingOptionsSet to:
content_owner = Yt::ContentOwner.new owner_name: 'CMSname', access_token: 'ya29.1.ABCDEFGHIJ'
ad_options = Yt::AdvertisingOptionsSet.new video_id: 'jNQXAC9IVRw', auth: $content_owner
ad_options.update ad_formats: %w(standard_instream long) #=> trueThe methods above require to be authenticated as the video’s content owner (see below).
Yt leverages Active Support Instrumentation to provide a hook which developers can use to be notified when HTTP requests to YouTube are made. This hook may be used to track the number of requests over time, monitor quota usage, provide an audit trail, or track how long a specific request takes to complete.
Subscribe to the request.yt notification within your application:
ActiveSupport::Notifications.subscribe 'request.yt' do |*args|
event = ActiveSupport::Notifications::Event.new(*args)
event.payload[:request_uri] #=> #
event.payload[:method] #=> :get
event.payload[:response] #=> #
event.end #=> 2014-08-22 16:57:17 -0700
event.duration #=> 141.867 (ms)
endIn order to use Yt you must register your app in the Google Developers Console.
If you don’t have a registered app, browse to the console and select "Create Project":
When your project is ready, select APIs & Auth in the menu and individually enable Google+, YouTube Analytics and YouTube Data API:
The next step is to create an API key. Depending on the nature of your app, you should pick one of the following strategies.
If you are building a read-only app that fetches public data from YouTube, then all you need is a Public API access.
Click on "Create new Key" in the Public API section and select "Server Key":
Once the key for server application is created, copy the API key and add it to your code with the following snippet of code (replacing with your own key):
Yt.configure do |config|
config.api_key = 'AIzaSyAO8dXpvZcaP2XSDFBD91H8yQ'
endRemember: this kind of app is not allowed to perform any destructive operation, so you won’t be able to like a video, subscribe to a channel or delete a playlist from a specific account. You will only be able to retrieve read-only data.
If you are building a web app that manages YouTube accounts, you need the owner of each account to authorize your app. There are three scenarios:
Scenario 1. If you already have the account’s access token, then you are ready to go. Just pass that access token to the account initializer, such as:
account = Yt::Account.new access_token: 'ya29.1.ABCDEFGHIJ'
account.email #=> (retrieves the account’s e-mail address)
account.videos #=> (lists a video to an account’s playlist)Scenario 2. If you don’t have the account’s access token, but you have the refresh token, then it’s almost as easy. In the Google Developers Console, find the web application that was used to obtain the refresh token, copy the Client ID and Client secret and add them to you code with the following snippet of code (replacing with your own keys):
Yt.configure do |config|
config.client_id = '1234567890.apps.googleusercontent.com'
config.client_secret = '1234567890'
endThen you can manage a YouTube accoun
No open issues yet, or sync has not completed.