docs/playlists.html
---
title: "Yt::Playlist"
h2: "Playlists"
---
<p>
<code>Yt::Playlist</code> represents a <a href="https://developers.google.com/youtube/v3/docs/playlists">YouTube playlist</a>.
Initialize using its YouTube ID:
</p>
<dl>
<dd><a class="anchor" id="new"></a><div class="highlight"><pre>
{% include doc.html instance="Playlist#initialize" %}{% include example.html object='playlist = <span class="no">Yt</span><span class="o">::</span><span class="no">Playlist</span>' method='new' params=' <span class="ss">id:</span> <span class="s1">"PL-LeTutc9GRKD3yBDhnRF_yE8UTaQI5Jf"</span>' %}
{% include example.html result='#<Yt::Playlist @id=PL-LeTutc9GRKD3yBDhnRF_yE8UTaQI5Jf>' %}
{% include doc.html instance="Playlist#canonical_url" %}{% include example.html object='playlist' method='canonical_url' %}
{% include example.html result='"https://www.youtube.com/playlist?list=PL-LeTutc9GRKD3yBDhnRF_yE8UTaQI5Jf"' %}</pre>
</div></dd>
</dl>
<hr />
<h4>Authentication</h4>
<p>
Most methods of <code>Yt::Playlist</code> <strong>retrieve public data</strong> from YouTube (e.g.: fetch a playlist’s title).<br />
To use these methods (marked with <span class="label label-success"> </span> below), you only need to <a href="{{ site.baseurl }}/#api_key">generate an API key</a> and configure:
</p>
{% highlight ruby %}
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"
{% endhighlight %}
<hr />
<h4>List of <code>Yt::Playlist</code> data methods</h4>
<dl>
{% include dt.html title="Playlist’s snippet" label="success" auth="any authentication works" %}
<dd><a class="anchor" id="snippet"></a><div class="highlight"><pre>
{% include doc.html instance="Playlist#id" %}{% include example.html object='playlist' method='id' result='"PL-LeTutc9GRKD3yBDhnRF_yE8UTaQI5Jf"' %}
{% include doc.html instance="Playlist#title" %}{% include example.html object='playlist' method='title' result='"First public playlist"' %}
{% include doc.html instance="Playlist#description" %}{% include example.html object='playlist' method='description' result='"A YouTube playlist to test the yt gem."' %}
{% include doc.html instance="Playlist#published_at" %}{% include example.html object='playlist' method='published_at' result='2016-11-18 00:40:02 UTC' %}
{% include doc.html instance="Playlist#thumbnail_url" %}{% include example.html object='playlist' method='thumbnail_url' result='"https://i.ytimg.com/vi/gknzFj_0vvY/default.jpg"' %}
{% include doc.html instance="Playlist#channel_id" %}{% include example.html object='playlist' method='channel_id' result='"UCwCnUcLcb9-eSrHa_RQGkQQ"' %}
{% include doc.html instance="Playlist#channel_title" %}{% include example.html object='playlist' method='channel_title' result='"Yt Test"' %}</pre>
</div></dd>
{% include dt.html title="Playlist’s status" label="success" auth="any authentication works" %}
<dd><a class="anchor" id="status"></a><div class="highlight"><pre>
{% include doc.html instance="Playlist#privacy_status" %}{% include example.html object='playlist' method='privacy_status' result='"public"' %}</pre>
</div></dd>
{% include dt.html title="Playlist’s content details" label="success" auth="any authentication works" %}
<dd><a class="anchor" id="content_details"></a><div class="highlight"><pre>
{% include doc.html instance="Playlist#item_count" %}{% include example.html object='playlist' method='item_count' result='2' %}</pre>
</div></dd>
</dl>
<p>
To limit the number of HTTP requests, use <code>select</code> to specify which <a href="https://developers.google.com/youtube/v3/docs/playlists/list#part">parts</a> of the playlist’s data to load:
</p>
<dl>
<dd><a class="anchor" id="select"></a><div class="highlight"><pre>
{% include example.html object='slow = playlist' result='without select: 2 HTTP requests' %}
{% include example.html object='slow' method='title' result='one HTTP request to fetch the playlist’s snippet' %}
{% include example.html object='slow' method='privacy_status' result='=> another HTTP request to fetch the playlist’s status' %}
{% include doc.html instance="Playlist#select" %}{% include example.html object='fast = playlist' method='select' params=' <span class="ss">:snippet</span><span class="p">,</span> <span class="ss">:status</span>' result='with select: 1 HTTP request' %}
{% include example.html object='fast' method='title' result='one HTTP request to fetch both the playlist’s snippet and status' %}
{% include example.html object='fast' method='privacy_status' result='=> no extra HTTP requests' %}</pre>
</div></dd>
</dl>
<dl>
{% include dt.html title="Playlist’s items" label="success" auth="any authentication works" %}
<dd><a class="anchor" id="items"></a><div class="highlight"><pre>
{% include doc.html instance="Playlist#items" %}{% include example.html object='playlist' method='items' %}
{% include example.html result='#<Yt::Relation [#<Yt::PlaylistItem @id=U...>, #<Yt::PlaylistItem @id=T...>, ...]>' %}</pre>
</div></dd>
</dl>
<dl>
{% include dt.html title="Playlist’s videos" label="success" auth="any authentication works" %}
<dd><a class="anchor" id="videos"></a><div class="highlight"><pre>
{% include doc.html instance="Playlist#videos" %}{% include example.html object='playlist' method='videos' %}
{% include example.html result='#<Yt::Relation [#<Yt::Video @id=gknz...>, #<Yt::Video @id=32Gc...>, ...]>' %}</pre>
</div></dd>
</dl>
<p>
Before iterating through items or videos, use <code>select</code> to specify which <a href="https://developers.google.com/youtube/v3/docs/videos/list#part">parts</a> to load:
</p>
<dl>
<dd><a class="anchor" id="select"></a><div class="highlight"><pre>
{% include doc.html instance="Relation#select" %}{% include example.html object='items = playlist.items' method='select' params=' <span class="ss">:snippet</span>, <span class="ss">:status</span>' %}
{% include example.html object='items' method='map <span class="ss">&:title</span>' result='["First public video", "Second public video", ...]' %}
{% include example.html object='items' method='map <span class="ss">&:privacy_status</span>' result='["public", "public", ...]' %}</pre>
</div></dd>
</dl>
<p>
You can also use <code>limit</code> to only fetch a certain number of items or videos:
</p>
<dl>
<dd><a class="anchor" id="limit"></a><div class="highlight"><pre>
{% include doc.html instance="Relation#limit" %}{% include example.html object='videos = playlist.videos' method='limit' params=' <span class="mi">2</span>' %}
{% include example.html object='videos' method='map <span class="ss">&:id</span>' result='["gknzFj_0vvY", "oO6WawhsxTA"]' %}</pre>
</div></dd>
</dl>
<p>
You can also use <code>size</code> to quickly obtain the estimated number of items or videos:
</p>
<dl>
<dd><a class="anchor" id="size"></a><div class="highlight"><pre>
{% include doc.html instance="Relation#size" %}{% include example.html object='playlist.items' method='size' result='63' %}</pre>
</div></dd>
</dl>