Skip to content

Commit 3b1837a

Browse files
Move remaining routes to new structure
1 parent cc59de0 commit 3b1837a

File tree

4 files changed

+44
-44
lines changed

4 files changed

+44
-44
lines changed

src/invidious.cr

Lines changed: 7 additions & 44 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

@@ -360,6 +360,7 @@ end
360360
Invidious::Routing.post "/playlist_ajax", Invidious::Routes::Playlists, :playlist_ajax
361361
Invidious::Routing.get "/playlist", Invidious::Routes::Playlists, :show
362362
Invidious::Routing.get "/mix", Invidious::Routes::Playlists, :mix
363+
Invidious::Routing.get "/watch_videos", Invidious::Routes::Playlists, :watch_videos
363364

364365
Invidious::Routing.get "/opensearch.xml", Invidious::Routes::Search, :opensearch
365366
Invidious::Routing.get "/results", Invidious::Routes::Search, :results
@@ -390,6 +391,8 @@ end
390391

391392
Invidious::Routing.post "/subscription_ajax", Invidious::Routes::Subscriptions, :toggle_subscription
392393
Invidious::Routing.get "/subscription_manager", Invidious::Routes::Subscriptions, :subscription_manager
394+
395+
Invidious::Routing.get "/Captcha", Invidious::Routes::Captcha, :get
393396
{% end %}
394397

395398
Invidious::Routing.get "/ggpht/*", Invidious::Routes::Images, :ggpht
@@ -405,53 +408,13 @@ Invidious::Routing.get "/c/:user/live", Invidious::Routes::Live, :check
405408

406409
# API routes (macro)
407410
define_v1_api_routes()
411+
Invidious::Routing.get "/api/v1/auth/notifications", Invidious::Routes::API::V1::Authenticated, :notifications_get
412+
Invidious::Routing.post "/api/v1/auth/notifications", Invidious::Routes::API::V1::Authenticated, :notifications_post
408413

409414
# Video playback (macros)
410415
define_api_manifest_routes()
411416
define_video_playback_routes()
412417

413-
# Authenticated endpoints
414-
415-
# The notification APIs can't be extracted yet
416-
# due to the requirement of the `connection_channel`
417-
# used by the `NotificationJob`
418-
419-
get "/api/v1/auth/notifications" do |env|
420-
env.response.content_type = "text/event-stream"
421-
422-
topics = env.params.query["topics"]?.try &.split(",").uniq.first(1000)
423-
topics ||= [] of String
424-
425-
create_notification_stream(env, topics, connection_channel)
426-
end
427-
428-
post "/api/v1/auth/notifications" do |env|
429-
env.response.content_type = "text/event-stream"
430-
431-
topics = env.params.body["topics"]?.try &.split(",").uniq.first(1000)
432-
topics ||= [] of String
433-
434-
create_notification_stream(env, topics, connection_channel)
435-
end
436-
437-
get "/Captcha" do |env|
438-
headers = HTTP::Headers{":authority" => "accounts.google.com"}
439-
response = YT_POOL.client &.get(env.request.resource, headers)
440-
env.response.headers["Content-Type"] = response.headers["Content-Type"]
441-
response.body
442-
end
443-
444-
# Undocumented, creates anonymous playlist with specified 'video_ids', max 50 videos
445-
get "/watch_videos" do |env|
446-
response = YT_POOL.client &.get(env.request.resource)
447-
if url = response.headers["Location"]?
448-
url = URI.parse(url).request_target
449-
next env.redirect url
450-
end
451-
452-
env.response.status_code = response.status_code
453-
end
454-
455418
error 404 do |env|
456419
if md = env.request.path.match(/^\/(?<id>([a-zA-Z0-9_-]{11})|(\w+))$/)
457420
item = md["id"]

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

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

398398
env.response.status_code = 204
399399
end
400+
401+
def self.notifications_get(env)
402+
env.response.content_type = "text/event-stream"
403+
404+
topics = env.params.query["topics"]?.try &.split(",").uniq.first(1000)
405+
topics ||= [] of String
406+
407+
create_notification_stream(env, topics, CONNECTION_CHANNEL)
408+
end
409+
410+
def self.notifications_post(env)
411+
env.response.content_type = "text/event-stream"
412+
413+
topics = env.params.body["topics"]?.try &.split(",").uniq.first(1000)
414+
topics ||= [] of String
415+
416+
create_notification_stream(env, topics, CONNECTION_CHANNEL)
417+
end
400418
end

src/invidious/routes/captcha.cr

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module Invidious::Routes::Captcha
2+
def self.get(env)
3+
headers = HTTP::Headers{":authority" => "accounts.google.com"}
4+
response = YT_POOL.client &.get(env.request.resource, headers)
5+
env.response.headers["Content-Type"] = response.headers["Content-Type"]
6+
response.body
7+
end
8+
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

0 commit comments

Comments
 (0)