From f77dcb7f9ba9fbb0cb51ccdd34cfbab5a85de5e3 Mon Sep 17 00:00:00 2001 From: Omar Roth Date: Fri, 19 Jan 2018 18:51:57 +0000 Subject: [PATCH 1/3] Randomly refresh HTTP clients --- src/invidious.cr | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/invidious.cr b/src/invidious.cr index a0a6a4f7e..8ba75cc22 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -21,9 +21,12 @@ end # Refresh all the connections in the pool by crawling recommended spawn do # Arbitrary start value - id = Deque.new(50,"0xjKNDMgE54") + id = Deque.new(50, "0xjKNDMgE54") while true client = get_client + if rand(30) > 1 + client = HTTP::Client.new(URL, CONTEXT) + end time = Time.now begin @@ -33,7 +36,7 @@ spawn do rvs << HTTP::Params.parse(rv).to_h end rvs.each do |rv| - id << rv["id"] + id << rv["id"] end puts "#{Time.now} 200 GET #{elapsed_text(Time.now - time)}" rescue ex From 19309f5ef69e8ad57beb7e8da73f28deeb7f2ced Mon Sep 17 00:00:00 2001 From: Omar Roth Date: Sat, 20 Jan 2018 00:20:12 +0000 Subject: [PATCH 2/3] Fix client refresh --- src/invidious.cr | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/invidious.cr b/src/invidious.cr index 8ba75cc22..116361188 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -15,7 +15,9 @@ CONTEXT.add_options( OpenSSL::SSL::Options::NO_SSL_V3 ) POOL = Deque.new(30) do - HTTP::Client.new(URL, CONTEXT) + client = HTTP::Client.new(URL, CONTEXT) + client.connect_timeout = Time::Span.new(0, 0, 0, 5) + client end # Refresh all the connections in the pool by crawling recommended @@ -24,26 +26,34 @@ spawn do id = Deque.new(50, "0xjKNDMgE54") while true client = get_client - if rand(30) > 1 + if rand(50) < 1 client = HTTP::Client.new(URL, CONTEXT) + client.connect_timeout = Time::Span.new(0, 0, 0, 5) end time = Time.now begin video = get_video(id[rand(id.size)], false) - rvs = [] of Hash(String, String) + rescue ex + puts ex + next + end + + rvs = [] of Hash(String, String) + if video.info.has_key?("rvs") video.info["rvs"].split(",").each do |rv| rvs << HTTP::Params.parse(rv).to_h end - rvs.each do |rv| + end + + rvs.each do |rv| + if rv.has_key?("id") id << rv["id"] end - puts "#{Time.now} 200 GET #{elapsed_text(Time.now - time)}" - rescue ex - next - ensure - POOL << client end + + POOL << client + puts "#{Time.now} 200 GET #{elapsed_text(Time.now - time)}" end end From 6418d87a63bb121062aab83a30271705d052a31b Mon Sep 17 00:00:00 2001 From: Omar Roth Date: Fri, 19 Jan 2018 18:31:47 -0600 Subject: [PATCH 3/3] Add fix for videos that don't have specific keys --- src/invidious.cr | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/invidious.cr b/src/invidious.cr index 116361188..0481b026a 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -185,20 +185,30 @@ get "/watch" do |env| next templated "error" end - player_response = JSON.parse(video.info["player_response"]) - + fmt_stream = [] of HTTP::Params video.info["url_encoded_fmt_stream_map"].split(",") do |string| fmt_stream << HTTP::Params.parse(string) end - + fmt_stream.reverse! # We want lowest quality first - + adaptive_fmts = [] of HTTP::Params - video.info["adaptive_fmts"].split(",") do |string| - adaptive_fmts << HTTP::Params.parse(string) + if video.info.has_key?("adaptive_fmts") + video.info["adaptive_fmts"].split(",") do |string| + adaptive_fmts << HTTP::Params.parse(string) + end end - + + rvs = [] of Hash(String, String) + if video.info.has_key?("rvs") + video.info["rvs"].split(",").each do |rv| + rvs << HTTP::Params.parse(rv).to_h + end + end + + player_response = JSON.parse(video.info["player_response"]) + likes = video.html.xpath_node(%q(//button[@title="I like this"]/span)) likes = likes ? likes.content.delete(",").to_i : 1 @@ -214,11 +224,6 @@ get "/watch" do |env| engagement = ((dislikes.to_f + likes.to_f)/views * 100) calculated_rating = (likes.to_f/(likes.to_f + dislikes.to_f) * 4 + 1) - rvs = [] of Hash(String, String) - video.info["rvs"].split(",").each do |rv| - rvs << HTTP::Params.parse(rv).to_h - end - templated "watch" end