Fix channel search API

This commit is contained in:
Svallinn 2021-03-24 04:31:08 +00:00
parent c481ca924b
commit 72d1439788
No known key found for this signature in database
GPG Key ID: 09FB527F34037CCA

View File

@ -231,20 +231,27 @@ end
alias SearchItem = SearchVideo | SearchChannel | SearchPlaylist
def channel_search(query, page, channel)
response = YT_POOL.client &.get("/channel/#{channel}?hl=en&gl=US")
response = YT_POOL.client &.get("/user/#{channel}?hl=en&gl=US") if response.headers["location"]?
response = YT_POOL.client &.get("/c/#{channel}?hl=en&gl=US") if response.headers["location"]?
response = YT_POOL.client &.get("/channel/#{channel}")
ucid = response.body.match(/\\"channelId\\":\\"(?<ucid>[^\\]+)\\"/).try &.["ucid"]?
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":"(?<ucid>[^\\"]+)"/).try &.["ucid"]?
else
ucid = channel
end
return 0, [] of SearchItem if !ucid
continuation = produce_channel_search_continuation(ucid, query, page)
response_json = request_youtube_api_browse(continuation)
url = produce_channel_search_url(ucid, query, page)
response = YT_POOL.client &.get(url)
initial_data = JSON.parse(response.body).as_a.find &.["response"]?
return 0, [] of SearchItem if !initial_data
author = initial_data["response"]?.try &.["metadata"]?.try &.["channelMetadataRenderer"]?.try &.["title"]?.try &.as_s
items = extract_items(initial_data.as_h, author, ucid)
result = JSON.parse(response_json.match(/"continuationItems": (?<items>\[.*\]),/m).try &.["items"] || "{}")
return 0, [] of SearchItem if result.size == 0
items = [] of SearchItem
result.as_a.select(&.as_h.has_key?("itemSectionRenderer")).each { |item|
extract_item(item["itemSectionRenderer"]["contents"].as_a[0])
.try { |t| items << t }
}
return items.size, items
end
@ -361,18 +368,23 @@ def produce_search_params(page = 1, sort : String = "relevance", date : String =
return params
end
def produce_channel_search_url(ucid, query, page)
def produce_channel_search_continuation(ucid, query, page)
object = {
"80226972:embedded" => {
"2:string" => ucid,
"3:base64" => {
"2:string" => "search",
"7:varint" => 1_i64,
"15:string" => "#{page}",
"23:varint" => 0_i64,
"2:string" => "search",
"6:varint" => 1_i64,
"7:varint" => 1_i64,
"12:varint" => 1_i64,
"15:base64" => {
"3:varint" => 30_i64 * (page - 1),
},
"23:varint" => 0_i64
},
"11:string" => query,
},
"35:string" => "browse-feed#{ucid}search"
}
}
continuation = object.try { |i| Protodec::Any.cast_json(object) }
@ -380,7 +392,7 @@ def produce_channel_search_url(ucid, query, page)
.try { |i| Base64.urlsafe_encode(i) }
.try { |i| URI.encode_www_form(i) }
return "/browse_ajax?continuation=#{continuation}&gl=US&hl=en"
return continuation
end
def process_search_query(query, page, user, region)