diff --git a/src/invidious/routes/api/manifest.cr b/src/invidious/routes/api/manifest.cr index d89e752c..1a851a9b 100644 --- a/src/invidious/routes/api/manifest.cr +++ b/src/invidious/routes/api/manifest.cr @@ -6,7 +6,7 @@ module Invidious::Routes::API::Manifest local = env.params.query["local"]?.try &.== "true" id = env.params.url["id"] - region = env.params.query["region"]? + region = find_region(env.params.query["region"]?) # Since some implementations create playlists based on resolution regardless of different codecs, # we can opt to only add a source to a representation if it has a unique height within that representation diff --git a/src/invidious/routes/api/v1/channels.cr b/src/invidious/routes/api/v1/channels.cr index 7faf200a..2368010d 100644 --- a/src/invidious/routes/api/v1/channels.cr +++ b/src/invidious/routes/api/v1/channels.cr @@ -430,7 +430,7 @@ module Invidious::Routes::API::V1::Channels def self.search(env) locale = env.get("preferences").as(Preferences).locale - region = env.params.query["region"]? + region = find_region(env.params.query["region"]?) env.response.content_type = "application/json" diff --git a/src/invidious/routes/api/v1/feeds.cr b/src/invidious/routes/api/v1/feeds.cr index 41865f34..d55605f7 100644 --- a/src/invidious/routes/api/v1/feeds.cr +++ b/src/invidious/routes/api/v1/feeds.cr @@ -4,7 +4,7 @@ module Invidious::Routes::API::V1::Feeds env.response.content_type = "application/json" - region = env.params.query["region"]? + region = find_region(env.params.query["region"]?) trending_type = env.params.query["type"]? begin diff --git a/src/invidious/routes/api/v1/search.cr b/src/invidious/routes/api/v1/search.cr index 2922b060..a85daf9d 100644 --- a/src/invidious/routes/api/v1/search.cr +++ b/src/invidious/routes/api/v1/search.cr @@ -1,7 +1,7 @@ module Invidious::Routes::API::V1::Search def self.search(env) locale = env.get("preferences").as(Preferences).locale - region = env.params.query["region"]? + region = find_region(env.params.query["region"]?) env.response.content_type = "application/json" @@ -24,7 +24,7 @@ module Invidious::Routes::API::V1::Search def self.search_suggestions(env) preferences = env.get("preferences").as(Preferences) - region = env.params.query["region"]? || preferences.region + region = find_region(env.params.query["region"]?) || preferences.region env.response.content_type = "application/json" @@ -65,7 +65,7 @@ module Invidious::Routes::API::V1::Search page = env.params.query["page"]?.try &.to_i? || 1 locale = env.get("preferences").as(Preferences).locale - region = env.params.query["region"]? + region = find_region(env.params.query["region"]?) env.response.content_type = "application/json" begin diff --git a/src/invidious/routes/api/v1/videos.cr b/src/invidious/routes/api/v1/videos.cr index 9281f4dd..7d22522f 100644 --- a/src/invidious/routes/api/v1/videos.cr +++ b/src/invidious/routes/api/v1/videos.cr @@ -5,7 +5,7 @@ module Invidious::Routes::API::V1::Videos env.response.content_type = "application/json" id = env.params.url["id"] - region = env.params.query["region"]? + region = find_region(env.params.query["region"]?) proxy = {"1", "true"}.any? &.== env.params.query["local"]? begin @@ -25,7 +25,7 @@ module Invidious::Routes::API::V1::Videos env.response.content_type = "application/json" id = env.params.url["id"] - region = env.params.query["region"]? || env.params.body["region"]? + region = find_region(env.params.query["region"]? || env.params.body["region"]?) if id.nil? || id.size != 11 || !id.matches?(/^[\w-]+$/) return error_json(400, "Invalid video ID") @@ -168,7 +168,7 @@ module Invidious::Routes::API::V1::Videos env.response.content_type = "application/json" id = env.params.url["id"] - region = env.params.query["region"]? + region = find_region(env.params.query["region"]?) begin video = get_video(id, region: region) @@ -297,7 +297,7 @@ module Invidious::Routes::API::V1::Videos def self.comments(env) locale = env.get("preferences").as(Preferences).locale - region = env.params.query["region"]? + region = find_region(env.params.query["region"]?) env.response.content_type = "application/json" diff --git a/src/invidious/routes/feeds.cr b/src/invidious/routes/feeds.cr index e20a7139..04bccf81 100644 --- a/src/invidious/routes/feeds.cr +++ b/src/invidious/routes/feeds.cr @@ -48,7 +48,7 @@ module Invidious::Routes::Feeds trending_type = env.params.query["type"]? trending_type ||= "Default" - region = env.params.query["region"]? + region = find_region(env.params.query["region"]?) region ||= env.get("preferences").as(Preferences).region begin diff --git a/src/invidious/routes/playlists.cr b/src/invidious/routes/playlists.cr index 9c6843e9..abd0e945 100644 --- a/src/invidious/routes/playlists.cr +++ b/src/invidious/routes/playlists.cr @@ -228,7 +228,7 @@ module Invidious::Routes::Playlists prefs = env.get("preferences").as(Preferences) locale = prefs.locale - region = env.params.query["region"]? || prefs.region + region = find_region(env.params.query["region"]?) || prefs.region user = env.get? "user" sid = env.get? "sid" diff --git a/src/invidious/routes/preferences.cr b/src/invidious/routes/preferences.cr index 112535bd..61856515 100644 --- a/src/invidious/routes/preferences.cr +++ b/src/invidious/routes/preferences.cr @@ -110,7 +110,7 @@ module Invidious::Routes::PreferencesRoute automatic_instance_redirect ||= "off" automatic_instance_redirect = automatic_instance_redirect == "on" - region = env.params.body["region"]?.try &.as(String) + region = find_region(env.params.body["region"]?) locale = env.params.body["locale"]?.try &.as(String) locale ||= CONFIG.default_user_preferences.locale diff --git a/src/invidious/routes/search.cr b/src/invidious/routes/search.cr index 5be33533..859e5ded 100644 --- a/src/invidious/routes/search.cr +++ b/src/invidious/routes/search.cr @@ -40,7 +40,7 @@ module Invidious::Routes::Search prefs = env.get("preferences").as(Preferences) locale = prefs.locale - region = env.params.query["region"]? || prefs.region + region = find_region(env.params.query["region"]?) || prefs.region query = Invidious::Search::Query.new(env.params.query, :regular, region) diff --git a/src/invidious/routes/video_playback.cr b/src/invidious/routes/video_playback.cr index ec18f3b8..74ad1d1d 100644 --- a/src/invidious/routes/video_playback.cr +++ b/src/invidious/routes/video_playback.cr @@ -9,7 +9,7 @@ module Invidious::Routes::VideoPlayback mns ||= [] of String if query_params["region"]? - region = query_params["region"] + region = find_region(query_params["region"]) query_params.delete("region") end @@ -265,7 +265,7 @@ module Invidious::Routes::VideoPlayback return error_template(400, "Invalid itag") end - region = env.params.query["region"]? + region = find_region(env.params.query["region"]?) local = (env.params.query["local"]? == "true") title = env.params.query["title"]? diff --git a/src/invidious/routes/watch.cr b/src/invidious/routes/watch.cr index aabe8dfc..410e625b 100644 --- a/src/invidious/routes/watch.cr +++ b/src/invidious/routes/watch.cr @@ -3,7 +3,7 @@ module Invidious::Routes::Watch def self.handle(env) locale = env.get("preferences").as(Preferences).locale - region = env.params.query["region"]? + region = find_region(env.params.query["region"]?) if env.params.query.to_s.includes?("%20") || env.params.query.to_s.includes?("+") url = "/watch?" + env.params.query.to_s.gsub("%20", "").delete("+") diff --git a/src/invidious/videos/regions.cr b/src/invidious/videos/regions.cr index 575f8c25..7b1fd3f0 100644 --- a/src/invidious/videos/regions.cr +++ b/src/invidious/videos/regions.cr @@ -25,3 +25,13 @@ REGIONS = { "TZ", "UA", "UG", "UM", "US", "UY", "UZ", "VA", "VC", "VE", "VG", "VI", "VN", "VU", "WF", "WS", "YE", "YT", "ZA", "ZM", "ZW", } + +# Utility function that searches in the array above for a given input. +def find_region(reg : String?) : String? + return nil if reg.nil? + + # Normalize input + region = (reg || "").upcase[0..1] + + return REGIONS.find(&.== region) +end diff --git a/src/invidious/videos/video_preferences.cr b/src/invidious/videos/video_preferences.cr index 34cf7ff0..0abcd42d 100644 --- a/src/invidious/videos/video_preferences.cr +++ b/src/invidious/videos/video_preferences.cr @@ -38,7 +38,9 @@ def process_video_params(query, preferences) preferred_captions = query["subtitles"]?.try &.split(",").map(&.downcase) quality = query["quality"]? quality_dash = query["quality_dash"]? - region = query["region"]? + + region = find_region(query["region"]?) + related_videos = query["related_videos"]?.try { |q| (q == "true" || q == "1").to_unsafe } speed = query["speed"]?.try &.rchop("x").to_f? video_loop = query["loop"]?.try { |q| (q == "true" || q == "1").to_unsafe }