class YoutubeVideo::YtApi
Service for all Youtube API calls
Constants
- API_VER
- TIME_TAG_PATTERN
- YT_API_URL
- YT_COMPANY
- YT_COMPANY_URL
- YT_URL
Public Class Methods
api_key()
click to toggle source
# File lib/YPBT/youtube_api.rb, line 14 def self.api_key return @api_key if @api_key @api_key = ENV['YOUTUBE_API_KEY'] end
channel_info(channel_id)
click to toggle source
# File lib/YPBT/youtube_api.rb, line 79 def self.channel_info(channel_id) fields = 'items(id,snippet(title,description,thumbnails(default(url))))' channel_response = HTTP.get(yt_resource_url('channels'), params: { id: channel_id, key: api_key, part: 'snippet', fields: fields }) channel_data = JSON.parse(channel_response.to_s)['items'].first if channel_data { 'title' => channel_data['snippet']['title'], 'description' => channel_data['snippet']['description'], 'image_url' => channel_data['snippet']['thumbnails']['default']['url'] } end end
comment_info(comment_id)
click to toggle source
# File lib/YPBT/youtube_api.rb, line 54 def self.comment_info(comment_id) comment_response = HTTP.get(yt_resource_url('comments'), params: { id: comment_id, key: api_key, part: 'snippet' }) item = JSON.parse(comment_response.to_s)['items'].first comment = item['snippet'] comment['id'] = comment_id comment end
config=(credentials)
click to toggle source
# File lib/YPBT/youtube_api.rb, line 19 def self.config=(credentials) @config ? @config.update(credentials) : @config = credentials end
extract_comment(comment_threads)
click to toggle source
# File lib/YPBT/youtube_api.rb, line 96 def self.extract_comment(comment_threads) comments = comment_threads['items'].map do |item| comment = item['snippet']['topLevelComment']['snippet'] comment['id'] = item['id'] comment end comments end
popular_videos_info(max_results = 25)
click to toggle source
# File lib/YPBT/youtube_api.rb, line 38 def self.popular_videos_info(max_results = 25) field = 'items(id,'\ 'snippet(thumbnails(medium),channelId,description,'\ 'publishedAt,title,categoryId),'\ 'statistics(likeCount,dislikeCount,viewCount),'\ 'contentDetails(duration))' video_response = HTTP.get(yt_resource_url('videos'), params: { chart: 'mostpopular', key: api_key, maxResults: max_results, part: 'snippet,statistics,'\ 'contentDetails', fields: field }) JSON.parse(video_response.to_s)['items'] end
time_tag?(comment)
click to toggle source
# File lib/YPBT/youtube_api.rb, line 118 def self.time_tag?(comment) !(comment['textDisplay'] =~ TIME_TAG_PATTERN).nil? end
video_comments_info(video_id, page_token = '', max_results = 100)
click to toggle source
# File lib/YPBT/youtube_api.rb, line 65 def self.video_comments_info(video_id, page_token = '', max_results = 100) comment_threads_response = HTTP.get(yt_resource_url('commentThreads'), params: { videoId: video_id, key: api_key, order: 'relevance', part: 'snippet', maxResults: max_results, pageToken: page_token }) comment_threads = JSON.parse(comment_threads_response.to_s) comments = extract_comment(comment_threads) next_page_token = comment_threads['nextPageToken'] [next_page_token, comments] end
video_info(video_id)
click to toggle source
# File lib/YPBT/youtube_api.rb, line 23 def self.video_info(video_id) field = 'items(id,'\ 'snippet(thumbnails(medium),channelId,description,'\ 'publishedAt,title,categoryId),'\ 'statistics(likeCount,dislikeCount,viewCount),'\ 'contentDetails(duration))' video_response = HTTP.get(yt_resource_url('videos'), params: { id: video_id, key: api_key, part: 'snippet,statistics,'\ 'contentDetails', fields: field }) JSON.parse(video_response.to_s)['items']&.first end
Private Class Methods
yt_resource_url(resouce_name)
click to toggle source
# File lib/YPBT/youtube_api.rb, line 123 def self.yt_resource_url(resouce_name) URI.join(YT_API_URL, resouce_name.to_s) end