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

Commit 5ac4943

Browse files
committed
Add Instagram profile downloader
1 parent 3f00600 commit 5ac4943

File tree

6 files changed

+166
-9
lines changed

6 files changed

+166
-9
lines changed

README.md

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,48 @@ Insta Utils uses a number of open source libraries to work properly:
2020
### Implemetation
2121

2222
```gradle
23-
implementation 'com.sanjaydevtech.instautils:instautils:1.0.1'
23+
implementation 'com.sanjaydevtech.instautils:instautils:1.0.2'
2424
```
2525

26-
### Initialisation
26+
### Instagram DP
27+
28+
#### Fetching the url
29+
```java
30+
String instaProfile = "https://instagram.com/SanjayDevTech";
31+
InstaScraper.getDP(this, instaProfile, new InstaResponse() {
32+
@Override
33+
public void onResponse(InstaPost post) {
34+
int type = post.getType(); // InstaPost.INSTA_PROFILE
35+
String url = post.getUrl();
36+
String thumbUrl = post.getThumbnailUrl();
37+
}
38+
@Override
39+
public void onError(Exception e) {}
40+
});
41+
```
42+
43+
#### Displaying
44+
After retrieving the InstaPost object you can set the image to an ImageView
45+
```java
46+
InstaDownloader downloader = new InstaDowloader(this);
47+
ImageView img = findViewById(R.id.imageView);
48+
downloader.setImage(post, img);
49+
```
50+
Or you can get a Bitmap object
51+
```java
52+
downloader.getBitmap(post, new InstaImage() {
53+
@Override
54+
public void onBitmapLoaded(Bitmap bitmap) {
55+
ImageView img = findViewById(R.id.imageView);
56+
img.setImageBitmap(bitmap);
57+
}
58+
});
59+
```
60+
61+
62+
### Image or Video Post
63+
64+
#### Initialisation
2765

2866
```java
2967
InstaDownloader downloader = new InstaDowloader(this);
@@ -39,15 +77,15 @@ downloader.setResponse(new InstaResponse() {
3977
});
4078
```
4179

42-
### Requesting a Post
80+
#### Requesting a Post
4381

4482
```java
4583
String sampleUrl = "https://www.instagram.com/p/123456";
4684
downloader.get(sampleUrl);
4785
```
4886

