From 5a455ec4f76e7a8ca339ec772911ea75ecb60cf7 Mon Sep 17 00:00:00 2001 From: Omar Roth Date: Fri, 5 Jul 2019 12:08:39 -0500 Subject: [PATCH] Fix redirect for livestream segments --- src/invidious.cr | 41 ++++++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/src/invidious.cr b/src/invidious.cr index b86adf48..d59d1aba 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -4429,7 +4429,6 @@ get "/api/manifest/hls_variant/*" do |env| manifest end -# TODO: Fix redirect for local streams get "/api/manifest/hls_playlist/*" do |env| client = make_client(YT_URL) manifest = client.get(env.request.path) @@ -4449,13 +4448,41 @@ get "/api/manifest/hls_playlist/*" do |env| manifest = manifest.body if local - manifest = manifest.gsub("https://www.youtube.com", host_url) - manifest = manifest.gsub(/https:\/\/r\d---.{11}\.c\.youtube\.com/, host_url) - manifest = manifest.gsub("seg.ts", "seg.ts?local=true") - end + manifest = manifest.gsub(/^https:\/\/r\d---.{11}\.c\.youtube\.com[^\n]*/m) do |match| + path = URI.parse(match).path - fvip = manifest.match(/hls_chunk_host\/r(?\d+)---/).not_nil!["fvip"] - manifest = manifest.gsub("seg.ts", "seg.ts/fvip/#{fvip}") + path = path.lchop("/videoplayback/") + path = path.rchop("/") + + path = path.gsub(/mime\/\w+\/\w+/) do |mimetype| + mimetype = mimetype.split("/") + mimetype[0] + "/" + mimetype[1] + "%2F" + mimetype[2] + end + + path = path.split("/") + + raw_params = {} of String => Array(String) + path.each_slice(2) do |pair| + key, value = pair + value = URI.unescape(value) + + if raw_params[key]? + raw_params[key] << value + else + raw_params[key] = [value] + end + end + + raw_params = HTTP::Params.new(raw_params) + if fvip = raw_params["hls_chunk_host"].match(/r(?\d+)---/) + raw_params["fvip"] = fvip["fvip"] + end + + raw_params["local"] = "true" + + "#{host_url}/videoplayback?#{raw_params}" + end + end manifest end