Restructure API routes to use more namespaces

This commit is contained in:
syeopite 2021-08-13 23:35:03 -07:00
parent 6aa65593ef
commit b3426fdc94
No known key found for this signature in database
GPG key ID: 6FA616E5A5294A82
6 changed files with 42 additions and 44 deletions

View file

@ -1,4 +1,4 @@
module Invidious::Routes::APIv1 module Invidious::Routes::APIv1::Channels
def self.home(env) def self.home(env)
locale = LOCALES[env.get("preferences").as(Preferences).locale]? locale = LOCALES[env.get("preferences").as(Preferences).locale]?
@ -240,4 +240,27 @@ module Invidious::Routes::APIv1
return error_json(500, ex) return error_json(500, ex)
end end
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 end

View file

@ -1,4 +1,4 @@
module Invidious::Routes::APIv1 module Invidious::Routes::APIv1::Feeds
def self.trending(env) def self.trending(env)
locale = LOCALES[env.get("preferences").as(Preferences).locale]? locale = LOCALES[env.get("preferences").as(Preferences).locale]?

View file

@ -1,4 +1,4 @@
module Invidious::Routes::APIv1 module Invidious::Routes::APIv1::Misc
# Stats API endpoint for Invidious # Stats API endpoint for Invidious
def self.stats(env) def self.stats(env)
locale = LOCALES[env.get("preferences").as(Preferences).locale]? locale = LOCALES[env.get("preferences").as(Preferences).locale]?

View file

@ -1,21 +1,21 @@
# There is far too many API routes to define in invidious.cr # There is far too many API routes to define in invidious.cr
# so we'll just do it here instead with a macro. # so we'll just do it here instead with a macro.
macro define_v1_api_routes(base_url = "/api/v1") 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 # Videos
Invidious::Routing.get "#{{{base_url}}}/storyboards/:id", Invidious::Routes::APIv1, :storyboards Invidious::Routing.get "#{{{base_url}}}/videos/:id", Invidious::Routes::APIv1::Videos, :videos
Invidious::Routing.get "#{{{base_url}}}/captions/:id", Invidious::Routes::APIv1, :captions Invidious::Routing.get "#{{{base_url}}}/storyboards/:id", Invidious::Routes::APIv1::Videos, :storyboards
Invidious::Routing.get "#{{{base_url}}}/annotations/:id", Invidious::Routes::APIv1, :annotations Invidious::Routing.get "#{{{base_url}}}/captions/:id", Invidious::Routes::APIv1::Videos, :captions
Invidious::Routing.get "#{{{base_url}}}/search/suggestions/:id", Invidious::Routes::APIv1, :search_suggestions Invidious::Routing.get "#{{{base_url}}}/annotations/:id", Invidious::Routes::APIv1::Videos, :annotations
Invidious::Routing.get "#{{{base_url}}}/comments/:id", Invidious::Routes::APIv1, :comments Invidious::Routing.get "#{{{base_url}}}/comments/:id", Invidious::Routes::APIv1::Videos, :comments
# Feeds # Feeds
Invidious::Routing.get "#{{{base_url}}}/trending", Invidious::Routes::APIv1, :trending Invidious::Routing.get "#{{{base_url}}}/trending", Invidious::Routes::APIv1::Feeds, :trending
Invidious::Routing.get "#{{{base_url}}}/popular", Invidious::Routes::APIv1, :popular Invidious::Routing.get "#{{{base_url}}}/popular", Invidious::Routes::APIv1::Feeds, :popular
# Channels # 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 { {% for route in {
{"home", "home"}, {"home", "home"},
{"videos", "videos"}, {"videos", "videos"},
@ -25,13 +25,11 @@ macro define_v1_api_routes(base_url = "/api/v1")
{"search", "channel_search"}, {"search", "channel_search"},
} %} } %}
Invidious::Routing.get "#{{{base_url}}}/channels/#{{{route[0]}}}/:ucid", 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, :{{route[1]}} Invidious::Routing.get "#{{{base_url}}}/channels/:ucid/#{{{route[0]}}}", Invidious::Routes::APIv1::Channels, :{{route[1]}}
{% end %} {% end %}
# Search # Search
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}}}/videos/:id", Invidious::Routes::APIv1, :videos Invidious::Routing.get "#{{{base_url}}}/search/suggestions/:id", Invidious::Routes::APIv1::Search, :search_suggestions
Invidious::Routing.get "#{{{base_url}}}/search", Invidious::Routes::APIv1, :search
end end

View file

@ -1,4 +1,4 @@
module Invidious::Routes::APIv1 module Invidious::Routes::APIv1::Search
def self.search(env) def self.search(env)
locale = LOCALES[env.get("preferences").as(Preferences).locale]? locale = LOCALES[env.get("preferences").as(Preferences).locale]?
region = env.params.query["region"]? region = env.params.query["region"]?
@ -42,29 +42,6 @@ module Invidious::Routes::APIv1
end end
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) def self.search_suggestions(env)
locale = LOCALES[env.get("preferences").as(Preferences).locale]? locale = LOCALES[env.get("preferences").as(Preferences).locale]?
region = env.params.query["region"]? region = env.params.query["region"]?

View file

@ -1,4 +1,4 @@
module Invidious::Routes::APIv1 module Invidious::Routes::APIv1::Videos
def self.videos(env) def self.videos(env)
locale = LOCALES[env.get("preferences").as(Preferences).locale]? locale = LOCALES[env.get("preferences").as(Preferences).locale]?