Parse responses to JSON instead of using regex

This commit is contained in:
Svallinn 2021-03-26 04:16:50 +00:00
parent d652ab9920
commit acfa9e8a55
No known key found for this signature in database
GPG Key ID: 09FB527F34037CCA
1 changed files with 9 additions and 4 deletions

View File

@ -236,7 +236,9 @@ def channel_search(query, page, channel)
if response.status_code == 404 if response.status_code == 404
response = YT_POOL.client &.get("/user/#{channel}") response = YT_POOL.client &.get("/user/#{channel}")
response = YT_POOL.client &.get("/c/#{channel}") if response.status_code == 404 response = YT_POOL.client &.get("/c/#{channel}") if response.status_code == 404
ucid = response.body.match(/HeaderRenderer":\{"channelId":"(?<ucid>[^\\"]+)"/).try &.["ucid"]? initial_data = extract_initial_data(response.body)
ucid = initial_data["header"]["c4TabbedHeaderRenderer"]?.try &.["channelId"].as_s?
raise InfoException.new("Impossible to extract channel ID from page") if !ucid
else else
ucid = channel ucid = channel
end end
@ -244,11 +246,14 @@ def channel_search(query, page, channel)
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 = request_youtube_api_browse(continuation)
result = JSON.parse(response_json.match(/"continuationItems": (?<items>\[.*\]),/m).try &.["items"] || "{}") result = JSON.parse(response_json)
return 0, [] of SearchItem if result.size == 0 continuationItems = result["onResponseReceivedActions"]?
.try &.[0]["appendContinuationItemsAction"]["continuationItems"]
return 0, [] of SearchItem if !continuationItems
items = [] of SearchItem items = [] of SearchItem
result.as_a.select(&.as_h.has_key?("itemSectionRenderer")).each { |item| continuationItems.as_a.select(&.as_h.has_key?("itemSectionRenderer")).each { |item|
extract_item(item["itemSectionRenderer"]["contents"].as_a[0]) extract_item(item["itemSectionRenderer"]["contents"].as_a[0])
.try { |t| items << t } .try { |t| items << t }
} }