Class: Yt::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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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