mirror of
https://gitea.invidious.io/iv-org/invidious-copy-2022-04-11.git
synced 2024-08-15 00:43:26 +00:00
Use '/youtubei/v1/search' endpoint for search queries
This commit is contained in:
parent
43bd331e48
commit
26a7e1b049
2 changed files with 42 additions and 7 deletions
|
@ -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
|
||||
)
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in a new issue