From 4e44a91d08e45103536235030407c871b3ec5082 Mon Sep 17 00:00:00 2001 From: Samantaz Fox Date: Tue, 1 Feb 2022 21:40:06 +0100 Subject: [PATCH] Add support for clips --- src/invidious.cr | 1 + src/invidious/routes/watch.cr | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/invidious.cr b/src/invidious.cr index b09f31c24..f4cae7ea2 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -339,6 +339,7 @@ end Invidious::Routing.post "/watch_ajax", Invidious::Routes::Watch, :mark_watched Invidious::Routing.get "/watch/:id", Invidious::Routes::Watch, :redirect Invidious::Routing.get "/shorts/:id", Invidious::Routes::Watch, :redirect + Invidious::Routing.get "/clip/:clip", Invidious::Routes::Watch, :clip Invidious::Routing.get "/w/:id", Invidious::Routes::Watch, :redirect Invidious::Routing.get "/v/:id", Invidious::Routes::Watch, :redirect Invidious::Routing.get "/e/:id", Invidious::Routes::Watch, :redirect diff --git a/src/invidious/routes/watch.cr b/src/invidious/routes/watch.cr index 7d048ce85..42bc42199 100644 --- a/src/invidious/routes/watch.cr +++ b/src/invidious/routes/watch.cr @@ -266,4 +266,19 @@ module Invidious::Routes::Watch "{}" end end + + def self.clip(env) + clip_id = env.params.url["clip"]? + + return error_template(400, "A clip ID is required") if !clip_id + + response = YoutubeAPI.resolve_url("https://www.youtube.com/clip/#{clip_id}") + return error_template(400, "Invalid clip ID") if response["error"]? + + if video_id = response.dig?("endpoint", "watchEndpoint", "videoId") + return env.redirect "/watch?v=#{video_id}&#{env.params.query}" + else + return error_template(404, "The requested clip doesn't exist") + end + end end