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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5f1975d4c1a40fe0912df7629828d26a268ae5c7
4
- data.tar.gz: 10b9e600b187b9e1116e63e0260e1056e8cb6eb6
3
+ metadata.gz: b11e5000de51c3368ae436830f73718c62ee3c79
4
+ data.tar.gz: 9c423d7614330f65be5dc065c773929219612162
5
5
  SHA512:
6
- metadata.gz: 125f6b584f591e0248fa719fc966ea616c3a00692df928075932fa23a2b79c6a7bfa4cac6f829234d09efd4890f047fccfd6735f91124ca67e24e0dd8a26be74
7
- data.tar.gz: 6062fe651de36ea7c683d9e8cecba7988609bb3c9dd21fe3c5143d9b5d2537ebfcd70eb69c256f58f66352c38017398fdbbb7b3eaf70294974073faa60104f2d
6
+ metadata.gz: 6627dc40354ae491ee2db22b372f6ef5dd610a70112838066bdd12e2af703cc3338378d589dd7ccaa1bb7410930d00af60ddad392c9f73e7407c333a379022c8
7
+ data.tar.gz: 267e86f1fbb728fa20d9059cf2cbdbd639d0045287261059160989f74eaf6c12781839a9f5365883221e5ade4258ca888557eafac2b33028e3212ee042cb6040
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -7,6 +7,10 @@ For more information about changelogs, check
7
7
  [Vandamme](http://tech-angels.github.io/vandamme).
8
8
 
9
9
 
10
+ ## 1.4.0 - 2017.01.13
11
+
12
+ * [FEATURE] Add End Screens
13
+
10
14
  ## 1.3.2 - 2016.09.08
11
15
 
12
16
  * [BUGFIX] Do not raise errors from videos with 'speech' style
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 cards of YouTube videos.
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
@@ -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
- request = Net::HTTP::Get.new "/annotations_invideo?video_id=#{video_id}"
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']
@@ -1,5 +1,5 @@
1
1
  module Yt
2
2
  module Annotations
3
- VERSION = '1.3.2'
3
+ VERSION = '1.4.0'
4
4
  end
5
5
  end
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.3.2
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: 2016-09-09 00:00:00.000000000 Z
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