Class: Yt::Models::Video

Inherits:
Resource show all
Defined in:
lib/yt/models/video.rb

Overview

Provides methods to interact with YouTube videos.

Instance Attribute Summary collapse

Attributes inherited from Resource

#auth

Instance Method Summary collapse

Methods inherited from Resource

#kind, #update, #username

Methods included from Associations::HasReports

#has_report

Methods included from Associations::HasViewerPercentages

#has_viewer_percentages

Methods included from Associations::HasOne

#has_one

Methods included from Associations::HasMany

#has_many

Methods included from Associations::HasAuthentication

#has_authentication

Constructor Details

#initialize(options = {}) ⇒ Video

Override Resource’s new to set statistics and content details as well if the response includes them



99
100
101
102
103
104
105
106
107
# File 'lib/yt/models/video.rb', line 99

def initialize(options = {})
  super options
  if options[:statistics]
    @statistics_set = StatisticsSet.new data: options[:statistics]
  end
  if options[:content_details]
    @content_detail = ContentDetail.new data: options[:content_details]
  end
end

Instance Attribute Details

#annotationsYt::Collections::Annotations (readonly)

Returns the video’s annotations.

Returns:



41
# File 'lib/yt/models/video.rb', line 41

has_many :annotations

#content_detailYt::Models::ContentDetail (readonly)

Returns the video’s content details.

Returns:



22
# File 'lib/yt/models/video.rb', line 22

has_one :content_detail

#live_streaming_detailYt::Models::LiveStreamingDetail (readonly)

Returns live streaming detail.

Returns:



35
# File 'lib/yt/models/video.rb', line 35

has_one :live_streaming_detail

#ratingYt::Models::Rating (readonly)

Returns the video’s rating.

Returns:



31
# File 'lib/yt/models/video.rb', line 31

has_one :rating

#resumable_sessionsYt::Collections::ResumableSessions (readonly)

Returns the sessions used to upload thumbnails using the resumable upload protocol.

Returns:



95
# File 'lib/yt/models/video.rb', line 95

has_many :resumable_sessions

#statistics_setYt::Models::StatisticsSet (readonly)

Returns the statistics for the video.

Returns:



88
# File 'lib/yt/models/video.rb', line 88

has_one :statistics_set

Instance Method Details

#comments(options = {}) ⇒ Hash<Date, Float>

Returns the comments for a range of a days.

Parameters:

  • options (Hash) (defaults to: {})

    the range of days to get the comments for.

