Skip to content

Commit

Permalink
feat: title detector (#180)
Browse files Browse the repository at this point in the history
* feat: title detector

* fix: prefix, nazo line break
  • Loading branch information
yuuahp authored Jul 9, 2024
1 parent 0222d85 commit 0c5e3e9
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/main/kotlin/com/jaoafa/vcspeaker/tts/replacers/UrlReplacer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand Down Expand Up @@ -69,6 +70,10 @@ object UrlReplacer : BaseReplacer {
coerceInputValues = true
})
}

install(HttpTimeout) {
requestTimeoutMillis = 10000
}
}

/**
Expand Down Expand Up @@ -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("</title>")) break
}
}

Expand Down Expand Up @@ -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
}

0 comments on commit 0c5e3e9

Please sign in to comment.