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

Commit a87d2d8

Browse files
committed
Fetching the url is made efficient
1 parent dfab7d7 commit a87d2d8

File tree

6 files changed

+67
-72
lines changed

6 files changed

+67
-72
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
*.iml
22
.gradle
3-
github.properties
43
local.properties
54
/local.properties
65
/.idea/caches

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Insta Utils is a Insta post downloader for Android applications written in **Jav
44

55
# New Features!
66

7-
- Download the Insta public post (Only Image)
7+
- Download the Insta public post
88
- Set the image directly to ImageView
99

1010
### Credits
@@ -17,7 +17,7 @@ Insta Utils uses a number of open source libraries to work properly:
1717
### Implemetation
1818

1919
```gradle
20-
implementation 'com.sanjaydevtech.instautils:instautils:1.0.0'
20+
implementation 'com.sanjaydevtech.instautils:instautils:1.0.1
2121
```
2222

2323
### Initialisation
@@ -29,6 +29,7 @@ downloader.setResponse(new InstaResponse() {
2929
public void onResponse(InstaPost post) {
3030
int type = post.getType(); // InstaPost.INSTA_IMAGE or InstaPost.INSTA_VIDEO
3131
String url = post.getUrl();
32+
String thumbUrl = post.getThumbnailUrl();
3233
}
3334
@Override
3435
public void onError(Exception e) {}

instautils/build.gradle

Lines changed: 1 addition & 3 deletions
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.0'
16+
libraryVersion = '1.0.1'
1717

1818
developerId = 'sanjaydevtech'
1919
developerName = 'Sanjay Developer'
@@ -32,8 +32,6 @@ android {
3232
defaultConfig {
3333
minSdkVersion 16
3434
targetSdkVersion 30
35-
versionCode 1
36-
versionName "1.0"
3735

3836
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
3937
consumerProguardFiles "consumer-rules.pro"

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

Lines changed: 55 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,22 @@
1313
import com.bumptech.glide.request.transition.Transition;
1414

1515
import org.jsoup.Jsoup;
16-
import org.jsoup.nodes.DataNode;
1716
import org.jsoup.nodes.Document;
1817
import org.jsoup.nodes.Element;
1918
import org.jsoup.select.Elements;
2019

2120
import java.io.IOException;
22-
import java.util.regex.Matcher;
23-
import java.util.regex.Pattern;
2421

2522
/**
2623
* Downloader Class to download insta posts
2724
*
2825
* @author Sanjay Developer
29-
* @version 1.0.0
26+
* @version 1.0.1
3027
*/
3128
public class InstaDownloader {
3229
private Activity activity;
3330
private InstaResponse response;
3431
private static final String TAG = InstaDownloader.class.getSimpleName();
35-
private static final String IMAGE_PATTERN = "\"src\":\"([^\"]*)\"";
36-
private static final String VIDEO_PATTERN = "\"video_url\":\"([^\"]*)\"";
37-
private static final String NOISE = "\\u0026";
38-
private static final String POST_PATTERN = "^(https://www.instagram.com/p/[^/]+/)";
3932

4033
/**
4134
* Public constructor
@@ -67,50 +60,69 @@ public void get(final String url) throws NullPointerException {
6760
}
6861
new Thread() {
6962
public void run() {
70-
boolean isData = false;
7163
try {
7264
Document document = Jsoup.connect(url).userAgent("Mozilla/5.0").get();
73-
Elements scripts = document.getElementsByTag("script");
74-
for (Element script : scripts) {
75-
if (isData) {
76-
break;
65+
Elements metas = document.getElementsByTag("meta");
66+
String img = null, vid = null, type = null;
67+
for (Element meta : metas) {
68+
String property = meta.attr("property");
69+
if (property.equals("og:image")) {
70+
img = meta.attr("content");
71+
continue;
7772
}
78-
for (DataNode node : script.dataNodes()) {
79-
String urlImg = matchPattern(node.getWholeData(), IMAGE_PATTERN);
80-
if (urlImg != null) {
81-
String urlVid = matchPattern(node.getWholeData(), VIDEO_PATTERN);
82-
if (urlVid != null) {
83-
urlVid = urlVid.replace(NOISE, "&");
84-
final String finalUrl = urlVid;
85-
activity.runOnUiThread(new Runnable() {
86-
@Override
87-
public void run() {
88-
response.onResponse(new InstaPost(finalUrl, InstaPost.INSTA_VIDEO, url));
89-
}
90-
});
91-
isData = true;
92-
break;
93-
}
94-
urlImg = urlImg.replace(NOISE, "&");
95-
final String finalUrl = urlImg;
96-
activity.runOnUiThread(new Runnable() {
97-
@Override
98-
public void run() {
99-
response.onResponse(new InstaPost(finalUrl, InstaPost.INSTA_IMAGE, url));
100-
}
101-
});
102-
isData = true;
103-
break;
104-
}
73+
if (property.equals("og:video")) {
74+
vid = meta.attr("content");
75+
continue;
76+
}
77+
if (property.equals("og:type")) {
78+
type = meta.attr("content");
10579
}
10680
}
107-
if (!isData) {
81+
if (type == null || (!type.equals("video") && !type.equals("instapp:photo"))) {
10882
activity.runOnUiThread(new Runnable() {
10983
@Override
11084
public void run() {
11185
response.onError(new NullPointerException("No data resource found"));
11286
}
11387
});
88+
} else {
89+
if (type.equals("instapp:photo")) {
90+
if (img != null) {
91+
final String finalImg = img;
92+
activity.runOnUiThread(new Runnable() {
93+
@Override
94+
public void run() {
95+
response.onResponse(new InstaPost(finalImg, InstaPost.INSTA_IMAGE, finalImg));
96+
}
97+
});
98+
} else {
99+
activity.runOnUiThread(new Runnable() {
100+
@Override
101+
public void run() {
102+
response.onError(new NullPointerException("No data resource found"));
103+
}
104+
});
105+
}
106+
} else {
107+
if (img != null && vid != null) {
108+
final String finalImg1 = img;
109+
final String finalVid = vid;
110+
activity.runOnUiThread(new Runnable() {
111+
@Override
112+
public void run() {
113+
response.onResponse(new InstaPost(finalVid, InstaPost.INSTA_VIDEO, finalImg1));
114+
}
115+
});
116+
} else {
117+
activity.runOnUiThread(new Runnable() {
118+
@Override
119+
public void run() {
120+
response.onError(new NullPointerException("No data resource found"));
121+
}
122+
});
123+
}
124+
}
125+
114126
}
115127

116128
} catch (final IOException e) {
@@ -128,15 +140,6 @@ public void run() {
128140
}.start();
129141
}
130142

131-
private String matchPattern(String data, String patTxt) {
132-
Pattern pattern = Pattern.compile(patTxt);
133-
Matcher matcher = pattern.matcher(data);
134-
boolean patMatch = matcher.find();
135-
if (!patMatch) {
136-
return null;
137-
}
138-
return matcher.group(1);
139-
}
140143

141144
/**
142145
* Retrieve the bitmap of the image post or thumbnail of video post
@@ -149,10 +152,7 @@ public void getBitmap(InstaPost post, final InstaImage instaImage) throws NullPo
149152
if (instaImage == null) {
150153
throw new NullPointerException("No InstaImage listener attached");
151154
}
152-
String imgUrl = post.getUrl();
153-
if (post.getType() == InstaPost.INSTA_VIDEO) {
154-
imgUrl = matchPattern(post.getOriginalUrl(), POST_PATTERN) + "media?size=l";
155-
}
155+
String imgUrl = post.getThumbnailUrl();
156156
Glide.with(activity)
157157
.asBitmap()
158158
.load(imgUrl)
@@ -175,10 +175,7 @@ public void onLoadCleared(@Nullable Drawable placeholder) {
175175
* @param imageView To which view that image has to set
176176
*/
177177
public void setImage(InstaPost post, ImageView imageView) {
178-
String imgUrl = post.getUrl();
179-
if (post.getType() == InstaPost.INSTA_VIDEO) {
180-
imgUrl = matchPattern(post.getOriginalUrl(), POST_PATTERN) + "media?size=l";
181-
}
178+
String imgUrl = post.getThumbnailUrl();
182179
Glide.with(activity)
183180
.load(imgUrl)
184181
.into(imageView);

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
public class InstaPost {
77
private String url;
88
private int type;
9-
private String originalUrl;
9+
private String thumbnailUrl;
1010
public static final int INSTA_IMAGE = 0;
1111
public static final int INSTA_VIDEO = 1;
1212

13-
InstaPost(String url, int type, String originalUrl) {
13+
InstaPost(String url, int type, String thumbnailUrl) {
1414
this.url = url;
1515
this.type = type;
16-
this.originalUrl = originalUrl;
16+
this.thumbnailUrl = thumbnailUrl;
1717
}
1818

1919
/**
@@ -35,11 +35,11 @@ public int getType() {
3535
}
3636

3737
/**
38-
* get he original post url
38+
* get the thumbnail
3939
*
40-
* @return a String of post url
40+
* @return a String
4141
*/
42-
public String getOriginalUrl() {
43-
return originalUrl;
42+
public String getThumbnailUrl() {
43+
return thumbnailUrl;
4444
}
4545
}

0 commit comments

Comments
 (0)