ImgScroll/src/main/java/com/rarchives/ripme/ripper/rippers/PhotobucketRipper.java

214 lines
7.5 KiB
Java
Raw Normal View History

2014-04-23 05:49:11 +02:00
package com.rarchives.ripme.ripper.rippers;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.json.JSONArray;
import org.json.JSONObject;
import org.jsoup.Connection.Response;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import com.rarchives.ripme.ripper.AlbumRipper;
import com.rarchives.ripme.utils.Http;
2014-04-23 05:49:11 +02:00
public class PhotobucketRipper extends AlbumRipper {
private static final String DOMAIN = "photobucket.com",
HOST = "photobucket";
private Response pageResponse = null;
public PhotobucketRipper(URL url) throws IOException {
super(url);
}
@Override
public String getHost() {
return HOST;
}
public URL sanitizeURL(URL url) throws MalformedURLException {
2018-06-03 03:14:41 +02:00
LOGGER.info(url);
2014-04-23 05:49:11 +02:00
String u = url.toExternalForm();
if (u.contains("?")) {
u = u.substring(0, u.indexOf("?"));
return new URL(u);
}
else {
return url;
}
}
2017-06-19 19:32:57 +02:00
2014-04-23 05:49:11 +02:00
public String getAlbumTitle(URL url) throws MalformedURLException {
try {
// Attempt to use album title as GID
if (pageResponse == null) {
pageResponse = Http.url(url).response();
2014-04-23 05:49:11 +02:00
}
Document albumDoc = pageResponse.parse();
Elements els = albumDoc.select("div.libraryTitle > h1");
if (els.isEmpty()) {
2014-04-23 05:49:11 +02:00
throw new IOException("Could not find libraryTitle at " + url);
}
return els.get(0).text();
} catch (IOException e) {
// Fall back to default album naming convention
}
return super.getAlbumTitle(url);
}
@Override
public String getGID(URL url) throws MalformedURLException {
Pattern p; Matcher m;
// http://s844.photobucket.com/user/SpazzySpizzy/library/Lady%20Gaga?sort=3&page=1
p = Pattern.compile("^https?://[a-zA-Z0-9]+\\.photobucket\\.com/user/([a-zA-Z0-9_\\-]+)/library.*$");
m = p.matcher(url.toExternalForm());
if (m.matches()) {
return m.group(1);
}
throw new MalformedURLException(
"Expected photobucket.com gallery formats: "
+ "http://x###.photobucket.com/username/library/..."
+ " Got: " + url);
}
@Override
public void rip() throws IOException {
List<String> subalbums = ripAlbumAndGetSubalbums(this.url.toExternalForm());
List<String> subsToRip = new ArrayList<>(),
rippedSubs = new ArrayList<>();
2014-04-23 05:49:11 +02:00
for (String sub : subalbums) {
subsToRip.add(sub);
}
while (!subsToRip.isEmpty() && !isStopped()) {
2014-04-23 05:49:11 +02:00
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
break;
}
String nextSub = subsToRip.remove(0);
rippedSubs.add(nextSub);
2018-06-03 03:14:41 +02:00
LOGGER.info("Attempting to rip next subalbum: " + nextSub);
2014-04-23 05:49:11 +02:00
try {
pageResponse = null;
subalbums = ripAlbumAndGetSubalbums(nextSub);
} catch (IOException e) {
2018-06-03 03:14:41 +02:00
LOGGER.error("Error while ripping " + nextSub, e);
2014-04-23 05:49:11 +02:00
break;
}
for (String subalbum : subalbums) {
if (!subsToRip.contains(subalbum) && !rippedSubs.contains(subalbum)) {
subsToRip.add(subalbum);
}
}
}
waitForThreads();
}
private List<String> ripAlbumAndGetSubalbums(String theUrl) throws IOException {
2014-04-23 05:49:11 +02:00
int filesIndex = 0,
filesTotal = 0,
pageIndex = 0;
String currentAlbumPath = null,
url = null;
while (pageIndex == 0 || filesIndex < filesTotal) {
if (isStopped()) {
break;
}
pageIndex++;
if (pageIndex > 1 || pageResponse == null) {
2014-08-03 05:16:11 +02:00
url = theUrl + String.format("?sort=3&page=%d", pageIndex);
2018-06-03 03:14:41 +02:00
LOGGER.info(" Retrieving " + url);
pageResponse = Http.url(url).response();
2014-04-23 05:49:11 +02:00
}
Document albumDoc = pageResponse.parse();
// Retrieve JSON from request
String jsonString = null;
for (Element script : albumDoc.select("script[type=text/javascript]")) {
String data = script.data();
2014-08-03 05:16:11 +02:00
// Ensure this chunk of javascript contains the album info
2014-04-23 05:49:11 +02:00
if (!data.contains("libraryAlbumsPageCollectionData")) {
continue;
}
// Grab the JSON
Pattern p; Matcher m;
p = Pattern.compile("^.*collectionData: (\\{.*}).*$", Pattern.DOTALL);
2014-04-23 05:49:11 +02:00
m = p.matcher(data);
if (m.matches()) {
jsonString = m.group(1);
break;
}
}
if (jsonString == null) {
2018-06-03 03:14:41 +02:00
LOGGER.error("Unable to find JSON data at URL: " + url);
2014-04-23 05:49:11 +02:00
break;
}
JSONObject json = new JSONObject(jsonString);
JSONObject items = json.getJSONObject("items");
JSONArray objects = items.getJSONArray("objects");
filesTotal = items.getInt("total");
currentAlbumPath = json.getString("currentAlbumPath");
for (int i = 0; i < objects.length(); i++) {
JSONObject object = objects.getJSONObject(i);
String image = object.getString("fullsizeUrl");
filesIndex += 1;
2017-06-19 19:32:57 +02:00
addURLToDownload(new URL(image),
2014-04-23 05:49:11 +02:00
"",
object.getString("location").replaceAll(" ", "_"),
2014-04-23 05:49:11 +02:00
albumDoc.location(),
pageResponse.cookies());
}
}
// Get subalbums
if (url != null) {
return getSubAlbums(url, currentAlbumPath);
} else {
return new ArrayList<>();
2014-04-23 05:49:11 +02:00
}
}
2017-06-19 19:32:57 +02:00
2014-04-23 05:49:11 +02:00
private List<String> getSubAlbums(String url, String currentAlbumPath) {
List<String> result = new ArrayList<>();
2014-04-23 05:49:11 +02:00
String subdomain = url.substring(url.indexOf("://")+3);
subdomain = subdomain.substring(0, subdomain.indexOf("."));
String apiUrl = "http://" + subdomain + ".photobucket.com/component/Albums-SubalbumList"
+ "?deferCollapsed=true"
+ "&albumPath=" + currentAlbumPath // %2Falbums%2Fab10%2FSpazzySpizzy"
+ "&json=1";
try {
2018-06-03 03:14:41 +02:00
LOGGER.info("Loading " + apiUrl);
JSONObject json = Http.url(apiUrl).getJSON();
2014-04-23 05:49:11 +02:00
JSONArray subalbums = json.getJSONObject("body").getJSONArray("subAlbums");
for (int i = 0; i < subalbums.length(); i++) {
2017-06-19 19:32:57 +02:00
String suburl =
2014-04-23 05:49:11 +02:00
"http://"
+ subdomain
+ ".photobucket.com"
+ subalbums.getJSONObject(i).getString("path");
suburl = suburl.replace(" ", "%20");
result.add(suburl);
}
} catch (IOException e) {
2018-06-03 03:14:41 +02:00
LOGGER.error("Failed to get subalbums from " + apiUrl, e);
2014-04-23 05:49:11 +02:00
}
return result;
}
public boolean canRip(URL url) {
return url.getHost().endsWith(DOMAIN);
}
}