Removes dependency on okhttp for the HTMLParser
This commit is contained in:
@@ -24,7 +24,6 @@ import com.vitorpamplona.amethyst.commons.preview.MetaTag
|
|||||||
import com.vitorpamplona.amethyst.commons.preview.MetaTagsParser
|
import com.vitorpamplona.amethyst.commons.preview.MetaTagsParser
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import okhttp3.MediaType
|
|
||||||
import okio.BufferedSource
|
import okio.BufferedSource
|
||||||
import okio.ByteString.Companion.decodeHex
|
import okio.ByteString.Companion.decodeHex
|
||||||
import okio.Options
|
import okio.Options
|
||||||
@@ -56,11 +55,11 @@ class HtmlParser {
|
|||||||
|
|
||||||
suspend fun parseHtml(
|
suspend fun parseHtml(
|
||||||
source: BufferedSource,
|
source: BufferedSource,
|
||||||
type: MediaType,
|
type: Charset?,
|
||||||
): Sequence<MetaTag> =
|
): Sequence<MetaTag> =
|
||||||
withContext(Dispatchers.IO) {
|
withContext(Dispatchers.IO) {
|
||||||
// sniff charset from Content-Type header or BOM
|
// sniff charset from Content-Type header or BOM
|
||||||
val sniffedCharset = type.charset() ?: source.readBomAsCharset()
|
val sniffedCharset = type ?: source.readBomAsCharset()
|
||||||
if (sniffedCharset != null) {
|
if (sniffedCharset != null) {
|
||||||
val content = source.readByteArray().toString(sniffedCharset)
|
val content = source.readByteArray().toString(sniffedCharset)
|
||||||
return@withContext MetaTagsParser.parse(content)
|
return@withContext MetaTagsParser.parse(content)
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ class UrlPreview {
|
|||||||
response.headers["Content-Type"]?.toMediaType()
|
response.headers["Content-Type"]?.toMediaType()
|
||||||
?: throw IllegalArgumentException("Website returned unknown mimetype: ${response.headers["Content-Type"]}")
|
?: throw IllegalArgumentException("Website returned unknown mimetype: ${response.headers["Content-Type"]}")
|
||||||
if (mimeType.type == "text" && mimeType.subtype == "html") {
|
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)
|
val data = OpenGraphParser().extractUrlInfo(metaTags)
|
||||||
UrlInfoItem(url, data.title, data.description, data.image, mimeType.toString())
|
UrlInfoItem(url, data.title, data.description, data.image, mimeType.toString())
|
||||||
} else if (mimeType.type == "image") {
|
} else if (mimeType.type == "image") {
|
||||||
|
|||||||
Reference in New Issue
Block a user