Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a ripper for nsfw.xxx #2000

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
133 changes: 133 additions & 0 deletions src/main/java/com/rarchives/ripme/ripper/rippers/NsfwXxxRipper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package com.rarchives.ripme.ripper.rippers;

import com.rarchives.ripme.ripper.AbstractJSONRipper;
import com.rarchives.ripme.utils.Http;
import org.apache.commons.lang.StringEscapeUtils;
import org.json.JSONArray;
import org.json.JSONObject;

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 java.util.stream.Collectors;
import java.util.stream.IntStream;

public class NsfwXxxRipper extends AbstractJSONRipper {

public NsfwXxxRipper(URL url) throws IOException {
super(url);
}

@Override
protected String getDomain() {
return "nsfw.xxx";
}

@Override
public String getHost() {
return "nsfw_xxx";
}


@Override
public URL sanitizeURL(URL url) throws MalformedURLException {
String u = url.toExternalForm();
// https://nsfw.xxx/user/kelly-kat/foo -> https://nsfw.xxx/user/kelly-kat
// https://nsfw.xxx/user/kelly-kat -> https://nsfw.xxx/user/kelly-kat
// keep up to and including the username
u = u.replaceAll("https?://nsfw.xxx/user/([^/]+)/?.*", "https://nsfw.xxx/user/$1");
if (!u.contains("nsfw.xxx/user")) {
throw new MalformedURLException("Invalid URL: " + url);
}

return new URL(u);
}

String getUser() throws MalformedURLException {
return getGID(url);
}

URL getPage(int page) throws MalformedURLException {
return new URL("https://nsfw.xxx/slide-page/" + page + "?nsfw%5B%5D=0&types%5B%5D=image&types%5B%5D=video&types%5B%5D=gallery&slider=1&jsload=1&user=" + getUser());
}


@Override
public String getGID(URL url) throws MalformedURLException {
Pattern p = Pattern.compile("https://nsfw.xxx/user/([^/]+)/?$");
Matcher m = p.matcher(url.toExternalForm());
if (m.matches()) {
return m.group(1);
}
throw new MalformedURLException("Expected URL format: " +
"nsfw.xxx/user/USER - got " + url + " instead");
}


int currentPage = 1;

@Override
protected JSONObject getFirstPage() throws IOException {
return Http.url(getPage(1)).getJSON();
}

List<String> descriptions = new ArrayList<>();

@Override
protected JSONObject getNextPage(JSONObject doc) throws IOException {
currentPage++;
JSONObject nextPage = Http.url(getPage(doc.getInt("page") + 1)).getJSON();
JSONArray items = nextPage.getJSONArray("items");
if (items.length() == 0) {
throw new IOException("No more pages");
}
return nextPage;
}

class ApiEntry {
String srcUrl;
String author;
String title;

public ApiEntry(String srcUrl, String author, String title) {
this.srcUrl = srcUrl;
this.author = author;
this.title = title;
}
}

@Override
protected List<String> getURLsFromJSON(JSONObject json) {
JSONArray items = json.getJSONArray("items");
List<ApiEntry> data = IntStream
.range(0, items.length())
.mapToObj(items::getJSONObject)
.map(o -> {
String srcUrl;
if(o.has("src")) {
srcUrl = o.getString("src");
} else {
// video source
Pattern videoHtmlSrcPattern = Pattern.compile("src=\"([^\"]+)\"");
Matcher matches = videoHtmlSrcPattern.matcher(o.getString("html"));
matches.find();
srcUrl = StringEscapeUtils.unescapeHtml(matches.group(1));
}

return new ApiEntry(srcUrl, o.getString("author"), o.getString("title"));
})
.collect(Collectors.toList());

data.forEach(e -> descriptions.add(e.title));
return data.stream().map(e -> e.srcUrl).collect(Collectors.toList());
}

@Override
protected void downloadURL(URL url, int index) {
addURLToDownload(url, getPrefix(index) + descriptions.get(index - 1) + "_" , "", "", null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.rarchives.ripme.tst.ripper.rippers;

import com.rarchives.ripme.ripper.rippers.NsfwXxxRipper;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.net.URL;

public class NsfwXxxRipperTest extends RippersTest {
@Test
public void testNsfwXxxUser() throws IOException {
NsfwXxxRipper ripper = new NsfwXxxRipper(new URL("https://nsfw.xxx/user/smay3991"));
testRipper(ripper);
}
}