Skip to content

Commit cc59de0

Browse files
Extract live endpoints to route
1 parent 997d936 commit cc59de0

File tree

2 files changed

+38
-37
lines changed

2 files changed

+38
-37
lines changed

src/invidious.cr

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -399,50 +399,17 @@ Invidious::Routing.get "/s_p/:id/:name", Invidious::Routes::Images, :s_p_image
399399
Invidious::Routing.get "/yts/img/:name", Invidious::Routes::Images, :yts_image
400400
Invidious::Routing.get "/vi/:id/:name", Invidious::Routes::Images, :thumbnails
401401

402+
Invidious::Routing.get "/channel/:ucid/live", Invidious::Routes::Live, :check
403+
Invidious::Routing.get "/user/:user/live", Invidious::Routes::Live, :check
404+
Invidious::Routing.get "/c/:user/live", Invidious::Routes::Live, :check
405+
402406
# API routes (macro)
403407
define_v1_api_routes()
404408

405409
# Video playback (macros)
406410
define_api_manifest_routes()
407411
define_video_playback_routes()
408412

409-
# Channels
410-
411-
{"/channel/:ucid/live", "/user/:user/live", "/c/:user/live"}.each do |route|
412-
get route do |env|
413-
locale = env.get("preferences").as(Preferences).locale
414-
415-
# Appears to be a bug in routing, having several routes configured
416-
# as `/a/:a`, `/b/:a`, `/c/:a` results in 404
417-
value = env.request.resource.split("/")[2]
418-
body = ""
419-
{"channel", "user", "c"}.each do |type|
420-
response = YT_POOL.client &.get("/#{type}/#{value}/live?disable_polymer=1")
421-
if response.status_code == 200
422-
body = response.body
423-
end
424-
end
425-
426-
video_id = body.match(/'VIDEO_ID': "(?<id>[a-zA-Z0-9_-]{11})"/).try &.["id"]?
427-
if video_id
428-
params = [] of String
429-
env.params.query.each do |k, v|
430-
params << "#{k}=#{v}"
431-
end
432-
params = params.join("&")
433-
434-
url = "/watch?v=#{video_id}"
435-
if !params.empty?
436-
url += "&#{params}"
437-
end
438-
439-
env.redirect url
440-
else
441-
env.redirect "/channel/#{value}"
442-
end
443-
end
444-
end
445-
446413
# Authenticated endpoints
447414

448415
# The notification APIs can't be extracted yet

src/invidious/routes/live.cr

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
module Invidious::Routes::Live
2+
def self.check(env)
3+
locale = env.get("preferences").as(Preferences).locale
4+
5+
# Appears to be a bug in routing, having several routes configured
6+
# as `/a/:a`, `/b/:a`, `/c/:a` results in 404
7+
value = env.request.resource.split("/")[2]
8+
body = ""
9+
{"channel", "user", "c"}.each do |type|
10+
response = YT_POOL.client &.get("/#{type}/#{value}/live?disable_polymer=1")
11+
if response.status_code == 200
12+
body = response.body
13+
end
14+
end
15+
16+
video_id = body.match(/'VIDEO_ID': "(?<id>[a-zA-Z0-9_-]{11})"/).try &.["id"]?
17+
if video_id
18+
params = [] of String
19+
env.params.query.each do |k, v|
20+
params << "#{k}=#{v}"
21+
end
22+
params = params.join("&")
23+
24+
url = "/watch?v=#{video_id}"
25+
if !params.empty?
26+
url += "&#{params}"
27+
end
28+
29+
env.redirect url
30+
else
31+
env.redirect "/channel/#{value}"
32+
end
33+
end
34+
end

0 commit comments

Comments
 (0)