Skip to content
This repository has been archived by the owner on Oct 27, 2023. It is now read-only.

Commit

Permalink
Migrated to Kotlin
Browse files Browse the repository at this point in the history
* Upgraded java to 1.8
* Added support for Fragments
* Made InstaImage.kt as functional Interface
  • Loading branch information
SanjayDevTech committed Jan 6, 2021
1 parent a0e6045 commit a44f7ea
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 66 deletions.
4 changes: 4 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import android.widget.ImageView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.sanjaydevtech.instautils.InstaDownloader
import com.sanjaydevtech.instautils.InstaResponse
import com.sanjaydevtech.instautils.InstaScraper
import com.sanjaydevtech.instautils.InstaTask
import java.util.regex.Pattern
Expand Down
45 changes: 18 additions & 27 deletions app/src/main/java/com/sanjaydevtech/instarepost/SecondActivity.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package com.sanjaydevtech.instarepost;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.sanjaydevtech.instautils.InstaDownloader;
import com.sanjaydevtech.instautils.InstaPost;
import com.sanjaydevtech.instautils.InstaResponse;
Expand All @@ -34,39 +33,31 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);

downloader = new InstaDownloader(this, new InstaResponse() {
@Override
public void onResponse(@NotNull InstaTask instaTask) {
onResponseMethod(instaTask);
}
});
downloader = new InstaDownloader(this, instaTask ->
onResponseMethod(instaTask)
);

final EditText urlTxt = findViewById(R.id.urlTxt);
final Button doneBtn = findViewById(R.id.doneBtn);
img = findViewById(R.id.imageView);

doneBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Pattern pattern = Pattern.compile(URL_PATTERN);
Matcher matcher = pattern.matcher(urlTxt.getText().toString());
doneBtn.setOnClickListener(view -> {
Pattern pattern = Pattern.compile(URL_PATTERN);
Matcher matcher = pattern.matcher(urlTxt.getText().toString());
if (matcher.find()) {
downloader.get(urlTxt.getText().toString()); // Request the post data
} else {
pattern = Pattern.compile(DP_URL_PATTERN);
matcher = pattern.matcher(urlTxt.getText().toString());
if (matcher.find()) {
downloader.get(urlTxt.getText().toString()); // Request the post data
InstaScraper.getDP(SecondActivity.this, urlTxt.getText().toString(), instaTask ->
onResponseMethod(instaTask)
);
} else {
pattern = Pattern.compile(DP_URL_PATTERN);
matcher = pattern.matcher(urlTxt.getText().toString());
if(matcher.find()) {
InstaScraper.getDP(SecondActivity.this, urlTxt.getText().toString(), new InstaResponse() {
@Override
public void onResponse(@NotNull InstaTask instaTask) {
onResponseMethod(instaTask);
}
});
} else {
Toast.makeText(SecondActivity.this, "Invalid insta url", Toast.LENGTH_SHORT).show();
}
Toast.makeText(SecondActivity.this, "Invalid insta url", Toast.LENGTH_SHORT).show();
}
}

});

}
Expand Down
8 changes: 4 additions & 4 deletions instautils/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ android {
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
api "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
def coroutines_version = "1.4.2"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version"
api "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"
api "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version"
def lifecycle_version = "2.2.0"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version"
api "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version"
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.2.0'
testImplementation 'junit:junit:4.12'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package com.sanjaydevtech.instautils

import android.content.Context
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.util.Log
import android.widget.ImageView
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.lifecycleScope
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.transition.Transition
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
Expand All @@ -18,23 +22,20 @@ import java.io.IOException
* Downloader Class to download insta posts
*
* @author Sanjay Developer
* @version 1.0.2
* @version 1.1.0
*/
class InstaDownloader(private val activity: FragmentActivity, private val response: InstaResponse) {
class InstaDownloader(private val context: Context?, private val scope: CoroutineScope, private val response: InstaResponse) {

constructor(activity: FragmentActivity, response: (InstaTask) -> Unit) : this(activity, object : InstaResponse {
override fun onResponse(instaTask: InstaTask) {
response(instaTask)
}
})
constructor(activity: FragmentActivity, response: InstaResponse) : this(activity, activity.lifecycleScope, response)
constructor(fragment: Fragment, response: InstaResponse) : this(fragment.context, fragment.viewLifecycleOwner.lifecycleScope, response)

/**
* Get the url of the post
*
* @param url URL of that post
*/
fun get(url: String) {
activity.lifecycleScope.launch(Dispatchers.IO) {
scope.launch(Dispatchers.IO) {
try {
val document = Jsoup.connect(url).userAgent("Mozilla/5.0").get()
val metas = document.getElementsByTag("meta")
Expand Down Expand Up @@ -78,6 +79,15 @@ class InstaDownloader(private val activity: FragmentActivity, private val respon
}
}

/**
* A Kotlin Extension function to set image
*
* @param post InstaPost instance
*/
fun ImageView.setImage(post: InstaPost) {
setImage(post, this)
}

/**
* Retrieve the bitmap of the image post or thumbnail of video post
*
Expand All @@ -86,16 +96,20 @@ class InstaDownloader(private val activity: FragmentActivity, private val respon
*/
fun getBitmap(post: InstaPost, instaImage: InstaImage) {
val imgUrl = post.thumbnailUrl
Glide.with(activity)
.asBitmap()
.load(imgUrl)
.into(object : CustomTarget<Bitmap?>() {
override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap?>?) {
instaImage.onBitmapLoaded(resource)
}
context?.let {
Glide.with(it)
.asBitmap()
.load(imgUrl)
.into(object : CustomTarget<Bitmap?>() {
override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap?>?) {
instaImage.onBitmapLoaded(resource)
}

override fun onLoadCleared(placeholder: Drawable?) {}
})
override fun onLoadCleared(placeholder: Drawable?) {}
})
} ?: run {
Log.e(TAG, "method: getBitmap can't set image.. [REASON] Context is null")
}
}

/**
Expand All @@ -106,12 +120,16 @@ class InstaDownloader(private val activity: FragmentActivity, private val respon
*/
fun setImage(post: InstaPost, imageView: ImageView) {
val imgUrl = post.thumbnailUrl
Glide.with(activity)
.load(imgUrl)
.into(imageView)
context?.let {
Glide.with(it)
.load(imgUrl)
.into(imageView)
} ?: run {
Log.e(TAG, "method: setImage can't set image.. [REASON] Context is null")
}
}

companion object {
private val TAG = InstaDownloader::class.java.simpleName
private val TAG = InstaDownloader::class.simpleName
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ package com.sanjaydevtech.instautils

import android.graphics.Bitmap

/**
* InstaImage
/** InstaImage
*/
interface InstaImage {
fun interface InstaImage {
/**
* OnBitmap loaded
*
Expand Down
12 changes: 12 additions & 0 deletions instautils/src/main/java/com/sanjaydevtech/instautils/InstaPost.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,27 @@ package com.sanjaydevtech.instautils

/**
* InstaPost Object
*
* [url] holds the actual image or video url
*
* [type] may be constants [INSTA_IMAGE] or [INSTA_VIDEO] or [INSTA_PROFILE]
*
* [thumbnailUrl] is the same as url for type [INSTA_IMAGE],
* but a low res image for [INSTA_VIDEO] and [INSTA_PROFILE]
*/
data class InstaPost internal constructor(
val url: String,
val type: Int,
val thumbnailUrl: String) {

companion object {
/** InstaPost object is from a Image */
const val INSTA_IMAGE = 0

/** InstaPost object is from a Video */
const val INSTA_VIDEO = 1

/** InstaPost object is from a Instagram Profile */
const val INSTA_PROFILE = 2
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.sanjaydevtech.instautils

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
Expand All @@ -18,26 +20,34 @@ object InstaScraper {
private const val PROFILE_HD_PATTERN = "\"profile_pic_url_hd\":\"([^\"]*)\""
private const val PROFILE_PATTERN = "\"profile_pic_url\":\"([^\"]*)\""

/**
* To retrieve Instagram profile
*
* @param activity Activity
* @param url Url of the instagram profile
* @param response InstaResponse instance
*/
@JvmStatic
fun getDP(activity: FragmentActivity, url: String, response: (InstaTask) -> Unit) {
getDP(activity, url, object : InstaResponse {
override fun onResponse(instaTask: InstaTask) {
response(instaTask)
}
})
fun getDP(activity: FragmentActivity, url: String, response: InstaResponse) {
getDP(activity.lifecycleScope, url, response)
}

/**
* To retrieve Instagram profile
*
* @param activity Current activity
* @param fragment Fragment
* @param url Url of the instagram profile
* @param response InstaResponse instance
* @throws IllegalArgumentException Throws if no InstaResponse is attached
*/
@JvmStatic
fun getDP(activity: FragmentActivity, url: String, response: InstaResponse) {
activity.lifecycleScope.launch(Dispatchers.IO) {
fun getDP(fragment: Fragment, url: String, response: InstaResponse) {
getDP(fragment.viewLifecycleOwner.lifecycleScope, url, response)
}


@JvmStatic
private fun getDP(scope: CoroutineScope, url: String, response: InstaResponse) {
scope.launch(Dispatchers.IO) {
try {
val document = Jsoup.connect(url).userAgent("Mozilla/5.0").get()
val scripts = document.getElementsByTag("script")
Expand Down

0 comments on commit a44f7ea

Please sign in to comment.