forked from midou/invidious
Multiple youtube_api.cr helper fixes
Add documentation Bump web client version string Add charset=UTF-8 to the 'content-type' header Parse JSON and return it as a Hash Handle API error messages
This commit is contained in:
@@ -4,8 +4,18 @@
|
||||
|
||||
# Hard-coded constants required by the API
|
||||
HARDCODED_API_KEY = "AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8"
|
||||
HARDCODED_CLIENT_VERS = "2.20210318.08.00"
|
||||
HARDCODED_CLIENT_VERS = "2.20210330.08.00"
|
||||
|
||||
####################################################################
|
||||
# request_youtube_api_browse(continuation)
|
||||
#
|
||||
# Requests the youtubei/vi/browse endpoint with the required headers
|
||||
# to get JSON in en-US (english US).
|
||||
#
|
||||
# The requested data is a continuation token (ctoken). Depending on
|
||||
# this token's contents, the returned data can be comments, playlist
|
||||
# videos, search results, channel community tab, ...
|
||||
#
|
||||
def request_youtube_api_browse(continuation)
|
||||
# JSON Request data, required by the API
|
||||
data = {
|
||||
@@ -20,12 +30,23 @@ def request_youtube_api_browse(continuation)
|
||||
"continuation": continuation,
|
||||
}
|
||||
|
||||
# Send the POST request and return result
|
||||
# Send the POST request and parse result
|
||||
response = YT_POOL.client &.post(
|
||||
"/youtubei/v1/browse?key=#{HARDCODED_API_KEY}",
|
||||
headers: HTTP::Headers{"content-type" => "application/json"},
|
||||
headers: HTTP::Headers{"content-type" => "application/json; charset=UTF-8"},
|
||||
body: data.to_json
|
||||
)
|
||||
|
||||
return response.body
|
||||
initial_data = JSON.parse(response.body).as_h
|
||||
|
||||
# Error handling
|
||||
if initial_data.has_key?("error")
|
||||
code = initial_data["error"]["code"]
|
||||
message = initial_data["error"]["message"].to_s.sub(/(\\n)+\^$/, "")
|
||||
|
||||
raise InfoException.new("Could not extract JSON. Youtube API returned \
|
||||
error #{code} with message:<br>\"#{message}\"")
|
||||
end
|
||||
|
||||
return initial_data
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user