Search: Add support for youtu.be and youtube.com URLs

This commit is contained in:
Samantaz Fox 2023-10-05 23:01:44 +02:00
parent eb0f651812
commit 4c0b5c314d
No known key found for this signature in database
GPG Key ID: F42821059186176E
2 changed files with 27 additions and 6 deletions

View File

@ -51,6 +51,12 @@ module Invidious::Routes::Search
else else
user = env.get? "user" user = env.get? "user"
# An URL was copy/pasted in the search box.
# Redirect the user to the appropriate page.
if query.is_url?
return env.redirect UrlSanitizer.process(query.text).to_s
end
begin begin
items = query.process items = query.process
rescue ex : ChannelSearchException rescue ex : ChannelSearchException

View File

@ -48,11 +48,12 @@ module Invidious::Search
) )
# Get the raw search query string (common to all search types). In # Get the raw search query string (common to all search types). In
# Regular search mode, also look for the `search_query` URL parameter # Regular search mode, also look for the `search_query` URL parameter
if @type.regular? _raw_query = params["q"]?
@raw_query = params["q"]? || params["search_query"]? || "" _raw_query ||= params["search_query"]? if @type.regular?
else _raw_query ||= ""
@raw_query = params["q"]? || ""
end # Remove surrounding whitespaces. Mostly useful for copy/pasted URLs.
@raw_query = _raw_query.strip
# Get the page number (also common to all search types) # Get the page number (also common to all search types)
@page = params["page"]?.try &.to_i? || 1 @page = params["page"]?.try &.to_i? || 1
@ -85,7 +86,7 @@ module Invidious::Search
@filters = Filters.from_iv_params(params) @filters = Filters.from_iv_params(params)
@channel = params["channel"]? || "" @channel = params["channel"]? || ""
if @filters.default? && @raw_query.includes?(':') if @filters.default? && @raw_query.index(/\w:\w/)
# Parse legacy filters from query # Parse legacy filters from query
@filters, @channel, @query, subs = Filters.from_legacy_filters(@raw_query) @filters, @channel, @query, subs = Filters.from_legacy_filters(@raw_query)
else else
@ -136,5 +137,19 @@ module Invidious::Search
return params return params
end end
# Checks if the query is a standalone URL
def is_url? : Bool
# Only supported in regular search mode
return false if !@type.regular?
# If filters are present, that's a regular search
return false if !@filters.default?
# Simple heuristics: domain name
return @raw_query.starts_with?(
/(https?:\/\/)?(www\.)?(m\.)?youtu(\.be|be\.com)\//
)
end
end end
end end