Playlists
Yt::Playlist represents a YouTube playlist.
Initialize using its YouTube ID:
Authentication
Most methods of Yt::Playlist retrieve public data from YouTube (e.g.: fetch a playlist’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
playlist = Yt::Playlist.new id: 'PL-LeTutc9GRKD3yBDhnRF_yE8UTa' ## use any playlist ID
playlist.title # => "First public playlist"List of Yt::Playlist data methods
- any authentication works Playlist’s snippet
→ Yt docs
playlist.id # => "PL-LeTutc9GRKD3yBDhnRF_yE8UTaQI5Jf"→ Yt docsplaylist.title # => "First public playlist"→ Yt docsplaylist.description # => "A YouTube playlist to test the yt gem."→ Yt docsplaylist.published_at # => 2016-11-18 00:40:02 UTC→ Yt docsplaylist.thumbnail_url # => "https://i.ytimg.com/vi/gknzFj_0vvY/default.jpg"→ Yt docsplaylist.channel_id # => "UCwCnUcLcb9-eSrHa_RQGkQQ"→ Yt docsplaylist.channel_title # => "Yt Test"- any authentication works Playlist’s status
→ Yt docs
playlist.privacy_status # => "public"- any authentication works Playlist’s content details
→ Yt docs
playlist.item_count # => 2
To limit the number of HTTP requests, use select to specify which parts of the playlist’s data to load:
slow = playlist # => without select: 2 HTTP requestsslow.title # => one HTTP request to fetch the playlist’s snippetslow.privacy_status # => => another HTTP request to fetch the playlist’s status→ Yt docsfast = playlist.select :snippet, :status # => with select: 1 HTTP requestfast.title # => one HTTP request to fetch both the playlist’s snippet and statusfast.privacy_status # => => no extra HTTP requests
- any authentication works Playlist’s items
→ Yt docs
playlist.items# => #<Yt::Relation [#<Yt::PlaylistItem @id=U...>, #<Yt::PlaylistItem @id=T...>, ...]>
- any authentication works Playlist’s videos
→ Yt docs
playlist.videos# => #<Yt::Relation [#<Yt::Video @id=gknz...>, #<Yt::Video @id=32Gc...>, ...]>
Before iterating through items or videos, use select to specify which parts to load:
→ Yt docs
items = playlist.items.select :snippet, :statusitems.map &:title # => ["First public video", "Second public video", ...]items.map &:privacy_status # => ["public", "public", ...]
You can also use limit to only fetch a certain number of items or videos:
→ Yt docs
videos = playlist.videos.limit 2videos.map &:id # => ["gknzFj_0vvY", "oO6WawhsxTA"]
You can also use size to quickly obtain the estimated number of items or videos:
→ Yt docs
playlist.items.size # => 63