summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authormatthewmcgarvey <matthewmcgarvey14@gmail.com>2022-02-22 23:04:30 -0600
committermatthewmcgarvey <matthewmcgarvey14@gmail.com>2022-02-22 23:04:30 -0600
commitcc59de0c9310b31b002e836ffe055dfaf5cfafd7 (patch)
treedb14a4eb505563346c2b576242a1a32546a78f52 /src
parent997d936e9c8d2b9ab87935fcbdd5b34a54d726b5 (diff)
downloadinvidious-cc59de0c9310b31b002e836ffe055dfaf5cfafd7.tar.gz
invidious-cc59de0c9310b31b002e836ffe055dfaf5cfafd7.tar.bz2
invidious-cc59de0c9310b31b002e836ffe055dfaf5cfafd7.zip
Extract live endpoints to route
Diffstat (limited to 'src')
-rw-r--r--src/invidious.cr41
-rw-r--r--src/invidious/routes/live.cr34
2 files changed, 38 insertions, 37 deletions
diff --git a/src/invidious.cr b/src/invidious.cr
index d4878759..24f49930 100644
--- a/src/invidious.cr
+++ b/src/invidious.cr
@@ -399,6 +399,10 @@ Invidious::Routing.get "/s_p/:id/:name", Invidious::Routes::Images, :s_p_image
Invidious::Routing.get "/yts/img/:name", Invidious::Routes::Images, :yts_image
Invidious::Routing.get "/vi/:id/:name", Invidious::Routes::Images, :thumbnails
+Invidious::Routing.get "/channel/:ucid/live", Invidious::Routes::Live, :check
+Invidious::Routing.get "/user/:user/live", Invidious::Routes::Live, :check
+Invidious::Routing.get "/c/:user/live", Invidious::Routes::Live, :check
+
# API routes (macro)
define_v1_api_routes()
@@ -406,43 +410,6 @@ define_v1_api_routes()
define_api_manifest_routes()
define_video_playback_routes()
-# Channels
-
-{"/channel/:ucid/live", "/user/:user/live", "/c/:user/live"}.each do |route|
- get route do |env|
- locale = env.get("preferences").as(Preferences).locale
-
- # Appears to be a bug in routing, having several routes configured
- # as `/a/:a`, `/b/:a`, `/c/:a` results in 404
- value = env.request.resource.split("/")[2]
- body = ""
- {"channel", "user", "c"}.each do |type|
- response = YT_POOL.client &.get("/#{type}/#{value}/live?disable_polymer=1")
- if response.status_code == 200
- body = response.body
- end
- end
-
- video_id = body.match(/'VIDEO_ID': "(?<id>[a-zA-Z0-9_-]{11})"/).try &.["id"]?
- if video_id
- params = [] of String
- env.params.query.each do |k, v|
- params << "#{k}=#{v}"
- end
- params = params.join("&")
-
- url = "/watch?v=#{video_id}"
- if !params.empty?
- url += "&#{params}"
- end
-
- env.redirect url
- else
- env.redirect "/channel/#{value}"
- end
- end
-end
-
# Authenticated endpoints
# The notification APIs can't be extracted yet
diff --git a/src/invidious/routes/live.cr b/src/invidious/routes/live.cr
new file mode 100644
index 00000000..e55111ce
--- /dev/null
+++ b/src/invidious/routes/live.cr
@@ -0,0 +1,34 @@
+module Invidious::Routes::Live
+ def self.check(env)
+ locale = env.get("preferences").as(Preferences).locale
+
+ # Appears to be a bug in routing, having several routes configured
+ # as `/a/:a`, `/b/:a`, `/c/:a` results in 404
+ value = env.request.resource.split("/")[2]
+ body = ""
+ {"channel", "user", "c"}.each do |type|
+ response = YT_POOL.client &.get("/#{type}/#{value}/live?disable_polymer=1")
+ if response.status_code == 200
+ body = response.body
+ end
+ end
+
+ video_id = body.match(/'VIDEO_ID': "(?<id>[a-zA-Z0-9_-]{11})"/).try &.["id"]?
+ if video_id
+ params = [] of String
+ env.params.query.each do |k, v|
+ params << "#{k}=#{v}"
+ end
+ params = params.join("&")
+
+ url = "/watch?v=#{video_id}"
+ if !params.empty?
+ url += "&#{params}"
+ end
+
+ env.redirect url
+ else
+ env.redirect "/channel/#{value}"
+ end
+ end
+end