Removes dependency on okhttp for the HTMLParser

This commit is contained in:
Vitor Pamplona
2026-01-07 12:22:57 -05:00
parent 0e59ea570e
commit cf174c7734
2 changed files with 3 additions and 4 deletions
@@ -24,7 +24,6 @@ import com.vitorpamplona.amethyst.commons.preview.MetaTag
import com.vitorpamplona.amethyst.commons.preview.MetaTagsParser
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import okhttp3.MediaType
import okio.BufferedSource
import okio.ByteString.Companion.decodeHex
import okio.Options
@@ -56,11 +55,11 @@ class HtmlParser {
suspend fun parseHtml(
source: BufferedSource,
type: MediaType,
type: Charset?,
): Sequence<MetaTag> =
withContext(Dispatchers.IO) {
// sniff charset from Content-Type header or BOM
val sniffedCharset = type.charset() ?: source.readBomAsCharset()
val sniffedCharset = type ?: source.readBomAsCharset()
if (sniffedCharset != null) {
val content = source.readByteArray().toString(sniffedCharset)
return@withContext MetaTagsParser.parse(content)
@@ -62,7 +62,7 @@ class UrlPreview {
response.headers["Content-Type"]?.toMediaType()
?: throw IllegalArgumentException("Website returned unknown mimetype: ${response.headers["Content-Type"]}")
if (mimeType.type == "text" && mimeType.subtype == "html") {
val metaTags = HtmlParser().parseHtml(response.body.source(), mimeType)
val metaTags = HtmlParser().parseHtml(response.body.source(), mimeType.charset())
val data = OpenGraphParser().extractUrlInfo(metaTags)
UrlInfoItem(url, data.title, data.description, data.image, mimeType.toString())
} else if (mimeType.type == "image") {