Skip to content

Commit

Permalink
Merge pull request #306 from skaengus2012/feature/issue305
Browse files Browse the repository at this point in the history
Parsing link only header
  • Loading branch information
skaengus2012 authored Jul 19, 2024
2 parents 183a1f2 + bf5b09d commit b2e2bfd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,26 @@ private fun Element.toContent(): String = attr("content")
class JsoupLinkMetadataRepository(private val dispatcher: CoroutineDispatcher) : LinkMetadataRepository {
override suspend fun get(link: Link): Result<LinkMetadata> = withContext(dispatcher) {
catching {
val tagNameToValues: Map<String, String> = buildMap {
Jsoup.connect(link.value)
.get()
.select("meta[property^=og:]")
.asSequence()
.filter { element -> element.toProperty() in TAGS_REQUIRED }
.forEach { element -> put(element.toProperty(), element.toContent()) }
}

val tagNameToValues = Jsoup.connect(link.value)
.execute()
.streamParser()
.use { parser ->
parser.selectFirst("head")
?.let { parseTagNameToValues(it) }
?: return@use emptyMap()
}
LinkMetadata(
title = tagNameToValues[OG_TITLE] ?: "",
imageUrl = tagNameToValues[OG_IMAGE] ?: ""
)
}
}

private fun parseTagNameToValues(headElement: Element): Map<String, String> = buildMap {
headElement.select("meta[property^=og:]").forEach { element ->
if (element.toProperty() in TAGS_REQUIRED) {
put(element.toProperty(), element.toContent())
}
}
}
}
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ googleHilt = "2.51.1"
googleMaterial = "1.12.0"
jacoco = "0.8.11" # use in configureJacocoToolVersion -> Jacoco.kt
javafaker = "1.0.2"
jsoup = "1.17.2"
jsoup = "1.18.1"
junit = "4.13.2"
kotlin = "1.9.24"
ksp = "1.9.24-1.0.20"
Expand Down

0 comments on commit b2e2bfd

Please sign in to comment.