summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorSamantaz Fox <coding@samantaz.fr>2023-10-05 23:01:44 +0200
committerSamantaz Fox <coding@samantaz.fr>2024-08-17 19:22:40 +0200
commit4c0b5c314d68ea45e69de9673f0bf43bedf3acc4 (patch)
treefad0cdb60beaec5499443ae8f71eeba47abbb7c3 /src
parenteb0f651812d7d01c038f5a052bf30fc8e26b877f (diff)
downloadinvidious-4c0b5c314d68ea45e69de9673f0bf43bedf3acc4.tar.gz
invidious-4c0b5c314d68ea45e69de9673f0bf43bedf3acc4.tar.bz2
invidious-4c0b5c314d68ea45e69de9673f0bf43bedf3acc4.zip
Search: Add support for youtu.be and youtube.com URLs
Diffstat (limited to 'src')
-rw-r--r--src/invidious/routes/search.cr6
-rw-r--r--src/invidious/search/query.cr27
2 files changed, 27 insertions, 6 deletions
diff --git a/src/invidious/routes/search.cr b/src/invidious/routes/search.cr
index 5be33533..85aa1c7e 100644
--- a/src/invidious/routes/search.cr
+++ b/src/invidious/routes/search.cr
@@ -51,6 +51,12 @@ module Invidious::Routes::Search
else
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
items = query.process
rescue ex : ChannelSearchException
diff --git a/src/invidious/search/query.cr b/src/invidious/search/query.cr
index e38845d9..f87c243e 100644
--- a/src/invidious/search/query.cr
+++ b/src/invidious/search/query.cr
@@ -48,11 +48,12 @@ module Invidious::Search
)
# Get the raw search query string (common to all search types). In
# Regular search mode, also look for the `search_query` URL parameter
- if @type.regular?
- @raw_query = params["q"]? || params["search_query"]? || ""
- else
- @raw_query = params["q"]? || ""
- end
+ _raw_query = params["q"]?
+ _raw_query ||= params["search_query"]? if @type.regular?
+ _raw_query ||= ""
+
+ # Remove surrounding whitespaces. Mostly useful for copy/pasted URLs.
+ @raw_query = _raw_query.strip
# Get the page number (also common to all search types)
@page = params["page"]?.try &.to_i? || 1
@@ -85,7 +86,7 @@ module Invidious::Search
@filters = Filters.from_iv_params(params)
@channel = params["channel"]? || ""
- if @filters.default? && @raw_query.includes?(':')
+ if @filters.default? && @raw_query.index(/\w:\w/)
# Parse legacy filters from query
@filters, @channel, @query, subs = Filters.from_legacy_filters(@raw_query)
else
@@ -136,5 +137,19 @@ module Invidious::Search
return params
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