From c79c61ed4e15bd94cb937fa0837d56c93bcb167c Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Sun, 2 Aug 2020 22:09:13 +1200 Subject: [PATCH] Randomise instance order in list --- build/index.html | 6 +++--- build/static/js/main.js | 36 ++++++++++++++++++++++++++---------- package.json | 2 +- src/index.pug | 12 ++++++++---- src/main.js | 36 ++++++++++++++++++++++++++---------- 5 files changed, 64 insertions(+), 28 deletions(-) diff --git a/build/index.html b/build/index.html index 5738ee8..6788ba3 100644 --- a/build/index.html +++ b/build/index.html @@ -1,9 +1,9 @@ -Select instance - Invidious

As of September 1st 2020, invidio.us has closed down.

To see this content, please select another instance, -or watch directly on YouTube.

DomainHealthAction
Loading instances...
\ No newline at end of file +

DomainHealthAction
Loading instances...
\ No newline at end of file diff --git a/build/static/js/main.js b/build/static/js/main.js index c7f95e7..1a00975 100644 --- a/build/static/js/main.js +++ b/build/static/js/main.js @@ -15,24 +15,40 @@ return e } + function shuffle(array) { + for (let i = 0; i < array.length; i++) { + let j = Math.floor(Math.random() * (array.length-i)) + i + ;[array[i], array[j]] = [array[j], array[i]] + } + return array + } + const destinationPath = window.location.href.slice(window.location.origin.length) q("#watch-on-youtube").href = "https://www.youtube.com" + destinationPath - fetch("https://instances.invidio.us/instances.json?pretty=1&sort_by=type,health").then(res => res.json()).then( + fetch("https://instances.invidio.us/instances.json?sort_by=type,health").then(res => res.json()).then( /** @param {[string, {monitor: any, flag: string, region: string, stats: any, type: string, uri: string}][]} root */ root => { - console.log(root) - root.filter(entry => entry[1].type === "https").forEach(entry => { - let healthUnknown = "health-unknown " - let health = "(unknown)" - if (entry[1].monitor && entry[1].monitor.dailyRatios && entry[1].monitor.dailyRatios[0]) { - health = entry[1].monitor.dailyRatios[0].ratio - healthUnknown = "" + shuffle(root) + root.map(entry => { + const healthKnown = !!entry[1].monitor + return { + name: entry[0], + details: entry[1], + health: +(healthKnown ? entry[1].monitor.dailyRatios[0].ratio : 99), + healthKnown } - let target = entry[1].uri.replace(/\/*$/, "") + destinationPath + }).filter(entry => { + return entry.details.type === "https" + }).sort((a, b) => { + return b.health - a.health + }).forEach(entry => { + let target = entry.details.uri.replace(/\/*$/, "") + destinationPath + const healthUnknown = entry.healthKnown ? "" : "health-unknown " + const health = entry.healthKnown ? entry.health.toFixed(0) : "(unknown)" q("#instances-tbody").appendChild( createElement("tr", {}, [ - createElement("td", {textContent: entry[0]}), + createElement("td", {textContent: entry.name}), createElement("td", {className: "column-center "+healthUnknown, textContent: health}), createElement("td", {className: "column-center"}, [ createElement("a", {href: target, textContent: "Go →"}) diff --git a/package.json b/package.json index 9a7a676..1967d87 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "build.js", "scripts": { "build": "node src/build.js", - "watch": "fish -c 'while true; inotifywait (find src -type f) -e close_write -q; npm run build; end'" + "watch": "fish -c 'while true; npm run build; inotifywait (find src -type f) -e close_write -q; end'" }, "keywords": [], "author": "", diff --git a/src/index.pug b/src/index.pug index f8257ff..e6a5d49 100644 --- a/src/index.pug +++ b/src/index.pug @@ -35,7 +35,7 @@ html(lang="en") .requires-scripts p. To see this content, please select another instance, - or #[a#watch-on-youtube watch directly on YouTube.] + or #[a#watch-on-youtube visit directly on YouTube.] noscript p. To see this content, please select another instance, @@ -58,11 +58,15 @@ html(lang="en") - const instances = [ "invidious.snopyta.org", - "yewtu.be", - "watch.nettohikari.com", + "invidious.ggc-project.de", "invidious.13ad.de", + "yewtu.be", + "invidious.fdn.fr", + "invidious.toot.koeln", + "invidiou.site", + "yt.iswleuven.be", "vid.mint.lgbt", - "invidiou.site" + "watch.nettohikari.com" ] for instance in instances li: a(href=`https://${instance}`)= instance diff --git a/src/main.js b/src/main.js index c7f95e7..1a00975 100644 --- a/src/main.js +++ b/src/main.js @@ -15,24 +15,40 @@ return e } + function shuffle(array) { + for (let i = 0; i < array.length; i++) { + let j = Math.floor(Math.random() * (array.length-i)) + i + ;[array[i], array[j]] = [array[j], array[i]] + } + return array + } + const destinationPath = window.location.href.slice(window.location.origin.length) q("#watch-on-youtube").href = "https://www.youtube.com" + destinationPath - fetch("https://instances.invidio.us/instances.json?pretty=1&sort_by=type,health").then(res => res.json()).then( + fetch("https://instances.invidio.us/instances.json?sort_by=type,health").then(res => res.json()).then( /** @param {[string, {monitor: any, flag: string, region: string, stats: any, type: string, uri: string}][]} root */ root => { - console.log(root) - root.filter(entry => entry[1].type === "https").forEach(entry => { - let healthUnknown = "health-unknown " - let health = "(unknown)" - if (entry[1].monitor && entry[1].monitor.dailyRatios && entry[1].monitor.dailyRatios[0]) { - health = entry[1].monitor.dailyRatios[0].ratio - healthUnknown = "" + shuffle(root) + root.map(entry => { + const healthKnown = !!entry[1].monitor + return { + name: entry[0], + details: entry[1], + health: +(healthKnown ? entry[1].monitor.dailyRatios[0].ratio : 99), + healthKnown } - let target = entry[1].uri.replace(/\/*$/, "") + destinationPath + }).filter(entry => { + return entry.details.type === "https" + }).sort((a, b) => { + return b.health - a.health + }).forEach(entry => { + let target = entry.details.uri.replace(/\/*$/, "") + destinationPath + const healthUnknown = entry.healthKnown ? "" : "health-unknown " + const health = entry.healthKnown ? entry.health.toFixed(0) : "(unknown)" q("#instances-tbody").appendChild( createElement("tr", {}, [ - createElement("td", {textContent: entry[0]}), + createElement("td", {textContent: entry.name}), createElement("td", {className: "column-center "+healthUnknown, textContent: health}), createElement("td", {className: "column-center"}, [ createElement("a", {href: target, textContent: "Go →"})