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