forked from pknotfound/OpenGraphParser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenGraphCacheProvider.kt
More file actions
103 lines (81 loc) · 3.36 KB
/
OpenGraphCacheProvider.kt
File metadata and controls
103 lines (81 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package com.kedia.ogparser
import android.content.Context
import android.content.SharedPreferences
import android.util.Log
import androidx.preference.PreferenceManager
class OpenGraphCacheProvider(context: Context) : CacheProvider {
private val pm: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
private fun setTitle(link: String, title: String) {
pm.edit().putString(TITLE + "_$link", title).apply()
}
private fun getTitle(link: String): String {
return pm.getString(TITLE + "_$link", "") ?: ""
}
private fun setDescription(link: String, description: String) {
pm.edit().putString(DESCRIPTION + "_$link", description).apply()
}
private fun getDescription(link: String): String {
return pm.getString(DESCRIPTION + "_$link", "") ?: ""
}
private fun setUrl(link: String, url: String) {
pm.edit().putString(URL + "_$link", url).apply()
}
private fun getUrl(link: String): String {
return pm.getString(URL + "_$link", "") ?: ""
}
private fun setImage(link: String, image: String) {
pm.edit().putString(IMAGE + "_$link", image).apply()
}
private fun getImage(link: String): String {
return pm.getString(IMAGE + "_$link", "") ?: ""
}
private fun setSiteName(link: String, siteName: String) {
pm.edit().putString(SITE_NAME + "_$link", siteName).apply()
}
private fun getSiteName(link: String): String {
return pm.getString(SITE_NAME + "_$link", "") ?: ""
}
private fun setType(link: String, type: String) {
pm.edit().putString(TYPE + "_$link", type).apply()
}
private fun getType(link: String): String {
return pm.getString(TYPE + "_$link", "") ?: ""
}
private fun urlExists(title: String, description: String, image: String): Boolean {
return title.isNotEmpty() &&
title != "null" &&
description.isNotEmpty() &&
title != "null" &&
image.isNotEmpty() &&
title != "null"
}
override suspend fun setOpenGraphResult(openGraphResult: OpenGraphResult, url: String) {
setTitle(url, openGraphResult.title.toString())
setDescription(url, openGraphResult.description.toString())
setImage(url, openGraphResult.image.toString())
setSiteName(url, openGraphResult.siteName.toString())
setType(url, openGraphResult.type.toString())
setUrl(url, openGraphResult.url.toString())
}
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)
}
companion object {
private const val OG_PARSER = "Og_Parser"
private const val TITLE = OG_PARSER + "_title"
private const val DESCRIPTION = OG_PARSER + "_description"
private const val URL = OG_PARSER + "_url"
private const val IMAGE = OG_PARSER + "_image"
private const val SITE_NAME = OG_PARSER + "_site_name"
private const val TYPE = OG_PARSER + "_type"
}
}