diff --git a/src/main/kotlin/org/koitharu/kotatsu/parsers/site/grouple/GroupleParser.kt b/src/main/kotlin/org/koitharu/kotatsu/parsers/site/grouple/GroupleParser.kt index 2808c2aa..4910deea 100644 --- a/src/main/kotlin/org/koitharu/kotatsu/parsers/site/grouple/GroupleParser.kt +++ b/src/main/kotlin/org/koitharu/kotatsu/parsers/site/grouple/GroupleParser.kt @@ -240,8 +240,8 @@ internal abstract class GroupleParser( val path = parts.last() val servers = parts.dropLast(1).toSet() val cachedServer = cachedPagesServer - if (cachedServer != null && cachedServer in servers && tryHead(cachedServer + path)) { - return cachedServer + path + if (cachedServer != null && cachedServer in servers && tryHead(concatUrl(cachedServer, path))) { + return concatUrl(cachedServer, path) } if (servers.isEmpty()) { throw ParseException("No servers found for page", page.url) @@ -250,7 +250,8 @@ internal abstract class GroupleParser( coroutineScope { servers.map { server -> async { - if (tryHead(server + path)) server else null + val host = server.trim().ifEmpty { "https://$domain/" } + if (tryHead(concatUrl(host, path))) host else null } }.awaitFirst { it != null } }.also { @@ -259,7 +260,7 @@ internal abstract class GroupleParser( } catch (e: NoSuchElementException) { servers.random() } - return checkNotNull(server) + path + return concatUrl(checkNotNull(server), path) } override suspend fun getTags(): Set { @@ -352,7 +353,7 @@ internal abstract class GroupleParser( return webClient.httpPost(url, payload) } - suspend fun tryHead(url: String): Boolean = runCatchingCancellable { + private suspend fun tryHead(url: String): Boolean = runCatchingCancellable { val response = webClient.httpHead(url) response.isSuccessful && response.headersContentLength() >= MIN_IMAGE_SIZE }.getOrDefault(false) diff --git a/src/main/kotlin/org/koitharu/kotatsu/parsers/util/Parse.kt b/src/main/kotlin/org/koitharu/kotatsu/parsers/util/Parse.kt index 7c965778..f6c193e1 100644 --- a/src/main/kotlin/org/koitharu/kotatsu/parsers/util/Parse.kt +++ b/src/main/kotlin/org/koitharu/kotatsu/parsers/util/Parse.kt @@ -67,6 +67,16 @@ fun String.toAbsoluteUrl(domain: String): String = when { else -> this } +fun concatUrl(host: String, path: String): String { + val hostWithSlash = host.endsWith('/') + val pathWithSlash = path.startsWith('/') + return when { + hostWithSlash && pathWithSlash -> host + path.drop(1) + !hostWithSlash && !pathWithSlash -> "$host/$path" + else -> host + path + } +} + fun DateFormat.tryParse(str: String?): Long = if (str.isNullOrEmpty()) { assert(false) { "Date string is null or empty" } 0L @@ -78,4 +88,4 @@ fun DateFormat.tryParse(str: String?): Long = if (str.isNullOrEmpty()) { }.getOrDefault(0L) } -private fun Response.requireBody(): ResponseBody = requireNotNull(body) { "Response body is null" } \ No newline at end of file +private fun Response.requireBody(): ResponseBody = requireNotNull(body) { "Response body is null" }