mirror of
https://github.com/msfjarvis/compose-lobsters.git
synced 2024-06-02 20:18:12 +05:30
refactor: move metadata-extractor
to android
This commit is contained in:
parent
9fdd72220f
commit
47a398f518
|
@ -30,13 +30,11 @@ graph LR
|
|||
android --> common
|
||||
android --> core
|
||||
android --> database
|
||||
android --> metadata-extractor
|
||||
android --> model
|
||||
api --> model
|
||||
common --> core
|
||||
common --> database
|
||||
common --> model
|
||||
metadata-extractor --> model
|
||||
|
||||
```
|
||||
## License
|
||||
|
|
|
@ -71,6 +71,7 @@ dependencies {
|
|||
implementation(libs.androidx.work.runtime.ktx)
|
||||
implementation(libs.coil)
|
||||
implementation(libs.copydown)
|
||||
implementation(libs.crux)
|
||||
implementation(libs.dagger)
|
||||
implementation(libs.jsoup)
|
||||
implementation(libs.kotlinx.collections.immutable)
|
||||
|
@ -81,7 +82,6 @@ dependencies {
|
|||
implementation(projects.common)
|
||||
implementation(projects.core)
|
||||
implementation(projects.database)
|
||||
implementation(projects.metadataExtractor)
|
||||
implementation(projects.model)
|
||||
|
||||
kapt(libs.dagger.compiler)
|
||||
|
|
|
@ -1,21 +1,52 @@
|
|||
/*
|
||||
* Copyright © 2022 Harsh Shandilya.
|
||||
* Copyright © 2022-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.viewmodel
|
||||
|
||||
import dev.msfjarvis.claw.metadata.MetadataExtractor
|
||||
import com.chimbori.crux.Crux
|
||||
import com.chimbori.crux.api.Fields
|
||||
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
|
||||
|
||||
class LinkMetadataRepository
|
||||
@Inject
|
||||
constructor(
|
||||
private val metadataExtractor: MetadataExtractor,
|
||||
private val crux: Crux,
|
||||
private val okHttpClient: OkHttpClient,
|
||||
) {
|
||||
suspend fun getLinkMetadata(url: String): LinkMetadata {
|
||||
return metadataExtractor.getExtractedMetadata(url)
|
||||
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,
|
||||
)
|
||||
}
|
||||
?: makeDefault(url)
|
||||
}
|
||||
|
||||
private fun makeDefault(url: String) =
|
||||
LinkMetadata(
|
||||
url,
|
||||
null,
|
||||
null,
|
||||
)
|
||||
}
|
||||
|
|
|
@ -1,17 +0,0 @@
|
|||
/*
|
||||
* Copyright © 2022-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.
|
||||
*/
|
||||
plugins { id("dev.msfjarvis.claw.kotlin-jvm") }
|
||||
|
||||
dependencies {
|
||||
api(libs.crux)
|
||||
api(libs.javax.inject)
|
||||
api(libs.okhttp.core)
|
||||
api(projects.model)
|
||||
|
||||
implementation(platform(libs.okhttp.bom))
|
||||
implementation(libs.jsoup)
|
||||
}
|
|
@ -1,4 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<issues format="6" by="lint 8.0.1" type="baseline" client="gradle" dependencies="false" name="AGP (8.0.1)" variant="all" version="8.0.1">
|
||||
|
||||
</issues>
|
|
@ -1,54 +0,0 @@
|
|||
/*
|
||||
* Copyright © 2022 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.metadata
|
||||
|
||||
import com.chimbori.crux.Crux
|
||||
import com.chimbori.crux.api.Fields.DURATION_MS
|
||||
import com.chimbori.crux.api.Fields.FAVICON_URL
|
||||
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
|
||||
|
||||
class MetadataExtractor
|
||||
@Inject
|
||||
constructor(
|
||||
private val crux: Crux,
|
||||
private val okHttpClient: OkHttpClient,
|
||||
) {
|
||||
|
||||
suspend fun getExtractedMetadata(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[FAVICON_URL].toString()
|
||||
val readingTime = extractedMetadata[DURATION_MS].toString()
|
||||
LinkMetadata(
|
||||
url = url,
|
||||
faviconUrl = faviconUrl,
|
||||
readingTime = readingTime,
|
||||
)
|
||||
}
|
||||
?: makeDefault(url)
|
||||
}
|
||||
|
||||
private fun makeDefault(url: String) =
|
||||
LinkMetadata(
|
||||
url,
|
||||
null,
|
||||
null,
|
||||
)
|
||||
}
|
|
@ -155,6 +155,5 @@ include(
|
|||
"common",
|
||||
"core",
|
||||
"database",
|
||||
"metadata-extractor",
|
||||
"model",
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue
Block a user