class Yt::Models::Video
Provides methods to interact with YouTube videos. @see developers.google.com/youtube/v3/docs/videos
Public Class Methods
@private Override Resource's new to set statistics and content details as well if the response includes them
Yt::Models::Resource::new
# File lib/yt/models/video.rb, line 606 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 if options[:file_details] @file_detail = FileDetail.new data: options[:file_details] end if options[:live_streaming_details] @live_streaming_detail = LiveStreamingDetail.new data: options[:live_streaming_details] end if options[:video_category] @video_category = VideoCategory.new data: options[:video_category] end if options[:player] @player = Player.new data: options[:player] end if options[:claim] @claim = options[:claim] end end
Public Instance Methods
@return [Boolean] whether the video failed to upload to YouTube because
the uploading process was aborted.
# File lib/yt/models/video.rb, line 121 def aborted? status.failure_reason == 'uploadAborted' end
@return [Boolean] whether the video is identified by YouTube as
age-restricted content.
# File lib/yt/models/video.rb, line 276 def age_restricted? content_detail.youtube_rating == 'ytAgeRestricted' end
@return [Boolean] whether the video was rejected by YouTube because
the account that uploaded the video has been closed.
# File lib/yt/models/video.rb, line 171 def belongs_to_closed_account? status.rejection_reason == 'uploaderAccountClosed' end
@return [Boolean] whether the video was rejected by YouTube because
the account that uploaded the video has been suspended.
# File lib/yt/models/video.rb, line 177 def belongs_to_suspended_account? status.rejection_reason == 'uploaderAccountSuspended' end
@return [Boolean] whether captions are available for the video.
# File lib/yt/models/video.rb, line 264 def captioned? content_detail.caption == 'true' end
@return [String] ID of the YouTube category associated with the video.
# File lib/yt/models/video.rb, line 56 def category_id ensure_complete_snippet :category_id end
@return [String] the video category’s title.
# File lib/yt/models/video.rb, line 345 def category_title video_category.title end
@return [<String>] the URL
of the channel that the video belongs to.
# File lib/yt/models/video.rb, line 41 def channel_url "https://www.youtube.com/channel/#{channel_id}" end
@!attribute [r] claim
@return [Yt::Models::Claim, nil] the first claim on the video by the content owner of the video, if eagerly loaded.
# File lib/yt/models/video.rb, line 413 def claim @claim end
@return [Boolean] whether the video was rejected by YouTube because
the video was claimed by a different account.
# File lib/yt/models/video.rb, line 127 def claimed? status.rejection_reason == 'claim' end
Deletes the video on behalf of the authenticated account. @return [Boolean] whether the video does not exist anymore. @raise [Yt::Errors::Unauthorized] if {Resource#auth auth} is not an
authenticated Yt::Account with permissions to delete the video.
# File lib/yt/models/video.rb, line 557 def delete(options = {}) do_delete {@id = nil} !exists? end
@return [Boolean] whether the video was deleted by the user.
# File lib/yt/models/video.rb, line 63 def deleted? status.upload_status == 'deleted' end
Dislikes the video on behalf of the authenticated account. @return [Boolean] whether the account does not like the video. @raise [Yt::Errors::Unauthorized] if {Resource#auth auth} is not an
authenticated Yt::Account.
# File lib/yt/models/video.rb, line 326 def dislike rating.set :dislike !liked? end
@return [Boolean] whether the video was rejected by YouTube because
the video is a duplicate of another video.
# File lib/yt/models/video.rb, line 139 def duplicate? status.rejection_reason == 'duplicate' end
@return [Boolean] whether the video can be embedded on other websites.
# File lib/yt/models/video.rb, line 222 def embeddable? status.embeddable end
@return [Boolean] whether the video failed to upload to YouTube because
the video file is empty.
# File lib/yt/models/video.rb, line 102 def empty? status.failure_reason == 'emptyFile' end
@private
# File lib/yt/models/video.rb, line 632 def exists? !@id.nil? end
@return [Boolean] whether the video failed to upload.
# File lib/yt/models/video.rb, line 68 def failed? status.upload_status == 'failed' end
@return [Boolean] whether the video failed to upload to YouTube because
YouTube was unable to convert the video.
# File lib/yt/models/video.rb, line 96 def has_failed_conversion? status.failure_reason == 'conversion' end
Returns whether the video statistics are publicly viewable. @return [Boolean] if the resource is a video, whether the extended
video statistics on the video’s watch page are publicly viewable. By default, those statistics are viewable, and statistics like a video’s viewcount and ratings will still be publicly visible even if this property’s value is set to false.
# File lib/yt/models/video.rb, line 217 def has_public_stats_viewable? status.public_stats_viewable end
@return [Boolean] whether the video is available in high definition.
# File lib/yt/models/video.rb, line 259 def hd? content_detail.definition == 'hd' end
@return [Boolean] whether the video was rejected by YouTube because
the video contains inappropriate content.
# File lib/yt/models/video.rb, line 145 def inappropriate? status.rejection_reason == 'inappropriate' end
@return [Boolean] whether the video was rejected by YouTube because
the video commits a copyright infringement.
# File lib/yt/models/video.rb, line 133 def infringes_copyright? status.rejection_reason == 'copyright' end
@return [Boolean] whether the video was rejected by YouTube because
the video infringes a trademark.
@see support.google.com/youtube/answer/2801979?hl=en
# File lib/yt/models/video.rb, line 165 def infringes_trademark? status.rejection_reason == 'trademark' end
@return [Boolean] whether the video failed to upload to YouTube because
the video uses an unsupported file format.
@see support.google.com/youtube/troubleshooter/2888402?hl=en
# File lib/yt/models/video.rb, line 109 def invalid? status.failure_reason == 'invalidFile' end
@return [Boolean] whether the video represents licensed content, which
means that the content has been claimed by a YouTube content partner.
# File lib/yt/models/video.rb, line 270 def licensed? content_detail.licensed_content || false end
@return [Boolean] whether the video uses a Creative Commons license. @see support.google.com/youtube/answer/2797468?hl=en
# File lib/yt/models/video.rb, line 207 def licensed_as_creative_commons? license == 'creativeCommon' end
@return [Boolean] whether the video uses the Standard YouTube license. @see www.youtube.com/static?template=terms
# File lib/yt/models/video.rb, line 201 def licensed_as_standard_youtube? license == 'youtube' end
Likes the video on behalf of the authenticated account. @return [Boolean] whether the authenticated account likes the video. @raise [Yt::Errors::Unauthorized] if {Resource#auth auth} is not an
authenticated Yt::Account.
# File lib/yt/models/video.rb, line 317 def like rating.set :like liked? end
@return [Boolean] whether the authenticated account likes the video. @raise [Yt::Errors::Unauthorized] if {Resource#auth auth} is not an
authenticated Yt::Account.
# File lib/yt/models/video.rb, line 309 def liked? rating.rating == :like end
@return [Boolean, nil] This value indicates whether the video is
designated as child-directed, and it contains the current "made for kids" status of the video.
# File lib/yt/models/video.rb, line 229 def made_for_kids? status.made_for_kids end
@return [Boolean] whether the video has been fully processed by YouTube.
# File lib/yt/models/video.rb, line 73 def processed? status.upload_status == 'processed' end
@return [Boolean] whether the video was rejected by YouTube.
# File lib/yt/models/video.rb, line 78 def rejected? status.upload_status == 'rejected' end
@private Tells `has_reports` to retrieve the reports from YouTube Analytics API either as a Channel
or as a Content Owner. @see developers.google.com/youtube/analytics/channel_reports @see developers.google.com/youtube/analytics/content_owner_reports
# File lib/yt/models/video.rb, line 641 def reports_params {}.tap do |params| if auth.owner_name params[:ids] = "contentOwner==#{auth.owner_name}" else params[:ids] = "channel==#{channel_id}" end params[:filters] = "video==#{id}" end end
@return [Boolean] whether the video is scheduled to be published.
# File lib/yt/models/video.rb, line 190 def scheduled? private? && status.publish_at end
Returns the time when a video is scheduled to be published. @return [Time] if the video is scheduled to be published, the time
when it will become public.
@return [nil] if the video is not scheduled to be published.
# File lib/yt/models/video.rb, line 185 def scheduled_at status.publish_at if scheduled? end
@return [Boolean, nil] In a videos.insert or videos.update request,
this property allows the channel owner to designate the video as being child-directed. In a videos.list request, the property value is only returned if the channel owner authorized the API request.
# File lib/yt/models/video.rb, line 237 def self_declared_made_for_kids? status.self_declared_made_for_kids end
@return [Boolean] whether the video is available in 3D.
# File lib/yt/models/video.rb, line 254 def stereoscopic? content_detail.dimension == '3d' end
@return [Boolean] whether the video was rejected by YouTube because
the video exceeds the maximum duration for YouTube.
@see support.google.com/youtube/answer/71673?hl=en
# File lib/yt/models/video.rb, line 152 def too_long? status.rejection_reason == 'length' end
@return [Boolean] whether the video failed to upload to YouTube because
the video file is too small for YouTube.
# File lib/yt/models/video.rb, line 115 def too_small? status.failure_reason == 'tooSmall' end
Resets the rating of the video on behalf of the authenticated account. @return [Boolean] whether the account does not like the video. @raise [Yt::Errors::Unauthorized] if {Resource#auth auth} is not an
authenticated Yt::Account.
# File lib/yt/models/video.rb, line 335 def unlike rating.set :none !liked? end
Updates the attributes of a video. @return [Boolean] whether the video was successfully updated. @raise [Yt::Errors::Unauthorized] if {Resource#auth auth} is not an
authenticated Yt::Account with permissions to update the video.
@param [Hash] attributes the attributes to update. @option attributes [String] :title The new video’s title.
Cannot have more than 100 characters. Can include the characters < and >, which are replaced to ‹ › in order to be accepted by YouTube.
@option attributes [String] :description The new video’s description.
Cannot have more than 5000 bytes. Can include the characters < and >, which are replaced to ‹ › in order to be accepted by YouTube.
@option attributes [Array<String>] :tags The new video’s tags.
Cannot have more than 500 characters. Can include the characters < and >, which are replaced to ‹ › in order to be accepted by YouTube.
@option attributes [String] :category_id The new video’s category ID. @option attributes [String] :privacy_status The new video’s privacy
status. Must be one of: private, unscheduled, public.
@option attributes [Boolean] :embeddable The new value to specify
whether the video can be embedded on other websites.
@option attributes [String] :license The new video’s license. Must be
one of: youtube, creativeCommon.
@option attributes [Boolean] :public_stats_viewable The new value to
specify whether the video’s statistics are publicly viewable.
@option attributes :publish_at The new timestamp when the video will be
made public. Must be in ISO 8601 format (YYYY-MM-DDThh:mm:ss.sZ).
@example Update title and description of a video.
video.update title: 'New title', description: 'New description'
@example Update tags and category ID of a video.
video.update tags: ['new', 'tags'], category_id: '22'
@example Update status of a video.
video.update privacy_status: 'public', public_stats_viewable: true
@example Update visibility/license of a video.
video.update embeddable: true, license: :youtube
@example Update the time when a private video will become public.
video.update publish_at: 3.days.from_now.utc.iso8601(3)
Yt::Models::Resource#update
# File lib/yt/models/video.rb, line 597 def update(attributes = {}) super end
@private Tells `has_many :resumable_sessions` what type of file can be uploaded.
# File lib/yt/models/video.rb, line 670 def upload_content_type 'application/octet-stream' end
@private Tells `has_many :resumable_sessions` what params are set for the object associated to the uploaded file. developers.google.com/youtube/v3/docs/thumbnails/set
# File lib/yt/models/video.rb, line 662 def upload_params params = {video_id: id} params.merge! auth.upload_thumbnail_params params end
@private Tells `has_many :resumable_sessions` what path to hit to upload a file.
# File lib/yt/models/video.rb, line 654 def upload_path '/upload/youtube/v3/thumbnails/set' end
Uploads a thumbnail @param [String] path_or_url the image to upload. Can either be the
path of a local file or the URL of a remote file.
@return the new thumbnail resource for the given image. @raise [Yt::Errors::RequestError] if path_or_url is not a valid path
or URL.
# File lib/yt/models/video.rb, line 544 def upload_thumbnail(path_or_url) file = open(path_or_url, 'rb') rescue StringIO.new session = resumable_sessions.insert file.size session.update(body: file) do |data| snippet.instance_variable_set :@thumbnails, data['items'].first end end
@return [Boolean] whether the video is being uploaded to YouTube.
# File lib/yt/models/video.rb, line 83 def uploading? status.upload_status == 'uploaded' end
@return [Boolean] whether the video failed to upload to YouTube because
of an unsupported codec.
@see support.google.com/youtube/answer/1722171
# File lib/yt/models/video.rb, line 90 def uses_unsupported_codec? status.failure_reason == 'codec' end
@return [Boolean] whether the video was rejected by YouTube because
the video violates the Terms of Use.
# File lib/yt/models/video.rb, line 158 def violates_terms_of_use? status.rejection_reason == 'termsOfUse' end
Private Instance Methods
NOTE: Another irrational behavior of YouTube API. If you are setting a video to public/unlisted then you should not pass publishAt at any cost, otherwise the API will fail (since setting publishAt means you want the video to be private). Similarly, you should not pass any past publishAt (for the same reason).
# File lib/yt/models/video.rb, line 703 def build_update_body_part(name, part, attributes = {}) {}.tap do |body_part| part[:keys].map do |key| body_part[camelize key] = attributes.fetch key, public_send(name).public_send(key) end if (body_part[:publishAt] || 1.day.from_now) < Time.now || body_part[:privacyStatus].in?(['public', 'unlisted']) body_part.delete(:publishAt) end end end
For updating video with content owner auth. @see developers.google.com/youtube/v3/docs/videos/update
Yt::Models::Resource#update_params
# File lib/yt/models/video.rb, line 691 def update_params params = super params[:params] ||= {} params[:params].merge! auth.update_video_params params end
TODO: instead of having Video
, Playlist
etc override this method,
they should define *what* can be updated in their own *update* method.
@see developers.google.com/youtube/v3/docs/videos/update @todo: Add recording details keys
# File lib/yt/models/video.rb, line 681 def update_parts snippet_keys = [:title, :description, :tags, :category_id] snippet = {keys: snippet_keys, sanitize_brackets: true} status_keys = [:privacy_status, :embeddable, :license, :public_stats_viewable, :publish_at, :self_declared_made_for_kids] {snippet: snippet, status: {keys: status_keys}} end