From b3426fdc94cd48412ab401636ff3b660fa75972f Mon Sep 17 00:00:00 2001 From: syeopite Date: Fri, 13 Aug 2021 23:35:03 -0700 Subject: [PATCH] Restructure API routes to use more namespaces --- src/invidious/routes/api/v1/channels.cr | 25 ++++++++++++++++++++- src/invidious/routes/api/v1/feeds.cr | 2 +- src/invidious/routes/api/v1/misc.cr | 2 +- src/invidious/routes/api/v1/routes.cr | 30 ++++++++++++------------- src/invidious/routes/api/v1/search.cr | 25 +-------------------- src/invidious/routes/api/v1/videos.cr | 2 +- 6 files changed, 42 insertions(+), 44 deletions(-) diff --git a/src/invidious/routes/api/v1/channels.cr b/src/invidious/routes/api/v1/channels.cr index a8b06bf7..3401232b 100644 --- a/src/invidious/routes/api/v1/channels.cr +++ b/src/invidious/routes/api/v1/channels.cr @@ -1,4 +1,4 @@ -module Invidious::Routes::APIv1 +module Invidious::Routes::APIv1::Channels def self.home(env) locale = LOCALES[env.get("preferences").as(Preferences).locale]? @@ -240,4 +240,27 @@ module Invidious::Routes::APIv1 return error_json(500, ex) end end + + def self.channel_search(env) + locale = LOCALES[env.get("preferences").as(Preferences).locale]? + + env.response.content_type = "application/json" + + ucid = env.params.url["ucid"] + + query = env.params.query["q"]? + query ||= "" + + page = env.params.query["page"]?.try &.to_i? + page ||= 1 + + count, search_results = channel_search(query, page, ucid) + JSON.build do |json| + json.array do + search_results.each do |item| + item.to_json(locale, json) + end + end + end + end end diff --git a/src/invidious/routes/api/v1/feeds.cr b/src/invidious/routes/api/v1/feeds.cr index c24266c6..0107b71d 100644 --- a/src/invidious/routes/api/v1/feeds.cr +++ b/src/invidious/routes/api/v1/feeds.cr @@ -1,4 +1,4 @@ -module Invidious::Routes::APIv1 +module Invidious::Routes::APIv1::Feeds def self.trending(env) locale = LOCALES[env.get("preferences").as(Preferences).locale]? diff --git a/src/invidious/routes/api/v1/misc.cr b/src/invidious/routes/api/v1/misc.cr index 4bf8b8b0..c7c32ca9 100644 --- a/src/invidious/routes/api/v1/misc.cr +++ b/src/invidious/routes/api/v1/misc.cr @@ -1,4 +1,4 @@ -module Invidious::Routes::APIv1 +module Invidious::Routes::APIv1::Misc # Stats API endpoint for Invidious def self.stats(env) locale = LOCALES[env.get("preferences").as(Preferences).locale]? diff --git a/src/invidious/routes/api/v1/routes.cr b/src/invidious/routes/api/v1/routes.cr index 5c61ed7c..4f06bdb4 100644 --- a/src/invidious/routes/api/v1/routes.cr +++ b/src/invidious/routes/api/v1/routes.cr @@ -1,21 +1,21 @@ # There is far too many API routes to define in invidious.cr # so we'll just do it here instead with a macro. macro define_v1_api_routes(base_url = "/api/v1") - Invidious::Routing.get "#{{{base_url}}}/stats", Invidious::Routes::APIv1, :stats + Invidious::Routing.get "#{{{base_url}}}/stats", Invidious::Routes::APIv1::Misc, :stats - # Widgets - Invidious::Routing.get "#{{{base_url}}}/storyboards/:id", Invidious::Routes::APIv1, :storyboards - Invidious::Routing.get "#{{{base_url}}}/captions/:id", Invidious::Routes::APIv1, :captions - Invidious::Routing.get "#{{{base_url}}}/annotations/:id", Invidious::Routes::APIv1, :annotations - Invidious::Routing.get "#{{{base_url}}}/search/suggestions/:id", Invidious::Routes::APIv1, :search_suggestions - Invidious::Routing.get "#{{{base_url}}}/comments/:id", Invidious::Routes::APIv1, :comments + # Videos + Invidious::Routing.get "#{{{base_url}}}/videos/:id", Invidious::Routes::APIv1::Videos, :videos + Invidious::Routing.get "#{{{base_url}}}/storyboards/:id", Invidious::Routes::APIv1::Videos, :storyboards + Invidious::Routing.get "#{{{base_url}}}/captions/:id", Invidious::Routes::APIv1::Videos, :captions + Invidious::Routing.get "#{{{base_url}}}/annotations/:id", Invidious::Routes::APIv1::Videos, :annotations + Invidious::Routing.get "#{{{base_url}}}/comments/:id", Invidious::Routes::APIv1::Videos, :comments # Feeds - Invidious::Routing.get "#{{{base_url}}}/trending", Invidious::Routes::APIv1, :trending - Invidious::Routing.get "#{{{base_url}}}/popular", Invidious::Routes::APIv1, :popular + Invidious::Routing.get "#{{{base_url}}}/trending", Invidious::Routes::APIv1::Feeds, :trending + Invidious::Routing.get "#{{{base_url}}}/popular", Invidious::Routes::APIv1::Feeds, :popular # Channels - Invidious::Routing.get "#{{{base_url}}}/channels/:ucid", Invidious::Routes::APIv1, :home + Invidious::Routing.get "#{{{base_url}}}/channels/:ucid", Invidious::Routes::APIv1::Channels, :home {% for route in { {"home", "home"}, {"videos", "videos"}, @@ -25,13 +25,11 @@ macro define_v1_api_routes(base_url = "/api/v1") {"search", "channel_search"}, } %} - Invidious::Routing.get "#{{{base_url}}}/channels/#{{{route[0]}}}/:ucid", Invidious::Routes::APIv1, :{{route[1]}} - Invidious::Routing.get "#{{{base_url}}}/channels/:ucid/#{{{route[0]}}}", Invidious::Routes::APIv1, :{{route[1]}} + Invidious::Routing.get "#{{{base_url}}}/channels/#{{{route[0]}}}/:ucid", Invidious::Routes::APIv1::Channels, :{{route[1]}} + Invidious::Routing.get "#{{{base_url}}}/channels/:ucid/#{{{route[0]}}}", Invidious::Routes::APIv1::Channels, :{{route[1]}} {% end %} # Search - Invidious::Routing.get "#{{{base_url}}}/search", Invidious::Routes::APIv1, :search - Invidious::Routing.get "#{{{base_url}}}/videos/:id", Invidious::Routes::APIv1, :videos - Invidious::Routing.get "#{{{base_url}}}/search", Invidious::Routes::APIv1, :search - + Invidious::Routing.get "#{{{base_url}}}/search", Invidious::Routes::APIv1::Search, :search + Invidious::Routing.get "#{{{base_url}}}/search/suggestions/:id", Invidious::Routes::APIv1::Search, :search_suggestions end diff --git a/src/invidious/routes/api/v1/search.cr b/src/invidious/routes/api/v1/search.cr index d1ed645d..e4d5809f 100644 --- a/src/invidious/routes/api/v1/search.cr +++ b/src/invidious/routes/api/v1/search.cr @@ -1,4 +1,4 @@ -module Invidious::Routes::APIv1 +module Invidious::Routes::APIv1::Search def self.search(env) locale = LOCALES[env.get("preferences").as(Preferences).locale]? region = env.params.query["region"]? @@ -42,29 +42,6 @@ module Invidious::Routes::APIv1 end end - def self.channel_search(env) - locale = LOCALES[env.get("preferences").as(Preferences).locale]? - - env.response.content_type = "application/json" - - ucid = env.params.url["ucid"] - - query = env.params.query["q"]? - query ||= "" - - page = env.params.query["page"]?.try &.to_i? - page ||= 1 - - count, search_results = channel_search(query, page, ucid) - JSON.build do |json| - json.array do - search_results.each do |item| - item.to_json(locale, json) - end - end - end - end - def self.search_suggestions(env) locale = LOCALES[env.get("preferences").as(Preferences).locale]? region = env.params.query["region"]? diff --git a/src/invidious/routes/api/v1/videos.cr b/src/invidious/routes/api/v1/videos.cr index 7b7433f2..0eb2fca3 100644 --- a/src/invidious/routes/api/v1/videos.cr +++ b/src/invidious/routes/api/v1/videos.cr @@ -1,4 +1,4 @@ -module Invidious::Routes::APIv1 +module Invidious::Routes::APIv1::Videos def self.videos(env) locale = LOCALES[env.get("preferences").as(Preferences).locale]?