Merge pull request #2821 from matthewmcgarvey/channel-search

Handle invalid channel id in channel: search
This commit is contained in:
Samantaz Fox 2022-01-25 19:34:43 +01:00 committed by GitHub
commit 2ae074a9a4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 2 deletions

View file

@ -55,6 +55,8 @@ module Invidious::Routes::Search
begin begin
search_query, count, videos, operators = process_search_query(query, page, user, region: region) search_query, count, videos, operators = process_search_query(query, page, user, region: region)
rescue ex : ChannelSearchException
return error_template(404, "Unable to find channel with id of '#{HTML.escape(ex.channel)}'. Are you sure that's an actual channel id? It should look like 'UC4QobU6STFB0P71PMvOGN5A'.")
rescue ex rescue ex
return error_template(500, ex) return error_template(500, ex)
end end

View file

@ -1,3 +1,10 @@
class ChannelSearchException < InfoException
getter channel : String
def initialize(@channel)
end
end
def channel_search(query, page, channel) def channel_search(query, page, channel)
response = YT_POOL.client &.get("/channel/#{channel}") response = YT_POOL.client &.get("/channel/#{channel}")
@ -5,8 +12,8 @@ def channel_search(query, page, channel)
response = YT_POOL.client &.get("/user/#{channel}") response = YT_POOL.client &.get("/user/#{channel}")
response = YT_POOL.client &.get("/c/#{channel}") if response.status_code == 404 response = YT_POOL.client &.get("/c/#{channel}") if response.status_code == 404
initial_data = extract_initial_data(response.body) initial_data = extract_initial_data(response.body)
ucid = initial_data["header"]["c4TabbedHeaderRenderer"]?.try &.["channelId"].as_s? ucid = initial_data.dig?("header", "c4TabbedHeaderRenderer", "channelId").try(&.as_s?)
raise InfoException.new("Impossible to extract channel ID from page") if !ucid raise ChannelSearchException.new(channel) if !ucid
else else
ucid = channel ucid = channel
end end