Fullscreen/yt-core

View on GitHub
docs/channels.html

Summary

Maintainability
Test Coverage
---
title:  "Yt::Channel"
h2: "Channels"
---

<p>
  <code>Yt::Channel</code> represents a <a href="https://developers.google.com/youtube/v3/docs/channels">YouTube channel</a>.
  Initialize using its YouTube ID:
</p>

<dl>
  <dd><a class="anchor" id="new"></a><div class="highlight"><pre>
{% include doc.html instance="Channel#initialize" %}{% include example.html object='channel = <span class="no">Yt</span><span class="o">::</span><span class="no">Channel</span>' method='new' params=' <span class="ss">id:</span> <span class="s1">"UCwCnUcLcb9-eSrHa_RQGkQQ"</span>' %}
{% include example.html result='#&lt;Yt::Channel @id=UCwCnUcLcb9-eSrHa_RQGkQQ&gt;' %}
{% include doc.html instance="Channel#canonical_url" %}{% include example.html object='channel' method='canonical_url' %}
{% include example.html result='"https://www.youtube.com/channel/UCwCnUcLcb9-eSrHa_RQGkQQ"' %}</pre>
  </div></dd>
</dl>

<hr />
<h4>Authentication</h4>

<p>
  Most methods of <code>Yt::Channel</code> <strong>retrieve public data</strong> from YouTube (e.g.: fetch a channel’s title).<br />
  To use these methods (marked with <span class="label label-success">&nbsp;</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

channel = Yt::Channel.new id: 'UCwCnUcLcb9-eSrHa_RQGkQQ'         ## use any channel ID
channel.title # => "Yt Test"
{% endhighlight %}


<p>
  Other methods <strong>acts on behalf of YouTube accounts</strong> (e.g.: subscribe to a channel, delete playlists).<br />
  To use these methods (marked with  <span class="label label-warning">&nbsp;</span> below), you need to <a href="{{ site.baseurl }}/#api_client">get an API Client ID/Secret from Google</a>, then <a href="{{ site.baseurl }}/#tokens">obtain a refresh token</a> from the account you want to act as, and finally configure the values:
</p>

{% highlight ruby %}
Yt.configuration.client_id = "<your ID>"           ## replace with your client ID
Yt.configuration.client_secret = "<your secret>"   ## replace with your client secret
Yt.configuration.refresh_token = "<token>"         ## use the account’s refresh token

channel = Yt::Channel.mine
# => #<Yt::Channel @id=UCwCnUcLcb9-eSrHa_RQGkQQ>
{% endhighlight %}

<hr />
<h4>List of <code>Yt::Channel</code> data methods</h4>
<dl>
  {% include dt.html title="Channel’s snippet" label="success" auth="any authentication works" %}
  <dd><a class="anchor" id="snippet"></a><div class="highlight"><pre>
{% include doc.html instance="Channel#id" %}{% include example.html object='channel' method='id' result='"UCwCnUcLcb9-eSrHa_RQGkQQ"' %}
{% include doc.html instance="Channel#title" %}{% include example.html object='channel' method='title' result='"Yt Test"' %}
{% include doc.html instance="Channel#description" %}{% include example.html object='channel' method='description' result='"A test channel."' %}
{% include doc.html instance="Channel#custom_url" %}{% include example.html object='channel' method='custom_url' result='"yt-test"' %}
{% include doc.html instance="Channel#vanity_url" %}{% include example.html object='channel' method='vanity_url' result='"https://www.youtube.com/yt-test"' %}
{% include doc.html instance="Channel#published_at" %}{% include example.html object='channel' method='published_at' result='2014-05-02 20:12:57 UTC' %}
{% include doc.html instance="Channel#thumbnail_url" %}{% include example.html object='channel' method='thumbnail_url' result='"https://yt3.ggpht.com/-KMnbKDBl60w/photo.jpg"' %}</pre>
  </div></dd>

  {% include dt.html title="Channel’s status" label="success" auth="any authentication works" %}
  <dd><a class="anchor" id="status"></a><div class="highlight"><pre>
{% include doc.html instance="Channel#privacy_status" %}{% include example.html object='channel' method='privacy_status' result='"public"' %}
{% include doc.html instance="Channel#is_linked" %}{% include example.html object='channel' method='is_linked' result='true' %}
{% include doc.html instance="Channel#long_uploads_status" %}{% include example.html object='channel' method='long_uploads_status' result='"longUploadsUnspecified"' %}</pre>
  </div></dd>

  {% include dt.html title="Channel’s statistics" label="success" auth="any authentication works" %}
  <dd><a class="anchor" id="statistics"></a><div class="highlight"><pre>
{% include doc.html instance="Channel#view_count" %}{% include example.html object='channel' method='view_count' result='123' %}
{% include doc.html instance="Channel#comment_count" %}{% include example.html object='channel' method='comment_count' result='93' %}
{% include doc.html instance="Channel#subscriber_count" %}{% include example.html object='channel' method='subscriber_count' result='42' %}
{% include doc.html instance="Channel#hidden_subscriber_count" %}{% include example.html object='channel' method='hidden_subscriber_count' result='false' %}
{% include doc.html instance="Channel#video_count" %}{% include example.html object='channel' method='video_count' result='62' %}</pre>
  </div></dd>

  {% include dt.html title="Channel’s branding settings" label="success" auth="any authentication works" %}
  <dd><a class="anchor" id="branding_settings"></a><div class="highlight"><pre>
{% include doc.html instance="Channel#keywords" %}{% include example.html object='channel' method='keywords' result='["Some", "tag"]' %}
{% include doc.html instance="Channel#unsubscribed_trailer" %}{% include example.html object='channel' method='unsubscribed_trailer' result='"gknzFj_0vvY"' %}
{% include doc.html instance="Channel#featured_channels_title" %}{% include example.html object='channel' method='featured_channels_title' result='"Featured channels"' %}
{% include doc.html instance="Channel#featured_channels_urls" %}{% include example.html object='channel' method='featured_channels_urls' result='["UCxO1tY8h1AhOz0T4ENwmpow"]' %}</pre>
  </div></dd>

  {% include dt.html title="Channel’s content details" label="warning" auth="must authenticate as the channel’s account" %}
  <dd><a class="anchor" id="content_details"></a><div class="highlight"><pre>
{% include doc.html instance="Channel#related_playlists" %}{% include example.html object='channel' method='related_playlists' result='{"likes"=>"LLwCncb9-e", "watchHistory"=>"HL"}' %}
{% include doc.html instance="Channel#like_playlists" %}{% include example.html object='channel' method='like_playlists' result='#&lt;Yt::Relation [#&lt;Yt::Playlist @id=LLWCncb...&gt;]&gt;' %}</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/channels/list#part">parts</a> of the channel’s data to load:
</p>
<dl>
  <dd><a class="anchor" id="select"></a><div class="highlight"><pre>
{% include example.html object='slow = channel' result='without select: 2 HTTP requests' %}
{% include example.html object='slow' method='title' result='one HTTP request to fetch the channel’s snippet' %}
{% include example.html object='slow' method='privacy_status' result='another HTTP request to fetch the channel’s status' %}

{% include doc.html instance="Channel#select" %}{% include example.html object='fast = channel' 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 channel’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="Channel’s (public) videos" label="success" auth="any authentication works" %}
  <dd><a class="anchor" id="videos"></a><div class="highlight"><pre>
{% include doc.html instance="Channel#videos" %}{% include example.html object='channel' method='videos' %}
{% include example.html result='#&lt;Yt::Relation [#&lt;Yt::Video @id=gknz...&gt;, #&lt;Yt::Video @id=32Gc...&gt;, ...]&gt;' %}</pre>
  </div></dd>
</dl>
<dl>
  {% include dt.html title="Channel’s (public) playlists" label="success" auth="any authentication works" %}
  <dd><a class="anchor" id="playlists"></a><div class="highlight"><pre>
{% include doc.html instance="Channel#playlists" %}{% include example.html object='channel' method='playlists' %}
{% include example.html result='#&lt;Yt::Relation [#&lt;Yt::Playlist @id=PL-L...&gt;, #&lt;Yt::Playlist @id=PL-N...&gt;, ...]&gt;' %}</pre>
  </div></dd>
</dl>
<p>
  Before iterating through videos or playlists, use <code>select</code> to specify which <a href="https://developers.google.com/youtube/v3/docs/videos/list#part">parts</a> of each video’s data 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='videos = channel.videos' method='select' params=' <span class="ss">:snippet</span>, <span class="ss">:statistics</span>' %}
{% include example.html object='videos' method='map <span class="ss">&amp;:title</span>' result='["First public video", "Second public video", ...]' %}
{% include example.html object='videos' method='map <span class="ss">&amp;:view_count</span>' result='[123, 456, ...]' %}</pre>
  </div></dd>
