From fdc014af6781a306efe91ecd317e13bffb0a282a Mon Sep 17 00:00:00 2001 From: Omar Roth Date: Mon, 11 Mar 2019 11:43:48 -0500 Subject: [PATCH] Add '&local=true' to watch and embed pages --- src/invidious.cr | 56 ++++++++++++++++------- src/invidious/videos.cr | 7 ++- src/invidious/views/components/player.ecr | 6 +-- 3 files changed, 47 insertions(+), 22 deletions(-) diff --git a/src/invidious.cr b/src/invidious.cr index a5fc3658..6ea4b54b 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -396,6 +396,12 @@ get "/watch" do |env| fmt_stream = video.fmt_stream(decrypt_function) adaptive_fmts = video.adaptive_fmts(decrypt_function) + + if params[:local] + fmt_stream.each { |fmt| fmt["url"] = URI.parse(fmt["url"]).query.not_nil! } + adaptive_fmts.each { |fmt| fmt["url"] = URI.parse(fmt["url"]).query.not_nil! } + end + video_streams = video.video_streams(adaptive_fmts) audio_streams = video.audio_streams(adaptive_fmts) @@ -496,6 +502,12 @@ get "/embed/:id" do |env| fmt_stream = video.fmt_stream(decrypt_function) adaptive_fmts = video.adaptive_fmts(decrypt_function) + + if params[:local] + fmt_stream.each { |fmt| fmt["url"] = URI.parse(fmt["url"]).query.not_nil! } + adaptive_fmts.each { |fmt| fmt["url"] = URI.parse(fmt["url"]).query.not_nil! } + end + video_streams = video.video_streams(adaptive_fmts) audio_streams = video.audio_streams(adaptive_fmts) @@ -4209,24 +4221,32 @@ get "/videoplayback" do |env| query_params = env.params.query fvip = query_params["fvip"]? || "3" - mn = query_params["mn"].split(",")[-1] + mn = query_params["mn"].split(",").pop host = "https://r#{fvip}---#{mn}.googlevideo.com" url = "/videoplayback?#{query_params.to_s}" - headers = env.request.headers - headers.delete("Host") - headers.delete("Cookie") - headers.delete("User-Agent") - headers.delete("Referer") + headers = HTTP::Headers.new + {"Range", "Accept", "Accept-Encoding"}.each do |header| + if env.request.headers[header]? + headers[header] = env.request.headers[header] + end + end region = query_params["region"]? response = HTTP::Client::Response.new(403) - loop do + 5.times do begin client = make_client(URI.parse(host), proxies, region) response = client.head(url, headers) break + rescue Socket::Addrinfo::Error + if fvip == "3" + break + end + + fvip = "3" + host = "https://r#{fvip}---#{mn}.googlevideo.com" rescue ex end end @@ -4284,11 +4304,12 @@ get "/ggpht/*" do |env| client = make_client(URI.parse(host)) url = env.request.path.lchop("/ggpht") - headers = env.request.headers - headers.delete("Host") - headers.delete("Cookie") - headers.delete("User-Agent") - headers.delete("Referer") + headers = HTTP::Headers.new + {"Range", "Accept", "Accept-Encoding"}.each do |header| + if env.request.headers[header]? + headers[header] = env.request.headers[header] + end + end client.get(url, headers) do |response| env.response.status_code = response.status_code @@ -4342,11 +4363,12 @@ get "/vi/:id/:name" do |env| end url = "/vi/#{id}/#{name}" - headers = env.request.headers - headers.delete("Host") - headers.delete("Cookie") - headers.delete("User-Agent") - headers.delete("Referer") + headers = HTTP::Headers.new + {"Range", "Accept", "Accept-Encoding"}.each do |header| + if env.request.headers[header]? + headers[header] = env.request.headers[header] + end + end client.get(url, headers) do |response| env.response.status_code = response.status_code diff --git a/src/invidious/videos.cr b/src/invidious/videos.cr index cd24afdc..b5bd2083 100644 --- a/src/invidious/videos.cr +++ b/src/invidious/videos.cr @@ -731,11 +731,12 @@ end def process_video_params(query, preferences) autoplay = query["autoplay"]?.try &.to_i? continue = query["continue"]?.try &.to_i? - related_videos = query["related_videos"]? listen = query["listen"]? && (query["listen"] == "true" || query["listen"] == "1").to_unsafe + local = query["local"]? && (query["local"] == "true").to_unsafe preferred_captions = query["subtitles"]?.try &.split(",").map { |a| a.downcase } quality = query["quality"]? region = query["region"]? + related_videos = query["related_videos"]? speed = query["speed"]?.try &.to_f? video_loop = query["loop"]?.try &.to_i? volume = query["volume"]?.try &.to_i? @@ -765,8 +766,9 @@ def process_video_params(query, preferences) autoplay = autoplay == 1 continue = continue == 1 - related_videos = related_videos == 1 listen = listen == 1 + local = local == 1 + related_videos = related_videos == 1 video_loop = video_loop == 1 if query["t"]? @@ -799,6 +801,7 @@ def process_video_params(query, preferences) continue: continue, controls: controls, listen: listen, + local: local, preferred_captions: preferred_captions, quality: quality, raw: raw, diff --git a/src/invidious/views/components/player.ecr b/src/invidious/views/components/player.ecr index e2cfa3a1..cecb9b2f 100644 --- a/src/invidious/views/components/player.ecr +++ b/src/invidious/views/components/player.ecr @@ -11,7 +11,7 @@ <% else %> <% if params[:listen] %> <% audio_streams.each_with_index do |fmt, i| %> - " type='<%= fmt["type"] %>' label="<%= fmt["bitrate"] %>k" selected="<%= i == 0 ? true : false %>"> + <% if params[:local] %>&local=true<% end %>" type='<%= fmt["type"] %>' label="<%= fmt["bitrate"] %>k" selected="<%= i == 0 ? true : false %>"> <% end %> <% else %> <% if params[:quality] == "dash" %> @@ -19,9 +19,9 @@ <% end %> <% fmt_stream.each_with_index do |fmt, i| %> <% if params[:quality] %> - " type='<%= fmt["type"] %>' label="<%= fmt["label"] %>" selected="<%= params[:quality] == fmt["label"].split(" - ")[0] %>"> + <% if params[:local] %>&local=true<% end %>" type='<%= fmt["type"] %>' label="<%= fmt["label"] %>" selected="<%= params[:quality] == fmt["label"].split(" - ")[0] %>"> <% else %> - " type='<%= fmt["type"] %>' label="<%= fmt["label"] %>" selected="<%= i == 0 ? true : false %>"> + <% if params[:local] %>&local=true<% end %>" type='<%= fmt["type"] %>' label="<%= fmt["label"] %>" selected="<%= i == 0 ? true : false %>"> <% end %> <% end %> <% end %>