Channels

Yt::Channel represents a YouTube channel. Initialize using its YouTube ID:

→ Yt docschannel = Yt::Channel.new id: "UCwCnUcLcb9-eSrHa_RQGkQQ"
# => #<Yt::Channel @id=UCwCnUcLcb9-eSrHa_RQGkQQ>
→ Yt docschannel.canonical_url
# => "https://www.youtube.com/channel/UCwCnUcLcb9-eSrHa_RQGkQQ"

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 docschannel.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
→ Yt docschannel.privacy_status # => "public"
→ Yt docschannel.is_linked # => true
→ Yt docschannel.long_uploads_status # => "longUploadsUnspecified"
any authentication works Channel’s statistics
→ Yt docschannel.view_count # => 123
→ Yt docschannel.comment_count # => 93
→ Yt docschannel.subscriber_count # => 42
→ Yt docschannel.hidden_subscriber_count # => false
→ Yt docschannel.video_count # => 62
any authentication works Channel’s branding settings
→ Yt docschannel.banner_image_url # => "https://yt3.ggpht.com/9dh4rj-k-no"
→ Yt docschannel.keywords # => ["Some", "tag"]
→ Yt docschannel.unsubscribed_trailer # => "gknzFj_0vvY"

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 requests
slow.title # => one HTTP request to fetch the channel’s snippet
slow.privacy_status # => another HTTP request to fetch the channel’s status

→ Yt docsfast = channel.select :snippet, :status # => with select: 1 HTTP request
fast.title # => one HTTP request to fetch both the channel’s snippet and status
fast.privacy_status # => no extra HTTP requests
any authentication works Channel’s (public) videos
→ Yt docschannel.videos
# => #<Yt::Relation [#<Yt::Video @id=gknz...>, #<Yt::Video @id=32Gc...>, ...]>
any authentication works Channel’s (public) playlists
→ Yt docschannel.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 docsvideos = channel.videos.select :snippet, :statistics
videos.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 docsvideos = channel.videos.limit 2
videos.map &:id # => ["gknzFj_0vvY", "oO6WawhsxTA"]

You can also use size to quickly obtain the estimated number of videos or playlists:

→ Yt docschannel.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 docsYt::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.