class Yt::Video
Provides methods to interact with YouTube videos. @see developers.google.com/youtube/v3/docs/videos
Constants
- CATEGORIES
@return [Hash<Integer, String>] the list of YouTube video categories.
Public Instance Methods
@return [String] the canonical form of the video’s URL.
# File lib/yt/video.rb, line 144 def canonical_url "https://www.youtube.com/watch?v=#{id}" end
@return [String] the title of the associated YouTube video category.
# File lib/yt/video.rb, line 163 def category_title CATEGORIES[category_id] end
@return [Yt::Channel] the channel the video belongs to.
# File lib/yt/video.rb, line 179 def channel @channel ||= Channel.new id: channel_id end
@return [<String>] the length of the video as an ISO 8601 time, HH:MM:SS.
# File lib/yt/video.rb, line 173 def length hh, mm, ss = seconds / 3600, seconds / 60 % 60, seconds % 60 [hh, mm, ss].map{|t| t.to_s.rjust(2,'0')}.join(':') end
@return [<Integer>] the length of the video in seconds.
# File lib/yt/video.rb, line 168 def seconds to_seconds duration end
@return [Yt::Relation<Yt::CommentThread>] the threads of the video.
# File lib/yt/video.rb, line 184 def threads @threads ||= Relation.new(CommentThread, video_id: id) do |options| get '/youtube/v3/commentThreads', video_threads_params(options) end end
Returns the URL of the video’s thumbnail. @param [Symbol, String] size The size of the video’s thumbnail. @return [String] if size
is :default
, the URL of a 120x90px image. @return [String] if size
is :medium
, the URL of a 320x180px image. @return [String] if size
is :high
, the URL of a 480x360px image. @return [String] if size
is :standard
, the URL of a 640x480px image. @return [String] if size
is :maxres
, the URL of a 1280x720px image. @return [nil] if the size
is none of the above.
# File lib/yt/video.rb, line 139 def thumbnail_url(size = :default) thumbnails.fetch(size.to_s, {})['url'] end
Private Instance Methods
@return [Integer] the duration of the resource as reported by YouTube. @see developers.google.com/youtube/v3/docs/videos
According to YouTube documentation, the value is an ISO 8601 duration in the format PT#M#S, in which the letters PT indicate that the value specifies a period of time, and the letters M and S refer to length in minutes and seconds, respectively. The # characters preceding the M and S letters are both integers that specify the number of minutes (or seconds) of the video. For example, a value of PT15M51S indicates that the video is 15 minutes and 51 seconds long.
The ISO 8601 duration standard, though, is not always
respected by the results returned by YouTube API. For instance: video 2XwmldWC_Ls reports a duration of 'P1W2DT6H21M32S', which is to be interpreted as 1 week, 2 days, 6 hours, 21 minutes, 32 seconds. Mixing weeks with other time units is not strictly part of ISO 8601; in this context, weeks will be interpreted as “the duration of 7 days”. Similarly, a day will be interpreted as “the duration of 24 hours”. Video
tPEE9ZwTmy0 reports a duration of 'PT2S'. Skipping time units such as minutes is not part of the standard either; in this context, it will be interpreted as “0 minutes and 2 seconds”.
# File lib/yt/video.rb, line 213 def to_seconds(iso8601_duration) match = iso8601_duration.match %r{^P(?:|(?<weeks>\d*?)W)(?:|(?<days>\d*?)D)(?:|T(?:|(?<hours>\d*?)H)(?:|(?<min>\d*?)M)(?:|(?<sec>\d*?)S))$} weeks = (match[:weeks] || '0').to_i days = (match[:days] || '0').to_i hours = (match[:hours] || '0').to_i minutes = (match[:min] || '0').to_i seconds = (match[:sec]).to_i (((((weeks * 7) + days) * 24 + hours) * 60) + minutes) * 60 + seconds end