Merge pull request #588 from cyian-1756/ehentaiTagSkip

Can now blacklist nhentai tags
This commit is contained in:
cyian-1756 2018-05-16 12:02:19 -04:00 committed by GitHub
commit 1cb845fc1e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 84 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package com.rarchives.ripme.ripper.rippers;
import com.rarchives.ripme.ripper.AbstractHTMLRipper; import com.rarchives.ripme.ripper.AbstractHTMLRipper;
import com.rarchives.ripme.ripper.DownloadThreadPool; import com.rarchives.ripme.ripper.DownloadThreadPool;
import com.rarchives.ripme.ui.RipStatusMessage;
import com.rarchives.ripme.utils.Http; import com.rarchives.ripme.utils.Http;
import com.rarchives.ripme.utils.Utils; import com.rarchives.ripme.utils.Utils;
import org.jsoup.nodes.Document; import org.jsoup.nodes.Document;
@ -64,6 +65,39 @@ public class NhentaiRipper extends AbstractHTMLRipper {
return "nhentai" + title; return "nhentai" + title;
} }
private List<String> getTags(Document doc) {
List<String> tags = new ArrayList<>();
for (Element tag : doc.select("a.tag")) {
tags.add(tag.attr("href").replaceAll("/tag/", "").replaceAll("/", ""));
}
return tags;
}
/**
* Checks for blacklisted tags on page. If it finds one it returns it, if not it return null
*
* @param doc
* @return String
*/
public String checkTags(Document doc, String[] blackListedTags) {
// If the user hasn't blacklisted any tags we return false;
if (blackListedTags == null) {
return null;
}
logger.info("Blacklisted tags " + blackListedTags[0]);
List<String> tagsOnPage = getTags(doc);
for (String tag : blackListedTags) {
for (String pageTag : tagsOnPage) {
// We replace all dashes in the tag with spaces because the tags we get from the site are separated using
// dashes
if (tag.trim().toLowerCase().equals(pageTag.replaceAll("-", " ").toLowerCase())) {
return tag;
}
}
}
return null;
}
@Override @Override
public String getGID(URL url) throws MalformedURLException { public String getGID(URL url) throws MalformedURLException {
// Ex: https://nhentai.net/g/159174/ // Ex: https://nhentai.net/g/159174/
@ -82,6 +116,13 @@ public class NhentaiRipper extends AbstractHTMLRipper {
if (firstPage == null) { if (firstPage == null) {
firstPage = Http.url(url).get(); firstPage = Http.url(url).get();
} }
String blacklistedTag = checkTags(firstPage, Utils.getConfigStringArray("nhentai.blacklist.tags"));
if (blacklistedTag != null) {
sendUpdate(RipStatusMessage.STATUS.DOWNLOAD_WARN, "Skipping " + url.toExternalForm() + " as it " +
"contains the blacklisted tag \"" + blacklistedTag + "\"");
return null;
}
return firstPage; return firstPage;
} }

View File

@ -92,6 +92,16 @@ public class Utils {
public static String getConfigString(String key, String defaultValue) { public static String getConfigString(String key, String defaultValue) {
return config.getString(key, defaultValue); return config.getString(key, defaultValue);
} }
public static String[] getConfigStringArray(String key) {
String[] s = config.getStringArray(key);
if (s.length == 0) {
return null;
} else {
return s;
}
}
public static int getConfigInteger(String key, int defaultValue) { public static int getConfigInteger(String key, int defaultValue) {
return config.getInt(key, defaultValue); return config.getInt(key, defaultValue);
} }

View File

@ -0,0 +1,33 @@
package com.rarchives.ripme.tst.ripper.rippers;
import java.io.IOException;
import java.net.URL;
import com.rarchives.ripme.ripper.rippers.NhentaiRipper;
public class NhentaiRipperTest extends RippersTest {
public void testRip() throws IOException {
NhentaiRipper ripper = new NhentaiRipper(new URL("https://nhentai.net/g/233295/"));
testRipper(ripper);
}
public void testGetGID() throws IOException {
NhentaiRipper ripper = new NhentaiRipper(new URL("https://nhentai.net/g/233295/"));
assertEquals("233295", ripper.getGID(new URL("https://nhentai.net/g/233295/")));
}
// Test the tag black listing
public void testTagBlackList() throws IOException {
URL url = new URL("https://nhentai.net/g/233295/");
NhentaiRipper ripper = new NhentaiRipper(url);
// Test multiple blacklisted tags
String[] tags = {"test", "one", "blowjob"};
String blacklistedTag = ripper.checkTags(ripper.getFirstPage(), tags);
assertEquals("blowjob", blacklistedTag);
// test tags with spaces in them
String[] tags2 = {"test", "one", "sole female"};
blacklistedTag = ripper.checkTags(ripper.getFirstPage(), tags2);
assertEquals("sole female", blacklistedTag);
}
}