</dl>
<p>
  You can also use <code>limit</code> to only fetch a certain number of videos or playlists:
</p>
<dl>
  <dd><a class="anchor" id="limit"></a><div class="highlight"><pre>
{% include doc.html instance="Relation#limit" %}{% include example.html object='videos = channel.videos' method='limit' params=' <span class="mi">2</span>' %}
{% include example.html object='videos' method='map <span class="ss">&amp;: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 videos or playlists:
</p>
<dl>
  <dd><a class="anchor" id="size"></a><div class="highlight"><pre>
{% include doc.html instance="Relation#size" %}{% include example.html object='channel.videos' method='size' result='312' %}</pre>
  </div></dd>
</dl>
<p>
  Note that, due to <a href="https://developers.google.com/youtube/v3/docs/search/list#channelId">YouTube API limitations</a>, only a maximum of 500 videos can be fetched for an unauthenticated channel.
</p>

<dl>
  {% include dt.html title="Collection of channels" label="success" auth="any authentication works" %}
  <dd><a class="anchor" id="where"></a><div class="highlight"><pre>
{% include doc.html class="Channel#where" %}{% include example.html object='<span class="no">Yt</span><span class="o">::</span><span class="no">Channel</span>' method='where'  params=' <span class="ss">id:</span> [<span class="s1">"UCwCnUcLcb9-eSrHa_RQGkQQ"</span>, <span class="s1">"UCKe_0fJtkT1dYnzn"</span>, ...]' %}
{% include example.html result='#&lt;Yt::Relation [#&lt;Yt::Channel @id=UCwCnUcLcb9-eSrHa_RQGkQQ&gt;, ...]&gt;' %}</pre>
  </div></dd>
</dl>
<p>
  The previous method returns existing channels that match the provided IDs, skipping any unrecognized ID.<br />
  As usual, use <code>select</code> to specify which <a href="https://developers.google.com/youtube/v3/docs/channels/list#part">parts</a> of each channels’s data to load before iterating through the list.
</p>

<dl>
  {% include dt.html title="Authenticated account’s channel" label="warning" auth="must authenticate as the channel’s account" %}
  <dd><a class="anchor" id="mine"></a><div class="highlight"><pre>
{% include doc.html class="Channel#mine" %}{% include example.html object='<span class="no">Yt</span><span class="o">::</span><span class="no">Channel</span>' method='mine' %}
{% include example.html result='#&lt;Yt::Channel @id=UCwCnUcLcb9-eSrHa_RQGkQQ&gt;' %}</pre>
  </div></dd>
</dl>