Put youtube API functions under the YoutubeAPI namespace

This commit is contained in:
Samantaz Fox 2021-07-20 11:42:45 +02:00
parent 1ee4cae802
commit b17ee5d4f7
No known key found for this signature in database
GPG Key ID: F42821059186176E
6 changed files with 119 additions and 113 deletions

View File

@ -1,6 +1,6 @@
def fetch_channel_playlists(ucid, author, continuation, sort_by)
if continuation
response_json = request_youtube_api_browse(continuation)
response_json = YoutubeAPI.browse(continuation)
continuationItems = response_json["onResponseReceivedActions"]?
.try &.[0]["appendContinuationItemsAction"]["continuationItems"]

View File

@ -61,7 +61,7 @@ def get_channel_videos_response(ucid, page = 1, auto_generated = nil, sort_by =
continuation = produce_channel_videos_continuation(ucid, page,
auto_generated: auto_generated, sort_by: sort_by, v2: true)
return request_youtube_api_browse(continuation)
return YoutubeAPI.browse(continuation)
end
def get_60_videos(ucid, author, page, auto_generated, sort_by = "newest")

View File

@ -2,120 +2,126 @@
# This file contains youtube API wrappers
#
# Hard-coded constants required by the API
HARDCODED_API_KEY = "AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8"
HARDCODED_CLIENT_VERS = "2.20210330.08.00"
module YoutubeAPI
extend self
####################################################################
# 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
# Hard-coded constants required by the API
HARDCODED_API_KEY = "AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8"
HARDCODED_CLIENT_VERS = "2.20210330.08.00"
####################################################################
# request_youtube_api_browse(continuation)
# request_youtube_api_browse(browse_id, params, region)
#
# Requests the youtubei/v1/browse endpoint with the required headers
# and POST data in order to get a JSON reply in english that can
# be easily parsed.
#
# The region can be provided, default is US.
#
# The requested data can either be:
#
# - 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)
# JSON Request data, required by the API
data = {
"context" => make_youtube_api_context("US"),
"continuation" => continuation,
}
return _youtube_api_post_json("/youtubei/v1/browse", data)
end
def request_youtube_api_browse(browse_id : String, params : String, region : String = "US")
# JSON Request data, required by the API
data = {
"browseId" => browse_id,
"context" => make_youtube_api_context(region),
}
# Append the additionnal parameters if those were provided
# (this is required for channel info, playlist and community, e.g)
if params != ""
data["params"] = params
####################################################################
# make_context(region)
#
# Return, as a Hash, the "context" data required to request the
# youtube API endpoints.
#
private def make_context(region : String | Nil) : Hash
return {
"client" => {
"hl" => "en",
"gl" => region || "US", # Can't be empty!
"clientName" => "WEB",
"clientVersion" => HARDCODED_CLIENT_VERS,
},
}
end
return _youtube_api_post_json("/youtubei/v1/browse", data)
end
####################################################################
# browse(continuation)
# browse(browse_id, params)
# browse(browse_id, params, region)
#
# Requests the youtubei/v1/browse endpoint with the required headers
# and POST data in order to get a JSON reply in english that can
# be easily parsed.
#
# A region can be provided, default is US.
#
# The requested data can either be:
#
# - 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 browse(continuation : String)
# JSON Request data, required by the API
data = {
"context" => self.make_context("US"),
"continuation" => continuation,
}
####################################################################
# request_youtube_api_search(search_query, params, region)
#
# Requests the youtubei/v1/search endpoint with the required headers
# and POST data in order to get a JSON reply. As the search results
# vary depending on the region, a region code can be specified in
# order to get non-US results.
#
# 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 = {
"query" => search_query,
"context" => make_youtube_api_context(region),
"params" => params,
}
return self._post_json("/youtubei/v1/browse", data)
end
return _youtube_api_post_json("/youtubei/v1/search", data)
end
# :ditto:
def browse(browse_id : String, *, params : String, region : String = "US")
# JSON Request data, required by the API
data = {
"browseId" => browse_id,
"context" => self.make_context(region),
}
####################################################################
# _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)
# Send the POST request and parse result
response = YT_POOL.client &.post(
"#{endpoint}?key=#{HARDCODED_API_KEY}",
headers: HTTP::Headers{"content-type" => "application/json; charset=UTF-8"},
body: data.to_json
)
# 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
initial_data = JSON.parse(response.body).as_h
return self._post_json("/youtubei/v1/browse", data)
end
# Error handling
if initial_data.has_key?("error")
code = initial_data["error"]["code"]
message = initial_data["error"]["message"].to_s.sub(/(\\n)+\^$/, "")
####################################################################
# search(search_query, params, region)
#
# Requests the youtubei/v1/search endpoint with the required headers
# and POST data in order to get a JSON reply. As the search results
# vary depending on the region, a region code can be specified in
# order to get non-US results.
#
# The requested data is a search string, with some additional
# paramters, formatted as a base64 string.
#
def search(search_query : String, params : String, region = nil)
# JSON Request data, required by the API
data = {
"query" => search_query,
"context" => self.make_context(region),
"params" => params,
}
raise InfoException.new("Could not extract JSON. Youtube API returned \
return self._post_json("/youtubei/v1/search", data)
end
####################################################################
# _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 _post_json(endpoint, data) : Hash(String, JSON::Any)
# Send the POST request and parse result
response = YT_POOL.client &.post(
"#{endpoint}?key=#{HARDCODED_API_KEY}",
headers: HTTP::Headers{"content-type" => "application/json; charset=UTF-8"},
body: data.to_json
)
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
end
return initial_data
end
return initial_data
end
end # End of module

View File

@ -361,7 +361,7 @@ def fetch_playlist(plid, locale)
plid = "UU#{plid.lchop("UC")}"
end
initial_data = request_youtube_api_browse("VL" + plid, params: "")
initial_data = YoutubeAPI.browse("VL" + plid, params: "")
playlist_sidebar_renderer = initial_data["sidebar"]?.try &.["playlistSidebarRenderer"]?.try &.["items"]?
raise InfoException.new("Could not extract playlistSidebarRenderer.") if !playlist_sidebar_renderer
@ -442,9 +442,9 @@ def get_playlist_videos(db, playlist, offset, locale = nil, continuation = nil)
offset = (offset / 100).to_i64 * 100_i64
ctoken = produce_playlist_continuation(playlist.id, offset)
initial_data = request_youtube_api_browse(ctoken)
initial_data = YoutubeAPI.browse(ctoken)
else
initial_data = request_youtube_api_browse("VL" + playlist.id, params: "")
initial_data = YoutubeAPI.browse("VL" + playlist.id, params: "")
end
return extract_playlist_videos(initial_data)

View File

@ -244,7 +244,7 @@ def channel_search(query, page, channel)
end
continuation = produce_channel_search_continuation(ucid, query, page)
response_json = request_youtube_api_browse(continuation)
response_json = YoutubeAPI.browse(continuation)
continuationItems = response_json["onResponseReceivedActions"]?
.try &.[0]["appendContinuationItemsAction"]["continuationItems"]
@ -263,7 +263,7 @@ end
def search(query, search_params = produce_search_params(content_type: "all"), region = nil)
return 0, [] of SearchItem if query.empty?
initial_data = request_youtube_api_search(query, search_params, region)
initial_data = YoutubeAPI.search(query, search_params, region)
items = extract_items(initial_data)
return items.size, items

View File

@ -14,7 +14,7 @@ def fetch_trending(trending_type, region, locale)
params = ""
end
initial_data = request_youtube_api_browse("FEtrending", params: params, region: region)
initial_data = YoutubeAPI.browse("FEtrending", params: params, region: region)
trending = extract_videos(initial_data)
return {trending, plid}