Skip to content

Commit

Permalink
fix: Url.location
Browse files Browse the repository at this point in the history
  • Loading branch information
cssxsh committed Jul 7, 2021
1 parent 17f2981 commit c2b62f3
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/main/kotlin/xyz/cssxsh/mirai/plugin/BiliBuilder.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package xyz.cssxsh.mirai.plugin

import io.ktor.client.*
import io.ktor.client.request.*
import io.ktor.http.*
import net.mamoe.mirai.console.permission.PermissionService.Companion.testPermission
Expand Down Expand Up @@ -153,13 +152,15 @@ internal val MediaReplier: MessageReplier = replier@{ result ->
}
}

private fun noRedirect() = HttpClient {
followRedirects = false
expectSuccess = false
private suspend fun Url.location(): String? {
return client.useHttpClient {
it.config {
followRedirects = false
expectSuccess = false
}.head<HttpMessage>(this@location)
}.headers[HttpHeaders.Location]
}

private suspend fun Url.getLocation() = noRedirect().use { it.head<HttpMessage>(this) }.headers[HttpHeaders.Location]

internal val Repliers by lazy {
mapOf(
DYNAMIC_REGEX to DynamicReplier,
Expand All @@ -175,7 +176,7 @@ internal val Repliers by lazy {

internal val ShortLinkReplier: MessageReplier = replier@{ result ->
logger.info { "[${sender}] 匹配SHORT_LINK(${result.value}) 尝试跳转" }
val location = Url("https://b23.tv/${result.value}").getLocation() ?: return@replier null
val location = Url("https://b23.tv/${result.value}").location() ?: return@replier null
for ((regex, replier) in Repliers) {
val new = regex.find(location) ?: continue
return@replier replier(new)
Expand Down

0 comments on commit c2b62f3

Please sign in to comment.