|
| 1 | +package com.rarchives.ripme.ripper.rippers; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.net.MalformedURLException; |
| 5 | +import java.net.URL; |
| 6 | +import java.util.HashMap; |
| 7 | +import java.util.Map; |
| 8 | +import java.util.regex.Matcher; |
| 9 | +import java.util.regex.Pattern; |
| 10 | + |
| 11 | +import org.apache.log4j.Logger; |
| 12 | +import org.json.JSONArray; |
| 13 | +import org.json.JSONObject; |
| 14 | +import org.jsoup.Connection.Method; |
| 15 | +import org.jsoup.Connection.Response; |
| 16 | +import org.jsoup.Jsoup; |
| 17 | +import org.jsoup.nodes.Document; |
| 18 | + |
| 19 | +import com.rarchives.ripme.ripper.AlbumRipper; |
| 20 | +import com.rarchives.ripme.ui.RipStatusMessage.STATUS; |
| 21 | + |
| 22 | +public class ModelmayhemRipper extends AlbumRipper { |
| 23 | + |
| 24 | + private static final String DOMAIN = "modelmayhem.com", |
| 25 | + HOST = "modelmayhem"; |
| 26 | + private static final Logger logger = Logger.getLogger(ModelmayhemRipper.class); |
| 27 | + |
| 28 | + public ModelmayhemRipper(URL url) throws IOException { |
| 29 | + super(url); |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + public boolean canRip(URL url) { |
| 34 | + return (url.getHost().endsWith(DOMAIN)); |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public URL sanitizeURL(URL url) throws MalformedURLException { |
| 39 | + return url; |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public void rip() throws IOException { |
| 44 | + Map<String,String> cookies = null, |
| 45 | + postData = new HashMap<String,String>(); |
| 46 | + String gid = getGID(this.url), |
| 47 | + ref = "http://www.modelmayhem.com/" + gid; |
| 48 | + |
| 49 | + Response resp = null; |
| 50 | + String theurl = "http://www.modelmayhem.com/" + gid; |
| 51 | + logger.info("Loading " + theurl); |
| 52 | + resp = Jsoup.connect(theurl) |
| 53 | + .timeout(5000) |
| 54 | + .referrer("") |
| 55 | + .userAgent(USER_AGENT) |
| 56 | + .method(Method.GET) |
| 57 | + .execute(); |
| 58 | + cookies = resp.cookies(); |
| 59 | + |
| 60 | + resp = Jsoup.connect("http://www.modelmayhem.com/includes/js/auth.php") |
| 61 | + .cookies(cookies) |
| 62 | + .referrer(ref) |
| 63 | + .userAgent(USER_AGENT) |
| 64 | + .method(Method.GET) |
| 65 | + .execute(); |
| 66 | + String authText = resp.parse().html(); |
| 67 | + String mmservice = authText.substring(authText.indexOf("token = '") + 9); |
| 68 | + mmservice = mmservice.substring(0, mmservice.indexOf("'")); |
| 69 | + |
| 70 | + cookies.putAll(resp.cookies()); |
| 71 | + |
| 72 | + cookies.put("worksafe", "0"); |
| 73 | + theurl = "http://www.modelmayhem.com/services/photo_viewer/albums/" + gid; |
| 74 | + postData.put("MMSERVICE", mmservice); |
| 75 | + resp = Jsoup.connect(theurl) |
| 76 | + .data(postData) |
| 77 | + .cookies(cookies) |
| 78 | + .referrer(ref) |
| 79 | + .userAgent(USER_AGENT) |
| 80 | + .method(Method.POST) |
| 81 | + .execute(); |
| 82 | + cookies.putAll(resp.cookies()); |
| 83 | + |
| 84 | + theurl = "http://www.modelmayhem.com/services/photo_viewer/pictures/" + gid + "/0/0/1/0"; |
| 85 | + this.sendUpdate(STATUS.LOADING_RESOURCE, theurl); |
| 86 | + logger.info("Loading " + theurl); |
| 87 | + resp = Jsoup.connect(theurl) |
| 88 | + .data(postData) |
| 89 | + .cookies(cookies) |
| 90 | + .referrer(ref) |
| 91 | + .userAgent(USER_AGENT) |
| 92 | + .method(Method.POST) |
| 93 | + .execute(); |
| 94 | + |
| 95 | + Document doc = resp.parse(); |
| 96 | + String jsonText = doc.body().html(); |
| 97 | + jsonText = jsonText.replace(""", "\""); |
| 98 | + System.err.println(jsonText); |
| 99 | + JSONObject json = new JSONObject(jsonText); |
| 100 | + JSONArray pictures = json.getJSONArray("pictures"); |
| 101 | + for (int i = 0; i < pictures.length(); i++) { |
| 102 | + JSONObject picture = pictures.getJSONObject(i); |
| 103 | + String bigImage = picture.getString("big_image"); |
| 104 | + if (bigImage.trim().equals("")) { |
| 105 | + logger.info("Got empty image for " + picture.toString(2)); |
| 106 | + continue; |
| 107 | + } |
| 108 | + addURLToDownload(new URL(bigImage), String.format("%03d_", i + 1)); |
| 109 | + } |
| 110 | + waitForThreads(); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public String getHost() { |
| 115 | + return HOST; |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public String getGID(URL url) throws MalformedURLException { |
| 120 | + Pattern p = Pattern.compile("^https?://[w.]*modelmayhem.com.*/([0-9]+)/?.*$"); |
| 121 | + Matcher m = p.matcher(url.toExternalForm()); |
| 122 | + if (m.matches()) { |
| 123 | + return m.group(1); |
| 124 | + } |
| 125 | + throw new MalformedURLException("Modelmayhem user ID not found in " + url + ", expected http://modelmayhem.com/userid"); |
| 126 | + } |
| 127 | + |
| 128 | +} |
0 commit comments