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

Commit

Permalink
Add Instagram profile downloader
Browse files Browse the repository at this point in the history
  • Loading branch information
SanjayDevTech committed Sep 7, 2020
1 parent 3f00600 commit 5ac4943
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 9 deletions.
49 changes: 43 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,48 @@ Insta Utils uses a number of open source libraries to work properly:
### Implemetation

```gradle
implementation 'com.sanjaydevtech.instautils:instautils:1.0.1'
implementation 'com.sanjaydevtech.instautils:instautils:1.0.2'
```

### Initialisation
### Instagram DP

#### Fetching the url
```java
String instaProfile = "https://instagram.com/SanjayDevTech";
InstaScraper.getDP(this, instaProfile, new InstaResponse() {
@Override
public void onResponse(InstaPost post) {
int type = post.getType(); // InstaPost.INSTA_PROFILE
String url = post.getUrl();
String thumbUrl = post.getThumbnailUrl();
}
@Override
public void onError(Exception e) {}
});
```

#### Displaying
After retrieving the InstaPost object you can set the image to an ImageView
```java
InstaDownloader downloader = new InstaDowloader(this);
ImageView img = findViewById(R.id.imageView);
downloader.setImage(post, img);
```
Or you can get a Bitmap object
```java
downloader.getBitmap(post, new InstaImage() {
@Override
public void onBitmapLoaded(Bitmap bitmap) {
ImageView img = findViewById(R.id.imageView);
img.setImageBitmap(bitmap);
}
});
```


### Image or Video Post

#### Initialisation

```java
InstaDownloader downloader = new InstaDowloader(this);
Expand All @@ -39,15 +77,15 @@ downloader.setResponse(new InstaResponse() {
});
```

### Requesting a Post
#### Requesting a Post

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

### Setting image
After retrieving the InstaPost object you can set the image to any ImageView
#### Setting image
After retrieving the InstaPost object you can set the image to an ImageView
```java
ImageView img = findViewById(R.id.imageView);
downloader.setImage(post, img);
Expand Down Expand Up @@ -83,7 +121,6 @@ git push
### Todos

- Find hidden bugs
- Add insta video downloading functionality

License
----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.sanjaydevtech.instautils.InstaImage;
import com.sanjaydevtech.instautils.InstaPost;
import com.sanjaydevtech.instautils.InstaResponse;
import com.sanjaydevtech.instautils.InstaScraper;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -24,6 +25,7 @@ public class MainActivity extends AppCompatActivity implements InstaResponse {
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
Expand All @@ -43,7 +45,13 @@ public void onClick(View view) {
if (matcher.find()) {
downloader.get(urlTxt.getText().toString()); // Request the post data
} else {
Toast.makeText(MainActivity.this, "Invalid insta url", Toast.LENGTH_SHORT).show();
pattern = Pattern.compile(DP_URL_PATTERN);
matcher = pattern.matcher(urlTxt.getText().toString());
if(matcher.find()) {
InstaScraper.getDP(MainActivity.this, urlTxt.getText().toString(), MainActivity.this);
} else {
Toast.makeText(MainActivity.this, "Invalid insta url", Toast.LENGTH_SHORT).show();
}
}
}
});
Expand Down
2 changes: 1 addition & 1 deletion instautils/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ ext {
siteUrl = 'https://github.com/SanjayDevTech/instautils'
gitUrl = 'https://github.com/SanjayDevTech/instautils.git'

libraryVersion = '1.0.1'
libraryVersion = '1.0.2'

developerId = 'sanjaydevtech'
developerName = 'Sanjay Developer'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* Downloader Class to download insta posts
*
* @author Sanjay Developer
* @version 1.0.1
* @version 1.0.2
*/
public class InstaDownloader {
private Activity activity;
Expand Down Expand Up @@ -59,6 +59,7 @@ public void get(final String url) throws NullPointerException {
throw new NullPointerException("No InstaResponse Listener is attached");
}
new Thread() {
@Override
public void run() {
try {
Document document = Jsoup.connect(url).userAgent("Mozilla/5.0").get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class InstaPost {
private String thumbnailUrl;
public static final int INSTA_IMAGE = 0;
public static final int INSTA_VIDEO = 1;
public static final int INSTA_PROFILE = 2;

InstaPost(String url, int type, String thumbnailUrl) {
this.url = url;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package com.sanjaydevtech.instautils;

import android.app.Activity;

import org.jsoup.Jsoup;
import org.jsoup.nodes.DataNode;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* InstaScraper class for downloading dp
*/
public class InstaScraper {

private static final String DP_URL = "^(https://(www\\.)?instagram\\.com/[0-9a-zA-Z_.]+)";
private static final String NOISE = "\\u0026";
private static final String PROFILE_HD_PATTERN = "\"profile_pic_url_hd\":\"([^\"]*)\"";
private static final String PROFILE_PATTERN = "\"profile_pic_url\":\"([^\"]*)\"";


InstaScraper() {

}

/**
* To retrieve Instagram profile
*
* @param activity Current activity
* @param url Url of the instagram profile
* @param response InstaResponse instance
* @throws NullPointerException Throws if no InstaResponse is attached
*/

public static void getDP(final Activity activity, final String url, final InstaResponse response) throws NullPointerException {
if (response == null) {
throw new NullPointerException("No InstaResponse Listener is attached");
}
new Thread() {
@Override
public void run() {
try {
boolean isData = false;
Document document = Jsoup.connect(url).userAgent("Mozilla/5.0").get();
Elements scripts = document.getElementsByTag("script");
for (Element script : scripts) {
if (isData) {
break;
}
for (DataNode dataNode : script.dataNodes()) {
String scriptData = dataNode.getWholeData().trim();
if (scriptData.startsWith("window._sharedData")) {
String profilePicHD = matchPattern(scriptData, PROFILE_HD_PATTERN);
String profilePic = matchPattern(scriptData, PROFILE_PATTERN);
if (profilePicHD != null) {
profilePicHD = profilePicHD.replace(NOISE, "&");
}
if (profilePic != null) {
profilePic = profilePic.replace(NOISE, "&");
}
final String finalProfilePicHD = profilePicHD;
final String finalProfilePic = profilePic;
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
response.onResponse(new InstaPost(finalProfilePicHD, InstaPost.INSTA_PROFILE, finalProfilePic));
}
});
isData = true;
break;
}
}
}
if (!isData) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
response.onError(new NullPointerException("No data resource found"));
}
});
}

} catch (final IOException e) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
response.onError(e);
}
});
}

}
}.start();

}

private static String matchPattern(String data, String patTxt) {
Pattern pattern = Pattern.compile(patTxt);
Matcher matcher = pattern.matcher(data);
boolean patMatch = matcher.find();
if (!patMatch) {
return null;
}
return matcher.group(1);
}
}

0 comments on commit 5ac4943

Please sign in to comment.