2014-06-05 09:24:29 +02:00
|
|
|
package com.rarchives.ripme.ripper.rippers;
|
|
|
|
|
|
|
|
import java.awt.Desktop;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.net.MalformedURLException;
|
|
|
|
import java.net.URISyntaxException;
|
|
|
|
import java.net.URL;
|
2014-06-25 04:05:54 +02:00
|
|
|
import java.util.ArrayList;
|
2014-06-05 09:24:29 +02:00
|
|
|
import java.util.HashMap;
|
2014-06-25 04:05:54 +02:00
|
|
|
import java.util.List;
|
2014-06-05 09:24:29 +02:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
2014-06-25 04:05:54 +02:00
|
|
|
import javax.net.ssl.SSLException;
|
2014-06-05 09:24:29 +02:00
|
|
|
import javax.swing.JOptionPane;
|
|
|
|
|
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
2014-06-25 04:05:54 +02:00
|
|
|
import com.rarchives.ripme.ripper.AbstractJSONRipper;
|
2014-06-05 09:24:29 +02:00
|
|
|
import com.rarchives.ripme.ui.RipStatusMessage.STATUS;
|
2014-06-22 02:08:42 +02:00
|
|
|
import com.rarchives.ripme.utils.Http;
|
2014-06-05 09:24:29 +02:00
|
|
|
|
2014-06-25 04:05:54 +02:00
|
|
|
public class MediacrushRipper extends AbstractJSONRipper {
|
2014-06-05 09:24:29 +02:00
|
|
|
|
|
|
|
/** Ordered list of preferred formats, sorted by preference (low-to-high) */
|
2017-10-24 16:33:28 +02:00
|
|
|
private static final Map<String, Integer> PREFERRED_FORMATS = new HashMap<>();
|
2014-06-05 09:24:29 +02:00
|
|
|
static {
|
|
|
|
PREFERRED_FORMATS.put("mp4", 0);
|
|
|
|
PREFERRED_FORMATS.put("wemb",1);
|
|
|
|
PREFERRED_FORMATS.put("ogv", 2);
|
|
|
|
PREFERRED_FORMATS.put("mp3", 3);
|
|
|
|
PREFERRED_FORMATS.put("ogg", 4);
|
|
|
|
PREFERRED_FORMATS.put("gif", 5);
|
|
|
|
PREFERRED_FORMATS.put("png", 6);
|
|
|
|
PREFERRED_FORMATS.put("jpg", 7);
|
|
|
|
PREFERRED_FORMATS.put("jpeg",8);
|
2017-10-24 16:33:28 +02:00
|
|
|
}
|
2014-06-05 09:24:29 +02:00
|
|
|
|
|
|
|
public MediacrushRipper(URL url) throws IOException {
|
|
|
|
super(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-06-25 04:05:54 +02:00
|
|
|
public String getHost() {
|
|
|
|
return "mediacrush";
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public String getDomain() {
|
|
|
|
return "mediacru.sh";
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getGID(URL url) throws MalformedURLException {
|
|
|
|
Pattern p = Pattern.compile("https?://[wm.]*mediacru\\.sh/([a-zA-Z0-9]+).*");
|
|
|
|
Matcher m = p.matcher(url.toExternalForm());
|
|
|
|
if (m.matches()) {
|
|
|
|
return m.group(1);
|
|
|
|
}
|
|
|
|
throw new MalformedURLException("Could not find mediacru.sh page ID from " + url
|
|
|
|
+ " expected format: http://mediacru.sh/pageid");
|
2014-06-05 09:24:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public URL sanitizeURL(URL url) throws MalformedURLException {
|
|
|
|
String u = url.toExternalForm();
|
|
|
|
// Strip trailing "/" characters
|
|
|
|
while (u.endsWith("/")) {
|
|
|
|
u = u.substring(0, u.length() - 1);
|
|
|
|
}
|
|
|
|
// Append .json
|
|
|
|
if (!u.endsWith(".json")) {
|
|
|
|
u += ".json";
|
|
|
|
}
|
|
|
|
return new URL(u);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-06-25 04:05:54 +02:00
|
|
|
public JSONObject getFirstPage() throws IOException {
|
2014-06-05 09:24:29 +02:00
|
|
|
try {
|
2014-06-25 04:05:54 +02:00
|
|
|
String jsonString = Http.url(url)
|
|
|
|
.ignoreContentType()
|
|
|
|
.connection()
|
|
|
|
.execute().body();
|
|
|
|
jsonString = jsonString.replace(""", "\"");
|
|
|
|
return new JSONObject(jsonString);
|
|
|
|
} catch (SSLException re) {
|
2014-06-05 09:24:29 +02:00
|
|
|
// Check for >1024 bit encryption but in older versions of Java
|
2014-06-25 04:05:54 +02:00
|
|
|
// It's the bug. Suggest downloading the latest version.
|
|
|
|
int selection = JOptionPane.showOptionDialog(null,
|
|
|
|
"You need to upgrade to the latest Java (7+) to rip this album.\n"
|
|
|
|
+ "Do you want to open java.com and download the latest version?",
|
|
|
|
"RipMe - Java Error",
|
|
|
|
JOptionPane.OK_CANCEL_OPTION,
|
|
|
|
JOptionPane.ERROR_MESSAGE,
|
|
|
|
null,
|
|
|
|
new String[] {"Go to java.com", "Cancel"},
|
|
|
|
0);
|
|
|
|
sendUpdate(STATUS.RIP_ERRORED, "Your version of Java can't handle some secure websites");
|
|
|
|
if (selection == 0) {
|
|
|
|
URL javaUrl = new URL("https://www.java.com/en/download/");
|
|
|
|
try {
|
|
|
|
Desktop.getDesktop().browse(javaUrl.toURI());
|
|
|
|
} catch (URISyntaxException use) { }
|
2014-06-05 09:24:29 +02:00
|
|
|
}
|
2014-06-25 04:05:54 +02:00
|
|
|
throw new IOException("Cannot rip due to limitations in Java installation, consider upgrading Java", re.getCause());
|
2014-06-05 09:24:29 +02:00
|
|
|
}
|
2014-06-25 04:05:54 +02:00
|
|
|
catch (Exception e) {
|
|
|
|
throw new IOException("Unexpected error: " + e.getMessage(), e);
|
2014-06-05 09:24:29 +02:00
|
|
|
}
|
2014-06-25 04:05:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public List<String> getURLsFromJSON(JSONObject json) {
|
2017-10-24 16:33:28 +02:00
|
|
|
List<String> imageURLs = new ArrayList<>();
|
2014-06-05 09:24:29 +02:00
|
|
|
// Iterate over all files
|
|
|
|
JSONArray files = json.getJSONArray("files");
|
|
|
|
for (int i = 0; i < files.length(); i++) {
|
|
|
|
JSONObject file = (JSONObject) files.get(i);
|
|
|
|
// Find preferred file format
|
|
|
|
JSONArray subfiles = file.getJSONArray("files");
|
|
|
|
String preferredUrl = getPreferredUrl(subfiles);
|
|
|
|
if (preferredUrl == null) {
|
2014-06-25 04:05:54 +02:00
|
|
|
logger.warn("Could not find 'file' inside of " + file);
|
2014-06-05 09:24:29 +02:00
|
|
|
sendUpdate(STATUS.DOWNLOAD_ERRORED, "Could not find file inside of " + file);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-06-25 04:05:54 +02:00
|
|
|
imageURLs.add(preferredUrl);
|
2014-06-05 09:24:29 +02:00
|
|
|
}
|
2014-06-25 04:05:54 +02:00
|
|
|
return imageURLs;
|
2014-06-05 09:24:29 +02:00
|
|
|
}
|
2014-06-25 04:05:54 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void downloadURL(URL url, int index) {
|
|
|
|
addURLToDownload(url, getPrefix(index));
|
|
|
|
}
|
|
|
|
|
2014-06-05 09:24:29 +02:00
|
|
|
/**
|
2017-05-10 00:22:55 +02:00
|
|
|
* Iterates over list if "file" objects and returns the preferred
|
2014-06-05 09:24:29 +02:00
|
|
|
* image format.
|
2017-05-10 00:22:55 +02:00
|
|
|
* @param subfiles Array of "files" (JSONObjects) which contain
|
2014-06-05 09:24:29 +02:00
|
|
|
* @return Preferred media format.
|
|
|
|
*/
|
|
|
|
private String getPreferredUrl(JSONArray subfiles) {
|
|
|
|
String preferredUrl = null;
|
|
|
|
int preferredIndex = Integer.MAX_VALUE;
|
|
|
|
// Iterate over all media types
|
|
|
|
for (int j = 0; j < subfiles.length(); j++) {
|
|
|
|
JSONObject subfile = subfiles.getJSONObject(j);
|
|
|
|
String thisurl = subfile.getString("url");
|
|
|
|
String extension = thisurl.substring(thisurl.lastIndexOf(".") + 1);
|
|
|
|
if (!PREFERRED_FORMATS.containsKey(extension)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Keep track of the most-preferred format
|
|
|
|
int thisindex = PREFERRED_FORMATS.get(extension);
|
|
|
|
if (preferredUrl == null || thisindex < preferredIndex) {
|
|
|
|
preferredIndex = thisindex;
|
|
|
|
preferredUrl = thisurl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return preferredUrl;
|
|
|
|
}
|
|
|
|
}
|