Invidious is an alternative front-end to YouTube
Refactor HTTP::Client calls into make_client
Omar Rothb6fff53b21e4
diff --git a/src/invidious.cr b/src/invidious.crindex 8ed4253f..3abd9c51 100644--- a/src/invidious.cr+++ b/src/invidious.cr@@ -45,7 +45,7 @@ ARCHIVE_URL = URI.parse("https://archive.org")LOGIN_URL = URI.parse("https://accounts.google.com")PUBSUB_URL = URI.parse("https://pubsubhubbub.appspot.com")REDDIT_URL = URI.parse("https://www.reddit.com")-TEXTCAPTCHA_URL = URI.parse("http://textcaptcha.com/[email protected]")+TEXTCAPTCHA_URL = URI.parse("http://textcaptcha.com")YT_URL = URI.parse("https://www.youtube.com")CHARS_SAFE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"CURRENT_BRANCH = {{ "#{`git branch | sed -n '/\* /s///p'`.strip}" }}@@ -2972,7 +2972,7 @@ get "/api/v1/annotations/:id" do |env|env.response.status_code = location.status_codeend- response = HTTP::Client.get(URI.parse(location.headers["Location"]))+ response = make_client(URI.parse(location.headers["Location"])).get(location.headers["Location"])if response.body.empty?env.response.status_code = 404diff --git a/src/invidious/helpers/helpers.cr b/src/invidious/helpers/helpers.crindex ac16fc0e..9b29210d 100644--- a/src/invidious/helpers/helpers.cr+++ b/src/invidious/helpers/helpers.cr@@ -619,6 +619,8 @@ def cache_annotation(db, id, annotations)endend- # TODO: Update on conflict?- db.exec("INSERT INTO annotations VALUES ($1, $2) ON CONFLICT DO NOTHING", id, annotations)+ if has_legacy_annotations+ # TODO: Update on conflict?+ db.exec("INSERT INTO annotations VALUES ($1, $2) ON CONFLICT DO NOTHING", id, annotations)+ endenddiff --git a/src/invidious/helpers/utils.cr b/src/invidious/helpers/utils.crindex eb304d8d..c1aea5b2 100644--- a/src/invidious/helpers/utils.cr+++ b/src/invidious/helpers/utils.cr@@ -18,13 +18,18 @@ def elapsed_text(elapsed)"#{(millis * 1000).round(2)}µs"end-def make_client(url, proxies = {} of String => Array({ip: String, port: Int32}), region = nil)- context = OpenSSL::SSL::Context::Client.new- context.add_options(- OpenSSL::SSL::Options::ALL |- OpenSSL::SSL::Options::NO_SSL_V2 |- OpenSSL::SSL::Options::NO_SSL_V3- )+def make_client(url : URI, proxies = {} of String => Array({ip: String, port: Int32}), region = nil)+ context = nil++ if url.scheme == "https"+ context = OpenSSL::SSL::Context::Client.new+ context.add_options(+ OpenSSL::SSL::Options::ALL |+ OpenSSL::SSL::Options::NO_SSL_V2 |+ OpenSSL::SSL::Options::NO_SSL_V3+ )+ end+client = HTTPClient.new(url, context)client.read_timeout = 10.secondsclient.connect_timeout = 10.secondsdiff --git a/src/invidious/users.cr b/src/invidious/users.crindex bad2b5c3..689d9434 100644--- a/src/invidious/users.cr+++ b/src/invidious/users.cr@@ -331,7 +331,7 @@ def generate_captcha(key, db)enddef generate_text_captcha(key, db)- response = HTTP::Client.get(TEXTCAPTCHA_URL).body+ response = make_client(TEXTCAPTCHA_URL).get("/[email protected]").bodyresponse = JSON.parse(response)tokens = response["a"].as_a.map do |answer|