Skip to content

Commit 7a32269

Browse files
authored
Merge pull request #2925 from matthewmcgarvey/routes-final
Move last remaining routes in main file to new pattern
2 parents 45839f8 + d5f43ba commit 7a32269

File tree

6 files changed

+71
-81
lines changed

6 files changed

+71
-81
lines changed

src/invidious.cr

Lines changed: 6 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ if CONFIG.popular_enabled
154154
Invidious::Jobs.register Invidious::Jobs::PullPopularVideosJob.new(PG_DB)
155155
end
156156

157-
connection_channel = Channel({Bool, Channel(PQ::Notification)}).new(32)
158-
Invidious::Jobs.register Invidious::Jobs::NotificationJob.new(connection_channel, CONFIG.database_url)
157+
CONNECTION_CHANNEL = Channel({Bool, Channel(PQ::Notification)}).new(32)
158+
Invidious::Jobs.register Invidious::Jobs::NotificationJob.new(CONNECTION_CHANNEL, CONFIG.database_url)
159159

160160
Invidious::Jobs.start_all
161161

@@ -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
@@ -360,6 +363,7 @@ end
360363
Invidious::Routing.post "/playlist_ajax", Invidious::Routes::Playlists, :playlist_ajax
361364
Invidious::Routing.get "/playlist", Invidious::Routes::Playlists, :show
362365
Invidious::Routing.get "/mix", Invidious::Routes::Playlists, :mix
366+
Invidious::Routing.get "/watch_videos", Invidious::Routes::Playlists, :watch_videos
363367

364368
Invidious::Routing.get "/opensearch.xml", Invidious::Routes::Search, :opensearch
365369
Invidious::Routing.get "/results", Invidious::Routes::Search, :results
@@ -406,85 +410,6 @@ define_v1_api_routes()
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-
446-
# Authenticated endpoints
447-
448-
# The notification APIs can't be extracted yet
449-
# due to the requirement of the `connection_channel`
450-
# used by the `NotificationJob`
451-
452-
get "/api/v1/auth/notifications" do |env|
453-
env.response.content_type = "text/event-stream"
454-
455-
topics = env.params.query["topics"]?.try &.split(",").uniq.first(1000)
456-
topics ||= [] of String
457-
458-
create_notification_stream(env, topics, connection_channel)
459-
end
460-
461-
post "/api/v1/auth/notifications" do |env|
462-
env.response.content_type = "text/event-stream"
463-
464-
topics = env.params.body["topics"]?.try &.split(",").uniq.first(1000)
465-
topics ||= [] of String
466-
467-
create_notification_stream(env, topics, connection_channel)
468-
end
469-
470-
get "/Captcha" do |env|
471-
headers = HTTP::Headers{":authority" => "accounts.google.com"}
472-
response = YT_POOL.client &.get(env.request.resource, headers)
473-
env.response.headers["Content-Type"] = response.headers["Content-Type"]
474-
response.body
475-
end
476-
477-
# Undocumented, creates anonymous playlist with specified 'video_ids', max 50 videos
478-
get "/watch_videos" do |env|
479-
response = YT_POOL.client &.get(env.request.resource)
480-
if url = response.headers["Location"]?
481-
url = URI.parse(url).request_target
482-
next env.redirect url
483-
end
484-
485-
env.response.status_code = response.status_code
486-
end
487-
488413
error 404 do |env|
489414
if md = env.request.path.match(/^\/(?<id>([a-zA-Z0-9_-]{11})|(\w+))$/)
490415
item = md["id"]

src/invidious/routes/api/v1/authenticated.cr

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,4 +397,14 @@ module Invidious::Routes::API::V1::Authenticated
397397

398398
env.response.status_code = 204
399399
end
400+
401+
def self.notifications(env)
402+
env.response.content_type = "text/event-stream"
403+
404+
raw_topics = env.params.body["topics"]? || env.params.query["topics"]?
405+
topics = raw_topics.try &.split(",").uniq.first(1000)
406+
topics ||= [] of String
407+
408+
create_notification_stream(env, topics, CONNECTION_CHANNEL)
409+
end
400410
end

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/login.cr

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,4 +481,11 @@ module Invidious::Routes::Login
481481

482482
env.redirect referer
483483
end
484+
485+
def self.captcha(env)
486+
headers = HTTP::Headers{":authority" => "accounts.google.com"}
487+
response = YT_POOL.client &.get(env.request.resource, headers)
488+
env.response.headers["Content-Type"] = response.headers["Content-Type"]
489+
response.body
490+
end
484491
end

src/invidious/routes/playlists.cr

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,4 +443,15 @@ module Invidious::Routes::Playlists
443443

444444
templated "mix"
445445
end
446+
447+
# Undocumented, creates anonymous playlist with specified 'video_ids', max 50 videos
448+
def self.watch_videos(env)
449+
response = YT_POOL.client &.get(env.request.resource)
450+
if url = response.headers["Location"]?
451+
url = URI.parse(url).request_target
452+
return env.redirect url
453+
end
454+
455+
env.response.status_code = response.status_code
456+
end
446457
end

src/invidious/routing.cr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ macro define_user_routes
1515
Invidious::Routing.get "/login", Invidious::Routes::Login, :login_page
1616
Invidious::Routing.post "/login", Invidious::Routes::Login, :login
1717
Invidious::Routing.post "/signout", Invidious::Routes::Login, :signout
18+
Invidious::Routing.get "/Captcha", Invidious::Routes::Login, :captcha
1819

1920
# User preferences
2021
Invidious::Routing.get "/preferences", Invidious::Routes::PreferencesRoute, :show
@@ -95,6 +96,9 @@ macro define_v1_api_routes
9596
Invidious::Routing.post "/api/v1/auth/tokens/register", {{namespace}}::Authenticated, :register_token
9697
Invidious::Routing.post "/api/v1/auth/tokens/unregister", {{namespace}}::Authenticated, :unregister_token
9798

99+
Invidious::Routing.get "/api/v1/auth/notifications", {{namespace}}::Authenticated, :notifications
100+
Invidious::Routing.post "/api/v1/auth/notifications", {{namespace}}::Authenticated, :notifications
101+
98102
# Misc
99103
Invidious::Routing.get "/api/v1/stats", {{namespace}}::Misc, :stats
100104
Invidious::Routing.get "/api/v1/playlists/:plid", {{namespace}}::Misc, :get_playlist

0 commit comments

Comments
 (0)