From 26a7e1b049bde355b5ac05d1923b92c6f4a20179 Mon Sep 17 00:00:00 2001 From: Samantaz Fox Date: Wed, 7 Apr 2021 03:15:02 +0200 Subject: [PATCH] Use '/youtubei/v1/search' endpoint for search queries --- src/invidious/helpers/youtube_api.cr | 42 +++++++++++++++++++++++++++- src/invidious/search.cr | 7 +---- 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/src/invidious/helpers/youtube_api.cr b/src/invidious/helpers/youtube_api.cr index 84e0c38f..dc3a7eb5 100644 --- a/src/invidious/helpers/youtube_api.cr +++ b/src/invidious/helpers/youtube_api.cr @@ -30,9 +30,49 @@ def request_youtube_api_browse(continuation) "continuation": continuation, } + return _youtube_api_post_json("/youtubei/v1/browse", data) +end + +#################################################################### +# request_youtube_api_search(search_query, params, region) +# +# Requests the youtubei/vi/search endpoint with the required headers +# to get JSON in en-US (english US). +# +# The requested data is a search string, with some additional +# paramters, formatted as a base64 string. +# +def request_youtube_api_search(search_query : String, params : String, region = nil) + # JSON Request data, required by the API + data = { + "query": URI.encode_www_form(search_query), + "context": { + "client": { + "hl": "en", + "gl": region || "US", # Can't be empty! + "clientName": "WEB", + "clientVersion": HARDCODED_CLIENT_VERS, + }, + }, + "params": params, + } + + return _youtube_api_post_json("/youtubei/v1/search", data) +end + +#################################################################### +# _youtube_api_post_json(endpoint, data) +# +# Internal function that does the actual request to youtube servers +# and handles errors. +# +# The requested data is an endpoint (URL without the domain part) +# and the data as a Hash object. +# +def _youtube_api_post_json(endpoint, data) # Send the POST request and parse result response = YT_POOL.client &.post( - "/youtubei/v1/browse?key=#{HARDCODED_API_KEY}", + "#{endpoint}?key=#{HARDCODED_API_KEY}", headers: HTTP::Headers{"content-type" => "application/json; charset=UTF-8"}, body: data.to_json ) diff --git a/src/invidious/search.cr b/src/invidious/search.cr index 7c9c389e..662173a0 100644 --- a/src/invidious/search.cr +++ b/src/invidious/search.cr @@ -263,14 +263,9 @@ end def search(query, search_params = produce_search_params(content_type: "all"), region = nil) return 0, [] of SearchItem if query.empty? - body = YT_POOL.client(region, &.get("/results?search_query=#{URI.encode_www_form(query)}&sp=#{search_params}&hl=en").body) - return 0, [] of SearchItem if body.empty? - - initial_data = extract_initial_data(body) + initial_data = request_youtube_api_search(query, search_params, region) items = extract_items(initial_data) - # initial_data["estimatedResults"]?.try &.as_s.to_i64 - return items.size, items end