Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

commonize pet bio parsing with ksoup #1603

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotl

kotlinx-serialization-json-okio = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json-okio", version.ref = "kotlinx-serialization" }

ksoup = "com.fleeksoft.ksoup:ksoup-ktor2:0.1.6-alpha1"

ksp = { module = "com.google.devtools.ksp:symbol-processing", version.ref = "ksp" }
ksp-api = { module = "com.google.devtools.ksp:symbol-processing-api", version.ref = "ksp" }
# Only present to trigger automatic renovate updates
Expand Down
1 change: 1 addition & 0 deletions samples/star/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ kotlin {
implementation(libs.coroutines)
implementation(libs.kotlinx.immutable)
implementation(libs.kotlinx.serialization.json.okio)
implementation(libs.ksoup)
implementation(libs.ktor.client)
implementation(libs.ktor.client.contentNegotiation)
implementation(libs.ktor.client.auth)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
package com.slack.circuit.star.data.petfinder

import com.slack.circuit.star.petdetail.PetBioParser
import com.slack.eithernet.ApiResult
import io.ktor.client.HttpClient
import io.ktor.client.request.get
Expand All @@ -13,16 +14,14 @@ interface PetBioParserApi {
suspend fun parseBio(url: String): ApiResult<String, Unit>
}

expect suspend fun parsePetBio(html: String): String

class PetBioParserApiImpl(private val httpClient: HttpClient) : PetBioParserApi {
override suspend fun parseBio(url: String) =
try {
val bio =
withContext(Dispatchers.IO) {
val response = httpClient.get(url)
val html = response.bodyAsText()
parsePetBio(html)
PetBioParser.parse(html)
}
ApiResult.success(bio)
} catch (e: Exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
package com.slack.circuit.star.petdetail

import org.jsoup.Jsoup
import com.fleeksoft.ksoup.Ksoup

/** PetFinder doesn't expose pet bios so we have to do gross things :( */
internal object PetBioParser {
Expand All @@ -11,7 +11,7 @@ internal object PetBioParser {
private fun String.reduceNewlines() = replace(newlines, "\n\n")

internal fun parse(fullBody: String): String {
return Jsoup.parse(fullBody, "https://www.petfinder.com/")
return Ksoup.parse(fullBody, "https://www.petfinder.com/")
.getElementsByClass("card-section")
.first { node -> node.hasAttr("data-test") && node.attr("data-test") == "Pet_Story_Section" }
.child(1)
Expand Down

This file was deleted.