Skip to content

Commit e215a20

Browse files
Move live endpoints into Channels route
1 parent 919413e commit e215a20

File tree

3 files changed

+36
-38
lines changed

3 files changed

+36
-38
lines changed

src/invidious.cr

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,9 @@ end
324324
Invidious::Routing.get "/channel/:ucid/playlists", Invidious::Routes::Channels, :playlists
325325
Invidious::Routing.get "/channel/:ucid/community", Invidious::Routes::Channels, :community
326326
Invidious::Routing.get "/channel/:ucid/about", Invidious::Routes::Channels, :about
327+
Invidious::Routing.get "/channel/:ucid/live", Invidious::Routes::Channels, :live
328+
Invidious::Routing.get "/user/:user/live", Invidious::Routes::Channels, :live
329+
Invidious::Routing.get "/c/:user/live", Invidious::Routes::Channels, :live
327330

328331
["", "/videos", "/playlists", "/community", "/about"].each do |path|
329332
# /c/LinusTechTips
@@ -400,10 +403,6 @@ Invidious::Routing.get "/s_p/:id/:name", Invidious::Routes::Images, :s_p_image
400403
Invidious::Routing.get "/yts/img/:name", Invidious::Routes::Images, :yts_image
401404
Invidious::Routing.get "/vi/:id/:name", Invidious::Routes::Images, :thumbnails
402405

403-
Invidious::Routing.get "/channel/:ucid/live", Invidious::Routes::Live, :check
404-
Invidious::Routing.get "/user/:user/live", Invidious::Routes::Live, :check
405-
Invidious::Routing.get "/c/:user/live", Invidious::Routes::Live, :check
406-
407406
# API routes (macro)
408407
define_v1_api_routes()
409408
Invidious::Routing.get "/api/v1/auth/notifications", Invidious::Routes::API::V1::Authenticated, :notifications_get

src/invidious/routes/channels.cr

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,39 @@ module Invidious::Routes::Channels
147147
end
148148
end
149149

150+
def self.live(env)
151+
locale = env.get("preferences").as(Preferences).locale
152+
153+
# Appears to be a bug in routing, having several routes configured
154+
# as `/a/:a`, `/b/:a`, `/c/:a` results in 404
155+
value = env.request.resource.split("/")[2]
156+
body = ""
157+
{"channel", "user", "c"}.each do |type|
158+
response = YT_POOL.client &.get("/#{type}/#{value}/live?disable_polymer=1")
159+
if response.status_code == 200
160+
body = response.body
161+
end
162+
end
163+
164+
video_id = body.match(/'VIDEO_ID': "(?<id>[a-zA-Z0-9_-]{11})"/).try &.["id"]?
165+
if video_id
166+
params = [] of String
167+
env.params.query.each do |k, v|
168+
params << "#{k}=#{v}"
169+
end
170+
params = params.join("&")
171+
172+
url = "/watch?v=#{video_id}"
173+
if !params.empty?
174+
url += "&#{params}"
175+
end
176+
177+
env.redirect url
178+
else
179+
env.redirect "/channel/#{value}"
180+
end
181+
end
182+
150183
private def self.fetch_basic_information(env)
151184
locale = env.get("preferences").as(Preferences).locale
152185

src/invidious/routes/live.cr

Lines changed: 0 additions & 34 deletions
This file was deleted.

0 commit comments

Comments
 (0)