Added single post support to the instagram ripper

This commit is contained in:
cyian-1756 2017-11-12 04:43:26 -05:00
parent 7f57768f3d
commit d1e021f0ef

View File

@ -14,14 +14,14 @@ import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.rarchives.ripme.ripper.AbstractJSONRipper;
import com.rarchives.ripme.ripper.AbstractHTMLRipper;
import com.rarchives.ripme.utils.Http;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
public class InstagramRipper extends AbstractJSONRipper {
public class InstagramRipper extends AbstractHTMLRipper {
private String userID;
@ -45,24 +45,36 @@ public class InstagramRipper extends AbstractJSONRipper {
@Override
public String getGID(URL url) throws MalformedURLException {
Pattern p = Pattern.compile("^https?://instagram.com/([^/]+)");
Pattern p = Pattern.compile("^https?://instagram.com/([^/]+)/?");
Matcher m = p.matcher(url.toExternalForm());
if (m.matches()) {
return m.group(1);
}
p = Pattern.compile("^https?://www.instagram.com/([^/]+)/?");
m = p.matcher(url.toExternalForm());
if (m.matches()) {
return m.group(1);
}
p = Pattern.compile("^https?://www.instagram.com/p/[a-zA-Z0-9_-]+/\\?taken-by=([^/]+)/?");
m = p.matcher(url.toExternalForm());
if (m.matches()) {
return m.group(1);
}
throw new MalformedURLException("Unable to find user in " + url);
}
@Override
public URL sanitizeURL(URL url) throws MalformedURLException {
Pattern p = Pattern.compile("^.*instagram\\.com/([a-zA-Z0-9\\-_.]+).*$");
Matcher m = p.matcher(url.toExternalForm());
if (m.matches()) {
return new URL("http://instagram.com/" + m.group(1));
}
throw new MalformedURLException("Expected username in URL (instagram.com/username and not " + url);
}
// @Override
// public URL sanitizeURL(URL url) throws MalformedURLException {
// Pattern p = Pattern.compile("^.*instagram\\.com/([a-zA-Z0-9\\-_.]+).*$");
// Matcher m = p.matcher(url.toExternalForm());
// if (m.matches()) {
// return new URL("http://instagram.com/" + m.group(1));
// }
//
// throw new MalformedURLException("Expected username in URL (instagram.com/username and not " + url);
// }
private String getUserID(URL url) throws IOException {
@ -72,12 +84,23 @@ public class InstagramRipper extends AbstractJSONRipper {
return m.group(1);
}
p = Pattern.compile("^https?://www.instagram.com/([^/]+)/?");
m = p.matcher(url.toExternalForm());
if (m.matches()) {
return m.group(1);
}
p = Pattern.compile("^https?://(www.)?instagram.com/p/[a-zA-Z0-9_-]+/\\?taken-by=([^/]+)");
m = p.matcher(url.toExternalForm());
if (m.matches()) {
return m.group(1);
}
throw new IOException("Unable to find userID at " + this.url);
}
private JSONObject getJSONFromPage(String url) throws IOException {
private JSONObject getJSONFromPage(Document firstPage) throws IOException {
String jsonText = "";
try {
Document firstPage = Http.url(url).get();
for (Element script : firstPage.select("script[type=text/javascript]")) {
if (script.data().contains("window._sharedData = ")) {
jsonText = script.data().replaceAll("window._sharedData = ", "");
@ -86,14 +109,14 @@ public class InstagramRipper extends AbstractJSONRipper {
}
return new JSONObject(jsonText);
} catch (JSONException e) {
throw new IOException("Could not get JSON from page " + url);
throw new IOException("Could not get JSON from page");
}
}
@Override
public JSONObject getFirstPage() throws IOException {
public Document getFirstPage() throws IOException {
userID = getUserID(url);
return getJSONFromPage("http://instagram.com/" + userID);
return Http.url(url).get();
}
private String getVideoFromPage(String videoID) {
@ -133,9 +156,19 @@ public class InstagramRipper extends AbstractJSONRipper {
}
@Override
public List<String> getURLsFromJSON(JSONObject json) {
public List<String> getURLsFromPage(Document doc) {
String nextPageID = "";
List<String> imageURLs = new ArrayList<>();
JSONObject json = new JSONObject();
try {
json = getJSONFromPage(doc);
} catch (IOException e) {
logger.warn("Unable to exact json from page");
}
Pattern p = Pattern.compile("^.*instagram\\.com/([a-zA-Z0-9\\-_.]+)/?");
Matcher m = p.matcher(url.toExternalForm());
if (m.matches()) {
JSONArray profilePage = json.getJSONObject("entry_data").getJSONArray("ProfilePage");
JSONArray datas = profilePage.getJSONObject(0).getJSONObject("user").getJSONObject("media").getJSONArray("nodes");
for (int i = 0; i < datas.length(); i++) {
@ -164,12 +197,26 @@ public class InstagramRipper extends AbstractJSONRipper {
break;
}
}
// Rip the next page
if (!nextPageID.equals("") && !isThisATest()) {
try {
// Sleep for a while to avoid a ban
sleep(2500);
getURLsFromJSON(getJSONFromPage("https://www.instagram.com/" + userID + "/?max_id=" + nextPageID));
} catch (IOException e){ return imageURLs;}
getURLsFromPage(Http.url("https://www.instagram.com/" + userID + "/?max_id=" + nextPageID).get());
} catch (IOException e) {
return imageURLs;
}
}
} else { // We're ripping from a single page
if (!doc.select("meta[property=og:video]").attr("content").equals("")) {
String videoURL = doc.select("meta[property=og:video]").attr("content");
// We're ripping a page with a video on it
imageURLs.add(videoURL);
} else {
// We're ripping a picture
imageURLs.add(doc.select("meta[property=og:image]").attr("content"));
}
}
return imageURLs;
}