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 =
"0.1.0"

Class Method Summary collapse

Class Method Details

.channel(channel_url, options = {}) ⇒ Object

Get channel (user).

Examples:

YoutubeDataApi.channel("https://www.youtube.com/user/CSPAN")

Parameters:

  • channel_url (String)

    "https://www.youtube.com/user/CSPAN" The url representing a specific youtube channel or user.

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

    [String] api_key

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

    [String] app_name

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

    [String] app_version

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

    [String] request_parts A comma separated string of response parts which you would like to request. Limit api usage by only requesting the parts you need.

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

    [String] page_token



57
58
59
60
61
62
63
64
# File 'lib/youtube_data_api.rb', line 57

def self.channel(channel_url, options = {})
  client, youtube_api = self.initialize_service(options)
  response = client.execute(
    :api_method => youtube_api.channels.list,
    :parameters => self.channel_request_params(channel_url, options)
  )
  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.

Examples:

YoutubeDataApi.channel_playlists("https://www.youtube.com/user/CSPAN")

Parameters:

  • channel_url (String)

    "https://www.youtube.com/user/CSPAN" The url representing a specific youtube channel or user.

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

    [String] api_key

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

    [String] app_name

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

    [String] app_version

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

    [String] request_parts A comma separated string of response parts which you would like to request. Limit api usage by only requesting the parts you need.

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

    [String] page_token



93
94
95
96
97
98
99
100
# File 'lib/youtube_data_api.rb', line 93

def self.channel_playlists(channel_url, options = {})
  client, youtube_api = self.initialize_service(options)
  response = client.execute(
    :api_method => youtube_api.playlists.list,
    :parameters => self.channel_playlists_request_params(channel_url, options)
  )
  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, options)
  {
    :part => options[:request_parts] || PLAYLIST_PARTS,
    :pageToken => options[: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, options)
  request_params = {
    :part => options[:request_parts] || CHANNEL_PARTS,
    :pageToken => options[: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(options)
  client = Google::APIClient.new(
    :key => options[:api_key] || ENV['YOUTUBE_DATA_API_KEY'] || "my-key-123",
    :application_name => options[:app_name] || "my-app",
    :application_version => options[:app_version] || "0.0.1"
  )
  client.authorization = nil
  youtube_api = client.discovered_api('youtube', 'v3')
  return client, youtube_api
end

.playlist(playlist_url, options = {}) ⇒ Object

Get playlist.

Parameters:

  • playlist_url (String)

    "https://www.youtube.com/playlist?list=PLf0o4wbW8SXqTSo6iJkolKCKJYBnpo9NZ" The url representing a specific youtube playlist.

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

    [String] api_key

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

    [String] app_name

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

    [String] app_version

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

    [String] request_parts A comma separated string of response parts which you would like to request. Limit api usage by only requesting the parts you need.

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

    [String] page_token



132
133
134
135
136
137
138
139
# File 'lib/youtube_data_api.rb', line 132

def self.playlist(playlist_url, options = {})
  client, youtube_api = self.initialize_service(options)
  response = client.execute(
    :api_method => youtube_api.playlists.list,
    :parameters => self.playlist_request_params(playlist_url, options)
  )
  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).

Parameters:

  • playlist_url (String)

    "https://www.youtube.com/playlist?list=PLf0o4wbW8SXqTSo6iJkolKCKJYBnpo9NZ" The url representing a specific youtube playlist.

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

    [String] api_key

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

    [String] app_name

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

    [String] app_version

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

    [String] request_parts A comma separated string of response parts which you would like to request. Limit api usage by only requesting the parts you need.

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

    [String] page_token



163
164
165
166
167
168
169
170
# File 'lib/youtube_data_api.rb', line 163

def self.playlist_items(playlist_url, options = {})
  client, youtube_api = self.initialize_service(options)
  response = client.execute(
    :api_method => youtube_api.playlist_items.list,
    :parameters => self.playlist_items_request_params(playlist_url, options)
  )
  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, options)
  {
    :part => options[:part] || PLAYLIST_ITEM_PARTS,
    :pageToken => options[: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, options)
  {
    :part => options[:part] || PLAYLIST_PARTS,
    :pageToken => options[: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.

Examples:

Parameters:

  • video_url (String)

    "https://www.youtube.com/watch?v=oBM7DIeMsP0" The url representing a specific youtube video.

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

    [String] api_key

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

    [String] app_name

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

    [String] app_version

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

    [String] request_parts A comma separated string of response parts which you would like to request. Limit api usage by only requesting the parts you need.

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

    [String] page_token



204
205
206
207
208
209
210
211
# File 'lib/youtube_data_api.rb', line 204

def self.video(video_url, options = {})
  client, youtube_api = self.initialize_service(options)
  response = client.execute(
    :api_method => youtube_api.videos.list,
    :parameters => self.video_request_params(video_url, options)
  )
  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, options)
  {
    :part => options[:part] || VIDEO_PARTS,
    :pageToken => options[:page_token],
    :id => self.video_id(video_url) #todo: obviate this call if video_id param is present...,
  }
end