From 0c5e3e977e73b6135592db0676cb93b86f94e065 Mon Sep 17 00:00:00 2001 From: yuuaHP Date: Tue, 9 Jul 2024 21:30:06 +0900 Subject: [PATCH] feat: title detector (#180) * feat: title detector * fix: prefix, nazo line break --- .../vcspeaker/tts/replacers/UrlReplacer.kt | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/main/kotlin/com/jaoafa/vcspeaker/tts/replacers/UrlReplacer.kt b/src/main/kotlin/com/jaoafa/vcspeaker/tts/replacers/UrlReplacer.kt index 2f4e10b5..5267f655 100644 --- a/src/main/kotlin/com/jaoafa/vcspeaker/tts/replacers/UrlReplacer.kt +++ b/src/main/kotlin/com/jaoafa/vcspeaker/tts/replacers/UrlReplacer.kt @@ -19,6 +19,7 @@ import dev.kord.core.entity.channel.thread.ThreadChannel import io.ktor.client.* import io.ktor.client.call.* import io.ktor.client.engine.cio.* +import io.ktor.client.plugins.* import io.ktor.client.plugins.contentnegotiation.* import io.ktor.client.request.* import io.ktor.http.* @@ -69,6 +70,10 @@ object UrlReplacer : BaseReplacer { coerceInputValues = true }) } + + install(HttpTimeout) { + requestTimeoutMillis = 10000 + } } /** @@ -337,16 +342,22 @@ object UrlReplacer : BaseReplacer { */ private suspend fun getPageTitle(url: String): String? { var byteArray = ByteArray(0) + val byteLimit = 1024 * 2 client.prepareGet(url).execute { val channel: ByteReadChannel = it.body() - if (!channel.isClosedForRead) { - val packet = channel.readRemaining(1024 * 2) - while (!packet.isEmpty) { - val bytes = packet.readBytes() - byteArray += bytes - } + while (!channel.isClosedForRead) { + val bytes = channel.readRemaining(16).readBytes() + + byteArray += bytes + + if (byteArray.size > byteLimit) break + + val raw = String(byteArray) + + if (!raw.startsWith("<")) break + if (raw.contains("")) break } } @@ -652,5 +663,6 @@ object UrlReplacer : BaseReplacer { /** * 文字列を指定した長さに短縮します。短縮後、末尾に「以下略」を付けます。 */ - private fun String.shorten(length: Int) = if (this.length > length) substringByCodePoints(0, length) + " 以下略" else this + private fun String.shorten(length: Int) = + if (this.length > length) substringByCodePoints(0, length) + " 以下略" else this } \ No newline at end of file