From 4b6ad35ed6498ffc49432506c818affdfae7ebd6 Mon Sep 17 00:00:00 2001 From: Offray Luna Date: Thu, 29 Dec 2022 16:58:56 -0500 Subject: [PATCH] Setting the default instance according to best response times and API availability. --- app/VideoWeb/Invidious.class.st | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/app/VideoWeb/Invidious.class.st b/app/VideoWeb/Invidious.class.st index c4e9120..9925cf7 100644 --- a/app/VideoWeb/Invidious.class.st +++ b/app/VideoWeb/Invidious.class.st @@ -10,12 +10,20 @@ Class { 'api', 'monitor' ], + #classInstVars : [ + 'default' + ], #category : #VideoWeb } +{ #category : #accessing } +Invidious class >> default: anObject [ + default := anObject +] + { #category : #accessing } Invidious class >> defaultInstance [ - ^ self instances select: [ :instance | instance api = true ] + ^ default ifNil: [ default := self responseTimes first key ] ] { #category : #accessing } @@ -27,6 +35,11 @@ Invidious class >> instances [ Invidious new fromDictionary: instanceDict ] ] +{ #category : #accessing } +Invidious class >> instancesWithAPIEnabled [ + ^ self instances select: [ :instance | instance api = true ] +] + { #category : #accessing } Invidious class >> rawInstances [ ^ STON @@ -34,6 +47,22 @@ Invidious class >> rawInstances [ retrieveContents ] +{ #category : #accessing } +Invidious class >> responseTimes [ + | responses | + responses := Dictionary new. + self instancesWithAPIEnabled do: [ :instance | | runTime| + runTime := [ ZnClient new + beOneShot; + url: instance uri; + get; + response ] timeToRun. + responses at: instance put: runTime. + ]. + ^ (responses associations asSortedCollection: [ :x :y| x value < y value ]) + collect: [:assoc | assoc ] +] + { #category : #accessing } Invidious >> api [ ^ api