From f08199e7cf70e6ed298c13ddf481845c4930a4f0 Mon Sep 17 00:00:00 2001 From: Omar Roth Date: Sun, 15 Apr 2018 20:47:37 -0500 Subject: [PATCH] Add /videoplayback endpoint --- src/invidious.cr | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/invidious.cr b/src/invidious.cr index 85645611..bd510ceb 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -790,6 +790,53 @@ get "/modify_theme" do |env| env.redirect referer end +get "/videoplayback" do |env| + query_params = env.params.query + + mn = query_params["mn"].split(",")[0] + host = "https://r5---#{mn}.googlevideo.com" + url = "/videoplayback?#{query_params.to_s}" + + client = make_client(URI.parse(host)) + response = client.head(url) + + headers = env.request.headers + headers.delete("Host") + headers.delete("Cookie") + headers.delete("User-Agent") + headers.delete("Referer") + + client.get(url, headers) do |response| + if response.headers["Location"]? + url = URI.parse(response.headers["Location"]) + env.redirect url.full_path + else + env.response.status_code = 206 + + response.headers.each do |key, value| + env.response.headers[key] = value + end + + chunk = Bytes[8] + + loop do + count = response.body_io.read(chunk) + + if count == 0 + break + end + + begin + env.response.write(chunk) + env.response.flush + rescue ex + break + end + end + end + end +end + error 404 do |env| error_message = "404 Page not found" templated "error"