Use '/youtubei/v1/browse' endpoint for playlists

This commit is contained in:
Samantaz Fox 2021-04-13 19:33:37 +02:00
parent 26a7e1b049
commit 344ccf3b03
No known key found for this signature in database
GPG key ID: F42821059186176E
2 changed files with 35 additions and 21 deletions

View file

@ -8,15 +8,20 @@ HARDCODED_CLIENT_VERS = "2.20210330.08.00"
#################################################################### ####################################################################
# request_youtube_api_browse(continuation) # request_youtube_api_browse(continuation)
# request_youtube_api_browse(browse_id, params)
# #
# Requests the youtubei/vi/browse endpoint with the required headers # Requests the youtubei/vi/browse endpoint with the required headers
# to get JSON in en-US (english US). # to get JSON in en-US (english US).
# #
# The requested data is a continuation token (ctoken). Depending on # The requested data can either be:
# this token's contents, the returned data can be comments, playlist
# videos, search results, channel community tab, ...
# #
def request_youtube_api_browse(continuation) # - 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 # JSON Request data, required by the API
data = { data = {
"context": { "context": {
@ -33,6 +38,29 @@ def request_youtube_api_browse(continuation)
return _youtube_api_post_json("/youtubei/v1/browse", data) return _youtube_api_post_json("/youtubei/v1/browse", data)
end end
def request_youtube_api_browse(browse_id : String, params : String)
# JSON Request data, required by the API
data = {
"browseId" => browse_id,
"context" => {
"client" => {
"hl" => "en",
"gl" => "US",
"clientName" => "WEB",
"clientVersion" => HARDCODED_CLIENT_VERS,
},
},
}
# 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
#################################################################### ####################################################################
# request_youtube_api_search(search_query, params, region) # request_youtube_api_search(search_query, params, region)
# #

View file

@ -361,16 +361,7 @@ def fetch_playlist(plid, locale)
plid = "UU#{plid.lchop("UC")}" plid = "UU#{plid.lchop("UC")}"
end end
response = YT_POOL.client &.get("/playlist?list=#{plid}&hl=en") initial_data = request_youtube_api_browse("VL" + plid, params: "")
if response.status_code != 200
if response.headers["location"]?.try &.includes? "/sorry/index"
raise InfoException.new("Could not extract playlist info. Instance is likely blocked.")
else
raise InfoException.new("Not a playlist.")
end
end
initial_data = extract_initial_data(response.body)
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
@ -453,15 +444,10 @@ def get_playlist_videos(db, playlist, offset, locale = nil, continuation = nil)
ctoken = produce_playlist_continuation(playlist.id, offset) ctoken = produce_playlist_continuation(playlist.id, offset)
initial_data = request_youtube_api_browse(ctoken) initial_data = request_youtube_api_browse(ctoken)
else else
response = YT_POOL.client &.get("/playlist?list=#{playlist.id}&gl=US&hl=en") initial_data = request_youtube_api_browse("VL" + playlist.id, params: "")
initial_data = extract_initial_data(response.body)
end end
if initial_data return extract_playlist_videos(initial_data)
return extract_playlist_videos(initial_data)
else
return [] of PlaylistVideo
end
end end
end end