Misc: Add an utility function to for 'region' URL parameter

This commit is contained in:
Samantaz Fox 2023-06-15 19:40:58 +02:00
parent 161fb64259
commit 700ca568c5
No known key found for this signature in database
GPG Key ID: F42821059186176E
13 changed files with 30 additions and 18 deletions

View File

@ -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

View File

@ -429,7 +429,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"

View File

@ -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

View File

@ -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

View File

@ -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"

View File

@ -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

View File

@ -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"

View File

@ -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

View File

@ -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)

View File

@ -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"]?

View File

@ -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("+")

View File

@ -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

View File

@ -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 }