2014-03-16 23:30:41 +01:00
|
|
|
package com.rarchives.ripme.ripper.rippers;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.net.MalformedURLException;
|
|
|
|
import java.net.URL;
|
2014-06-23 04:12:29 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2014-03-16 23:30:41 +01:00
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
2014-06-23 04:12:29 +02:00
|
|
|
import com.rarchives.ripme.ripper.AbstractJSONRipper;
|
2014-06-22 02:08:42 +02:00
|
|
|
import com.rarchives.ripme.utils.Http;
|
2014-03-16 23:30:41 +01:00
|
|
|
import com.rarchives.ripme.utils.Utils;
|
|
|
|
|
2014-06-23 04:12:29 +02:00
|
|
|
public class GonewildRipper extends AbstractJSONRipper {
|
2014-03-16 23:30:41 +01:00
|
|
|
|
2014-06-23 04:12:29 +02:00
|
|
|
private static final int count = 50;
|
|
|
|
private int startIndex = 0;
|
2014-03-16 23:30:41 +01:00
|
|
|
private static String API_DOMAIN;
|
|
|
|
private String username;
|
|
|
|
|
|
|
|
public GonewildRipper(URL url) throws IOException {
|
|
|
|
super(url);
|
|
|
|
API_DOMAIN = Utils.getConfigString("gw.api", "gonewild");
|
|
|
|
}
|
|
|
|
|
2014-06-23 04:12:29 +02:00
|
|
|
@Override
|
|
|
|
public String getHost() {
|
|
|
|
return "gonewild";
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public String getDomain() {
|
|
|
|
return "gonewild.com";
|
|
|
|
}
|
|
|
|
|
2014-03-16 23:30:41 +01:00
|
|
|
@Override
|
|
|
|
public boolean canRip(URL url) {
|
|
|
|
return getUsernameMatcher(url).matches();
|
|
|
|
}
|
|
|
|
|
|
|
|
private Matcher getUsernameMatcher(URL url) {
|
2014-04-04 09:38:40 +02:00
|
|
|
Pattern p = Pattern.compile("^.*gonewild(\\.com?/|:)(user/)?([a-zA-Z0-9\\-_]{3,})[/?]?.*$");
|
2014-03-16 23:30:41 +01:00
|
|
|
return p.matcher(url.toExternalForm());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-06-23 04:12:29 +02:00
|
|
|
public String getGID(URL url) throws MalformedURLException {
|
|
|
|
Matcher m = getUsernameMatcher(url);
|
|
|
|
if (m.matches()) {
|
|
|
|
this.username = m.group(m.groupCount());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw new MalformedURLException("Expected format: gonewild.com/<user>");
|
|
|
|
}
|
|
|
|
return username;
|
2014-03-16 23:30:41 +01:00
|
|
|
}
|
2014-06-23 04:12:29 +02:00
|
|
|
|
2014-03-16 23:30:41 +01:00
|
|
|
@Override
|
2014-06-23 04:12:29 +02:00
|
|
|
public JSONObject getFirstPage() throws IOException {
|
|
|
|
String gwURL = "http://" + API_DOMAIN + ".rarchives.com/api.cgi"
|
2014-03-16 23:30:41 +01:00
|
|
|
+ "?method=get_user"
|
|
|
|
+ "&user=" + username
|
2014-06-23 04:12:29 +02:00
|
|
|
+ "&count=" + count
|
|
|
|
+ "&start=" + startIndex;
|
|
|
|
JSONObject nextJSON = Http.url(gwURL).getJSON();
|
|
|
|
if (nextJSON.has("error")) {
|
|
|
|
throw new IOException(nextJSON.getString("error"));
|
2014-03-16 23:30:41 +01:00
|
|
|
}
|
2014-06-23 04:12:29 +02:00
|
|
|
if (nextJSON.getJSONArray("posts").length() == 0) {
|
|
|
|
throw new IOException("No posts found");
|
|
|
|
}
|
|
|
|
return nextJSON;
|
2014-03-16 23:30:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-06-23 04:12:29 +02:00
|
|
|
public JSONObject getNextPage(JSONObject json) throws IOException {
|
|
|
|
startIndex += count;
|
|
|
|
sleep(1000);
|
|
|
|
return getFirstPage();
|
2014-03-16 23:30:41 +01:00
|
|
|
}
|
2014-06-23 04:12:29 +02:00
|
|
|
|
2014-03-16 23:30:41 +01:00
|
|
|
@Override
|
2014-06-23 04:12:29 +02:00
|
|
|
public List<String> getURLsFromJSON(JSONObject json) {
|
|
|
|
List<String> imageURLs = new ArrayList<String>();
|
|
|
|
JSONArray posts = json.getJSONArray("posts");
|
|
|
|
for (int i = 0; i < posts.length(); i++) {
|
|
|
|
JSONObject post = posts.getJSONObject(i);
|
|
|
|
JSONArray images = post.getJSONArray("images");
|
|
|
|
for (int j = 0; j < images.length(); j++) {
|
|
|
|
JSONObject image = images.getJSONObject(j);
|
|
|
|
String imagePath = image.getString("path");
|
|
|
|
if (imagePath.startsWith("..")) {
|
|
|
|
imagePath = imagePath.substring(2);
|
|
|
|
}
|
|
|
|
imagePath = "http://" + API_DOMAIN + ".rarchives.com" + imagePath;
|
|
|
|
imageURLs.add(imagePath);
|
|
|
|
}
|
2014-03-16 23:30:41 +01:00
|
|
|
}
|
2014-06-23 04:12:29 +02:00
|
|
|
return imageURLs;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void downloadURL(URL url, int index) {
|
|
|
|
addURLToDownload(url, getPrefix(index));
|
2014-03-16 23:30:41 +01:00
|
|
|
}
|
|
|
|
}
|