Parse response to JSON instead of using regex

This commit is contained in:
Svallinn 2021-03-26 03:52:28 +00:00
parent e248e7ebaf
commit c5ccefe6f7
No known key found for this signature in database
GPG key ID: 09FB527F34037CCA

View file

@ -358,16 +358,20 @@ end
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 = request_youtube_api_browse(continuation)
result = JSON.parse(response_json.match(/"continuationItems": (?<items>\[.*\]),/m).try &.["items"] || "{}") # result = JSON.parse(response_json.match(/"continuationItems": (?<items>\[.*\]),/m).try &.["items"] || "{}")
result = JSON.parse(response_json)
continuationItems = result["onResponseReceivedActions"]?
.try &.[0]["appendContinuationItemsAction"]["continuationItems"]
return [] of SearchItem, nil if result.size == 0 return [] of SearchItem, nil if !continuationItems
items = [] of SearchItem items = [] of SearchItem
result.as_a.select(&.as_h.has_key?("gridPlaylistRenderer")).each { |item| continuationItems.as_a.select(&.as_h.has_key?("gridPlaylistRenderer")).each { |item|
extract_item(item, author, ucid).try { |t| items << t } extract_item(item, author, ucid).try { |t| items << t }
} }
continuation = result.as_a.last["continuationItemRenderer"]?.try &.["continuationEndpoint"]["continuationCommand"]["token"].as_s continuation = continuationItems.as_a.last["continuationItemRenderer"]?
.try &.["continuationEndpoint"]["continuationCommand"]["token"].as_s
else else
url = "/channel/#{ucid}/playlists?flow=list&view=1" url = "/channel/#{ucid}/playlists?flow=list&view=1"