This repository has been archived by the owner on Oct 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from SanjayDevTech/migrate-kotlin
Migrated to kotlin Replaced threads with coroutines
- Loading branch information
Showing
21 changed files
with
533 additions
and
457 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 0 additions & 73 deletions
73
app/src/main/java/com/sanjaydevtech/instarepost/MainActivity.java
This file was deleted.
Oops, something went wrong.
74 changes: 74 additions & 0 deletions
74
app/src/main/java/com/sanjaydevtech/instarepost/MainActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.sanjaydevtech.instarepost | ||
|
||
import android.content.Intent | ||
import android.os.Bundle | ||
import android.util.Log | ||
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.InstaScraper | ||
import com.sanjaydevtech.instautils.InstaTask | ||
import java.util.regex.Pattern | ||
|
||
class MainActivity : AppCompatActivity() { | ||
private lateinit var downloader: InstaDownloader | ||
private lateinit var img: ImageView | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_main) | ||
|
||
downloader = InstaDownloader(this) { instaTask -> | ||
onResponse(instaTask) | ||
} | ||
|
||
val urlTxt: EditText = findViewById(R.id.urlTxt) | ||
val doneBtn: Button = findViewById(R.id.doneBtn) | ||
val testBtn: Button = findViewById(R.id.test_btn) | ||
img = findViewById(R.id.imageView) | ||
|
||
testBtn.setOnClickListener { | ||
val intent = Intent(this, SecondActivity::class.java) | ||
startActivity(intent) | ||
} | ||
|
||
doneBtn.setOnClickListener { | ||
var pattern = Pattern.compile(URL_PATTERN) | ||
var matcher = pattern.matcher(urlTxt.text.toString()) | ||
if (matcher.find()) { | ||
downloader.get(urlTxt.text.toString()) // Request the post data | ||
} else { | ||
pattern = Pattern.compile(DP_URL_PATTERN) | ||
matcher = pattern.matcher(urlTxt.text.toString()) | ||
if (matcher.find()) { | ||
InstaScraper.getDP(this@MainActivity, urlTxt.text.toString()) { instaTask -> | ||
onResponse(instaTask) | ||
} | ||
} else { | ||
Toast.makeText(this@MainActivity, "Invalid insta url", Toast.LENGTH_SHORT).show() | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun onResponse(instaTask: InstaTask) { | ||
val instaPost = instaTask.instaPost | ||
if (instaPost != null) { | ||
Log.d(TAG, instaPost.url) | ||
// Retrieve the post object after request | ||
downloader.setImage(instaPost, img) | ||
Log.d(TAG, "Type: " + instaPost.type) | ||
} else { | ||
instaTask.exception!!.printStackTrace() | ||
} | ||
} | ||
|
||
companion object { | ||
private val TAG = MainActivity::class.java.simpleName | ||
private const val URL_PATTERN = "^https://www.instagram.com/p/.+" | ||
private const val DP_URL_PATTERN = "^(https://(www\\.)?instagram\\.com/[^p][0-9a-zA-Z_.]+)" | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
app/src/main/java/com/sanjaydevtech/instarepost/SecondActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.sanjaydevtech.instarepost; | ||
|
||
import android.os.Bundle; | ||
import android.util.Log; | ||
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; | ||
import com.sanjaydevtech.instautils.InstaScraper; | ||
import com.sanjaydevtech.instautils.InstaTask; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class SecondActivity extends AppCompatActivity { | ||
|
||
private InstaDownloader downloader; // Declare the InstaDownloader | ||
private static final String TAG = MainActivity.class.getSimpleName(); | ||
private static final String URL_PATTERN = "^https://www.instagram.com/p/.+"; | ||
private static final String DP_URL_PATTERN = "^(https://(www\\.)?instagram\\.com/[^p][0-9a-zA-Z_.]+)"; | ||
private ImageView img; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_second); | ||
|
||
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(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()) { | ||
InstaScraper.getDP(SecondActivity.this, urlTxt.getText().toString(), instaTask -> | ||
onResponseMethod(instaTask) | ||
); | ||
} else { | ||
Toast.makeText(SecondActivity.this, "Invalid insta url", Toast.LENGTH_SHORT).show(); | ||
} | ||
} | ||
|
||
}); | ||
|
||
} | ||
|
||
private void onResponseMethod(@NotNull InstaTask instaTask) { | ||
InstaPost instaPost = instaTask.getInstaPost(); | ||
if (instaPost != null) { | ||
Log.d(TAG, instaPost.getUrl()); | ||
// Retrieve the post object after request | ||
downloader.setImage(instaPost, img); | ||
Log.d(TAG, "Type: " + instaPost.getType()); | ||
} else { | ||
instaTask.getException().printStackTrace(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
tools:context=".SecondActivity"> | ||
|
||
|
||
<EditText | ||
android:id="@+id/urlTxt" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_alignParentStart="true" | ||
android:layout_alignParentLeft="true" | ||
android:layout_alignParentTop="true" | ||
android:layout_alignParentEnd="true" | ||
android:layout_alignParentRight="true" | ||
android:ems="10" | ||
android:hint="Instagram URL" | ||
android:inputType="textUri" /> | ||
|
||
<Button | ||
android:id="@+id/doneBtn" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_below="@id/urlTxt" | ||
android:layout_alignParentStart="true" | ||
android:layout_alignParentLeft="true" | ||
android:layout_alignParentEnd="true" | ||
android:layout_alignParentRight="true" | ||
android:text="Done" /> | ||
|
||
<ImageView | ||
android:id="@+id/imageView" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_below="@id/doneBtn" | ||
android:layout_alignParentLeft="true" | ||
android:layout_alignParentRight="true" | ||
android:layout_alignParentBottom="true" | ||
android:scaleType="fitCenter" | ||
tools:srcCompat="@tools:sample/avatars" | ||
android:layout_alignParentStart="true" | ||
android:layout_alignParentEnd="true" /> | ||
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.