From acfa9e8a5525bea7f87429aa51e84cacd2ad641d Mon Sep 17 00:00:00 2001 From: Svallinn <41585298+Svallinn@users.noreply.github.com> Date: Fri, 26 Mar 2021 04:16:50 +0000 Subject: [PATCH] Parse responses to JSON instead of using regex --- src/invidious/search.cr | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/invidious/search.cr b/src/invidious/search.cr index 26161fab..4b216613 100644 --- a/src/invidious/search.cr +++ b/src/invidious/search.cr @@ -236,7 +236,9 @@ def channel_search(query, page, channel) if response.status_code == 404 response = YT_POOL.client &.get("/user/#{channel}") response = YT_POOL.client &.get("/c/#{channel}") if response.status_code == 404 - ucid = response.body.match(/HeaderRenderer":\{"channelId":"(?[^\\"]+)"/).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 ucid = channel end @@ -244,11 +246,14 @@ def channel_search(query, page, channel) continuation = produce_channel_search_continuation(ucid, query, page) response_json = request_youtube_api_browse(continuation) - result = JSON.parse(response_json.match(/"continuationItems": (?\[.*\]),/m).try &.["items"] || "{}") - return 0, [] of SearchItem if result.size == 0 + result = JSON.parse(response_json) + continuationItems = result["onResponseReceivedActions"]? + .try &.[0]["appendContinuationItemsAction"]["continuationItems"] + + return 0, [] of SearchItem if !continuationItems 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]) .try { |t| items << t } }