summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorsyeopite <syeopite@syeopite.dev>2021-03-26 18:35:28 -0700
committersyeopite <syeopite@syeopite.dev>2021-06-19 04:16:17 -0700
commit5b47438b7112d46f7d19e899475e393c5eb0aa52 (patch)
treeaf15ba45f17e7fbe646cfb50b18d096d779d4d8e /src
parentc33ee83d87346d945d0c49447918303d502e291d (diff)
downloadinvidious-5b47438b7112d46f7d19e899475e393c5eb0aa52.tar.gz
invidious-5b47438b7112d46f7d19e899475e393c5eb0aa52.tar.bz2
invidious-5b47438b7112d46f7d19e899475e393c5eb0aa52.zip
Add helper function to fetch random instance
Diffstat (limited to 'src')
-rw-r--r--src/invidious/helpers/utils.cr16
1 files changed, 16 insertions, 0 deletions
diff --git a/src/invidious/helpers/utils.cr b/src/invidious/helpers/utils.cr
index 66ad6961..8c893310 100644
--- a/src/invidious/helpers/utils.cr
+++ b/src/invidious/helpers/utils.cr
@@ -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