Skip to content

zeeshanali-k/MetaProbe

Repository files navigation

Maven Central

MetaProbe

A simple Android Kotlin library for fetching link details like Title, Description, Icon and OG Image effortlessly based on Ktor for fetching HTML from provided URL and Jsoup for parsing Meta Data. The usecase of this library could be seen in Social Media and Messaging Apps.

For Kotlin Multiplatform Check MetaProbeKMP

Usage

A sample usage is also provided on code along with the design most social media apps use.

Add following dependency to your module level gradle file and make sure mavenCentral() is added in repositories in your settings.gradle file.

For build.gradle.kts:

implementation("tech.dev-scion:metaprobe:1.0.3")

For build.gradle:

implementation "tech.dev-scion:metaprobe:1.0.3"

You can use this library in Coroutines or via Callback method (without managing any Coroutine or Thread).

With Coroutine Suspend Method:

MetaProbe(url)
            .apply {
                setClient(
                    HttpClient(Android) {
                        engine {
                            connectTimeout = 100_000
                            socketTimeout = 100_000
                        }
                    }
                )
            }
            .probeLink()
            .fold(
                onSuccess = {
                    isLoading.value = false
                    probedData.value = it
                },
                onFailure = {
                    isLoading.value = false
                },
            )

Fold here is just an Convinience Extension Method of Kotlin Result Class, so you can also use MetaProbe without it. For Callback Method Use this code:

MetaProbe(url)
  .probeLink(object : OnMetaDataProbed {
    override fun onMetaDataProbed(pb: Result<ProbedData>) {
        isLoading.value = false
        Log.d(TAG, "onMetaDataProbed: $pb")
        Log.d(TAG, "onMetaDataProbed: ${pb.getOrNull()?.title}")
        Log.d(TAG, "onMetaDataProbed: ${pb.getOrNull()?.icon}")
        Log.d(TAG,"onMetaDataProbed: ${pb.getOrNull()?.description}")
        probedData.value = pb.getOrNull()
      }
  })

You can remove setClient as it is not necessary and only needed when you want to cutomise your HTTPClient, for example for logging etc.

ProbedData is the class returned in result which contains the actual values of Title, Description, Icon and OGImage.