Bato.to: randomize auto mirror better (#9797)

* Bato.to: randomize auto mirror better

* simplify implementation

Co-authored-by: stevenyomi <95685115+stevenyomi@users.noreply.github.com>

---------

Co-authored-by: stevenyomi <95685115+stevenyomi@users.noreply.github.com>
This commit is contained in:
Vetle Ledaal 2025-07-26 20:02:56 +02:00 committed by Draff
parent ffff87d5a0
commit a42f5c0479
Signed by: Draff
GPG Key ID: E8A89F3211677653
2 changed files with 9 additions and 3 deletions

View File

@ -1,7 +1,7 @@
ext {
extName = 'Bato.to'
extClass = '.BatoToFactory'
extVersionCode = 51
extVersionCode = 52
isNsfw = true
}

View File

@ -43,6 +43,7 @@ import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Locale
import kotlin.random.Random
open class BatoTo(
final override val lang: String,
@ -107,14 +108,19 @@ open class BatoTo(
return preferences.getString("${MIRROR_PREF_KEY}_$lang", MIRROR_PREF_DEFAULT_VALUE)
?.takeUnless { it == MIRROR_PREF_DEFAULT_VALUE }
?: let {
/* Semi-sticky mirror:
* - Don't randomize on boot
* - Don't randomize per language
* - Fallback for non-Android platform
*/
val seed = runCatching {
val pm = Injekt.get<Application>().packageManager
pm.getPackageInfo(BuildConfig.APPLICATION_ID, 0).lastUpdateTime
}.getOrElse {
BuildConfig.VERSION_NAME.hashCode().toLong()
}.coerceAtLeast(0)
}
MIRROR_PREF_ENTRY_VALUES[1 + (seed % (MIRROR_PREF_ENTRIES.size - 1)).toInt()]
MIRROR_PREF_ENTRY_VALUES.drop(1).random(Random(seed))
}
}