yt-core 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +14 -22
- data/.rspec +3 -0
- data/.travis.yml +8 -0
- data/.yardopts +3 -0
- data/CHANGELOG.md +11 -0
- data/Gemfile +2 -1
- data/LICENSE.txt +17 -18
- data/README.md +81 -17
- data/Rakefile +8 -1
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/docs/_config.yml +10 -0
- data/docs/_includes/doc.html +1 -0
- data/docs/_includes/dt.html +4 -0
- data/docs/_includes/example.html +1 -0
- data/docs/_includes/footer.html +3 -0
- data/docs/_includes/head.html +21 -0
- data/docs/_includes/header.html +57 -0
- data/docs/_layouts/default.html +58 -0
- data/docs/apple-touch-icon-precomposed.png +0 -0
- data/docs/channels.html +142 -0
- data/docs/css/yt.css +40 -0
- data/docs/errors.html +28 -0
- data/docs/favicon.ico +0 -0
- data/docs/fonts/SecondaRound-Bold.eot +0 -0
- data/docs/fonts/SecondaRound-Bold.svg +2160 -0
- data/docs/fonts/SecondaRound-Bold.ttf +0 -0
- data/docs/fonts/SecondaRound-Bold.woff +0 -0
- data/docs/fonts/SecondaRound-Bold.woff2 +0 -0
- data/docs/fonts/SecondaRound-Regular.eot +0 -0
- data/docs/fonts/SecondaRound-Regular.svg +1873 -0
- data/docs/fonts/SecondaRound-Regular.ttf +0 -0
- data/docs/fonts/SecondaRound-Regular.woff +0 -0
- data/docs/fonts/SecondaRound-Regular.woff2 +0 -0
- data/docs/images/console-01.png +0 -0
- data/docs/images/console-02.png +0 -0
- data/docs/images/console-03.png +0 -0
- data/docs/images/console-04.png +0 -0
- data/docs/images/console-05.png +0 -0
- data/docs/images/console-06.png +0 -0
- data/docs/images/console-07.png +0 -0
- data/docs/images/logo.png +0 -0
- data/docs/images/robot.png +0 -0
- data/docs/images/robots.png +0 -0
- data/docs/index.html +37 -0
- data/docs/playlist_items.html +65 -0
- data/docs/playlists.html +114 -0
- data/docs/urls.html +52 -0
- data/docs/videos.html +104 -0
- data/lib/yt/channel.rb +127 -0
- data/lib/yt/core/version.rb +8 -0
- data/lib/yt/core.rb +18 -0
- data/lib/yt/no_items_error.rb +8 -0
- data/lib/yt/playlist.rb +75 -0
- data/lib/yt/playlist_item.rb +59 -0
- data/lib/yt/relation.rb +100 -0
- data/lib/yt/resource.rb +85 -0
- data/lib/yt/response.rb +91 -0
- data/lib/yt/video.rb +216 -0
- data/yt-core.gemspec +27 -15
- metadata +155 -19
- data/lib/yt-core/version.rb +0 -3
- data/lib/yt-core.rb +0 -5
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
data/docs/index.html
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
---
|
2
|
+
title: "Installing Yt"
|
3
|
+
h2: "Installing Yt"
|
4
|
+
---
|
5
|
+
|
6
|
+
<p>Add Yt to any bundled Ruby project by adding the line <code>gem 'yt', '~> {{ page.version }}'</code> to its Gemfile.</p>
|
7
|
+
<p>You can also install Yt in your system by opening the Terminal and typing <code>gem install yt</code>.</p>
|
8
|
+
<p>If you need to update version, type <code>gem update yt</code>, or run <code>bundle update</code> if inside a Ruby project.</p>
|
9
|
+
|
10
|
+
<hr />
|
11
|
+
<h4>Configuring your app</h4>
|
12
|
+
|
13
|
+
<p>
|
14
|
+
In order to use the gem, you must register your app in the Google Developers Console.
|
15
|
+
</p>
|
16
|
+
|
17
|
+
<dl>
|
18
|
+
<dt>Create a project</dt>
|
19
|
+
<dd>
|
20
|
+
Browse to the <a href="https://console.developers.google.com">Google Developers Console</a> and select “Create Project”:
|
21
|
+
<img src="{{ site.baseurl }}/images/console-01.png" width="600" height="420" style="display: block" />
|
22
|
+
From the menu select “API Manager”, then “Credentials”:
|
23
|
+
<img src="{{ site.baseurl }}/images/console-02.png" width="600" height="420" style="display: block" />
|
24
|
+
</dd>
|
25
|
+
<dt><a class="anchor" id="api_key"></a>Generate an API key</dt>
|
26
|
+
<dd>
|
27
|
+
If you are building an app that only retrieves public data from YouTube, all you need is an API key:
|
28
|
+
<img src="{{ site.baseurl }}/images/console-03.png" width="600" height="420" style="display: block" />
|
29
|
+
Once you have generated an API key, you can use it to configure your app:
|
30
|
+
<img src="{{ site.baseurl }}/images/console-04.png" width="600" height="420" style="display: block" />
|
31
|
+
{% highlight ruby %}
|
32
|
+
# run this block before using any method of the gem (replace with your key)
|
33
|
+
Yt.configure do |config|
|
34
|
+
config.api_key = 'AIzaSyAkHdn_oBEjZIEBlpMiGZP-3Y05NXtrzqY'
|
35
|
+
end{% endhighlight %}
|
36
|
+
</dd>
|
37
|
+
</dl>
|
@@ -0,0 +1,65 @@
|
|
1
|
+
---
|
2
|
+
title: "Yt::PlaylistItem"
|
3
|
+
h2: "Playlist items"
|
4
|
+
---
|
5
|
+
|
6
|
+
<p>
|
7
|
+
<code>Yt::PlaylistItem</code> represents a <a href="https://developers.google.com/youtube/v3/docs/playlistItems">YouTube playlist item</a>.
|
8
|
+
Initialize using its YouTube ID:
|
9
|
+
</p>
|
10
|
+
|
11
|
+
{% highlight ruby %}
|
12
|
+
item = Yt::PlaylistItem.new id: 'UEwtTGVUdXRjOUdSS0Qze'
|
13
|
+
# => #<Yt::PlaylistItem:0x... @id=UEwtTGVUdXRjOUdSS0Qze>
|
14
|
+
{% endhighlight %}
|
15
|
+
|
16
|
+
<hr />
|
17
|
+
<h4>Authentication</h4>
|
18
|
+
|
19
|
+
<p>
|
20
|
+
Most methods of <code>Yt::PlaylistItem</code> <strong>retrieve public data</strong> from YouTube (e.g.: fetch an item’s position).<br />
|
21
|
+
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:
|
22
|
+
</p>
|
23
|
+
|
24
|
+
{% highlight ruby %}
|
25
|
+
Yt.configuration.api_key = "<your api key>" ## use your API key
|
26
|
+
item = Yt::PlaylistItem.new id: 'UEwtTGVUdXRjOUdSS0Qze' ## use any playlist item ID
|
27
|
+
item.position # => 0
|
28
|
+
{% endhighlight %}
|
29
|
+
|
30
|
+
<hr />
|
31
|
+
<h4>List of <code>Yt::PlaylistItem</code> data methods</h4>
|
32
|
+
<dl>
|
33
|
+
{% include dt.html title="Playlist item’s snippet" label="success" auth="any authentication works" %}
|
34
|
+
<dd><a class="anchor" id="snippet"></a><div class="highlight"><pre>
|
35
|
+
{% include doc.html instance="PlaylistItem#id" %}{% include example.html object='item' method='id' result='"UEwtTGVUdXRjOUdSS0Qze"' %}
|
36
|
+
{% include doc.html instance="PlaylistItem#title" %}{% include example.html object='item' method='title' result='"First public video"' %}
|
37
|
+
{% include doc.html instance="PlaylistItem#description" %}{% include example.html object='item' method='description' result='"A YouTube video to test the yt gem."' %}
|
38
|
+
{% include doc.html instance="PlaylistItem#published_at" %}{% include example.html object='item' method='published_at' result='2016-11-18 23:40:55 UTC' %}
|
39
|
+
{% include doc.html instance="PlaylistItem#thumbnail_url" %}{% include example.html object='item' method='thumbnail_url' result='"https://i.ytimg.com/vi/gknzFj_0vvY/default.jpg"' %}
|
40
|
+
{% include doc.html instance="PlaylistItem#channel_id" %}{% include example.html object='item' method='channel_id' result='"UCwCnUcLcb9-eSrHa_RQGkQQ"' %}
|
41
|
+
{% include doc.html instance="PlaylistItem#channel_title" %}{% include example.html object='item' method='channel_title' result='"Yt Test"' %}
|
42
|
+
{% include doc.html instance="PlaylistItem#playlist_id" %}{% include example.html object='item' method='playlist_id' result='"PL-LeTutc9GRKD3yBDhnRF_yE8UTaQI5Jf"' %}
|
43
|
+
{% include doc.html instance="PlaylistItem#position" %}{% include example.html object='item' method='position' result='0' %}
|
44
|
+
{% include doc.html instance="PlaylistItem#video_id" %}{% include example.html object='item' method='video_id' result='"gknzFj_0vvY"' %}</pre>
|
45
|
+
</div></dd>
|
46
|
+
|
47
|
+
{% include dt.html title="Playlist item’s status" label="success" auth="any authentication works" %}
|
48
|
+
<dd><a class="anchor" id="status"></a><div class="highlight"><pre>
|
49
|
+
{% include doc.html instance="PlaylistItem#privacy_status" %}{% include example.html object='item' method='privacy_status' result='"public"' %}</pre>
|
50
|
+
</div></dd>
|
51
|
+
</dl>
|
52
|
+
<p>
|
53
|
+
To limit the number of HTTP requests, use <code>select</code> to specify which <a href="https://developers.google.com/youtube/v3/docs/playlistItems/list#part">parts</a> of the item’s data to load:
|
54
|
+
</p>
|
55
|
+
<dl>
|
56
|
+
<dd><a class="anchor" id="select"></a><div class="highlight"><pre>
|
57
|
+
{% include example.html object='slow = item' result='without select: 2 HTTP requests' %}
|
58
|
+
{% include example.html object='slow' method='title' result='one HTTP request to fetch the item’s snippet' %}
|
59
|
+
{% include example.html object='slow' method='privacy_status' result='=> another HTTP request to fetch the item’s status' %}
|
60
|
+
|
61
|
+
{% include doc.html instance="PlaylistItem#select" %}{% include example.html object='fast = item' method='select' params=' <span class="ss">:snippet</span><span class="p">,</span> <span class="ss">:status</span>' result='with select: 1 HTTP request' %}
|
62
|
+
{% include example.html object='fast' method='title' result='one HTTP request to fetch both the item’s snippet and status' %}
|
63
|
+
{% include example.html object='fast' method='privacy_status' result='=> no extra HTTP requests' %}</pre>
|
64
|
+
</div></dd>
|
65
|
+
</dl>
|
data/docs/playlists.html
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
---
|
2
|
+
title: "Yt::Playlist"
|
3
|
+
h2: "Playlists"
|
4
|
+
---
|
5
|
+
|
6
|
+
<p>
|
7
|
+
<code>Yt::Playlist</code> represents a <a href="https://developers.google.com/youtube/v3/docs/playlists">YouTube playlist</a>.
|
8
|
+
Initialize using its YouTube ID:
|
9
|
+
</p>
|
10
|
+
|
11
|
+
<dl>
|
12
|
+
<dd><a class="anchor" id="new"></a><div class="highlight"><pre>
|
13
|
+
{% 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>' %}
|
14
|
+
{% include example.html result='#<Yt::Playlist @id=PL-LeTutc9GRKD3yBDhnRF_yE8UTaQI5Jf>' %}
|
15
|
+
{% include doc.html instance="Playlist#canonical_url" %}{% include example.html object='playlist' method='canonical_url' %}
|
16
|
+
{% include example.html result='"https://www.youtube.com/playlist?list=PL-LeTutc9GRKD3yBDhnRF_yE8UTaQI5Jf"' %}</pre>
|
17
|
+
</div></dd>
|
18
|
+
</dl>
|
19
|
+
|
20
|
+
<hr />
|
21
|
+
<h4>Authentication</h4>
|
22
|
+
|
23
|
+
<p>
|
24
|
+
Most methods of <code>Yt::Playlist</code> <strong>retrieve public data</strong> from YouTube (e.g.: fetch a playlist’s title).<br />
|
25
|
+
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:
|
26
|
+
</p>
|
27
|
+
|
28
|
+
{% highlight ruby %}
|
29
|
+
Yt.configuration.api_key = "<your api key>" ## use your API key
|
30
|
+
playlist = Yt::Playlist.new id: 'PL-LeTutc9GRKD3yBDhnRF_yE8UTa' ## use any playlist ID
|
31
|
+
playlist.title # => "First public playlist"
|
32
|
+
{% endhighlight %}
|
33
|
+
|
34
|
+
<hr />
|
35
|
+
<h4>List of <code>Yt::Playlist</code> data methods</h4>
|
36
|
+
<dl>
|
37
|
+
{% include dt.html title="Playlist’s snippet" label="success" auth="any authentication works" %}
|
38
|
+
<dd><a class="anchor" id="snippet"></a><div class="highlight"><pre>
|
39
|
+
{% include doc.html instance="Playlist#id" %}{% include example.html object='playlist' method='id' result='"PL-LeTutc9GRKD3yBDhnRF_yE8UTaQI5Jf"' %}
|
40
|
+
{% include doc.html instance="Playlist#title" %}{% include example.html object='playlist' method='title' result='"First public playlist"' %}
|
41
|
+
{% include doc.html instance="Playlist#description" %}{% include example.html object='playlist' method='description' result='"A YouTube playlist to test the yt gem."' %}
|
42
|
+
{% include doc.html instance="Playlist#published_at" %}{% include example.html object='playlist' method='published_at' result='2016-11-18 00:40:02 UTC' %}
|
43
|
+
{% 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"' %}
|
44
|
+
{% include doc.html instance="Playlist#channel_id" %}{% include example.html object='playlist' method='channel_id' result='"UCwCnUcLcb9-eSrHa_RQGkQQ"' %}
|
45
|
+
{% include doc.html instance="Playlist#channel_title" %}{% include example.html object='playlist' method='channel_title' result='"Yt Test"' %}</pre>
|
46
|
+
</div></dd>
|
47
|
+
|
48
|
+
{% include dt.html title="Playlist’s status" label="success" auth="any authentication works" %}
|
49
|
+
<dd><a class="anchor" id="status"></a><div class="highlight"><pre>
|
50
|
+
{% include doc.html instance="Playlist#privacy_status" %}{% include example.html object='playlist' method='privacy_status' result='"public"' %}</pre>
|
51
|
+
</div></dd>
|
52
|
+
|
53
|
+
{% include dt.html title="Playlist’s content details" label="success" auth="any authentication works" %}
|
54
|
+
<dd><a class="anchor" id="content_details"></a><div class="highlight"><pre>
|
55
|
+
{% include doc.html instance="Playlist#item_count" %}{% include example.html object='playlist' method='item_count' result='2' %}</pre>
|
56
|
+
</div></dd>
|
57
|
+
|
58
|
+
</dl>
|
59
|
+
<p>
|
60
|
+
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:
|
61
|
+
</p>
|
62
|
+
<dl>
|
63
|
+
<dd><a class="anchor" id="select"></a><div class="highlight"><pre>
|
64
|
+
{% include example.html object='slow = playlist' result='without select: 2 HTTP requests' %}
|
65
|
+
{% include example.html object='slow' method='title' result='one HTTP request to fetch the playlist’s snippet' %}
|
66
|
+
{% include example.html object='slow' method='privacy_status' result='=> another HTTP request to fetch the playlist’s status' %}
|
67
|
+
|
68
|
+
{% 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' %}
|
69
|
+
{% include example.html object='fast' method='title' result='one HTTP request to fetch both the playlist’s snippet and status' %}
|
70
|
+
{% include example.html object='fast' method='privacy_status' result='=> no extra HTTP requests' %}</pre>
|
71
|
+
</div></dd>
|
72
|
+
</dl>
|
73
|
+
|
74
|
+
<dl>
|
75
|
+
{% include dt.html title="Playlist’s items" label="success" auth="any authentication works" %}
|
76
|
+
<dd><a class="anchor" id="items"></a><div class="highlight"><pre>
|
77
|
+
{% include doc.html instance="Playlist#items" %}{% include example.html object='playlist' method='items' %}
|
78
|
+
{% include example.html result='#<Yt::Relation [#<Yt::PlaylistItem @id=U...>, #<Yt::PlaylistItem @id=T...>, ...]>' %}</pre>
|
79
|
+
</div></dd>
|
80
|
+
</dl>
|
81
|
+
<dl>
|
82
|
+
{% include dt.html title="Playlist’s videos" label="success" auth="any authentication works" %}
|
83
|
+
<dd><a class="anchor" id="videos"></a><div class="highlight"><pre>
|
84
|
+
{% include doc.html instance="Playlist#videos" %}{% include example.html object='playlist' method='videos' %}
|
85
|
+
{% include example.html result='#<Yt::Relation [#<Yt::Video @id=gknz...>, #<Yt::Video @id=32Gc...>, ...]>' %}</pre>
|
86
|
+
</div></dd>
|
87
|
+
</dl>
|
88
|
+
<p>
|
89
|
+
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:
|
90
|
+
</p>
|
91
|
+
<dl>
|
92
|
+
<dd><a class="anchor" id="select"></a><div class="highlight"><pre>
|
93
|
+
{% 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>' %}
|
94
|
+
{% include example.html object='items' method='map <span class="ss">&:title</span>' result='["First public video", "Second public video", ...]' %}
|
95
|
+
{% include example.html object='items' method='map <span class="ss">&:privacy_status</span>' result='["public", "public", ...]' %}</pre>
|
96
|
+
</div></dd>
|
97
|
+
</dl>
|
98
|
+
<p>
|
99
|
+
You can also use <code>limit</code> to only fetch a certain number of items or videos:
|
100
|
+
</p>
|
101
|
+
<dl>
|
102
|
+
<dd><a class="anchor" id="limit"></a><div class="highlight"><pre>
|
103
|
+
{% include doc.html instance="Relation#limit" %}{% include example.html object='videos = playlist.videos' method='limit' params=' <span class="mi">2</span>' %}
|
104
|
+
{% include example.html object='videos' method='map <span class="ss">&:id</span>' result='["gknzFj_0vvY", "oO6WawhsxTA"]' %}</pre>
|
105
|
+
</div></dd>
|
106
|
+
</dl>
|
107
|
+
<p>
|
108
|
+
You can also use <code>size</code> to quickly obtain the estimated number of items or videos:
|
109
|
+
</p>
|
110
|
+
<dl>
|
111
|
+
<dd><a class="anchor" id="size"></a><div class="highlight"><pre>
|
112
|
+
{% include doc.html instance="Relation#size" %}{% include example.html object='playlist.items' method='size' result='63' %}</pre>
|
113
|
+
</div></dd>
|
114
|
+
</dl>
|
data/docs/urls.html
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
---
|
2
|
+
title: "Yt::URL"
|
3
|
+
h2: "URLs"
|
4
|
+
---
|
5
|
+
|
6
|
+
<p>
|
7
|
+
<code>Yt::URL</code> is a convenience class to identify YouTube resources based on their URL patterns:
|
8
|
+
</p>
|
9
|
+
|
10
|
+
<dl>
|
11
|
+
<dd><a class="anchor" id="snippet"></a><div class="highlight"><pre>
|
12
|
+
{% include doc.html library="yt-url" instance="URL#initialize" %}{% include example.html object='url = <span class="no">Yt</span><span class="o">::</span><span class="no">URL</span>' method='new' params=' <span class="s1">"youtu.be/gknzFj_0vvY"</span>' %}
|
13
|
+
{% include doc.html library="yt-url" instance="URL#kind" %}{% include example.html object='url' method='kind' result=':video' %}
|
14
|
+
{% include doc.html library="yt-url" instance="URL#id" %}{% include example.html object='url' method='id' result='"gknzFj_0vvY"' %}
|
15
|
+
{% include doc.html library="yt-url" instance="URL#resource" %}{% include example.html object='url' method='resource' result='#<Yt::Video @id=gknzFj_0vvY>' %}</pre>
|
16
|
+
</div></dd>
|
17
|
+
</dl>
|
18
|
+
|
19
|
+
<hr />
|
20
|
+
<h4>Configuration</h4>
|
21
|
+
|
22
|
+
<p>
|
23
|
+
<code>Yt::URL</code> <strong>is not part of the <code>yt</code> library</strong>, but comes from the <a href="https://github.com/claudiob/yt-url">yt-url</a> extension.
|
24
|
+
</p>
|
25
|
+
<p>
|
26
|
+
To use in any bundled Ruby project, remember to add the line <code>gem 'yt-url', '~> 0.0.0'</code> to its Gemfile.
|
27
|
+
</p>
|
28
|
+
|
29
|
+
<hr />
|
30
|
+
<h4>Authentication</h4>
|
31
|
+
|
32
|
+
<p>
|
33
|
+
Using <code>Yt::URL</code> does not require authentication since it is based on matching a set of hard-coded patterns:
|
34
|
+
</p>
|
35
|
+
|
36
|
+
{% highlight ruby %}
|
37
|
+
Yt::URL::PLAYLIST_PATTERNS # => [
|
38
|
+
# %r{^(?:https?://)?(?:www\.)?youtube\.com/playlist/?\?list=(?<id>[a-zA-Z0-9_-]+)},
|
39
|
+
# ]
|
40
|
+
|
41
|
+
Yt::URL::VIDEO_PATTERNS # => [
|
42
|
+
# %r{^(?:https?://)?(?:www\.)?youtube\.com/watch\?v=(?<id>[a-zA-Z0-9_-]{11})},
|
43
|
+
# %r{^(?:https?://)?(?:www\.)?youtu\.be/(?<id>[a-zA-Z0-9_-]{11})},
|
44
|
+
# %r{^(?:https?://)?(?:www\.)?youtube\.com/embed/(?<id>[a-zA-Z0-9_-]{11})},
|
45
|
+
# %r{^(?:https?://)?(?:www\.)?youtube\.com/v/(?<id>[a-zA-Z0-9_-]{11})},
|
46
|
+
# ]
|
47
|
+
|
48
|
+
Yt::URL::CHANNEL_PATTERNS # => [
|
49
|
+
# %r{^(?:https?://)?(?:www\.)?youtube\.com/channel/(?<id>UC[a-zA-Z0-9_-]{22})},
|
50
|
+
# %r{^(?:https?://)?(?:www\.)?youtube\.com/(?<format>c/|user/)?(?<name>[a-zA-Z0-9_-]+)}
|
51
|
+
# ]
|
52
|
+
{% endhighlight %}
|
data/docs/videos.html
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
---
|
2
|
+
title: "Yt::Video"
|
3
|
+
h2: "Videos"
|
4
|
+
---
|
5
|
+
|
6
|
+
<p>
|
7
|
+
<code>Yt::Video</code> represents a <a href="https://developers.google.com/youtube/v3/docs/videos">YouTube video</a>.
|
8
|
+
Initialize using its YouTube ID:
|
9
|
+
</p>
|
10
|
+
|
11
|
+
<dl>
|
12
|
+
<dd><a class="anchor" id="new"></a><div class="highlight"><pre>
|
13
|
+
{% include doc.html instance="Video#initialize" %}{% include example.html object='video = <span class="no">Yt</span><span class="o">::</span><span class="no">Video</span>' method='new' params=' <span class="ss">id:</span> <span class="s1">"gknzFj_0vvY"</span>' %}
|
14
|
+
{% include example.html result='#<Yt::Video @id=gknzFj_0vvY>' %}
|
15
|
+
{% include doc.html instance="Video#canonical_url" %}{% include example.html object='video' method='canonical_url' %}
|
16
|
+
{% include example.html result='"https://www.youtube.com/watch?v=gknzFj_0vvY"' %}</pre>
|
17
|
+
</div></dd>
|
18
|
+
</dl>
|
19
|
+
|
20
|
+
<hr />
|
21
|
+
<h4>Authentication</h4>
|
22
|
+
|
23
|
+
<p>
|
24
|
+
Most methods of <code>Yt::Video</code> <strong>retrieve public data</strong> from YouTube (e.g.: fetch a video’s title).<br />
|
25
|
+
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:
|
26
|
+
</p>
|
27
|
+
|
28
|
+
{% highlight ruby %}
|
29
|
+
Yt.configuration.api_key = "<your api key>" ## use your API key
|
30
|
+
|
31
|
+
video = Yt::Video.new id: 'gknzFj_0vvY' ## use any video ID
|
32
|
+
video.title # => "First public video"
|
33
|
+
{% endhighlight %}
|
34
|
+
|
35
|
+
<hr />
|
36
|
+
<h4>List of <code>Yt::Video</code> data methods</h4>
|
37
|
+
<dl>
|
38
|
+
{% include dt.html title="Video’s snippet" label="success" auth="any authentication works" %}
|
39
|
+
<dd><a class="anchor" id="snippet"></a><div class="highlight"><pre>
|
40
|
+
{% include doc.html instance="Video#id" %}{% include example.html object='video' method='id' result='"gknzFj_0vvY"' %}
|
41
|
+
{% include doc.html instance="Video#title" %}{% include example.html object='video' method='title' result='"First public video"' %}
|
42
|
+
{% include doc.html instance="Video#description" %}{% include example.html object='video' method='description' result='"A YouTube video to test the yt gem."' %}
|
43
|
+
{% include doc.html instance="Video#published_at" %}{% include example.html object='video' method='published_at' result='2016-10-20 02:19:05 UTC' %}
|
44
|
+
{% include doc.html instance="Video#thumbnail_url" %}{% include example.html object='video' method='thumbnail_url' result='"https://i.ytimg.com/vi/gknzFj_0vvY/default.jpg"' %}
|
45
|
+
{% include doc.html instance="Video#channel_id" %}{% include example.html object='video' method='channel_id' result='"UCwCnUcLcb9-eSrHa_RQGkQQ"' %}
|
46
|
+
{% include doc.html instance="Video#channel_title" %}{% include example.html object='video' method='channel_title' result='"Yt Test"' %}
|
47
|
+
{% include doc.html instance="Video#tags" %}{% include example.html object='video' method='tags' result='["yt", "test", "tag"]' %}
|
48
|
+
{% include doc.html instance="Video#category_id" %}{% include example.html object='video' method='category_id' result='22' %}
|
49
|
+
{% include doc.html instance="Video#category_title" %}{% include example.html object='video' method='category_title' result='"People & Blogs"' %}
|
50
|
+
{% include doc.html instance="Video#live_broadcast_content" %}{% include example.html object='video' method='live_broadcast_content' result='"none"' %}</pre>
|
51
|
+
</div></dd>
|
52
|
+
|
53
|
+
{% include dt.html title="Video’s status" label="success" auth="any authentication works" %}
|
54
|
+
<dd><a class="anchor" id="status"></a><div class="highlight"><pre>
|
55
|
+
{% include doc.html instance="Video#privacy_status" %}{% include example.html object='video' method='privacy_status' result='"public"' %}
|
56
|
+
{% include doc.html instance="Video#upload_status" %}{% include example.html object='video' method='upload_status' result='"processed"' %}
|
57
|
+
{% include doc.html instance="Video#license" %}{% include example.html object='video' method='license' result='"creative_common"' %}
|
58
|
+
{% include doc.html instance="Video#embeddable" %}{% include example.html object='video' method='embeddable' result='true' %}
|
59
|
+
{% include doc.html instance="Video#public_stats_viewable" %}{% include example.html object='video' method='public_stats_viewable' result='false' %}</pre>
|
60
|
+
</div></dd>
|
61
|
+
|
62
|
+
{% include dt.html title="Video’s statistics" label="success" auth="any authentication works" %}
|
63
|
+
<dd><a class="anchor" id="statistics"></a><div class="highlight"><pre>
|
64
|
+
{% include doc.html instance="Video#view_count" %}{% include example.html object='video' method='view_count' result='123' %}
|
65
|
+
{% include doc.html instance="Video#like_count" %}{% include example.html object='video' method='like_count' result='93' %}
|
66
|
+
{% include doc.html instance="Video#dislike_count" %}{% include example.html object='video' method='dislike_count' result='42' %}
|
67
|
+
{% include doc.html instance="Video#comment_count" %}{% include example.html object='video' method='comment_count' result='62' %}</pre>
|
68
|
+
</div></dd>
|
69
|
+
|
70
|
+
{% include dt.html title="Video’s content details" label="success" auth="any authentication works" %}
|
71
|
+
<dd><a class="anchor" id="content_details"></a><div class="highlight"><pre>
|
72
|
+
{% include doc.html instance="Video#duration" %}{% include example.html object='video' method='duration' result='"PT2S"' %}
|
73
|
+
{% include doc.html instance="Video#seconds" %}{% include example.html object='video' method='seconds' result='2' %}
|
74
|
+
{% include doc.html instance="Video#length" %}{% include example.html object='video' method='length' result='"00:00:02"' %}
|
75
|
+
{% include doc.html instance="Video#dimension" %}{% include example.html object='video' method='dimension' result='"2d"' %}
|
76
|
+
{% include doc.html instance="Video#definition" %}{% include example.html object='video' method='definition' result='"sd"' %}
|
77
|
+
{% include doc.html instance="Video#caption" %}{% include example.html object='video' method='caption' result='false' %}
|
78
|
+
{% include doc.html instance="Video#licensed_content" %}{% include example.html object='video' method='licensed_content' result='false' %}
|
79
|
+
{% include doc.html instance="Video#projection" %}{% include example.html object='video' method='projection' result='"rectangular"' %}</pre>
|
80
|
+
</div></dd>
|
81
|
+
|
82
|
+
</dl>
|
83
|
+
<p>
|
84
|
+
To limit the number of HTTP requests, use <code>select</code> to specify which <a href="https://developers.google.com/youtube/v3/docs/videos/list#part">parts</a> of the video’s data to load:
|
85
|
+
</p>
|
86
|
+
<dl>
|
87
|
+
<dd><a class="anchor" id="select"></a><div class="highlight"><pre>
|
88
|
+
{% include example.html object='slow = video' result='without select: 2 HTTP requests' %}
|
89
|
+
{% include example.html object='slow' method='title' result='one HTTP request to fetch the video’s snippet' %}
|
90
|
+
{% include example.html object='slow' method='privacy_status' result='=> another HTTP request to fetch the video’s status' %}
|
91
|
+
|
92
|
+
{% include doc.html instance="Video#select" %}{% include example.html object='fast = video' method='select' params=' <span class="ss">:snippet</span><span class="p">,</span> <span class="ss">:status</span>' result='with select: 1 HTTP request' %}
|
93
|
+
{% include example.html object='fast' method='title' result='one HTTP request to fetch both the video’s snippet and status' %}
|
94
|
+
{% include example.html object='fast' method='privacy_status' result='=> no extra HTTP requests' %}</pre>
|
95
|
+
</div></dd>
|
96
|
+
</dl>
|
97
|
+
|
98
|
+
<dl>
|
99
|
+
{% include dt.html title="Video’s channel" label="success" auth="any authentication works" %}
|
100
|
+
<dd><a class="anchor" id="channel"></a><div class="highlight"><pre>
|
101
|
+
{% include doc.html instance="Video#channel" %}{% include example.html object='video' method='channel' %}
|
102
|
+
{% include example.html result='#<Yt::Channel @id=UCwCnUcLcb9-eSrHa_RQGkQQ>' %}</pre>
|
103
|
+
</div></dd>
|
104
|
+
</dl>
|
data/lib/yt/channel.rb
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
module Yt
|
2
|
+
# Provides methods to interact with YouTube channels.
|
3
|
+
# @see https://developers.google.com/youtube/v3/docs/channels
|
4
|
+
class Channel < Resource
|
5
|
+
# @!attribute [r] title
|
6
|
+
# @return [String] the channel’s title.
|
7
|
+
has_attribute :title, in: :snippet
|
8
|
+
|
9
|
+
# @!attribute [r] description
|
10
|
+
# @return [String] the channel’s description.
|
11
|
+
has_attribute :description, in: :snippet
|
12
|
+
|
13
|
+
# @!attribute [r] published_at
|
14
|
+
# @return [Time] the date and time that the channel was created.
|
15
|
+
has_attribute :published_at, in: :snippet, type: Time
|
16
|
+
|
17
|
+
# @!attribute [r] thumbnails
|
18
|
+
# @return [Hash<String, Hash>] the thumbnails associated with the video.
|
19
|
+
has_attribute :thumbnails, in: :snippet
|
20
|
+
|
21
|
+
# @!attribute [r] custom_url
|
22
|
+
# @return [<String, nil>] the path component of the channel’s custom URL.
|
23
|
+
has_attribute :custom_url, in: :snippet
|
24
|
+
|
25
|
+
# has_attribute :default_language, in: :snippet not sure how to set to test
|
26
|
+
# has_attribute :localized, in: :snippet not yet implemented
|
27
|
+
# has_attribute :country, in: :snippet not sure how to set to test
|
28
|
+
|
29
|
+
# @!attribute [r] privacy_status
|
30
|
+
# @return [String] the privacy status of the channel. Valid values are:
|
31
|
+
# +"private"+, +"public"+, +"unlisted"+.
|
32
|
+
has_attribute :privacy_status, in: :status
|
33
|
+
|
34
|
+
# @!attribute [r] is_linked
|
35
|
+
# @return [Boolean] whether the channel data identifies a user that is
|
36
|
+
# already linked to either a YouTube username or a Google+ account.
|
37
|
+
has_attribute :is_linked, in: :status
|
38
|
+
|
39
|
+
# @!attribute [r] long_uploads_status
|
40
|
+
# @return [String] whether the channel is eligible to upload videos that
|
41
|
+
# are more than 15 minutes long. Valid values are: +"allowed"+,
|
42
|
+
# +"disallowed"+, +"eligible"+, +"longUploadsUnspecified"+.
|
43
|
+
# @note +"longUploadsUnspecified"+ is not documented by the YouTube API.
|
44
|
+
has_attribute :long_uploads_status, in: :status
|
45
|
+
|
46
|
+
# @!attribute [r] view_count
|
47
|
+
# @return [<Integer>] the number of times the channel has been viewed.
|
48
|
+
has_attribute :view_count, in: :statistics, type: Integer
|
49
|
+
|
50
|
+
# @!attribute [r] comment_count
|
51
|
+
# @return [<Integer>] the number of comments for the channel.
|
52
|
+
has_attribute :comment_count, in: :statistics, type: Integer
|
53
|
+
|
54
|
+
# @!attribute [r] subscriber_count
|
55
|
+
# @return [<Integer>] the number of subscribers that the channel has.
|
56
|
+
has_attribute :subscriber_count, in: :statistics, type: Integer
|
57
|
+
|
58
|
+
# @!attribute [r] hidden_subscriber_count
|
59
|
+
# @return [<Boolean>] whether the channel’s subscriber count is publicly
|
60
|
+
# visible.
|
61
|
+
has_attribute :hidden_subscriber_count, in: :statistics
|
62
|
+
|
63
|
+
# @!attribute [r] video_count
|
64
|
+
# @return [<Integer>] the number of videos uploaded to the channel.
|
65
|
+
has_attribute :video_count, in: :statistics, type: Integer
|
66
|
+
|
67
|
+
# @!attribute [r] banner_image_url
|
68
|
+
# @return [String] the URL for the banner image shown on the channel page
|
69
|
+
# on the YouTube website. The image is 1060px by 175px.
|
70
|
+
has_attribute :banner_image_url, in: %i(branding_settings image)
|
71
|
+
|
72
|
+
# @!attribute [r] keywords
|
73
|
+
# @return [Array<String>] the keywords associated with the channel.
|
74
|
+
has_attribute :keywords, in: %i(branding_settings channel) do |keywords|
|
75
|
+
(keywords || '').split ' '
|
76
|
+
end
|
77
|
+
|
78
|
+
# @!attribute [r] unsubscribed_trailer
|
79
|
+
# @return [<String, nil>] if specified, the ID of a public or unlisted video
|
80
|
+
# owned by the channel owner that should play in the featured video module
|
81
|
+
# in the channel page’s browse view for unsubscribed viewers.
|
82
|
+
has_attribute :unsubscribed_trailer, in: %i(branding_settings channel)
|
83
|
+
|
84
|
+
# @return [String] the canonical form of the channel’s URL.
|
85
|
+
def canonical_url
|
86
|
+
"https://www.youtube.com/channel/#{id}"
|
87
|
+
end
|
88
|
+
|
89
|
+
# @return [<String] the full channel’s URL (custom or canonical).
|
90
|
+
# @see https://support.google.com/youtube/answer/2657968
|
91
|
+
def vanity_url
|
92
|
+
if custom_url
|
93
|
+
"https://www.youtube.com/#{custom_url}"
|
94
|
+
else
|
95
|
+
canonical_url
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
# Returns the URL of one of the channel’s thumbnail.
|
100
|
+
# @param [Symbol, String] size The size of the channel’s thumbnail.
|
101
|
+
# @return [String] if +size+ is +:default+, the URL of a 88x88px image.
|
102
|
+
# @return [String] if +size+ is +:medium+, the URL of a 240x240px image.
|
103
|
+
# @return [String] if +size+ is +:high+, the URL of a 800x800px image.
|
104
|
+
# @return [nil] if the +size+ is none of the above.
|
105
|
+
def thumbnail_url(size = :default)
|
106
|
+
thumbnails.fetch(size.to_s, {})['url']
|
107
|
+
end
|
108
|
+
|
109
|
+
# @return [Yt::Relation<Yt::Video>] the public videos of the channel.
|
110
|
+
# @note For unauthenticated channels, results are constrained to a maximum
|
111
|
+
# of 500 videos.
|
112
|
+
# @see https://developers.google.com/youtube/v3/docs/search/list#channelId
|
113
|
+
def videos
|
114
|
+
@videos ||= Relation.new(Video, channel_id: id, limit: 500) do |options|
|
115
|
+
items = fetch '/youtube/v3/search', channel_videos_params(options)
|
116
|
+
videos_for items, 'id', options
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
# @return [Yt::Relation<Yt::Playlist>] the public playlists of the channel.
|
121
|
+
def playlists
|
122
|
+
@playlists ||= Relation.new(Playlist, channel_id: id) do |options|
|
123
|
+
fetch '/youtube/v3/playlists', channel_playlists_params(options)
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
data/lib/yt/core.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'json' # for JSON.parse
|
2
|
+
|
3
|
+
require 'yt/config'
|
4
|
+
require 'yt/no_items_error'
|
5
|
+
require 'yt/http_request'
|
6
|
+
require 'yt/relation'
|
7
|
+
require 'yt/resource'
|
8
|
+
require 'yt/response'
|
9
|
+
|
10
|
+
require 'yt/channel'
|
11
|
+
require 'yt/playlist'
|
12
|
+
require 'yt/playlist_item'
|
13
|
+
require 'yt/video'
|
14
|
+
|
15
|
+
# An object-oriented Ruby client for YouTube.
|
16
|
+
# @see http://www.rubydoc.info/gems/yt/
|
17
|
+
module Yt
|
18
|
+
end
|
@@ -0,0 +1,8 @@
|
|
1
|
+
module Yt
|
2
|
+
# Raised when items are expected from YouTube but none are returned.
|
3
|
+
# @example Fetch the title of a channel with an unknown ID.
|
4
|
+
# Yt::Channel.new(id: 'unknown-id').title
|
5
|
+
# # => raise Yt::NoItemsError
|
6
|
+
class NoItemsError < StandardError
|
7
|
+
end
|
8
|
+
end
|