[Grouple] Fix relative page urls

pull/168/head
Koitharu 3 years ago
parent 2340100999
commit 5e3fddb652
No known key found for this signature in database
GPG Key ID: 8E861F8CE6E7CE27

@ -240,8 +240,8 @@ internal abstract class GroupleParser(
val path = parts.last() val path = parts.last()
val servers = parts.dropLast(1).toSet() val servers = parts.dropLast(1).toSet()
val cachedServer = cachedPagesServer val cachedServer = cachedPagesServer
if (cachedServer != null && cachedServer in servers && tryHead(cachedServer + path)) { if (cachedServer != null && cachedServer in servers && tryHead(concatUrl(cachedServer, path))) {
return cachedServer + path return concatUrl(cachedServer, path)
} }
if (servers.isEmpty()) { if (servers.isEmpty()) {
throw ParseException("No servers found for page", page.url) throw ParseException("No servers found for page", page.url)
@ -250,7 +250,8 @@ internal abstract class GroupleParser(
coroutineScope { coroutineScope {
servers.map { server -> servers.map { server ->
async { 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 } }.awaitFirst { it != null }
}.also { }.also {
@ -259,7 +260,7 @@ internal abstract class GroupleParser(
} catch (e: NoSuchElementException) { } catch (e: NoSuchElementException) {
servers.random() servers.random()
} }
return checkNotNull(server) + path return concatUrl(checkNotNull(server), path)
} }
override suspend fun getTags(): Set<MangaTag> { override suspend fun getTags(): Set<MangaTag> {
@ -352,7 +353,7 @@ internal abstract class GroupleParser(
return webClient.httpPost(url, payload) 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) val response = webClient.httpHead(url)
response.isSuccessful && response.headersContentLength() >= MIN_IMAGE_SIZE response.isSuccessful && response.headersContentLength() >= MIN_IMAGE_SIZE
}.getOrDefault(false) }.getOrDefault(false)

@ -67,6 +67,16 @@ fun String.toAbsoluteUrl(domain: String): String = when {
else -> this 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()) { fun DateFormat.tryParse(str: String?): Long = if (str.isNullOrEmpty()) {
assert(false) { "Date string is null or empty" } assert(false) { "Date string is null or empty" }
0L 0L

Loading…
Cancel
Save