2021-03-20 20:57:18 +00:00
|
|
|
#
|
|
|
|
# This file contains youtube API wrappers
|
|
|
|
#
|
|
|
|
|
|
|
|
# Hard-coded constants required by the API
|
|
|
|
HARDCODED_API_KEY = "AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8"
|
2021-04-01 02:36:43 +00:00
|
|
|
HARDCODED_CLIENT_VERS = "2.20210330.08.00"
|
2021-03-20 20:57:18 +00:00
|
|
|
|
2021-05-24 11:33:46 +00:00
|
|
|
####################################################################
|
|
|
|
# make_youtube_api_context(region)
|
|
|
|
#
|
|
|
|
# Return, as a Hash, the "context" data required to request the
|
|
|
|
# youtube API endpoints.
|
|
|
|
#
|
|
|
|
def make_youtube_api_context(region : String | Nil) : Hash
|
|
|
|
return {
|
|
|
|
"client" => {
|
|
|
|
"hl" => "en",
|
|
|
|
"gl" => region || "US", # Can't be empty!
|
|
|
|
"clientName" => "WEB",
|
|
|
|
"clientVersion" => HARDCODED_CLIENT_VERS,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-04-01 02:36:43 +00:00
|
|
|
####################################################################
|
|
|
|
# request_youtube_api_browse(continuation)
|
2021-04-13 17:33:37 +00:00
|
|
|
# request_youtube_api_browse(browse_id, params)
|
2021-04-01 02:36:43 +00:00
|
|
|
#
|
|
|
|
# Requests the youtubei/vi/browse endpoint with the required headers
|
|
|
|
# to get JSON in en-US (english US).
|
|
|
|
#
|
2021-04-13 17:33:37 +00:00
|
|
|
# The requested data can either be:
|
2021-04-01 02:36:43 +00:00
|
|
|
#
|
2021-04-13 17:33:37 +00:00
|
|
|
# - A continuation token (ctoken). Depending on this token's
|
|
|
|
# contents, the returned data can be comments, playlist videos,
|
|
|
|
# search results, channel community tab, ...
|
|
|
|
#
|
|
|
|
# - A playlist ID (parameters MUST be an empty string)
|
|
|
|
#
|
|
|
|
def request_youtube_api_browse(continuation : String)
|
2021-03-20 20:57:18 +00:00
|
|
|
# JSON Request data, required by the API
|
|
|
|
data = {
|
2021-05-24 11:33:46 +00:00
|
|
|
"context" => make_youtube_api_context("US"),
|
|
|
|
"continuation" => continuation,
|
2021-03-20 20:57:18 +00:00
|
|
|
}
|
|
|
|
|
2021-04-07 01:15:02 +00:00
|
|
|
return _youtube_api_post_json("/youtubei/v1/browse", data)
|
|
|
|
end
|
|
|
|
|
2021-04-13 17:33:37 +00:00
|
|
|
def request_youtube_api_browse(browse_id : String, params : String)
|
|
|
|
# JSON Request data, required by the API
|
|
|
|
data = {
|
|
|
|
"browseId" => browse_id,
|
2021-05-24 11:33:46 +00:00
|
|
|
"context" => make_youtube_api_context("US"),
|
2021-04-13 17:33:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Append the additionnal parameters if those were provided
|
|
|
|
# (this is required for channel info, playlist and community, e.g)
|
|
|
|
if params != ""
|
|
|
|
data["params"] = params
|
|
|
|
end
|
|
|
|
|
|
|
|
return _youtube_api_post_json("/youtubei/v1/browse", data)
|
|
|
|
end
|
|
|
|
|
2021-04-07 01:15:02 +00:00
|
|
|
####################################################################
|
|
|
|
# request_youtube_api_search(search_query, params, region)
|
|
|
|
#
|
|
|
|
# Requests the youtubei/vi/search endpoint with the required headers
|
|
|
|
# to get JSON in en-US (english US).
|
|
|
|
#
|
|
|
|
# The requested data is a search string, with some additional
|
|
|
|
# paramters, formatted as a base64 string.
|
|
|
|
#
|
|
|
|
def request_youtube_api_search(search_query : String, params : String, region = nil)
|
|
|
|
# JSON Request data, required by the API
|
|
|
|
data = {
|
2021-05-24 11:33:46 +00:00
|
|
|
"query" => URI.encode_www_form(search_query),
|
|
|
|
"context" => make_youtube_api_context(region),
|
|
|
|
"params" => params,
|
2021-04-07 01:15:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return _youtube_api_post_json("/youtubei/v1/search", data)
|
|
|
|
end
|
|
|
|
|
|
|
|
####################################################################
|
|
|
|
# _youtube_api_post_json(endpoint, data)
|
|
|
|
#
|
|
|
|
# Internal function that does the actual request to youtube servers
|
|
|
|
# and handles errors.
|
|
|
|
#
|
|
|
|
# The requested data is an endpoint (URL without the domain part)
|
|
|
|
# and the data as a Hash object.
|
|
|
|
#
|
|
|
|
def _youtube_api_post_json(endpoint, data)
|
2021-04-01 02:36:43 +00:00
|
|
|
# Send the POST request and parse result
|
2021-03-20 20:57:18 +00:00
|
|
|
response = YT_POOL.client &.post(
|
2021-04-07 01:15:02 +00:00
|
|
|
"#{endpoint}?key=#{HARDCODED_API_KEY}",
|
2021-04-01 02:36:43 +00:00
|
|
|
headers: HTTP::Headers{"content-type" => "application/json; charset=UTF-8"},
|
2021-03-20 20:57:18 +00:00
|
|
|
body: data.to_json
|
|
|
|
)
|
|
|
|
|
2021-04-01 02:36:43 +00:00
|
|
|
initial_data = JSON.parse(response.body).as_h
|
|
|
|
|
|
|
|
# Error handling
|
|
|
|
if initial_data.has_key?("error")
|
|
|
|
code = initial_data["error"]["code"]
|
|
|
|
message = initial_data["error"]["message"].to_s.sub(/(\\n)+\^$/, "")
|
|
|
|
|
|
|
|
raise InfoException.new("Could not extract JSON. Youtube API returned \
|
|
|
|
error #{code} with message:<br>\"#{message}\"")
|
|
|
|
end
|
|
|
|
|
|
|
|
return initial_data
|
2021-03-20 20:57:18 +00:00
|
|
|
end
|