49-
### Setting image
50-
After retrieving the InstaPost object you can set the image to any ImageView
87+
#### Setting image
88+
After retrieving the InstaPost object you can set the image to an ImageView
5189
```java
5290
ImageView img = findViewById(R.id.imageView);
5391
downloader.setImage(post, img);
@@ -83,7 +121,6 @@ git push
83121
### Todos
84122

85123
- Find hidden bugs
86-
- Add insta video downloading functionality
87124

88125
License
89126
----

app/src/main/java/com/sanjaydevtech/instarepost/MainActivity.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import com.sanjaydevtech.instautils.InstaImage;
1616
import com.sanjaydevtech.instautils.InstaPost;
1717
import com.sanjaydevtech.instautils.InstaResponse;
18+
import com.sanjaydevtech.instautils.InstaScraper;
1819

1920
import java.util.regex.Matcher;
2021
import java.util.regex.Pattern;
@@ -24,6 +25,7 @@ public class MainActivity extends AppCompatActivity implements InstaResponse {
2425
private InstaDownloader downloader; // Declare the InstaDownloader
2526
private static final String TAG = MainActivity.class.getSimpleName();
2627
private static final String URL_PATTERN = "^https://www.instagram.com/p/.+";
28+
private static final String DP_URL_PATTERN = "^(https://(www\\.)?instagram\\.com/[^p][0-9a-zA-Z_.]+)";
2729
private ImageView img;
2830

2931
@Override
@@ -43,7 +45,13 @@ public void onClick(View view) {
4345
if (matcher.find()) {
4446
downloader.get(urlTxt.getText().toString()); // Request the post data
4547
} else {
46-
Toast.makeText(MainActivity.this, "Invalid insta url", Toast.LENGTH_SHORT).show();
48+
pattern = Pattern.compile(DP_URL_PATTERN);
49+
matcher = pattern.matcher(urlTxt.getText().toString());
50+
if(matcher.find()) {
51+
InstaScraper.getDP(MainActivity.this, urlTxt.getText().toString(), MainActivity.this);
52+
} else {
53+
Toast.makeText(MainActivity.this, "Invalid insta url", Toast.LENGTH_SHORT).show();
54+
}
4755
}
4856
}
4957
});

instautils/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ ext {
1313
siteUrl = 'https://github.com/SanjayDevTech/instautils'
1414
gitUrl = 'https://github.com/SanjayDevTech/instautils.git'
1515

16-
libraryVersion = '1.0.1'
16+
libraryVersion = '1.0.2'
1717

1818
developerId = 'sanjaydevtech'
1919
developerName = 'Sanjay Developer'

instautils/src/main/java/com/sanjaydevtech/instautils/InstaDownloader.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* Downloader Class to download insta posts
2424
*
2525
* @author Sanjay Developer
26-
* @version 1.0.1
26+
* @version 1.0.2
2727
*/
2828
public class InstaDownloader {
2929
private Activity activity;
@@ -59,6 +59,7 @@ public void get(final String url) throws NullPointerException {
5959
throw new NullPointerException("No InstaResponse Listener is attached");
6060
}
6161
new Thread() {
62+
@Override
6263
public void run() {
6364
try {
6465
Document document = Jsoup.connect(url).userAgent("Mozilla/5.0").get();

instautils/src/main/java/com/sanjaydevtech/instautils/InstaPost.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public class InstaPost {
99
private String thumbnailUrl;
1010
public static final int INSTA_IMAGE = 0;
1111
public static final int INSTA_VIDEO = 1;
12+
public static final int INSTA_PROFILE = 2;
1213

1314
InstaPost(String url, int type, String thumbnailUrl) {
1415
this.url = url;
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package com.sanjaydevtech.instautils;
2+
3+
import android.app.Activity;
4+
5+
import org.jsoup.Jsoup;
6+
import org.jsoup.nodes.DataNode;
7+
import org.jsoup.nodes.Document;
8+
import org.jsoup.nodes.Element;
9+
import org.jsoup.select.Elements;
10+
11+
import java.io.IOException;
12+
import java.util.regex.Matcher;
13+
import java.util.regex.Pattern;
14+
15+
/**
16+
* InstaScraper class for downloading dp
17+
*/
18+
public class InstaScraper {
19+
20+
private static final String DP_URL = "^(https://(www\\.)?instagram\\.com/[0-9a-zA-Z_.]+)";
21+
private static final String NOISE = "\\u0026";
22+
private static final String PROFILE_HD_PATTERN = "\"profile_pic_url_hd\":\"([^\"]*)\"";
23+
private static final String PROFILE_PATTERN = "\"profile_pic_url\":\"([^\"]*)\"";
24+
25+
26+
InstaScraper() {
27+
28+
}
29+
30+
/**
31+
* To retrieve Instagram profile
32+
*
33+
* @param activity Current activity
34+
* @param url Url of the instagram profile
35+
* @param response InstaResponse instance
36+
* @throws NullPointerException Throws if no InstaResponse is attached
37+
*/
38+
39+
public static void getDP(final Activity activity, final String url, final InstaResponse response) throws NullPointerException {
40+
if (response == null) {
41+
throw new NullPointerException("No InstaResponse Listener is attached");
42+
}
43+
new Thread() {
44+
@Override
45+
public void run() {
46+
try {
47+
boolean isData = false;
48+
Document document = Jsoup.connect(url).userAgent("Mozilla/5.0").get();
49+
Elements scripts = document.getElementsByTag("script");
50+
for (Element script : scripts) {
51+
if (isData) {
52+
break;
53+
}
54+
for (DataNode dataNode : script.dataNodes()) {
55+
String scriptData = dataNode.getWholeData().trim();
56+
if (scriptData.startsWith("window._sharedData")) {
57+
String profilePicHD = matchPattern(scriptData, PROFILE_HD_PATTERN);
58+
String profilePic = matchPattern(scriptData, PROFILE_PATTERN);
59+
if (profilePicHD != null) {
60+
profilePicHD = profilePicHD.replace(NOISE, "&");
61+
}
62+
if (profilePic != null) {
63+
profilePic = profilePic.replace(NOISE, "&");
64+
}
65+
final String finalProfilePicHD = profilePicHD;
66+
final String finalProfilePic = profilePic;
67+
activity.runOnUiThread(new Runnable() {
68+
@Override
69+
public void run() {
70+
response.onResponse(new InstaPost(finalProfilePicHD, InstaPost.INSTA_PROFILE, finalProfilePic));
71+
}
72+
});
73+
isData = true;
74+
break;
75+
}
76+
}
77+
}
78+
if (!isData) {
79+
activity.runOnUiThread(new Runnable() {
80+
@Override
81+
public void run() {
82+
response.onError(new NullPointerException("No data resource found"));
83+
}
84+
});
85+
}
86+
87+
} catch (final IOException e) {
88+
activity.runOnUiThread(new Runnable() {
89+
@Override
90+
public void run() {
91+
response.onError(e);
92+
}
93+
});
94+
}
95+
96+
}
97+
}.start();
98+
99+
}
100+
101+
private static String matchPattern(String data, String patTxt) {
102+
Pattern pattern = Pattern.compile(patTxt);
103+
Matcher matcher = pattern.matcher(data);
104+
boolean patMatch = matcher.find();
105+
if (!patMatch) {
106+
return null;
107+
}
108+
return matcher.group(1);
109+
}
110+
}

0 commit comments

Comments
 (0)