Skip to content
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
6 changes: 6 additions & 0 deletions OGParser/src/main/java/com/kedia/ogparser/CacheProvider.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.kedia.ogparser

interface CacheProvider {
suspend fun getOpenGraphResult(url: String): OpenGraphResult?
suspend fun setOpenGraphResult(openGraphResult: OpenGraphResult, url: String)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import android.content.SharedPreferences
import android.util.Log
import androidx.preference.PreferenceManager

class SharedPrefs(context: Context) {
class OpenGraphCacheProvider(context: Context) : CacheProvider {

private val pm: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)

Expand Down Expand Up @@ -65,7 +65,16 @@ class SharedPrefs(context: Context) {
return pm.getString(TYPE + "_$link", "") ?: ""
}

fun setOpenGraphResult(openGraphResult: OpenGraphResult, url: String) {
private fun urlExists(title: String, description: String, image: String): Boolean {
return title.isNotEmpty() &&
title.equals("null").not() &&
description.isNotEmpty() &&
description.equals("null").not() &&
image.isNotEmpty() &&
image.equals("null").not()
}

override suspend fun setOpenGraphResult(openGraphResult: OpenGraphResult, url: String) {
setTitle(url, openGraphResult.title.toString())
setDescription(url, openGraphResult.description.toString())
setImage(url, openGraphResult.image.toString())
Expand All @@ -74,26 +83,18 @@ class SharedPrefs(context: Context) {
setUrl(url, openGraphResult.url.toString())
}

fun getOpenGraphResult(url: String): OpenGraphResult {
override suspend fun getOpenGraphResult(url: String): OpenGraphResult? {
val title = getTitle(url)
val description = getDescription(url)
val image = getImage(url)

if (!urlExists(title, description, image)) {
return null
}

val siteName = getSiteName(url)
val type = getType(url)
val url = getUrl(url)
return OpenGraphResult(title, description, url, image, siteName, type)
}

fun urlExists(url: String): Boolean {
val title = getTitle(url)
val description = getDescription(url)
val image = getImage(url)
return title.isNotEmpty() &&
title.equals("null").not() &&
description.isNotEmpty() &&
description.equals("null").not() &&
image.isNotEmpty() &&
image.equals("null").not()
}

}
}
16 changes: 7 additions & 9 deletions OGParser/src/main/java/com/kedia/ogparser/OpenGraphParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@ import kotlin.coroutines.CoroutineContext
class OpenGraphParser(
private val listener: OpenGraphCallback,
private var showNullOnEmpty: Boolean = false,
context: Context? = null
private val cacheProvider: CacheProvider? = null
) {

private val sharedPrefs: SharedPrefs? = context?.let { SharedPrefs(it) }

private var url: String = ""

private val AGENTS = mutableListOf(
Expand Down Expand Up @@ -52,15 +49,16 @@ class OpenGraphParser(
if (!url.contains("http")) {
url = "http://$url"
}
if (sharedPrefs?.urlExists(url) == true) {
return@withContext sharedPrefs?.getOpenGraphResult(url)

cacheProvider?.getOpenGraphResult(url)?.let {
return@withContext it
}

AGENTS.forEach {
openGraphResult = jsoupNetworkCall.callUrl(url, it)
val isResultNull = checkNullParserResult(openGraphResult)
if (!isResultNull) {
openGraphResult?.let { sharedPrefs?.setOpenGraphResult(it, url) }
openGraphResult?.let { cacheProvider?.setOpenGraphResult(it, url) }
return@withContext openGraphResult
}
}
Expand All @@ -71,7 +69,7 @@ class OpenGraphParser(
}
return@withContext null
}
openGraphResult?.let { sharedPrefs?.setOpenGraphResult(it, url) }
openGraphResult?.let { cacheProvider?.setOpenGraphResult(it, url) }
return@withContext openGraphResult
}
}
}
11 changes: 9 additions & 2 deletions app/src/main/java/com/kedia/opengraphpreview/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@ import androidx.appcompat.app.AppCompatActivity
import com.kedia.ogparser.OpenGraphCallback
import com.kedia.ogparser.OpenGraphParser
import com.kedia.ogparser.OpenGraphResult
import com.kedia.ogparser.OpenGraphCacheProvider
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity(), OpenGraphCallback {

private val openGraphParser by lazy { OpenGraphParser(this, showNullOnEmpty = true) }
private val openGraphParser by lazy {
OpenGraphParser(
listener = this,
showNullOnEmpty = true,
cacheProvider = OpenGraphCacheProvider(this)
)
}

private val LINKS_TO_TEST = mutableListOf(
"https://www.linkedin.com/posts/madhusmita-padhy_machinelearning-datascience-activity-6886390508722163712-yhQ0",
Expand Down Expand Up @@ -63,4 +70,4 @@ class MainActivity : AppCompatActivity(), OpenGraphCallback {
LINKS_TO_TEST.removeFirstOrNull()
}
}
}
}