Skip to content

Commit

Permalink
Merge pull request #19 from Fullscreen/endscreen-fix
Browse files Browse the repository at this point in the history
get Endscreen back to work
  • Loading branch information
kangkyu committed Mar 31, 2017
2 parents 78807e3 + 63ed5c8 commit bad5ac9
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ For more information about changelogs, check
[Vandamme](http://tech-angels.github.io/vandamme).


## 1.4.1 - 2017.03.31

* [BUGFIX] Change `EndScreen` according to its endpoint change on YouTube

## 1.4.0 - 2017.01.13

* [FEATURE] Add End Screens
Expand Down
34 changes: 27 additions & 7 deletions lib/yt/annotations/end_screen.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,54 @@ class EndScreen < Base
# @param [Hash] data the Hash representation of the XML data returned by
# YouTube for each end screen of a video.
def initialize(json = {})
@text = json['title']
@starts_at = json['startMs'] / 1000.0
@text = json['title']['runs'][0]['text']
@starts_at = json['startMs'].to_i / 1000.0
@ends_at = ends_at_in json
@link = to_link json
end

private

def ends_at_in(json)
(json['startMs'] + json['durationMs']) / 1000.0
json['endMs'].to_i / 1000.0
end

def to_link(json)
target_url = case json['style']
when 'WEBSITE'
json['endpoint']['urlEndpoint']['url']
when 'PLAYLIST'
"https://www.youtube.com/watch?v=" +
json['endpoint']['watchEndpoint']['videoId'] +
"&list=" +
json['endpoint']['watchEndpoint']['playlistId']
when 'VIDEO'
"https://www.youtube.com/watch?v=" +
json['endpoint']['watchEndpoint']['videoId']
when 'CHANNEL'
if json['isSubscribe']
"https://www.youtube.com/channel/" +
json['hovercardButton']['subscribeButtonRenderer']['channelId']
else
"https://www.youtube.com/channel/" +
json['endpoint']['browseEndpoint']["browseId"]
end
end
{
url: json['targetUrl'], new_window: new_window(json['type']),
url: target_url, new_window: new_window(json['style']),
type: link_type(json)
}
end

def link_type(json)
case json['type']
case json['style']
when 'WEBSITE' then :website
when 'PLAYLIST' then :playlist
when 'VIDEO' then :video
when 'CHANNEL' then (json['isSubscribe'] ? :subscribe : :channel)
end
end

def new_window(type)
%w(WEBSITE CHANNEL).include? type
end
Expand Down
2 changes: 1 addition & 1 deletion lib/yt/annotations/for.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def xml_to_annotations(xml)

def json_to_annotations(json)
annotations = json['elements']
annotations.map{|data| Annotations::EndScreen.new data}
annotations.map{|data| Annotations::EndScreen.new data['endscreenElementRenderer']}
end

def annotation_class(data)
Expand Down
2 changes: 1 addition & 1 deletion lib/yt/annotations/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Yt
module Annotations
VERSION = '1.4.0'
VERSION = '1.4.1'
end
end
13 changes: 11 additions & 2 deletions spec/yt/annotations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@
expect(annotations[1].ends_at).to be 35.005
expect(annotations[1].text).to eq 'PewDiePie'
expect(annotations[1].link).to be_a Hash
expect(annotations[1].link[:url]).to eq 'https://www.youtube.com/user/PewDiePie'
expect(annotations[1].link[:url]).to eq 'https://www.youtube.com/channel/UC-lHJZR3Gqxm24_Vd_AJ5Yw'
expect(annotations[1].link[:type]).to be :channel
expect(annotations[1].link[:new_window]).to be true

Expand Down Expand Up @@ -193,6 +193,15 @@
end
end

context 'given another video with endscreen' do
let(:video_id) { 'EuqmXkjhisE' }

it 'returns an end screen annotation with subscribe type' do
expect(annotations[1]).to be_a Yt::Annotations::EndScreen
expect(annotations[1].link[:type]).to be :subscribe
end
end

context 'given a video without cards and with a featured playlist' do
let(:video_id) { 'GFxm7khsS3g' }

Expand Down Expand Up @@ -242,7 +251,7 @@

# NOTE: Third-party video, read above.
context 'given a video an "associated website" link in a note' do
let(:video_id) { '-W37TDK6dBM' }
let(:video_id) { 'jeCSnH9mQFo' }

it 'also returns the featured video' do
expect(annotations[5]).to be_a Yt::Annotations::Note
Expand Down

0 comments on commit bad5ac9

Please sign in to comment.