From eb911de92842ecfdb2005822dbaa94d82855f191 Mon Sep 17 00:00:00 2001 From: syeopite Date: Mon, 14 Jun 2021 02:31:51 -0700 Subject: [PATCH] Handle if inst. api is down for rand inst fetch --- src/invidious/helpers/utils.cr | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/invidious/helpers/utils.cr b/src/invidious/helpers/utils.cr index 72cb1b1d..32359e9a 100644 --- a/src/invidious/helpers/utils.cr +++ b/src/invidious/helpers/utils.cr @@ -411,12 +411,22 @@ def convert_theme(theme) end def fetch_random_instance - instance_list = HTTP::Client.get "https://api.invidious.io/instances.json" - instance_list = JSON.parse(instance_list.body) + begin + instance_api_client = HTTP::Client.new("api.invidious.io") + + # Timeouts + instance_api_client.connect_timeout = 10.seconds + instance_api_client.dns_timeout = 10.seconds + + instance_list = JSON.parse(instance_api_client.get("/instances.json").body).as_a + instance_api_client.close + rescue Socket::ConnectError | IO::TimeoutError | JSON::ParseException + instance_list = [] of JSON::Any + end filtered_instance_list = [] of String - instance_list.as_a.each do |data| + instance_list.each do |data| # TODO Check if current URL is onion instance and use .onion types if so. if data[1]["type"] == "https" # Instances can have statisitics disabled, which is an requirement of version validation.