13
13
import com .bumptech .glide .request .transition .Transition ;
14
14
15
15
import org .jsoup .Jsoup ;
16
- import org .jsoup .nodes .DataNode ;
17
16
import org .jsoup .nodes .Document ;
18
17
import org .jsoup .nodes .Element ;
19
18
import org .jsoup .select .Elements ;
20
19
21
20
import java .io .IOException ;
22
- import java .util .regex .Matcher ;
23
- import java .util .regex .Pattern ;
24
21
25
22
/**
26
23
* Downloader Class to download insta posts
27
24
*
28
25
* @author Sanjay Developer
29
- * @version 1.0.0
26
+ * @version 1.0.1
30
27
*/
31
28
public class InstaDownloader {
32
29
private Activity activity ;
33
30
private InstaResponse response ;
34
31
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/[^/]+/)" ;
39
32
40
33
/**
41
34
* Public constructor
@@ -67,50 +60,69 @@ public void get(final String url) throws NullPointerException {
67
60
}
68
61
new Thread () {
69
62
public void run () {
70
- boolean isData = false ;
71
63
try {
72
64
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 ;
77
72
}
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" );
105
79
}
106
80
}
107
- if (! isData ) {
81
+ if (type == null || (! type . equals ( "video" ) && ! type . equals ( "instapp:photo" )) ) {
108
82
activity .runOnUiThread (new Runnable () {
109
83
@ Override
110
84
public void run () {
111
85
response .onError (new NullPointerException ("No data resource found" ));
112
86
}
113
87
});
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
+
114
126
}
115
127
116
128
} catch (final IOException e ) {
@@ -128,15 +140,6 @@ public void run() {
128
140
}.start ();
129
141
}
130
142
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
- }
140
143
141
144
/**
142
145
* 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
149
152
if (instaImage == null ) {
150
153
throw new NullPointerException ("No InstaImage listener attached" );
151
154
}
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 ();
156
156
Glide .with (activity )
157
157
.asBitmap ()
158
158
.load (imgUrl )
@@ -175,10 +175,7 @@ public void onLoadCleared(@Nullable Drawable placeholder) {
175
175
* @param imageView To which view that image has to set
176
176
*/
177
177
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 ();
182
179
Glide .with (activity )
183
180
.load (imgUrl )
184
181
.into (imageView );
0 commit comments