From d937c7e6abb13101855e0e2655f392890f1a28b7 Mon Sep 17 00:00:00 2001 From: Koitharu Date: Tue, 20 Aug 2024 17:17:46 +0300 Subject: [PATCH] [ExHentai] Fix IP ban detection --- .../parsers/exception/TooManyRequestExceptions.kt | 11 ++++++++++- .../kotatsu/parsers/site/all/ExHentaiParser.kt | 12 ++++++------ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/main/kotlin/org/koitharu/kotatsu/parsers/exception/TooManyRequestExceptions.kt b/src/main/kotlin/org/koitharu/kotatsu/parsers/exception/TooManyRequestExceptions.kt index d0650b48..6ec98577 100644 --- a/src/main/kotlin/org/koitharu/kotatsu/parsers/exception/TooManyRequestExceptions.kt +++ b/src/main/kotlin/org/koitharu/kotatsu/parsers/exception/TooManyRequestExceptions.kt @@ -6,7 +6,16 @@ import java.time.Instant class TooManyRequestExceptions( val url: String, val retryAfter: Long, -) : IOException() { +) : IOException( + buildString { + append("Too man requests") + if (retryAfter > 0) { + append(", retry after ") + append(retryAfter) + append("ms") + } + }, +) { val retryAt: Instant? get() = if (retryAfter > 0 && retryAfter < Long.MAX_VALUE) { diff --git a/src/main/kotlin/org/koitharu/kotatsu/parsers/site/all/ExHentaiParser.kt b/src/main/kotlin/org/koitharu/kotatsu/parsers/site/all/ExHentaiParser.kt index 1c26b2f6..a2043e4b 100644 --- a/src/main/kotlin/org/koitharu/kotatsu/parsers/site/all/ExHentaiParser.kt +++ b/src/main/kotlin/org/koitharu/kotatsu/parsers/site/all/ExHentaiParser.kt @@ -321,14 +321,14 @@ internal class ExHentaiParser( if (response.headersContentLength() <= 256) { val text = response.peekBody(256).string() if (text.startsWith("Your IP address has been temporarily banned")) { - @Language("RegExp") - val regex = kotlin.text.Regex("ban expires in ([0-9]+) minutes? and ([0-9]+) seconds?") - val groups = regex.find(text)?.groupValues ?: return response - val minutes = groups.getOrNull(1)?.toLongOrNull() ?: 0L - val seconds = groups.getOrNull(2)?.toLongOrNull() ?: 0L + val hours = Regex("([0-9]+) hours?").find(text)?.groupValues?.getOrNull(1)?.toLongOrNull() ?: 0 + val minutes = Regex("([0-9]+) minutes?").find(text)?.groupValues?.getOrNull(1)?.toLongOrNull() ?: 0 + val seconds = Regex("([0-9]+) seconds?").find(text)?.groupValues?.getOrNull(1)?.toLongOrNull() ?: 0 throw TooManyRequestExceptions( url = response.request.url.toString(), - retryAfter = TimeUnit.MINUTES.toMillis(minutes) + TimeUnit.SECONDS.toMillis(seconds), + retryAfter = TimeUnit.HOURS.toMillis(hours) + + TimeUnit.MINUTES.toMillis(minutes) + + TimeUnit.SECONDS.toMillis(seconds), ) } }