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 8caa317c63
commit 3a4ca20309
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" local = env.params.query["local"]?.try &.== "true"
id = env.params.url["id"] 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, # 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 # we can opt to only add a source to a representation if it has a unique height within that representation

View File

@ -431,7 +431,7 @@ module Invidious::Routes::API::V1::Channels
def self.search(env) def self.search(env)
locale = env.get("preferences").as(Preferences).locale 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" env.response.content_type = "application/json"

View File

@ -4,7 +4,7 @@ module Invidious::Routes::API::V1::Feeds
env.response.content_type = "application/json" env.response.content_type = "application/json"
region = env.params.query["region"]? region = find_region(env.params.query["region"]?)
trending_type = env.params.query["type"]? trending_type = env.params.query["type"]?
begin begin

View File

@ -1,7 +1,7 @@
module Invidious::Routes::API::V1::Search module Invidious::Routes::API::V1::Search
def self.search(env) def self.search(env)
locale = env.get("preferences").as(Preferences).locale 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" env.response.content_type = "application/json"
@ -24,7 +24,7 @@ module Invidious::Routes::API::V1::Search
def self.search_suggestions(env) def self.search_suggestions(env)
preferences = env.get("preferences").as(Preferences) 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" 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 page = env.params.query["page"]?.try &.to_i? || 1
locale = env.get("preferences").as(Preferences).locale 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" env.response.content_type = "application/json"
begin begin

View File

@ -5,7 +5,7 @@ module Invidious::Routes::API::V1::Videos
env.response.content_type = "application/json" env.response.content_type = "application/json"
id = env.params.url["id"] 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"]? proxy = {"1", "true"}.any? &.== env.params.query["local"]?
begin begin
@ -25,7 +25,7 @@ module Invidious::Routes::API::V1::Videos
env.response.content_type = "application/json" env.response.content_type = "application/json"
id = env.params.url["id"] 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-]+$/) if id.nil? || id.size != 11 || !id.matches?(/^[\w-]+$/)
return error_json(400, "Invalid video ID") return error_json(400, "Invalid video ID")
@ -177,7 +177,7 @@ module Invidious::Routes::API::V1::Videos
env.response.content_type = "application/json" env.response.content_type = "application/json"
id = env.params.url["id"] id = env.params.url["id"]
region = env.params.query["region"]? region = find_region(env.params.query["region"]?)
begin begin
video = get_video(id, region: region) video = get_video(id, region: region)
@ -306,7 +306,7 @@ module Invidious::Routes::API::V1::Videos
def self.comments(env) def self.comments(env)
locale = env.get("preferences").as(Preferences).locale 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" env.response.content_type = "application/json"

View File

@ -48,7 +48,7 @@ module Invidious::Routes::Feeds
trending_type = env.params.query["type"]? trending_type = env.params.query["type"]?
trending_type ||= "Default" trending_type ||= "Default"
region = env.params.query["region"]? region = find_region(env.params.query["region"]?)
region ||= env.get("preferences").as(Preferences).region region ||= env.get("preferences").as(Preferences).region
begin begin

View File

@ -228,7 +228,7 @@ module Invidious::Routes::Playlists
prefs = env.get("preferences").as(Preferences) prefs = env.get("preferences").as(Preferences)
locale = prefs.locale locale = prefs.locale
region = env.params.query["region"]? || prefs.region region = find_region(env.params.query["region"]?) || prefs.region
user = env.get? "user" user = env.get? "user"
sid = env.get? "sid" sid = env.get? "sid"

View File

@ -110,7 +110,7 @@ module Invidious::Routes::PreferencesRoute
automatic_instance_redirect ||= "off" automatic_instance_redirect ||= "off"
automatic_instance_redirect = automatic_instance_redirect == "on" 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 = env.params.body["locale"]?.try &.as(String)
locale ||= CONFIG.default_user_preferences.locale locale ||= CONFIG.default_user_preferences.locale

View File

@ -40,7 +40,7 @@ module Invidious::Routes::Search
prefs = env.get("preferences").as(Preferences) prefs = env.get("preferences").as(Preferences)
locale = prefs.locale 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) query = Invidious::Search::Query.new(env.params.query, :regular, region)

View File

@ -9,7 +9,7 @@ module Invidious::Routes::VideoPlayback
mns ||= [] of String mns ||= [] of String
if query_params["region"]? if query_params["region"]?
region = query_params["region"] region = find_region(query_params["region"])
query_params.delete("region") query_params.delete("region")
end end
@ -265,7 +265,7 @@ module Invidious::Routes::VideoPlayback
return error_template(400, "Invalid itag") return error_template(400, "Invalid itag")
end end
region = env.params.query["region"]? region = find_region(env.params.query["region"]?)
local = (env.params.query["local"]? == "true") local = (env.params.query["local"]? == "true")
title = env.params.query["title"]? title = env.params.query["title"]?

View File

@ -3,7 +3,7 @@
module Invidious::Routes::Watch module Invidious::Routes::Watch
def self.handle(env) def self.handle(env)
locale = env.get("preferences").as(Preferences).locale 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?("+") if env.params.query.to_s.includes?("%20") || env.params.query.to_s.includes?("+")
url = "/watch?" + env.params.query.to_s.gsub("%20", "").delete("+") 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", "TZ", "UA", "UG", "UM", "US", "UY", "UZ", "VA", "VC", "VE", "VG", "VI",
"VN", "VU", "WF", "WS", "YE", "YT", "ZA", "ZM", "ZW", "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) preferred_captions = query["subtitles"]?.try &.split(",").map(&.downcase)
quality = query["quality"]? quality = query["quality"]?
quality_dash = query["quality_dash"]? 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 } related_videos = query["related_videos"]?.try { |q| (q == "true" || q == "1").to_unsafe }
speed = query["speed"]?.try &.rchop("x").to_f? speed = query["speed"]?.try &.rchop("x").to_f?
video_loop = query["loop"]?.try { |q| (q == "true" || q == "1").to_unsafe } video_loop = query["loop"]?.try { |q| (q == "true" || q == "1").to_unsafe }