mirror of
https://github.com/msfjarvis/compose-lobsters
synced 2025-08-14 23:27:04 +05:30
refactor(android): move link metadata extraction to Unfurl
Crux is doing major changes for no good reason
This commit is contained in:
parent
37f42dc107
commit
0359860b82
7 changed files with 41 additions and 43 deletions
|
@ -71,7 +71,6 @@ dependencies {
|
|||
implementation(libs.androidx.work.runtime)
|
||||
implementation(libs.coil)
|
||||
implementation(libs.copydown)
|
||||
implementation(libs.crux)
|
||||
implementation(libs.dagger)
|
||||
implementation(libs.jsoup)
|
||||
implementation(libs.kotlinx.collections.immutable)
|
||||
|
@ -79,6 +78,7 @@ dependencies {
|
|||
implementation(libs.napier)
|
||||
implementation(libs.soloader)
|
||||
implementation(libs.sqldelight.extensions.coroutines)
|
||||
implementation(libs.unfurl)
|
||||
implementation(projects.api)
|
||||
implementation(projects.common)
|
||||
implementation(projects.core)
|
||||
|
|
|
@ -1,25 +1,44 @@
|
|||
/*
|
||||
* Copyright © 2021-2022 Harsh Shandilya.
|
||||
* Copyright © 2021-2023 Harsh Shandilya.
|
||||
* Use of this source code is governed by an MIT-style
|
||||
* license that can be found in the LICENSE file or at
|
||||
* https://opensource.org/licenses/MIT.
|
||||
*/
|
||||
package dev.msfjarvis.claw.android.injection
|
||||
|
||||
import com.chimbori.crux.Crux
|
||||
import com.deliveryhero.whetstone.app.ApplicationScope
|
||||
import com.squareup.anvil.annotations.ContributesTo
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import io.github.aakira.napier.Napier
|
||||
import me.saket.unfurl.UnfurlLogger
|
||||
import me.saket.unfurl.Unfurler
|
||||
import okhttp3.OkHttpClient
|
||||
|
||||
@Module
|
||||
@ContributesTo(ApplicationScope::class)
|
||||
object MetadataExtractorModule {
|
||||
@Provides
|
||||
fun provideCrux(
|
||||
fun provideUnfurlLogger(): UnfurlLogger {
|
||||
return object : UnfurlLogger {
|
||||
override fun log(message: String) {
|
||||
Napier.d { message }
|
||||
}
|
||||
|
||||
override fun log(e: Throwable, message: String) {
|
||||
Napier.e(e) { message }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Provides
|
||||
fun provideUnfurler(
|
||||
okHttpClient: OkHttpClient,
|
||||
): Crux {
|
||||
return Crux(okHttpClient = okHttpClient)
|
||||
logger: UnfurlLogger,
|
||||
): Unfurler {
|
||||
return Unfurler(
|
||||
httpClient = okHttpClient,
|
||||
logger = logger,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,44 +6,21 @@
|
|||
*/
|
||||
package dev.msfjarvis.claw.android.viewmodel
|
||||
|
||||
import com.chimbori.crux.Crux
|
||||
import com.chimbori.crux.api.Fields
|
||||
import dev.msfjarvis.claw.core.injection.IODispatcher
|
||||
import dev.msfjarvis.claw.model.LinkMetadata
|
||||
import javax.inject.Inject
|
||||
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Request
|
||||
import org.jsoup.Jsoup
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
import kotlinx.coroutines.withContext
|
||||
import me.saket.unfurl.Unfurler
|
||||
|
||||
class LinkMetadataRepository
|
||||
@Inject
|
||||
constructor(
|
||||
private val crux: Crux,
|
||||
private val okHttpClient: OkHttpClient,
|
||||
private val unfurler: Unfurler,
|
||||
@IODispatcher private val dispatcher: CoroutineDispatcher,
|
||||
) {
|
||||
suspend fun getLinkMetadata(url: String): LinkMetadata {
|
||||
return run {
|
||||
val parsedUrl = url.toHttpUrlOrNull() ?: return@run null
|
||||
if (!parsedUrl.isHttps) return@run null
|
||||
val request = Request.Builder().url(parsedUrl).build()
|
||||
val htmlContent =
|
||||
okHttpClient.newCall(request).execute().use { response ->
|
||||
val body = response.body ?: return@run null
|
||||
body.string()
|
||||
}
|
||||
val extractedMetadata = crux.extractFrom(parsedUrl, Jsoup.parse(htmlContent, url))
|
||||
val faviconUrl = extractedMetadata[Fields.FAVICON_URL].toString()
|
||||
val readingTime = extractedMetadata[Fields.DURATION_MS].toString()
|
||||
LinkMetadata(
|
||||
url = url,
|
||||
faviconUrl = faviconUrl,
|
||||
readingTime = readingTime,
|
||||
)
|
||||
}
|
||||
?: LinkMetadata(
|
||||
url = url,
|
||||
faviconUrl = null,
|
||||
readingTime = null,
|
||||
)
|
||||
val result = withContext(dispatcher) { unfurler.unfurl(url) }
|
||||
return LinkMetadata(url, result?.favicon?.toString())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue