├── .gitignore ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── net │ │ └── vrgsoft │ │ └── rxurlparser │ │ └── MainActivity.kt │ └── res │ ├── layout │ └── activity_main.xml │ ├── mipmap-hdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-mdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xxhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xxxhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ └── values │ ├── colors.xml │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml ├── build.gradle ├── dependencies.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── library ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── net │ │ └── vrgsoft │ │ └── library │ │ ├── LinkCrawler.kt │ │ ├── LinkPreviewCallback.kt │ │ ├── ParseContent.kt │ │ ├── Regex.kt │ │ ├── Result.kt │ │ └── SearchUrls.kt │ └── res │ └── values │ └── strings.xml └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea 5 | .DS_Store 6 | /build 7 | /captures 8 | .externalNativeBuild 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Kotlin Link Parser 2 | 3 | ### Here is our implementation of Link Preview written in Kotlin with RxKotlin2 4 | 5 | ![](https://github.com/VRGsoftUA/Java-Link-Parser/blob/master/image.png) 6 | 7 | ## Usage 8 | 9 | Include the library as local library project. 10 | 11 | ```gradle 12 | allprojects { 13 | repositories { 14 | maven { url 'https://jitpack.io' } 15 | } 16 | } 17 | 18 | dependencies { 19 | 20 | implementation 'com.github.VRGsoftUA:Kotlin-Link-Parser:1.0.0' 21 | } 22 | ``` 23 | 24 | Initialize LinkCrawler: 25 | 26 | ```kotlin 27 | val crawler = LinkCrawler() 28 | ``` 29 | 30 | If you need to do something before parsing url, you can use the preload callback: 31 | 32 | ```kotlin 33 | crawler.onPreload { 34 | // Do something 35 | } 36 | ``` 37 | 38 | To start parsing you need to use crawler.parseUrl and pass desired url, it returns `Flowable`: 39 | 40 | ```kotlin 41 | val subscription = 42 | crawler.parseUrl("https://github.com").subscribe { t -> 43 | mBinding.content = t.result 44 | } 45 | 46 | subscription.dispose() // avoid leaks 47 | ``` 48 | 49 | Result object contains ParseContent field which contains all parsed data of passed url, such as 50 | title, description etc. 51 | 52 | #### [Java version](https://github.com/VRGsoftUA/Java-Link-Parser/) 53 | 54 | License 55 | ================================= 56 | 57 | Copyright 2018 VRG Soft 58 | 59 | Licensed under the Apache License, Version 2.0 (the "License"); 60 | you may not use this file except in compliance with the License. 61 | You may obtain a copy of the License at 62 | 63 | http://www.apache.org/licenses/LICENSE-2.0 64 | 65 | Unless required by applicable law or agreed to in writing, software 66 | distributed under the License is distributed on an "AS IS" BASIS, 67 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 68 | See the License for the specific language governing permissions and 69 | limitations under the License. 70 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'kotlin-android' 3 | apply plugin: 'kotlin-kapt' 4 | apply from: '../dependencies.gradle' 5 | 6 | android { 7 | compileSdkVersion versions.compileSdk 8 | buildToolsVersion versions.buildTools 9 | 10 | defaultConfig { 11 | applicationId "net.vrgsoft.rxurlparser" 12 | minSdkVersion versions.minSdk 13 | targetSdkVersion versions.compileSdk 14 | versionCode versions.publishVersionCode 15 | versionName versions.publishVersion 16 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 17 | } 18 | 19 | dataBinding { 20 | enabled = true 21 | } 22 | 23 | buildTypes { 24 | release { 25 | minifyEnabled false 26 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 27 | } 28 | } 29 | } 30 | 31 | kapt { 32 | generateStubs = true 33 | } 34 | 35 | repositories { 36 | google() 37 | jcenter() 38 | } 39 | 40 | dependencies { 41 | implementation project(path: ':library') 42 | 43 | implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:' + versions.kotlin 44 | implementation 'com.android.support:appcompat-v7:' + versions.supportLib 45 | implementation 'com.android.support.constraint:constraint-layout:' + versions.constraintLayout 46 | implementation 'com.github.bumptech.glide:glide:' + versions.glide 47 | kapt 'com.android.databinding:compiler:' + versions.dataBinding 48 | } -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in F:\AndroidSDK/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | 19 | # Uncomment this to preserve the line number information for 20 | # debugging stack traces. 21 | #-keepattributes SourceFile,LineNumberTable 22 | 23 | # If you keep the line number information, uncomment this to 24 | # hide the original source file name. 25 | #-renamesourcefileattribute SourceFile 26 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /app/src/main/java/net/vrgsoft/rxurlparser/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package net.vrgsoft.rxurlparser 2 | 3 | import android.databinding.DataBindingUtil 4 | import android.os.Bundle 5 | import android.support.v7.app.AppCompatActivity 6 | import android.widget.Toast 7 | import io.reactivex.disposables.Disposable 8 | import net.vrgsoft.library.LinkCrawler 9 | import net.vrgsoft.rxurlparser.databinding.ActivityMainBinding 10 | 11 | class MainActivity : AppCompatActivity() { 12 | 13 | private lateinit var mBinding: ActivityMainBinding 14 | private val crawler = LinkCrawler() 15 | 16 | private var parseSubscription: Disposable? = null 17 | 18 | override fun onCreate(savedInstanceState: Bundle?) { 19 | super.onCreate(savedInstanceState) 20 | mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main) 21 | 22 | crawler.onPreload { 23 | Toast.makeText(this, "Preload url", Toast.LENGTH_SHORT) 24 | .show() 25 | } 26 | 27 | parseSubscription = 28 | crawler.parseUrl("https://github.com") 29 | .subscribe { t -> 30 | mBinding.content = t.result 31 | } 32 | } 33 | 34 | override fun onPause() { 35 | parseSubscription?.dispose() 36 | super.onPause() 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 11 | 12 | 13 | 17 | 18 | 31 | 32 | 45 | 46 | 59 | 60 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VRGsoftUA/Kotlin-Link-Parser/6eb49be161d83cdc17986d10f85a3f2fb8cdc50b/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VRGsoftUA/Kotlin-Link-Parser/6eb49be161d83cdc17986d10f85a3f2fb8cdc50b/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VRGsoftUA/Kotlin-Link-Parser/6eb49be161d83cdc17986d10f85a3f2fb8cdc50b/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VRGsoftUA/Kotlin-Link-Parser/6eb49be161d83cdc17986d10f85a3f2fb8cdc50b/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VRGsoftUA/Kotlin-Link-Parser/6eb49be161d83cdc17986d10f85a3f2fb8cdc50b/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VRGsoftUA/Kotlin-Link-Parser/6eb49be161d83cdc17986d10f85a3f2fb8cdc50b/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VRGsoftUA/Kotlin-Link-Parser/6eb49be161d83cdc17986d10f85a3f2fb8cdc50b/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VRGsoftUA/Kotlin-Link-Parser/6eb49be161d83cdc17986d10f85a3f2fb8cdc50b/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VRGsoftUA/Kotlin-Link-Parser/6eb49be161d83cdc17986d10f85a3f2fb8cdc50b/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VRGsoftUA/Kotlin-Link-Parser/6eb49be161d83cdc17986d10f85a3f2fb8cdc50b/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 60dp 4 | 8dp 5 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | RxUrlParser 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | apply from: './dependencies.gradle' 3 | 4 | repositories { 5 | google() 6 | jcenter() 7 | mavenCentral() 8 | } 9 | dependencies { 10 | classpath 'com.android.tools.build:gradle:' + versions.gradlePlugin 11 | classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:' + versions.kotlin 12 | } 13 | } 14 | 15 | allprojects { 16 | repositories { 17 | google() 18 | jcenter() 19 | } 20 | } 21 | 22 | task clean(type: Delete) { 23 | delete rootProject.buildDir 24 | } 25 | -------------------------------------------------------------------------------- /dependencies.gradle: -------------------------------------------------------------------------------- 1 | ext.versions = [ 2 | minSdk : 16, 3 | compileSdk : 28, 4 | buildTools : '28.0.3', 5 | publishVersion : '1.0.0', 6 | publishVersionCode: 1, 7 | 8 | gradlePlugin : '3.2.1', 9 | 10 | kotlin : '1.3.10', 11 | supportLib : '28.0.0', 12 | rxKotlin : '2.0.3', 13 | jsoup : '1.10.2', 14 | 15 | anko : '0.10.1', 16 | glide : '4.7.1', 17 | dataBinding : '2.3.2', 18 | constraintLayout : '1.0.2' 19 | ] -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | org.gradle.jvmargs=-Xmx1536m 13 | 14 | # When configured, Gradle will run in incubating parallel mode. 15 | # This option should only be used with decoupled projects. More details, visit 16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 17 | # org.gradle.parallel=true 18 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VRGsoftUA/Kotlin-Link-Parser/6eb49be161d83cdc17986d10f85a3f2fb8cdc50b/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Thu Jun 08 14:30:43 EEST 2017 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /library/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /library/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'kotlin-android' 3 | apply from: '../dependencies.gradle' 4 | 5 | android { 6 | compileSdkVersion versions.compileSdk 7 | buildToolsVersion versions.buildTools 8 | 9 | defaultConfig { 10 | minSdkVersion versions.minSdk 11 | targetSdkVersion versions.compileSdk 12 | versionCode versions.publishVersionCode 13 | versionName versions.publishVersion 14 | } 15 | 16 | buildTypes { 17 | release { 18 | minifyEnabled false 19 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 20 | } 21 | } 22 | } 23 | 24 | repositories { 25 | google() 26 | jcenter() 27 | } 28 | 29 | dependencies { 30 | api 'io.reactivex.rxjava2:rxkotlin:' + versions.rxKotlin 31 | implementation 'org.jetbrains.anko:anko-commons:' + versions.anko 32 | implementation 'org.jsoup:jsoup:' + versions.jsoup 33 | } 34 | -------------------------------------------------------------------------------- /library/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in F:\AndroidSDK/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | 19 | # Uncomment this to preserve the line number information for 20 | # debugging stack traces. 21 | #-keepattributes SourceFile,LineNumberTable 22 | 23 | # If you keep the line number information, uncomment this to 24 | # hide the original source file name. 25 | #-renamesourcefileattribute SourceFile 26 | -------------------------------------------------------------------------------- /library/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /library/src/main/java/net/vrgsoft/library/LinkCrawler.kt: -------------------------------------------------------------------------------- 1 | package net.vrgsoft.library 2 | 3 | import io.reactivex.* 4 | import io.reactivex.schedulers.Schedulers 5 | import org.jsoup.Jsoup 6 | import org.jsoup.nodes.Document 7 | import org.jsoup.nodes.Element 8 | import org.jsoup.select.Elements 9 | import java.io.IOException 10 | import java.net.MalformedURLException 11 | import java.net.URL 12 | import java.net.URLConnection 13 | import io.reactivex.Flowable 14 | import io.reactivex.processors.PublishProcessor 15 | 16 | typealias LinkPreloadCallback = (String) -> Unit 17 | 18 | class LinkCrawler { 19 | companion object { 20 | fun extendedTrim(content: String): String = content 21 | .replace("\\s+", " ") 22 | .replace("\n", " ") 23 | .replace("\r", " ") 24 | .trim { it <= ' ' } 25 | 26 | private const val HTTP_PROTOCOL = "http://" 27 | private const val HTTPS_PROTOCOL = "https://" 28 | } 29 | 30 | @Deprecated(message = "Prefer the onPreload { } function instead.") 31 | var mPreloadCallback: LinkPreviewCallback? = null 32 | 33 | private var preloadCallback: LinkPreloadCallback? = null 34 | private var mCache: MutableMap = mutableMapOf() 35 | private val processor: PublishProcessor = PublishProcessor.create() 36 | 37 | fun onPreload(callback: LinkPreloadCallback) { 38 | this.preloadCallback = callback 39 | } 40 | 41 | fun parseUrl(url: String): Flowable { 42 | initUrl(url) 43 | return processor 44 | } 45 | 46 | private fun initUrl(url: String) { 47 | mPreloadCallback?.onPre() 48 | preloadCallback?.invoke(url) 49 | 50 | if (mCache.containsKey(url)) { 51 | processor.onNext(Result(mCache[url], isNull(mCache[url]!!), url)) 52 | } else { 53 | getCode(url) 54 | .subscribeOn(Schedulers.io()) 55 | .subscribe( 56 | { 57 | mCache[url] = it 58 | processor.onNext(Result(mCache[url], isNull(it), url)) 59 | }, 60 | { t -> t.printStackTrace() } 61 | ) 62 | } 63 | } 64 | 65 | private fun getCode(url: String): Single { 66 | val content = ParseContent() 67 | return Single.fromCallable { 68 | val urls: List = SearchUrls.matches(url) 69 | when { 70 | urls.isNotEmpty() -> content.finalUrl = unshortenUrl(extendedTrim(urls[0])) 71 | else -> content.finalUrl = "" 72 | } 73 | if (content.finalUrl != "") { 74 | when { 75 | isImage(content.finalUrl) && !content.finalUrl.contains("dropbox") -> { 76 | content.success = true 77 | content.images.add(content.finalUrl) 78 | content.title = "" 79 | content.description = "" 80 | } 81 | else -> try { 82 | val doc: Document = Jsoup.connect(content.finalUrl).userAgent("Mozzila").get() 83 | content.htmlCode = extendedTrim(doc.toString()) 84 | val metaTags: Map = getMetaTags(content.htmlCode) 85 | content.metaTags = metaTags 86 | content.title = metaTags["title"]!! 87 | content.description = metaTags["description"]!! 88 | 89 | when { 90 | content.title == "" -> { 91 | val matchTitle = Regex.match(content.htmlCode, Regex.TITLE_PATTERN, 2) 92 | if (matchTitle != "") { 93 | content.title = htmlDecode(matchTitle) 94 | } 95 | } 96 | } 97 | if (content.description == "") { 98 | content.description = crawlCode(content.htmlCode) 99 | } 100 | content.description = content.description.replace(Regex.SCRIPT_PATTERN, "") 101 | when { 102 | metaTags["image"] != "" -> content.images.add( 103 | metaTags["image"]!!) 104 | else -> content.images = getImages(doc).toMutableList() 105 | } 106 | content.success = true 107 | } catch (e: Exception) { 108 | content.success = false 109 | } 110 | } 111 | } 112 | val linksSet = content.finalUrl.split("&") 113 | content.url = linksSet[0] 114 | content.canonicalUrl = canonicalPage(content.finalUrl) 115 | content.description = trimTags(content.description) 116 | //return content 117 | content 118 | } 119 | } 120 | 121 | private fun getMetaTags(content: String): MutableMap { 122 | val metaTags = mutableMapOf().apply { 123 | this["url"] = "" 124 | this["title"] = "" 125 | this["description"] = "" 126 | this["image"] = "" 127 | } 128 | 129 | val matches = Regex.matchAll(content, 130 | Regex.METATAG_PATTERN) 131 | 132 | for (match in matches) { 133 | val lowerCase = match.toLowerCase() 134 | if (lowerCase.contains("property=\"og:url\"") 135 | || lowerCase.contains("property='og:url'") 136 | || lowerCase.contains("name=\"url\"") 137 | || lowerCase.contains("name='url'")) 138 | updateMetaTag(metaTags, "url", separateMetaTagsContent(match)) 139 | else if (lowerCase.contains("property=\"og:title\"") 140 | || lowerCase.contains("property='og:title'") 141 | || lowerCase.contains("name=\"title\"") 142 | || lowerCase.contains("name='title'")) 143 | updateMetaTag(metaTags, "title", separateMetaTagsContent(match)) 144 | else if (lowerCase 145 | .contains("property=\"og:description\"") 146 | || lowerCase 147 | .contains("property='og:description'") 148 | || lowerCase.contains("name=\"description\"") 149 | || lowerCase.contains("name='description'")) 150 | updateMetaTag(metaTags, "description", separateMetaTagsContent(match)) 151 | else if (lowerCase.contains("property=\"og:image\"") 152 | || lowerCase.contains("property='og:image'") 153 | || lowerCase.contains("name=\"image\"") 154 | || lowerCase.contains("name='image'")) 155 | updateMetaTag(metaTags, "image", separateMetaTagsContent(match)) 156 | } 157 | 158 | return metaTags 159 | } 160 | 161 | private fun updateMetaTag(metaTags: MutableMap, url: String, value: String?) { 162 | if (value?.isNotEmpty() == true) { 163 | metaTags[url] = value 164 | } 165 | } 166 | 167 | /** 168 | * Gets content from metatag 169 | */ 170 | private fun separateMetaTagsContent(content: String): String { 171 | return Jsoup.parse(content).getElementsByAttribute("content").attr("content") 172 | } 173 | 174 | private fun crawlCode(content: String): String { 175 | val resultSpan = getTagContent("span", content) 176 | val resultParagraph = getTagContent("p", content) 177 | val resultDiv = getTagContent("div", content) 178 | 179 | val result = when { 180 | resultParagraph.length > resultSpan.length && resultParagraph.length >= resultDiv.length -> resultParagraph 181 | resultParagraph.length > resultSpan.length && resultParagraph.length < resultDiv.length -> resultDiv 182 | else -> resultParagraph 183 | } 184 | 185 | return htmlDecode(result) 186 | } 187 | 188 | private fun canonicalPage(oldUrl: String): String { 189 | var newUrl = oldUrl 190 | 191 | var cannonical = "" 192 | if (newUrl.startsWith(HTTP_PROTOCOL)) { 193 | newUrl = newUrl.substring(HTTP_PROTOCOL.length) 194 | } else if (newUrl.startsWith(HTTPS_PROTOCOL)) { 195 | newUrl = newUrl.substring(HTTPS_PROTOCOL.length) 196 | } 197 | 198 | val urlLength = newUrl.length 199 | (0 until urlLength) 200 | .takeWhile { newUrl[it] != '/' } 201 | .forEach { cannonical += newUrl[it] } 202 | 203 | return cannonical 204 | 205 | } 206 | 207 | private fun isNull(sourceContent: ParseContent): Boolean = !sourceContent.success && 208 | extendedTrim(sourceContent.htmlCode) == "" && 209 | !isImage(sourceContent.finalUrl) 210 | 211 | private fun getTagContent(tag: String, content: String): String { 212 | val pattern = "<$tag(.*?)>(.*?)" 213 | var result = "" 214 | var currentMatch: String 215 | 216 | val matches: MutableList = Regex.matchAll(content, pattern).toMutableList() 217 | val matchesSize = matches.size 218 | 219 | for (i in 0..matchesSize) { 220 | currentMatch = trimTags(matches[i]) 221 | if (currentMatch.length >= 120) { 222 | result = extendedTrim(currentMatch) 223 | break 224 | } 225 | } 226 | if (result == "") { 227 | val final: String = Regex.match(content, pattern, 2) 228 | result = extendedTrim(final) 229 | } 230 | 231 | result = result.replace(" ", "") 232 | return htmlDecode(result) 233 | } 234 | 235 | private fun getImages(document: Document): List { 236 | val matches: MutableList = mutableListOf() 237 | val media: Elements = document.select("[src]") 238 | 239 | media.forEach { 240 | element: Element? -> 241 | run { 242 | if (element!!.tagName() == "img") { 243 | matches.add(element.attr("abs:src")) 244 | } 245 | } 246 | } 247 | return matches 248 | } 249 | 250 | private fun unshortenUrl(url: String): String { 251 | if (!url.startsWith(HTTP_PROTOCOL) && !url.startsWith(HTTPS_PROTOCOL)) { 252 | return "" 253 | } 254 | 255 | var urlConn = connectURL(url) 256 | urlConn?.headerFields 257 | 258 | var finalResult = urlConn?.url.toString() 259 | 260 | urlConn = connectURL(finalResult) 261 | urlConn?.headerFields 262 | 263 | while (urlConn?.url.toString() != finalResult) { 264 | finalResult = unshortenUrl(finalResult) 265 | } 266 | 267 | return finalResult 268 | 269 | } 270 | 271 | private fun connectURL(strURL: String): URLConnection? { 272 | var conn: URLConnection? = null 273 | try { 274 | val inputURL = URL(strURL) 275 | conn = inputURL.openConnection() 276 | } catch (e: MalformedURLException) { 277 | println("Please input a valid URL") 278 | } catch (ioe: IOException) { 279 | println("Can not connect to the URL") 280 | } 281 | 282 | return conn 283 | } 284 | 285 | private fun htmlDecode(content: String): String = Jsoup.parse(content).text() 286 | 287 | private fun trimTags(content: String): String = Jsoup.parse(content).text() 288 | 289 | private fun isImage(url: String): Boolean = url.matches(Regex.IMAGE_PATTERN.toRegex()) 290 | } -------------------------------------------------------------------------------- /library/src/main/java/net/vrgsoft/library/LinkPreviewCallback.kt: -------------------------------------------------------------------------------- 1 | package net.vrgsoft.library 2 | 3 | interface LinkPreviewCallback { 4 | fun onPre() 5 | 6 | } -------------------------------------------------------------------------------- /library/src/main/java/net/vrgsoft/library/ParseContent.kt: -------------------------------------------------------------------------------- 1 | package net.vrgsoft.library 2 | 3 | data class ParseContent( 4 | var success: Boolean = false, 5 | var htmlCode: String = "", 6 | var raw: String = "", 7 | var title: String = "", 8 | var description: String = "", 9 | var url: String = "", 10 | var finalUrl: String = "", 11 | var canonicalUrl: String = "", 12 | var metaTags: Map = mapOf(), 13 | var images: MutableList = mutableListOf(), 14 | var urlData: List = listOf() 15 | ) -------------------------------------------------------------------------------- /library/src/main/java/net/vrgsoft/library/Regex.kt: -------------------------------------------------------------------------------- 1 | package net.vrgsoft.library 2 | 3 | import java.util.regex.Matcher 4 | import kotlin.text.Regex 5 | 6 | class Regex { 7 | companion object { 8 | val IMAGE_PATTERN = "(.+?)\\.(jpg|png|gif|bmp)$" 9 | val IMAGE_TAG_PATTERN = "()?" 10 | val ICON_TAG_PATTERN = "()?" 11 | val ICON_REV_TAG_PATTERN = "()?" 12 | val ITEMPROP_IMAGE_TAG_PATTERN = "()?" 13 | val ITEMPROP_IMAGE_REV_TAG_PATTERN = "()?" 14 | val TITLE_PATTERN = "(.*?)" 15 | val SCRIPT_PATTERN = "(.*?)" 16 | val METATAG_PATTERN = "" 17 | val METATAG_CONTENT_PATTERN = "content=\"(.*?)\"" 18 | val URL_PATTERN = "<\\b(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]>" 19 | 20 | fun match(content: String, patter: String, index: Int): String { 21 | val r: Regex = kotlin.text.Regex(patter) 22 | 23 | return LinkCrawler.extendedTrim(r.find(content)!!.value) 24 | } 25 | 26 | fun matchAll(content: String, patter: String): List { 27 | val r: Regex = kotlin.text.Regex(patter) 28 | val matches: List = r.findAll(content).map { matchResult -> matchResult.value }.toList() 29 | return matches 30 | } 31 | } 32 | } 33 | 34 | -------------------------------------------------------------------------------- /library/src/main/java/net/vrgsoft/library/Result.kt: -------------------------------------------------------------------------------- 1 | package net.vrgsoft.library 2 | 3 | data class Result( 4 | var result: ParseContent?, 5 | var isNull: Boolean, 6 | var url: String 7 | ) -------------------------------------------------------------------------------- /library/src/main/java/net/vrgsoft/library/SearchUrls.kt: -------------------------------------------------------------------------------- 1 | package net.vrgsoft.library 2 | 3 | import java.net.URL 4 | import java.util.ArrayList 5 | 6 | object SearchUrls { 7 | 8 | const val ALL = 0 9 | const val FIRST = 1 10 | 11 | /** It finds urls inside the text and return the matched ones */ 12 | fun matches(text: String): ArrayList { 13 | return matches(text, ALL) 14 | } 15 | 16 | /** It finds urls inside the text and return the matched ones */ 17 | fun matches( 18 | text: String, 19 | results: Int 20 | ): ArrayList { 21 | 22 | val urls = ArrayList() 23 | val splitString = text.split(' ') 24 | .dropLastWhile { it.isEmpty() } 25 | .toTypedArray() 26 | 27 | for (string in splitString) { 28 | try { 29 | val item = URL(string) 30 | urls.add(item.toString()) 31 | } catch (_: Exception) { 32 | } 33 | 34 | if (results == FIRST && urls.size > 0) break 35 | } 36 | 37 | return urls 38 | } 39 | } -------------------------------------------------------------------------------- /library/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | library 3 | 4 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app', ':library' 2 | --------------------------------------------------------------------------------