Channels
Yt::Channel represents a YouTube channel.
Initialize using its YouTube ID:
Authentication
Most methods of Yt::Channel retrieve public data from YouTube (e.g.: fetch a channel’s title).
To use these methods (marked with below), you only need to generate an API key and configure:
Yt.configuration.api_key = "<your api key>" ## use your API key
channel = Yt::Channel.new id: 'UCwCnUcLcb9-eSrHa_RQGkQQ' ## use any channel ID
channel.title # => "Yt Test"List of Yt::Channel data methods
- any authentication works Channel’s snippet
→ Yt docs
channel.id # => "UCwCnUcLcb9-eSrHa_RQGkQQ"→ Yt docschannel.title # => "Yt Test"→ Yt docschannel.description # => "A test channel."→ Yt docschannel.custom_url # => "yt-test"→ Yt docschannel.vanity_url # => "https://www.youtube.com/yt-test"→ Yt docschannel.published_at # => 2014-05-02 20:12:57 UTC→ Yt docschannel.thumbnail_url # => "https://yt3.ggpht.com/-KMnbKDBl60w/photo.jpg"- any authentication works Channel’s status
- any authentication works Channel’s statistics
- any authentication works Channel’s branding settings
To limit the number of HTTP requests, use select to specify which parts of the channel’s data to load:
slow = channel # => without select: 2 HTTP requestsslow.title # => one HTTP request to fetch the channel’s snippetslow.privacy_status # => another HTTP request to fetch the channel’s status→ Yt docsfast = channel.select :snippet, :status # => with select: 1 HTTP requestfast.title # => one HTTP request to fetch both the channel’s snippet and statusfast.privacy_status # => no extra HTTP requests
- any authentication works Channel’s (public) videos
→ Yt docs
channel.videos# => #<Yt::Relation [#<Yt::Video @id=gknz...>, #<Yt::Video @id=32Gc...>, ...]>
- any authentication works Channel’s (public) playlists
→ Yt docs
channel.playlists# => #<Yt::Relation [#<Yt::Playlist @id=PL-L...>, #<Yt::Playlist @id=PL-N...>, ...]>
Before iterating through videos or playlists, use select to specify which parts of each video’s data to load:
→ Yt docs
videos = channel.videos.select :snippet, :statisticsvideos.map &:title # => ["First public video", "Second public video", ...]videos.map &:view_count # => [123, 456, ...]
You can also use limit to only fetch a certain number of videos or playlists:
→ Yt docs
videos = channel.videos.limit 2videos.map &:id # => ["gknzFj_0vvY", "oO6WawhsxTA"]
You can also use size to quickly obtain the estimated number of videos or playlists:
→ Yt docs
channel.videos.size # => 312
Note that, due to YouTube API limitations, only a maximum of 500 videos can be fetched for an unauthenticated channel.
- any authentication works Collection of channels
→ Yt docs
Yt::Channel.where id: ["UCwCnUcLcb9-eSrHa_RQGkQQ", "UCKe_0fJtkT1dYnzn", ...]# => #<Yt::Relation [#<Yt::Channel @id=UCwCnUcLcb9-eSrHa_RQGkQQ>, ...]>
The previous method returns existing channels that match the provided IDs, skipping any unrecognized ID.
As usual, use select to specify which parts of each channels’s data to load before iterating through the list.