Add helper function to fetch random instance

This commit is contained in:
syeopite 2021-03-26 18:35:28 -07:00
parent c33ee83d87
commit 5b47438b71
No known key found for this signature in database
GPG Key ID: 6FA616E5A5294A82

View File

@ -409,3 +409,19 @@ def convert_theme(theme)
theme
end
end
def fetch_random_instance()
instance_list = HTTP::Client.get "https://api.invidious.io/instances.json"
instance_list = JSON.parse(instance_list.body)
filtered_instance_list = [] of String
instance_list.as_a.each do |data|
if data[1]["type"] == "https"
if data[1]["monitor"]
health = data[1]["monitor"].as_h["dailyRatios"][0].as_h["ratio"]
filtered_instance_list << data[0].as_s if health.to_s.to_f > 90
end
end
end
return filtered_instance_list.sample(1)[0]
end