Skip to content

Commit 9a5c8f7

Browse files
committed
init repo
0 parents  commit 9a5c8f7

52 files changed

Lines changed: 1241 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# These are some examples of commonly ignored file patterns.
2+
# You should customize this list as applicable to your project.
3+
# Learn more about .gitignore:
4+
# https://www.atlassian.com/git/tutorials/saving-changes/gitignore
5+
6+
# Node artifact files
7+
node_modules/
8+
dist/
9+
10+
.gradle
11+
# Compiled Java class files
12+
*.class
13+
14+
# Compiled Python bytecode
15+
*.py[cod]
16+
17+
# Log files
18+
*.log
19+
20+
# Package files
21+
*.jar
22+
23+
# Maven
24+
target/
25+
26+
# JetBrains IDE
27+
.idea/
28+
29+
# Unit test reports
30+
TEST*.xml
31+
32+
# Generated by MacOS
33+
.DS_Store
34+
35+
# Generated by Windows
36+
Thumbs.db
37+
38+
# Applications
39+
*.app
40+
*.exe
41+
*.war
42+
43+
# Large media files
44+
*.mp4
45+
*.tiff
46+
*.avi
47+
*.flv
48+
*.mov
49+
*.wmv
50+
51+
*.iml
52+
.gradle
53+
/local.properties
54+
/build
55+
/captures
56+
.externalNativeBuild
57+
.cxx
58+
local.properties
59+
gradle.properties
60+
app/release/
61+
local.properties

OGParser/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

OGParser/build.gradle

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
plugins {
2+
id 'com.android.library'
3+
id 'kotlin-android'
4+
}
5+
6+
android {
7+
compileSdkVersion 30
8+
buildToolsVersion "30.0.3"
9+
10+
defaultConfig {
11+
minSdkVersion 16
12+
targetSdkVersion 30
13+
versionCode 1
14+
versionName "1.0"
15+
16+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
17+
consumerProguardFiles "consumer-rules.pro"
18+
}
19+
20+
buildTypes {
21+
release {
22+
minifyEnabled false
23+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
24+
}
25+
}
26+
compileOptions {
27+
sourceCompatibility JavaVersion.VERSION_1_8
28+
targetCompatibility JavaVersion.VERSION_1_8
29+
}
30+
kotlinOptions {
31+
jvmTarget = '1.8'
32+
}
33+
}
34+
35+
dependencies {
36+
37+
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
38+
implementation 'androidx.core:core-ktx:1.6.0'
39+
implementation 'androidx.appcompat:appcompat:1.3.1'
40+
implementation 'com.google.android.material:material:1.4.0'
41+
testImplementation 'junit:junit:4.+'
42+
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
43+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
44+
45+
// OpenGraph
46+
implementation 'org.jsoup:jsoup:1.10.3'
47+
48+
//Architecture
49+
def lifecycle_version = '2.2.0'
50+
51+
implementation "androidx.lifecycle:lifecycle-extensions:${lifecycle_version}"
52+
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:${lifecycle_version}"
53+
}

OGParser/consumer-rules.pro

Whitespace-only changes.

OGParser/proguard-rules.pro

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.kedia.ogparser
2+
3+
import androidx.test.platform.app.InstrumentationRegistry
4+
import androidx.test.ext.junit.runners.AndroidJUnit4
5+
6+
import org.junit.Test
7+
import org.junit.runner.RunWith
8+
9+
import org.junit.Assert.*
10+
11+
/**
12+
* Instrumented test, which will execute on an Android device.
13+
*
14+
* See [testing documentation](http://d.android.com/tools/testing).
15+
*/
16+
@RunWith(AndroidJUnit4::class)
17+
class ExampleInstrumentedTest {
18+
@Test
19+
fun useAppContext() {
20+
// Context of the app under test.
21+
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
22+
assertEquals("com.kedia.ogparser.test", appContext.packageName)
23+
}
24+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.kedia.ogparser">
4+
5+
</manifest>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.kedia.ogparser
2+
3+
interface OpenGraphCallback {
4+
fun onPostResponse(openGraphResult: OpenGraphResult)
5+
fun onError(error: String)
6+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.kedia.ogparser
2+
3+
import kotlinx.coroutines.Dispatchers
4+
import kotlinx.coroutines.GlobalScope
5+
import kotlinx.coroutines.launch
6+
import kotlinx.coroutines.withContext
7+
import org.jsoup.Jsoup
8+
9+
10+
class OpenGraphParser(
11+
private val listener: OpenGraphCallback
12+
) {
13+
14+
private var url: String = ""
15+
16+
private val AGENT = "Mozilla"
17+
private val REFERRER = "http://www.google.com"
18+
private val TIMEOUT = 10000
19+
private val DOC_SELECT_QUERY = "meta[property^=og:]"
20+
private val OPEN_GRAPH_KEY = "content"
21+
private val PROPERTY = "property"
22+
private val OG_IMAGE = "og:image"
23+
private val OG_DESCRIPTION = "og:description"
24+
private val OG_URL = "og:url"
25+
private val OG_TITLE = "og:title"
26+
private val OG_SITE_NAME = "og:site_name"
27+
private val OG_TYPE = "og:type"
28+
29+
private val openGraphResult = OpenGraphResult()
30+
31+
fun parse(url: String) {
32+
this.url = url
33+
34+
GlobalScope.launch {
35+
val result = fetchContent()
36+
listener.onPostResponse(result)
37+
}
38+
39+
}
40+
41+
private suspend fun fetchContent() = withContext(Dispatchers.IO) {
42+
if (!url.contains("http")) {
43+
url = "http://$url"
44+
}
45+
46+
try {
47+
val response = Jsoup.connect(url)
48+
.ignoreContentType(true)
49+
.userAgent(AGENT)
50+
.referrer(REFERRER)
51+
.timeout(TIMEOUT)
52+
.followRedirects(true)
53+
.execute()
54+
55+
val doc = response.parse()
56+
57+
val ogTags = doc.select(DOC_SELECT_QUERY)
58+
when {
59+
ogTags.size > 0 ->
60+
ogTags.forEachIndexed { index, _ ->
61+
val tag = ogTags[index]
62+
val text = tag.attr(PROPERTY)
63+
when (text) {
64+
OG_IMAGE -> {
65+
openGraphResult.image = (tag.attr(OPEN_GRAPH_KEY))
66+
}
67+
OG_DESCRIPTION -> {
68+
openGraphResult.description = (tag.attr(OPEN_GRAPH_KEY))
69+
}
70+
OG_URL -> {
71+
openGraphResult.url = (tag.attr(OPEN_GRAPH_KEY))
72+
}
73+
OG_TITLE -> {
74+
openGraphResult.title = (tag.attr(OPEN_GRAPH_KEY))
75+
}
76+
OG_SITE_NAME -> {
77+
openGraphResult.siteName = (tag.attr(OPEN_GRAPH_KEY))
78+
}
79+
OG_TYPE -> {
80+
openGraphResult.type = (tag.attr(OPEN_GRAPH_KEY))
81+
}
82+
}
83+
}
84+
}
85+
} catch (e: Exception) {
86+
e.printStackTrace()
87+
listener.onError(e.localizedMessage)
88+
}
89+
90+
return@withContext openGraphResult
91+
}
92+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.kedia.ogparser
2+
3+
data class OpenGraphResult(
4+
var title: String = "",
5+
var description: String = "",
6+
var url: String = "",
7+
var image: String = "",
8+
var siteName: String = "",
9+
var type: String = ""
10+
)

0 commit comments

Comments
 (0)