better header fetching

This commit is contained in:
Josef František Straka 2025-04-09 19:40:23 +02:00
parent 21f6e43e68
commit 3657c77c58

View file

@ -32,6 +32,7 @@ import java.io.IOException
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
import java.util.Date import java.util.Date
import java.util.Locale import java.util.Locale
import java.util.concurrent.locks.ReentrantLock
@Suppress("unused") @Suppress("unused")
class AniPlay : AniListAnimeHttpSource(), ConfigurableAnimeSource { class AniPlay : AniListAnimeHttpSource(), ConfigurableAnimeSource {
@ -381,14 +382,16 @@ class AniPlay : AniListAnimeHttpSource(), ConfigurableAnimeSource {
return if (bracketCount == 0) input.substring(startIndex - 1, endIndex) else null return if (bracketCount == 0) input.substring(startIndex - 1, endIndex) else null
} }
private val headerFetchLock = ReentrantLock()
private var lastHeaderFetch = 0L private var lastHeaderFetch = 0L
private fun fetchHeaders() { private fun fetchHeaders() {
val internalFetchHeaders = internalFetchHeaders@{
val currentTimestamp = Date().time val currentTimestamp = Date().time
val timeout = lastHeaderFetch + (HEADERS_TIMEOUT_MINUTES * 60 * 1000) val timeout = lastHeaderFetch + (HEADERS_TIMEOUT_MINUTES * 60 * 1000)
// check only after 15 minutes // check only after 15 minutes
if (timeout > currentTimestamp) { if (timeout > currentTimestamp) {
Log.i("AniPlay", "Skipping header update. $timeout > $currentTimestamp (${timeout - currentTimestamp}).") Log.i("AniPlay", "Skipping header update. $timeout > $currentTimestamp (${timeout - currentTimestamp}).")
return return@internalFetchHeaders
} }
val baseUrl = Base64.decode("aHR0cHM6Ly9qb3NlZmZzdHJha2EuZ2l0aHViLmlvL2FuaXBsYXktaGVhZGVycy8=", Base64.DEFAULT).toString(Charsets.UTF_8) val baseUrl = Base64.decode("aHR0cHM6Ly9qb3NlZmZzdHJha2EuZ2l0aHViLmlvL2FuaXBsYXktaGVhZGVycy8=", Base64.DEFAULT).toString(Charsets.UTF_8)
@ -404,12 +407,20 @@ class AniPlay : AniListAnimeHttpSource(), ConfigurableAnimeSource {
Log.i("AniPlay", "Fetched headers($preferredDomain): $domainHeaders") Log.i("AniPlay", "Fetched headers($preferredDomain): $domainHeaders")
} catch (e: Exception) { } catch (e: Exception) {
Log.e("AniPlay", "Failed to fetch new headers: \"e\"") Log.e("AniPlay", "Failed to fetch new headers: \"e\"")
return return@internalFetchHeaders
} }
lastHeaderFetch = Date().time lastHeaderFetch = Date().time
} }
headerFetchLock.lock()
try {
internalFetchHeaders()
} finally {
headerFetchLock.unlock()
}
}
private fun getHeaderValue(serverHost: String, key: String): String { private fun getHeaderValue(serverHost: String, key: String): String {
fetchHeaders() fetchHeaders()
try { try {