yt-annotations 1.3.2 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rspec +2 -0
- data/CHANGELOG.md +4 -0
- data/README.md +1 -1
- data/lib/yt/annotations/end_screen.rb +43 -0
- data/lib/yt/annotations/for.rb +24 -4
- data/lib/yt/annotations/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b11e5000de51c3368ae436830f73718c62ee3c79
|
4
|
+
data.tar.gz: 9c423d7614330f65be5dc065c773929219612162
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6627dc40354ae491ee2db22b372f6ef5dd610a70112838066bdd12e2af703cc3338378d589dd7ccaa1bb7410930d00af60ddad392c9f73e7407c333a379022c8
|
7
|
+
data.tar.gz: 267e86f1fbb728fa20d9059cf2cbdbd639d0045287261059160989f74eaf6c12781839a9f5365883221e5ade4258ca888557eafac2b33028e3212ee042cb6040
|
data/.rspec
ADDED
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
A Ruby gem to fetch YouTube annotations
|
2
2
|
=======================================
|
3
3
|
|
4
|
-
Yt::Annotations is a Ruby library to fetch annotations and
|
4
|
+
Yt::Annotations is a Ruby library to fetch annotations, cards and end screens of YouTube videos.
|
5
5
|
|
6
6
|
The **source code** is available on [GitHub](https://github.com/fullscreen/yt-annotations) and the **documentation** on [RubyDoc](http://www.rubydoc.info/github/fullscreen/yt-annotations/Yt/Annotations).
|
7
7
|
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'yt/annotations/base'
|
2
|
+
|
3
|
+
module Yt
|
4
|
+
module Annotations
|
5
|
+
# An end screen annotation shows in the last 5 to seconds of the video.
|
6
|
+
class EndScreen < Base
|
7
|
+
# @param [Hash] data the Hash representation of the XML data returned by
|
8
|
+
# YouTube for each end screen of a video.
|
9
|
+
def initialize(json = {})
|
10
|
+
@text = json['title']
|
11
|
+
@starts_at = json['startMs'] / 1000.0
|
12
|
+
@ends_at = ends_at_in json
|
13
|
+
@link = to_link json
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def ends_at_in(json)
|
19
|
+
(json['startMs'] + json['durationMs']) / 1000.0
|
20
|
+
end
|
21
|
+
|
22
|
+
def to_link(json)
|
23
|
+
{
|
24
|
+
url: json['targetUrl'], new_window: new_window(json['type']),
|
25
|
+
type: link_type(json)
|
26
|
+
}
|
27
|
+
end
|
28
|
+
|
29
|
+
def link_type(json)
|
30
|
+
case json['type']
|
31
|
+
when 'WEBSITE' then :website
|
32
|
+
when 'PLAYLIST' then :playlist
|
33
|
+
when 'VIDEO' then :video
|
34
|
+
when 'CHANNEL' then (json['isSubscribe'] ? :subscribe : :channel)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def new_window(type)
|
39
|
+
%w(WEBSITE CHANNEL).include? type
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/lib/yt/annotations/for.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'yt/annotations/branding'
|
2
2
|
require 'yt/annotations/card'
|
3
|
+
require 'yt/annotations/end_screen'
|
3
4
|
require 'yt/annotations/featured'
|
4
5
|
require 'yt/annotations/label'
|
5
6
|
require 'yt/annotations/note'
|
@@ -12,14 +13,29 @@ module Yt
|
|
12
13
|
module Annotations
|
13
14
|
module For
|
14
15
|
def for(video_id)
|
15
|
-
|
16
|
-
options = ['www.youtube.com', 443, {use_ssl: true}]
|
17
|
-
response = Net::HTTP.start(*options) {|http| http.request request}
|
18
|
-
xml_to_annotations(Hash.from_xml response.body).sort_by &:starts_at
|
16
|
+
(annotations(video_id) + end_screens(video_id)).sort_by &:starts_at
|
19
17
|
end
|
20
18
|
|
21
19
|
private
|
22
20
|
|
21
|
+
def annotations(video_id)
|
22
|
+
data = fetch "/annotations_invideo?video_id=#{video_id}"
|
23
|
+
xml_to_annotations(Hash.from_xml data)
|
24
|
+
end
|
25
|
+
|
26
|
+
def end_screens(video_id)
|
27
|
+
data = fetch "/get_endscreen?v=#{video_id}"
|
28
|
+
data = data.partition("\n").last
|
29
|
+
data.present? ? json_to_annotations(JSON data) : []
|
30
|
+
end
|
31
|
+
|
32
|
+
def fetch(path)
|
33
|
+
request = Net::HTTP::Get.new path
|
34
|
+
options = ['www.youtube.com', 443, {use_ssl: true}]
|
35
|
+
response = Net::HTTP.start(*options) {|http| http.request request}
|
36
|
+
response.body
|
37
|
+
end
|
38
|
+
|
23
39
|
def xml_to_annotations(xml)
|
24
40
|
annotations = xml['document']['annotations']
|
25
41
|
annotations = Array.wrap (annotations || {})['annotation']
|
@@ -28,6 +44,10 @@ module Yt
|
|
28
44
|
annotations.map{|data| annotation_class(data).new data}
|
29
45
|
end
|
30
46
|
|
47
|
+
def json_to_annotations(json)
|
48
|
+
annotations = json['elements']
|
49
|
+
annotations.map{|data| Annotations::EndScreen.new data}
|
50
|
+
end
|
31
51
|
|
32
52
|
def annotation_class(data)
|
33
53
|
case data['style']
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yt-annotations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- claudiob
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-01-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -110,6 +110,7 @@ extensions: []
|
|
110
110
|
extra_rdoc_files: []
|
111
111
|
files:
|
112
112
|
- ".gitignore"
|
113
|
+
- ".rspec"
|
113
114
|
- ".travis.yml"
|
114
115
|
- CHANGELOG.md
|
115
116
|
- Gemfile
|
@@ -122,6 +123,7 @@ files:
|
|
122
123
|
- lib/yt/annotations/base.rb
|
123
124
|
- lib/yt/annotations/branding.rb
|
124
125
|
- lib/yt/annotations/card.rb
|
126
|
+
- lib/yt/annotations/end_screen.rb
|
125
127
|
- lib/yt/annotations/featured.rb
|
126
128
|
- lib/yt/annotations/for.rb
|
127
129
|
- lib/yt/annotations/label.rb
|