Options Hash (options):

  • :since (#to_date)

    The first day of the range. Also aliased as :from.

  • :until (#to_date)

    The last day of the range. Also aliased as :to.

Returns:

  • (Hash<Date, Float>)

    the comments for a range of a days.



50
# File 'lib/yt/models/video.rb', line 50

has_report :comments

#comments_on(date) ⇒ Float

Returns the comments for a single day.

Parameters:

  • date (#to_date)

    The single day to get the comments for.

Returns:

  • (Float)

    the comments for a single day.



50
# File 'lib/yt/models/video.rb', line 50

has_report :comments

#delete(options = {}) ⇒ Boolean

Deletes the video.

This method requires auth to return an authenticated instance of Account with permissions to delete the video.

Returns:

  • (Boolean)

    whether the video does not exist anymore.

Raises:



131
132
133
134
# File 'lib/yt/models/video.rb', line 131

def delete(options = {})
  do_delete {@id = nil}
  !exists?
end

#dislikeBoolean

Dislikes the video on behalf of the authenticated account.

This method requires auth to return an authenticated instance of Account.

Returns:

  • (Boolean)

    whether the account does not like the video.

Raises:



170
171
172
173
# File 'lib/yt/models/video.rb', line 170

def dislike
  rating.set :dislike
  !liked?
end

#dislikes(options = {}) ⇒ Hash<Date, Float>

Returns the dislikes for a range of a days.

Parameters:

  • options (Hash) (defaults to: {})

    the range of days to get the dislikes for.

Options Hash (options):

  • :since (#to_date)

    The first day of the range. Also aliased as :from.

  • :until (#to_date)

    The last day of the range. Also aliased as :to.

Returns:

  • (Hash<Date, Float>)

    the dislikes for a range of a days.



56
# File 'lib/yt/models/video.rb', line 56

has_report :dislikes

#dislikes_on(date) ⇒ Float

Returns the dislikes for a single day.

Parameters:

  • date (#to_date)

    The single day to get the dislikes for.

Returns:

  • (Float)

    the dislikes for a single day.



56
# File 'lib/yt/models/video.rb', line 56

has_report :dislikes

#earnings(options = {}) ⇒ Hash<Date, Float>

Returns the earnings for a range of a days.

Parameters:

  • options (Hash) (defaults to: {})

    the range of days to get the earnings for.

Options Hash (options):

  • :since (#to_date)

    The first day of the range. Also aliased as :from.

  • :until (#to_date)

    The last day of the range. Also aliased as :to.

Returns:

  • (Hash<Date, Float>)

    the earnings for a range of a days.



44
# File 'lib/yt/models/video.rb', line 44

has_report :earnings

#earnings_on(date) ⇒ Float

Returns the earnings for a single day.

Parameters:

  • date (#to_date)

    The single day to get the earnings for.

Returns:

  • (Float)

    the earnings for a single day.



44
# File 'lib/yt/models/video.rb', line 44

has_report :earnings

#exists?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/yt/models/video.rb', line 136

def exists?
  !@id.nil?
end

#favorites_added(options = {}) ⇒ Hash<Date, Float>

Returns the favorites_added for a range of a days.

Parameters:

  • options (Hash) (defaults to: {})

    the range of days to get the favorites_added for.

Options Hash (options):

  • :since (#to_date)

    The first day of the range. Also aliased as :from.

  • :until (#to_date)

    The last day of the range. Also aliased as :to.

Returns:

  • (Hash<Date, Float>)

    the favorites_added for a range of a days.



72
# File 'lib/yt/models/video.rb', line 72

has_report :favorites_added

#favorites_added_on(date) ⇒ Float

Returns the favorites_added for a single day.

Parameters:

  • date (#to_date)

    The single day to get the favorites_added for.

Returns:

  • (Float)

    the favorites_added for a single day.



72
# File 'lib/yt/models/video.rb', line 72

has_report :favorites_added

#favorites_removed(options = {}) ⇒ Hash<Date, Float>

Returns the favorites_removed for a range of a days.

Parameters:

  • options (Hash) (defaults to: {})

    the range of days to get the favorites_removed for.

Options Hash (options):

  • :since (#to_date)

    The first day of the range. Also aliased as :from.

  • :until (#to_date)

    The last day of the range. Also aliased as :to.

Returns:

  • (Hash<Date, Float>)

    the favorites_removed for a range of a days.



75
# File 'lib/yt/models/video.rb', line 75

has_report :favorites_removed

#favorites_removed_on(date) ⇒ Float

Returns the favorites_removed for a single day.

Parameters:

  • date (#to_date)

    The single day to get the favorites_removed for.

Returns:

  • (Float)

    the favorites_removed for a single day.



75
# File 'lib/yt/models/video.rb', line 75

has_report :favorites_removed

#impressions(options = {}) ⇒ Hash<Date, Float>

Returns the impressions for a range of a days.

Parameters:

  • options (Hash) (defaults to: {})

    the range of days to get the impressions for.

Options Hash (options):

  • :since (#to_date)

    The first day of the range. Also aliased as :from.

  • :until (#to_date)

    The last day of the range. Also aliased as :to.

Returns:

  • (Hash<Date, Float>)

    the impressions for a range of a days.



78
# File 'lib/yt/models/video.rb', line 78

has_report :impressions

#impressions_on(date) ⇒ Float

Returns the impressions for a single day.

Parameters:

  • date (#to_date)

    The single day to get the impressions for.

Returns:

  • (Float)

    the impressions for a single day.



78
# File 'lib/yt/models/video.rb', line 78

has_report :impressions

#likeBoolean

Likes the video on behalf of the authenticated account.

This method requires auth to return an authenticated instance of Account.

Returns:

  • (Boolean)

    whether the account likes the video.

Raises:



158
159
160
161
# File 'lib/yt/models/video.rb', line 158

def like
  rating.set :like
  liked?
end

#liked?Boolean

Returns whether the authenticated account likes the video.

This method requires auth to return an authenticated instance of Account.

Returns:

  • (Boolean)

    whether the account likes the video.

Raises:



147
148
149
# File 'lib/yt/models/video.rb', line 147

def liked?
  rating.rating == :like
end

#likes(options = {}) ⇒ Hash<Date, Float>

Returns the likes for a range of a days.

Parameters:

  • options (Hash) (defaults to: {})

    the range of days to get the likes for.

Options Hash (options):

  • :since (#to_date)

    The first day of the range. Also aliased as :from.

  • :until (#to_date)

    The last day of the range. Also aliased as :to.

Returns:

  • (Hash<Date, Float>)

    the likes for a range of a days.



53
# File 'lib/yt/models/video.rb', line 53

has_report :likes

#likes_on(date) ⇒ Float

Returns the likes for a single day.

Parameters:

  • date (#to_date)

    The single day to get the likes for.

Returns:

  • (Float)

    the likes for a single day.



53
# File 'lib/yt/models/video.rb', line 53

has_report :likes

#monetized_playbacks(options = {}) ⇒ Hash<Date, Float>

Returns the monetized_playbacks for a range of a days.

Parameters:

  • options (Hash) (defaults to: {})

    the range of days to get the monetized_playbacks for.

Options Hash (options):

  • :since (#to_date)

    The first day of the range. Also aliased as :from.

  • :until (#to_date)

    The last day of the range. Also aliased as :to.

Returns:

  • (Hash<Date, Float>)

    the monetized_playbacks for a range of a days.



81
# File 'lib/yt/models/video.rb', line 81

has_report :monetized_playbacks

#monetized_playbacks_on(date) ⇒ Float

Returns the monetized_playbacks for a single day.

Parameters:

  • date (#to_date)

    The single day to get the monetized_playbacks for.

Returns:

  • (Float)

    the monetized_playbacks for a single day.



81
# File 'lib/yt/models/video.rb', line 81

has_report :monetized_playbacks

#shares(options = {}) ⇒ Hash<Date, Float>

Returns the shares for a range of a days.

Parameters:

  • options (Hash) (defaults to: {})

    the range of days to get the shares for.

Options Hash (options):

  • :since (#to_date)

    The first day of the range. Also aliased as :from.

  • :until (#to_date)

    The last day of the range. Also aliased as :to.

Returns:

  • (Hash<Date, Float>)

    the shares for a range of a days.



59
# File 'lib/yt/models/video.rb', line 59

has_report :shares

#shares_on(date) ⇒ Float

Returns the shares for a single day.

Parameters:

  • date (#to_date)

    The single day to get the shares for.

Returns:

  • (Float)

    the shares for a single day.



59
# File 'lib/yt/models/video.rb', line 59

has_report :shares

#subscribers_gained(options = {}) ⇒ Hash<Date, Float>

Returns the subscribers_gained for a range of a days.

Parameters:

  • options (Hash) (defaults to: {})

    the range of days to get the subscribers_gained for.

Options Hash (options):

  • :since (#to_date)

    The first day of the range. Also aliased as :from.

  • :until (#to_date)

    The last day of the range. Also aliased as :to.

Returns:

  • (Hash<Date, Float>)

    the subscribers_gained for a range of a days.



64
# File 'lib/yt/models/video.rb', line 64

has_report :subscribers_gained

#subscribers_gained_on(date) ⇒ Float

Returns the subscribers_gained for a single day.

Parameters:

  • date (#to_date)

    The single day to get the subscribers_gained for.

Returns:

  • (Float)

    the subscribers_gained for a single day.



64
# File 'lib/yt/models/video.rb', line 64

has_report :subscribers_gained

#subscribers_lost(options = {}) ⇒ Hash<Date, Float>

Returns the subscribers_lost for a range of a days.

Parameters:

  • options (Hash) (defaults to: {})

    the range of days to get the subscribers_lost for.

Options Hash (options):

  • :since (#to_date)

    The first day of the range. Also aliased as :from.

  • :until (#to_date)

    The last day of the range. Also aliased as :to.

Returns:

  • (Hash<Date, Float>)

    the subscribers_lost for a range of a days.



69
# File 'lib/yt/models/video.rb', line 69

has_report :subscribers_lost

#subscribers_lost_on(date) ⇒ Float

Returns the subscribers_lost for a single day.

Parameters:

  • date (#to_date)

    The single day to get the subscribers_lost for.

Returns:

  • (Float)

    the subscribers_lost for a single day.



69
# File 'lib/yt/models/video.rb', line 69

has_report :subscribers_lost

#tagsArray<Yt::Models::Tag>

Returns the list of keyword tags associated with the video. Since YouTube API only returns tags on Videos#list, the memoized (e.g., by calling account.videos or channel.videos), so that the full snippet (with tags) is loaded, rather than the partial one.

Returns:

  • (Array<Yt::Models::Tag>)

    the list of keyword tags associated with the video.

See Also:



117
118
119
120
121
122
# File 'lib/yt/models/video.rb', line 117

def tags
  unless snippet.tags.any? || snippet.includes_tags || @auth.nil?
    @snippet = nil
  end
  snippet.tags
end

#unlikeBoolean

Resets the rating of the video on behalf of the authenticated account.

This method requires auth to return an authenticated instance of Account.

Returns:

  • (Boolean)

    whether the account does not like the video.

Raises:



182
183
184
185
# File 'lib/yt/models/video.rb', line 182

def unlike
  rating.set :none
  !liked?
end

#upload_thumbnail(path_or_url) ⇒ Object

Uploads a thumbnail

Parameters:

  • path_or_url (String)

    the image to upload. Can either be the path of a local file or the URL of a remote file.

Returns:

  • the new thumbnail resource for the given image.

See Also:



192
193
194
195
196
197
198
199
# File 'lib/yt/models/video.rb', line 192

def upload_thumbnail(path_or_url)
  file = open path_or_url, 'rb'
  session = resumable_sessions.insert file.size

  session.update(body: file) do |data|
    snippet.instance_variable_set :@thumbnails, data['items'].first
  end
end

#viewer_percentage(filters = {}) ⇒ Float

Returns the percentage of viewer matching the filters.

Examples:

Return the % of male viewers of a video

channel.viewer_percentage(gender: :male) #=> 52.02

Parameters:

  • filters (Hash) (defaults to: {})

    The type of viewers to be accounted for.

Options Hash (filters):

  • :gender (Symbol)

    Either :male or :female.

Returns:

  • (Float)

    the percentage of viewer matching the filters.



84
# File 'lib/yt/models/video.rb', line 84

has_viewer_percentages

#viewer_percentagesHash<Symbol,Hash<String,Float>>

Returns the viewer percentages. The first-level hash identifies the genres (:female, :male). The second-level hash identifies the age ranges (‘18-24’, ‘25-34’, ‘35-44’, ‘45-54’, ‘55-64’, ‘65-’).

Examples:

Return the % of male viewers of a channel older than 64

channel.viewer_percentages[:male]['65-'] #=> 12.02

Returns:

  • (Hash<Symbol,Hash<String,Float>>)

    the viewer percentages. The first-level hash identifies the genres (:female, :male). The second-level hash identifies the age ranges (‘18-24’, ‘25-34’, ‘35-44’, ‘45-54’, ‘55-64’, ‘65-’)



84
# File 'lib/yt/models/video.rb', line 84

has_viewer_percentages

#views(options = {}) ⇒ Hash<Date, Float>

Returns the views for a range of a days.

Parameters:

  • options (Hash) (defaults to: {})

    the range of days to get the views for.

Options Hash (options):

  • :since (#to_date)

    The first day of the range. Also aliased as :from.

  • :until (#to_date)

    The last day of the range. Also aliased as :to.

Returns:

  • (Hash<Date, Float>)

    the views for a range of a days.



47
# File 'lib/yt/models/video.rb', line 47

has_report :views

#views_on(date) ⇒ Float

Returns the views for a single day.

Parameters:

  • date (#to_date)

    The single day to get the views for.

Returns:

  • (Float)

    the views for a single day.



47
# File 'lib/yt/models/video.rb', line 47

has_report :views