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) def fetch_channel_playlists(ucid, author, continuation, sort_by)
if continuation if continuation
response_json = request_youtube_api_browse(continuation) response_json = YoutubeAPI.browse(continuation)
continuationItems = response_json["onResponseReceivedActions"]? continuationItems = response_json["onResponseReceivedActions"]?
.try &.[0]["appendContinuationItemsAction"]["continuationItems"] .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, continuation = produce_channel_videos_continuation(ucid, page,
auto_generated: auto_generated, sort_by: sort_by, v2: true) auto_generated: auto_generated, sort_by: sort_by, v2: true)
return request_youtube_api_browse(continuation) return YoutubeAPI.browse(continuation)
end end
def get_60_videos(ucid, author, page, auto_generated, sort_by = "newest") def get_60_videos(ucid, author, page, auto_generated, sort_by = "newest")

View file

@ -2,17 +2,20 @@
# This file contains youtube API wrappers # This file contains youtube API wrappers
# #
# Hard-coded constants required by the API module YoutubeAPI
HARDCODED_API_KEY = "AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8" extend self
HARDCODED_CLIENT_VERS = "2.20210330.08.00"
#################################################################### # Hard-coded constants required by the API
# make_youtube_api_context(region) HARDCODED_API_KEY = "AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8"
# HARDCODED_CLIENT_VERS = "2.20210330.08.00"
# Return, as a Hash, the "context" data required to request the
# youtube API endpoints. ####################################################################
# # make_context(region)
def make_youtube_api_context(region : String | Nil) : Hash #
# Return, as a Hash, the "context" data required to request the
# youtube API endpoints.
#
private def make_context(region : String | Nil) : Hash
return { return {
"client" => { "client" => {
"hl" => "en", "hl" => "en",
@ -21,41 +24,43 @@ def make_youtube_api_context(region : String | Nil) : Hash
"clientVersion" => HARDCODED_CLIENT_VERS, "clientVersion" => HARDCODED_CLIENT_VERS,
}, },
} }
end end
#################################################################### ####################################################################
# request_youtube_api_browse(continuation) # browse(continuation)
# request_youtube_api_browse(browse_id, params, region) # 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 # Requests the youtubei/v1/browse endpoint with the required headers
# be easily parsed. # 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. #
# # A region can be provided, default is US.
# The requested data can either be: #
# # The requested data can either be:
# - A continuation token (ctoken). Depending on this token's #
# contents, the returned data can be comments, playlist videos, # - A continuation token (ctoken). Depending on this token's
# search results, channel community tab, ... # contents, the returned data can be comments, playlist videos,
# # search results, channel community tab, ...
# - A playlist ID (parameters MUST be an empty string) #
# # - A playlist ID (parameters MUST be an empty string)
def request_youtube_api_browse(continuation : String) #
def browse(continuation : String)
# JSON Request data, required by the API # JSON Request data, required by the API
data = { data = {
"context" => make_youtube_api_context("US"), "context" => self.make_context("US"),
"continuation" => continuation, "continuation" => continuation,
} }
return _youtube_api_post_json("/youtubei/v1/browse", data) return self._post_json("/youtubei/v1/browse", data)
end end
def request_youtube_api_browse(browse_id : String, params : String, region : String = "US") # :ditto:
def browse(browse_id : String, *, params : String, region : String = "US")
# JSON Request data, required by the API # JSON Request data, required by the API
data = { data = {
"browseId" => browse_id, "browseId" => browse_id,
"context" => make_youtube_api_context(region), "context" => self.make_context(region),
} }
# Append the additionnal parameters if those were provided # Append the additionnal parameters if those were provided
@ -64,41 +69,41 @@ def request_youtube_api_browse(browse_id : String, params : String, region : Str
data["params"] = params data["params"] = params
end end
return _youtube_api_post_json("/youtubei/v1/browse", data) return self._post_json("/youtubei/v1/browse", data)
end end
#################################################################### ####################################################################
# request_youtube_api_search(search_query, params, region) # search(search_query, params, region)
# #
# Requests the youtubei/v1/search endpoint with the required headers # Requests the youtubei/v1/search endpoint with the required headers
# and POST data in order to get a JSON reply. As the search results # 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 # vary depending on the region, a region code can be specified in
# order to get non-US results. # order to get non-US results.
# #
# The requested data is a search string, with some additional # The requested data is a search string, with some additional
# paramters, formatted as a base64 string. # paramters, formatted as a base64 string.
# #
def request_youtube_api_search(search_query : String, params : String, region = nil) def search(search_query : String, params : String, region = nil)
# JSON Request data, required by the API # JSON Request data, required by the API
data = { data = {
"query" => search_query, "query" => search_query,
"context" => make_youtube_api_context(region), "context" => self.make_context(region),
"params" => params, "params" => params,
} }
return _youtube_api_post_json("/youtubei/v1/search", data) return self._post_json("/youtubei/v1/search", data)
end end
#################################################################### ####################################################################
# _youtube_api_post_json(endpoint, data) # _post_json(endpoint, data)
# #
# Internal function that does the actual request to youtube servers # Internal function that does the actual request to youtube servers
# and handles errors. # and handles errors.
# #
# The requested data is an endpoint (URL without the domain part) # The requested data is an endpoint (URL without the domain part)
# and the data as a Hash object. # and the data as a Hash object.
# #
def _youtube_api_post_json(endpoint, data) def _post_json(endpoint, data) : Hash(String, JSON::Any)
# Send the POST request and parse result # Send the POST request and parse result
response = YT_POOL.client &.post( response = YT_POOL.client &.post(
"#{endpoint}?key=#{HARDCODED_API_KEY}", "#{endpoint}?key=#{HARDCODED_API_KEY}",
@ -118,4 +123,5 @@ def _youtube_api_post_json(endpoint, data)
end end
return initial_data return initial_data
end end
end # End of module

View file

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

View file

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

View file

@ -14,7 +14,7 @@ def fetch_trending(trending_type, region, locale)
params = "" params = ""
end 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) trending = extract_videos(initial_data)
return {trending, plid} return {trending, plid}