Skip to content

Commit

Permalink
new URI instead of new URL.
Browse files Browse the repository at this point in the history
  • Loading branch information
soloturn committed Mar 20, 2024
1 parent 404da9b commit b8a2953
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,6 @@ public String getGID(URL url) throws MalformedURLException {
+ " Got: " + url);
}

/**
* Attempts to get page, checks for IP ban, waits.
*
* @param url
* @return Page document
* @throws IOException If page loading errors, or if retries are exhausted
*/
private Document getPageWithRetries(URL url) throws IOException {
Document doc;
int retries = 3;
Expand Down Expand Up @@ -251,16 +244,16 @@ private void fetchImage() {
savePath += String.format("%03d_", index);
}
savePath += m.group(1);
addURLToDownload(new URL(imgsrc), Paths.get(savePath));
addURLToDownload(new URI(imgsrc).toURL(), Paths.get(savePath));
} else {
// Provide prefix and let the AbstractRipper "guess" the filename
String prefix = "";
if (Utils.getConfigBoolean("download.save_order", true)) {
prefix = String.format("%03d_", index);
}
addURLToDownload(new URL(imgsrc), prefix);
addURLToDownload(new URI(imgsrc).toURL(), prefix);
}
} catch (IOException e) {
} catch (IOException | URISyntaxException e) {
LOGGER.error("[!] Exception while loading/parsing " + this.url, e);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.rarchives.ripme.ripper.rippers;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.*;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
Expand Down Expand Up @@ -164,8 +162,8 @@ private String getUserID(String username) throws IOException {
}

@Override
public JSONObject getFirstPage() throws IOException {
URL apiURL = new URL(baseURL + "&consumer_key=" + CONSUMER_KEY);
public JSONObject getFirstPage() throws IOException, URISyntaxException {
URL apiURL = new URI(baseURL + "&consumer_key=" + CONSUMER_KEY).toURL();
LOGGER.debug("apiURL: " + apiURL);
JSONObject json = Http.url(apiURL).getJSON();

Expand Down Expand Up @@ -232,7 +230,7 @@ else if (baseURL.contains("/blogs?")) {
}

@Override
public JSONObject getNextPage(JSONObject json) throws IOException {
public JSONObject getNextPage(JSONObject json) throws IOException, URISyntaxException {
if (isThisATest()) {
return null;
}
Expand All @@ -249,9 +247,9 @@ public JSONObject getNextPage(JSONObject json) throws IOException {

sleep(500);
++page;
URL apiURL = new URL(baseURL
URL apiURL = new URI(baseURL
+ "&page=" + page
+ "&consumer_key=" + CONSUMER_KEY);
+ "&consumer_key=" + CONSUMER_KEY).toURL();
return Http.url(apiURL).getJSON();
}

Expand Down Expand Up @@ -296,28 +294,23 @@ public List<String> getURLsFromJSON(JSONObject json) {
}
}
}
if (imageURL == null) {
LOGGER.error("Failed to find image for photo " + photo.toString());
}
else {
imageURLs.add(imageURL);
if (isThisATest()) {
break;
}
imageURLs.add(imageURL);
if (isThisATest()) {
break;
}
}
return imageURLs;
}

private boolean urlExists(String url) {
try {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
HttpURLConnection connection = (HttpURLConnection) new URI(url).toURL().openConnection();
connection.setRequestMethod("HEAD");
if (connection.getResponseCode() != 200) {
throw new IOException("Couldn't find full-size image at " + url);
}
return true;
} catch (IOException e) {
} catch (IOException | URISyntaxException e) {
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -40,15 +42,15 @@ public String getDomain() {
}

@Override
public URL sanitizeURL(URL url) throws MalformedURLException {
public URL sanitizeURL(URL url) throws MalformedURLException, URISyntaxException {
String u = url.toExternalForm();
if (u.contains("/thumbs/")) {
u = u.replace("/thumbs/", "/full/");
}
if (u.contains("/expanded/")) {
u = u.replaceAll("/expanded/", "/full/");
}
return new URL(u);
return new URI(u).toURL();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Base64;
Expand Down Expand Up @@ -80,16 +82,15 @@ protected List<String> getURLsFromJSON(JSONObject json) throws JSONException {
}

@Override
protected JSONObject getFirstPage() throws IOException {
protected JSONObject getFirstPage() throws IOException, URISyntaxException {
String jsonEncodedString = getJsonEncodedStringFromPage();
String jsonDecodedString = decodeJsonString(jsonEncodedString);
return new JSONObject(jsonDecodedString);
}

public String getJsonEncodedStringFromPage() throws MalformedURLException, IOException
{
public String getJsonEncodedStringFromPage() throws MalformedURLException, IOException, URISyntaxException {
// Image data only appears on the /read/ page and not on the /view/ one.
URL readUrl = new URL(String.format("http://hentainexus.com/read/%s",getGID(url)));
URL readUrl = new URI(String.format("http://hentainexus.com/read/%s",getGID(url))).toURL();
Document document = Http.url(readUrl).response().parse();

for (Element scripts : document.getElementsByTag("script")) {
Expand Down Expand Up @@ -143,7 +144,7 @@ The following code is a Java adaptation of the initRender() JavaScript function
}

magicByte = (byte) (magicByte & 0x7);
ArrayList<Integer> newArray = new ArrayList();
ArrayList<Integer> newArray = new ArrayList<>();

for (int i = 0x0; i < 0x100; i++) {
newArray.add(i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -34,7 +36,7 @@ public String getHost() {


@Override
public URL sanitizeURL(URL url) throws MalformedURLException {
public URL sanitizeURL(URL url) throws MalformedURLException, URISyntaxException {
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
Expand All @@ -44,15 +46,15 @@ public URL sanitizeURL(URL url) throws MalformedURLException {
throw new MalformedURLException("Invalid URL: " + url);
}

return new URL(u);
return new URI(u).toURL();
}

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());
URL getPage(int page) throws MalformedURLException, URISyntaxException {
return new URI("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()).toURL();
}


Expand All @@ -71,18 +73,18 @@ public String getGID(URL url) throws MalformedURLException {
int currentPage = 1;

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

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

@Override
protected JSONObject getNextPage(JSONObject doc) throws IOException {
protected JSONObject getNextPage(JSONObject doc) throws IOException, URISyntaxException {
currentPage++;
JSONObject nextPage = Http.url(getPage(doc.getInt("page") + 1)).getJSON();
JSONArray items = nextPage.getJSONArray("items");
if (items.length() == 0) {
if (items.isEmpty()) {
throw new IOException("No more pages");
}
return nextPage;
Expand Down Expand Up @@ -120,7 +122,7 @@ protected List<String> getURLsFromJSON(JSONObject json) {

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

data.forEach(e -> descriptions.add(e.title));
return data.stream().map(e -> e.srcUrl).collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -56,7 +58,7 @@ public String getGID(URL url) throws MalformedURLException {
}

@Override
public void rip() throws IOException {
public void rip() throws IOException, URISyntaxException {
String vidUrl = "";
LOGGER.info(" Retrieving " + this.url.toExternalForm());
Document doc = Http.url(this.url).get();
Expand Down Expand Up @@ -146,7 +148,7 @@ public void rip() throws IOException {
if (vidUrl.equals("")) {
throw new IOException("Unable to find encrypted video URL at " + this.url);
}
addURLToDownload(new URL(vidUrl), HOST + "_" + bestQuality + "p_" + getGID(this.url));
addURLToDownload(new URI(vidUrl).toURL(), HOST + "_" + bestQuality + "p_" + getGID(this.url));

waitForThreads();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -32,11 +34,6 @@ public boolean canRip(URL url) {
return m.matches();
}

@Override
public URL sanitizeURL(URL url) throws MalformedURLException {
return url;
}

@Override
public String getGID(URL url) throws MalformedURLException {
Pattern p = Pattern.compile("^https?://.*stickyxxx\\.com(/)(.*)/$");
Expand All @@ -52,15 +49,15 @@ public String getGID(URL url) throws MalformedURLException {
}

@Override
public void rip() throws IOException {
public void rip() throws IOException, URISyntaxException {
LOGGER.info("Retrieving " + this.url);
Document doc = Http.url(url).get();
Elements videos = doc.select(".wp-video > video > source");
if (videos.isEmpty()) {
throw new IOException("Could not find Embed code at " + url);
}
String vidUrl = videos.attr("src");
addURLToDownload(new URL(vidUrl), HOST + "_" + getGID(this.url));
addURLToDownload(new URI(vidUrl).toURL(), HOST + "_" + getGID(this.url));
waitForThreads();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.List;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -33,11 +35,6 @@ public boolean canRip(URL url) {
return m.matches();
}

@Override
public URL sanitizeURL(URL url) throws MalformedURLException {
return url;
}

@Override
public String getGID(URL url) throws MalformedURLException {
Pattern p = Pattern.compile("^https?://[wm.]*videarn\\.com/[a-zA-Z0-9\\-]+/([0-9]+).*$");
Expand All @@ -53,15 +50,15 @@ public String getGID(URL url) throws MalformedURLException {
}

@Override
public void rip() throws IOException {
public void rip() throws IOException, URISyntaxException {
LOGGER.info("Retrieving " + this.url);
Document doc = Http.url(url).get();
List<String> mp4s = Utils.between(doc.html(), "file:\"", "\"");
if (mp4s.isEmpty()) {
throw new IOException("Could not find files at " + url);
}
String vidUrl = mp4s.get(0);
addURLToDownload(new URL(vidUrl), HOST + "_" + getGID(this.url));
String vidUrl = mp4s.getFirst();
addURLToDownload(new URI(vidUrl).toURL(), HOST + "_" + getGID(this.url));
waitForThreads();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
import com.rarchives.ripme.ripper.rippers.video.StickyXXXRipper;
// import com.rarchives.ripme.tst.ripper.rippers.RippersTest;
import com.rarchives.ripme.utils.Utils;
import org.junit.jupiter.api.Test;

public class StickyXXXRipperTest extends RippersTest {

@Test
public void testStickyXXXVideo() throws IOException, URISyntaxException {
// This test fails on the CI - possibly due to checking for a file before it's written - so we're skipping it
if (Utils.getConfigBoolean("test.run_flaky_tests", false)) {
Expand Down

0 comments on commit b8a2953

Please sign in to comment.