Class: Yt::Models::Annotation

Inherits:
Object
  • Object
show all
Defined in:
lib/yt/models/annotation.rb

Overview

Provides methods to access and analyze a single YouTube annotation.

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Annotation

Note:

There is no documented way to access annotations through API. There is an endpoint that returns an XML in an undocumented format, which is here parsed into a comprehensible set of attributes.

Instantiate an Annotation object from its YouTube XML representation.

Parameters:

  • xml_data (String)

    The YouTube XML representation of an annotation



12
13
14
# File 'lib/yt/models/annotation.rb', line 12

def initialize(options = {})
  @data = options[:data]
end

Instance Method Details

#above?(y) ⇒ Boolean

Checks whether the entire annotation box remains above y

Parameters:

  • y (Integer)

    Vertical position in the Youtube video (0 to 100)

Returns:

  • (Boolean)

    Whether the box remains above y



21
22
23
# File 'lib/yt/models/annotation.rb', line 21

def above?(y)
  top && top < y
end

#below?(y) ⇒ Boolean

Checks whether the entire annotation box remains below y

Parameters:

  • y (Integer)

    Vertical position in the Youtube video (0 to 100)

Returns:

  • (Boolean)

    Whether the box remains below y



30
31
32
# File 'lib/yt/models/annotation.rb', line 30

def below?(y)
  bottom && bottom > y
end

#has_invideo_programming?Boolean

Checks whether the annotation comes from InVideo Programming

Returns:

  • (Boolean)

    Whether the annotation comes from InVideo Programming



68
69
70
# File 'lib/yt/models/annotation.rb', line 68

def has_invideo_programming?
  type == 'promotion' || type == 'branding'
end

Checks whether there is a link to a playlist. A link to a video with the playlist in the URL also counts

Returns:

  • (Boolean)

    Whether there is a link to a playlist in the annotation



54
55
56
# File 'lib/yt/models/annotation.rb', line 54

def has_link_to_playlist?
  link_class == '2' || text.include?('&list=')
end

Checks whether the link opens in the same window.

Returns:

  • (Boolean)

    Whether the link opens in the same window



61
62
63
# File 'lib/yt/models/annotation.rb', line 61

def has_link_to_same_window?
  link_target == 'current'
end

Checks whether there is a link to subscribe. Should a branding watermark also counts, because it links to the channel?

Returns:

  • (Boolean)

    Whether there is a link to subscribe in the annotation



38
39
40
# File 'lib/yt/models/annotation.rb', line 38

def has_link_to_subscribe?(options = {}) # TODO: options for which videos
  link_class == '5'
end

Checks whether there is a link to a video. An Invideo featured video also counts

Returns:

  • (Boolean)

    Whether there is a link to a video in the annotation



46
47
48
# File 'lib/yt/models/annotation.rb', line 46

def has_link_to_video?(options = {}) # TODO: options for which videos
  link_class == '1' || type == 'promotion'
end

#starts_after?(seconds) ⇒ Boolean

Note:

This is broken for invideo programming, because they do not

have the timestamp in the region, but in the “data” field

Returns:

  • (Boolean)

    Whether the annotation starts after the number of seconds



75
76
77
# File 'lib/yt/models/annotation.rb', line 75

def starts_after?(seconds)
  timestamps.first > seconds if timestamps.any?
end

#starts_before?(seconds) ⇒ Boolean

Note:

This is broken for invideo programming, because they do not

have the timestamp in the region, but in the “data” field

Returns:

  • (Boolean)

    Whether the annotation starts before the number of seconds



82
83
84
# File 'lib/yt/models/annotation.rb', line 82

def starts_before?(seconds)
  timestamps.first < seconds if timestamps.any?
end