API: make /api/v1/videos respect the 'local' parameter

This commit is contained in:
Samantaz Fox 2023-01-15 17:04:04 +01:00
parent 05258d56bd
commit 1af846e58c
No known key found for this signature in database
GPG key ID: F42821059186176E
5 changed files with 36 additions and 11 deletions

View file

@ -34,6 +34,7 @@ require "protodec/utils"
require "./invidious/database/*" require "./invidious/database/*"
require "./invidious/database/migrations/*" require "./invidious/database/migrations/*"
require "./invidious/http_server/*"
require "./invidious/helpers/*" require "./invidious/helpers/*"
require "./invidious/yt_backend/*" require "./invidious/yt_backend/*"
require "./invidious/frontend/*" require "./invidious/frontend/*"

View file

@ -0,0 +1,20 @@
module Invidious::HttpServer
module Utils
extend self
def proxy_video_url(raw_url : String, *, region : String? = nil, absolute : Bool = false)
url = URI.parse(raw_url)
# Add some URL parameters
params = url.query_params
params["host"] = url.host.not_nil! # Should never be nil, in theory
params["region"] = region if !region.nil?
if absolute
return "#{HOST_URL}#{url.request_target}?#{params}"
else
return "#{url.request_target}?#{params}"
end
end
end
end

View file

@ -3,7 +3,7 @@ require "json"
module Invidious::JSONify::APIv1 module Invidious::JSONify::APIv1
extend self extend self
def video(video : Video, json : JSON::Builder, *, locale : String?) def video(video : Video, json : JSON::Builder, *, locale : String?, proxy : Bool = false)
json.object do json.object do
json.field "type", video.video_type json.field "type", video.video_type
@ -89,7 +89,14 @@ module Invidious::JSONify::APIv1
# Not available on MPEG-4 Timed Text (`text/mp4`) streams (livestreams only) # Not available on MPEG-4 Timed Text (`text/mp4`) streams (livestreams only)
json.field "bitrate", fmt["bitrate"].as_i.to_s if fmt["bitrate"]? json.field "bitrate", fmt["bitrate"].as_i.to_s if fmt["bitrate"]?
if proxy
json.field "url", Invidious::HttpServer::Utils.proxy_video_url(
fmt["url"].to_s, absolute: true
)
else
json.field "url", fmt["url"] json.field "url", fmt["url"]
end
json.field "itag", fmt["itag"].as_i.to_s json.field "itag", fmt["itag"].as_i.to_s
json.field "type", fmt["mimeType"] json.field "type", fmt["mimeType"]
json.field "clen", fmt["contentLength"]? || "-1" json.field "clen", fmt["contentLength"]? || "-1"

View file

@ -6,6 +6,7 @@ module Invidious::Routes::API::V1::Videos
id = env.params.url["id"] id = env.params.url["id"]
region = env.params.query["region"]? region = env.params.query["region"]?
proxy = {"1", "true"}.any? &.== env.params.query["local"]?
begin begin
video = get_video(id, region: region) video = get_video(id, region: region)
@ -15,7 +16,9 @@ module Invidious::Routes::API::V1::Videos
return error_json(500, ex) return error_json(500, ex)
end end
video.to_json(locale, nil) return JSON.build do |json|
Invidious::JSONify::APIv1.video(video, json, locale: locale, proxy: proxy)
end
end end
def self.captions(env) def self.captions(env)

View file

@ -91,14 +91,8 @@ module Invidious::Routes::VideoPlayback
env.response.headers["Access-Control-Allow-Origin"] = "*" env.response.headers["Access-Control-Allow-Origin"] = "*"
if location = resp.headers["Location"]? if location = resp.headers["Location"]?
location = URI.parse(location) url = Invidious::HttpServer::Utils.proxy_video_url(location, region: region)
location = "#{location.request_target}&host=#{location.host}" return env.redirect url
if region
location += "&region=#{region}"
end
return env.redirect location
end end
IO.copy(resp.body_io, env.response) IO.copy(resp.body_io, env.response)