Module: YoutubeDataApi
- Defined in:
- lib/youtube_data_api.rb,
lib/youtube_data_api/version.rb
Overview
todo: refactor!
Defined Under Namespace
Classes: ChannelUrlError
Constant Summary collapse
- CHANNEL_URL_PREFIX =
CHANNELS
"https://www.youtube.com/channel/"
- USER_URL_PREFIX =
"https://www.youtube.com/user/"
- CHANNEL_PARTS =
"id, contentDetails, contentOwnerDetails, snippet, statistics"
- PLAYLIST_PARTS =
"id, contentDetails, player, snippet, status"
- PLAYLIST_URL_PREFIX =
PLAYLISTS
"https://www.youtube.com/playlist?list="
- PLAYLIST_ITEM_PARTS =
"id, contentDetails, snippet, status"
- VIDEO_URL_PREFIX =
VIDEOS
"https://www.youtube.com/watch?v="
- VIDEO_PARTS =
"id, contentDetails, liveStreamingDetails, player, recordingDetails, snippet, statistics, status, topicDetails"
- VERSION =
"3.0.0"
Class Method Summary collapse
-
.channel(channel_url, options = {}) ⇒ Object
Get channel (user).
- .channel_id(channel_url) ⇒ Object
-
.channel_playlists(channel_url, options = {}) ⇒ Object
List channel playlists.
- .channel_playlists_request_params(channel_url, options) ⇒ Object
- .channel_request_params(channel_url, options) ⇒ Object
- .initialize_service(options) ⇒ Object
-
.playlist(playlist_url, options = {}) ⇒ Object
Get playlist.
- .playlist_id(playlist_url) ⇒ Object
-
.playlist_items(playlist_url, options = {}) ⇒ Object
List playlist items (videos).
- .playlist_items_request_params(playlist_url, options) ⇒ Object
- .playlist_request_params(playlist_url, options) ⇒ Object
-
.video(video_url, options = {}) ⇒ Object
Get video.
-
.video_id(video_url) ⇒ Object
VIDEO_PRIVATE_PARTS = “fileDetails, processingDetails, suggestions” # :-) can’t request these unless authenticated.
- .video_request_params(video_url, options) ⇒ Object
Class Method Details
.channel(channel_url, options = {}) ⇒ Object
Get channel (user).
57 58 59 60 61 62 63 64 |
# File 'lib/youtube_data_api.rb', line 57 def self.channel(channel_url, = {}) client, youtube_api = self.initialize_service() response = client.execute( :api_method => youtube_api.channels.list, :parameters => self.channel_request_params(channel_url, ) ) result = JSON.parse(response.data.to_json) end |
.channel_id(channel_url) ⇒ Object
68 69 70 71 |
# File 'lib/youtube_data_api.rb', line 68 def self.channel_id(channel_url) channel_response = self.channel(channel_url) channel_response["items"].first["id"] end |
.channel_playlists(channel_url, options = {}) ⇒ Object
List channel playlists.
93 94 95 96 97 98 99 100 |
# File 'lib/youtube_data_api.rb', line 93 def self.channel_playlists(channel_url, = {}) client, youtube_api = self.initialize_service() response = client.execute( :api_method => youtube_api.playlists.list, :parameters => self.channel_playlists_request_params(channel_url, ) ) result = JSON.parse(response.data.to_json) end |
.channel_playlists_request_params(channel_url, options) ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/youtube_data_api.rb', line 73 def self.channel_playlists_request_params(channel_url, ) { :part => [:request_parts] || PLAYLIST_PARTS, :pageToken => [:page_token], :channelId => self.channel_id(channel_url) #todo: obviate this call if channel_id param is present... } end |
.channel_request_params(channel_url, options) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/youtube_data_api.rb', line 28 def self.channel_request_params(channel_url, ) request_params = { :part => [:request_parts] || CHANNEL_PARTS, :pageToken => [:page_token] } if channel_url.include?(CHANNEL_URL_PREFIX) request_params.merge!({:id => channel_url.gsub(CHANNEL_URL_PREFIX,'')}) elsif channel_url.include?(USER_URL_PREFIX) request_params.merge!({:forUsername => channel_url.gsub(USER_URL_PREFIX,'')}) else raise ChannelUrlError.new("could not recognize the channel_url #{channel_url}. try a url that contains either #{CHANNEL_URL_PREFIX} or #{USER_URL_PREFIX} ...") end return request_params end |
.initialize_service(options) ⇒ Object
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/youtube_data_api.rb', line 9 def self.initialize_service() client = Google::APIClient.new( :key => [:api_key] || ENV['YOUTUBE_DATA_API_KEY'] || "my-key-123", :application_name => [:app_name] || "my-app", :application_version => [:app_version] || "0.0.1" ) client. = nil youtube_api = client.discovered_api('youtube', 'v3') return client, youtube_api end |
.playlist(playlist_url, options = {}) ⇒ Object
Get playlist.
132 133 134 135 136 137 138 139 |
# File 'lib/youtube_data_api.rb', line 132 def self.playlist(playlist_url, = {}) client, youtube_api = self.initialize_service() response = client.execute( :api_method => youtube_api.playlists.list, :parameters => self.playlist_request_params(playlist_url, ) ) result = JSON.parse(response.data.to_json) end |
.playlist_id(playlist_url) ⇒ Object
108 109 110 |
# File 'lib/youtube_data_api.rb', line 108 def self.playlist_id(playlist_url) playlist_url.gsub(PLAYLIST_URL_PREFIX,'') end |
.playlist_items(playlist_url, options = {}) ⇒ Object
List playlist items (videos).
163 164 165 166 167 168 169 170 |
# File 'lib/youtube_data_api.rb', line 163 def self.playlist_items(playlist_url, = {}) client, youtube_api = self.initialize_service() response = client.execute( :api_method => youtube_api.playlist_items.list, :parameters => self.playlist_items_request_params(playlist_url, ) ) result = JSON.parse(response.data.to_json) end |
.playlist_items_request_params(playlist_url, options) ⇒ Object
143 144 145 146 147 148 149 |
# File 'lib/youtube_data_api.rb', line 143 def self.playlist_items_request_params(playlist_url, ) { :part => [:part] || PLAYLIST_ITEM_PARTS, :pageToken => [:page_token], :playlistId => self.playlist_id(playlist_url) #todo: obviate this call if playlist_id param is present..., } end |
.playlist_request_params(playlist_url, options) ⇒ Object
112 113 114 115 116 117 118 |
# File 'lib/youtube_data_api.rb', line 112 def self.playlist_request_params(playlist_url, ) { :part => [:part] || PLAYLIST_PARTS, :pageToken => [:page_token], :id => self.playlist_id(playlist_url) #todo: obviate this call if playlist_id param is present..., } end |
.video(video_url, options = {}) ⇒ Object
Get video.
204 205 206 207 208 209 210 211 |
# File 'lib/youtube_data_api.rb', line 204 def self.video(video_url, = {}) client, youtube_api = self.initialize_service() response = client.execute( :api_method => youtube_api.videos.list, :parameters => self.video_request_params(video_url, ) ) result = JSON.parse(response.data.to_json) end |
.video_id(video_url) ⇒ Object
VIDEO_PRIVATE_PARTS = “fileDetails, processingDetails, suggestions” # :-) can’t request these unless authenticated
180 181 182 |
# File 'lib/youtube_data_api.rb', line 180 def self.video_id(video_url) video_url.gsub(VIDEO_URL_PREFIX,'') end |
.video_request_params(video_url, options) ⇒ Object
184 185 186 187 188 189 190 |
# File 'lib/youtube_data_api.rb', line 184 def self.video_request_params(video_url, ) { :part => [:part] || VIDEO_PARTS, :pageToken => [:page_token], :id => self.video_id(video_url) #todo: obviate this call if video_id param is present..., } end |