mirror of
https://github.com/LamGC/ContentGrabbingJi.git
synced 2025-04-30 06:37:36 +00:00
[Add] 初步添加成人内容检测器, 尚未使用;
This commit is contained in:
parent
b53aafa81b
commit
5a52dd9208
33
src/main/java/net/lamgc/cgj/pixiv/AdultContentDetector.java
Normal file
33
src/main/java/net/lamgc/cgj/pixiv/AdultContentDetector.java
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
package net.lamgc.cgj.pixiv;
|
||||||
|
|
||||||
|
public interface AdultContentDetector {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查某一作品的成人内容判断指数
|
||||||
|
* @param illustId 作品Id
|
||||||
|
* @param isUgoira 是否为动图
|
||||||
|
* @param pageIndex 指定页数, 设为0或负数则视为单页面作品
|
||||||
|
* @return 返回成人作品判断指数(0 ~ 1), 需按照情况设置阀值.
|
||||||
|
*/
|
||||||
|
double detect(int illustId, boolean isUgoira, int pageIndex) throws Exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查某一作品是否为成人内容
|
||||||
|
* @param illustId 作品Id
|
||||||
|
* @param isUgoira 是否为动图
|
||||||
|
* @param pageIndex 指定页数, 设为0或负数则视为单页面作品
|
||||||
|
* @return 如果为true则为成人作品, 该方法将由检测器决定如何为成人作品.
|
||||||
|
*/
|
||||||
|
boolean isAdultContent(int illustId, boolean isUgoira, int pageIndex) throws Exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查某一作品是否为成人内容
|
||||||
|
* @param illustId 作品Id
|
||||||
|
* @param isUgoira 是否为动图
|
||||||
|
* @param pageIndex 指定页数, 设为0或负数则视为单页面作品
|
||||||
|
* @param threshold 指数阀值, 当等于或大于该阀值时返回true.
|
||||||
|
* @return 如果为true则为成人作品, 该方法将由 threshold 参数决定是否为成人作品.
|
||||||
|
*/
|
||||||
|
boolean isAdultContent(int illustId, boolean isUgoira, int pageIndex, double threshold) throws Exception;
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,65 @@
|
|||||||
|
package net.lamgc.cgj.pixiv;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
import org.apache.http.HttpResponse;
|
||||||
|
import org.apache.http.client.HttpClient;
|
||||||
|
import org.apache.http.client.methods.HttpGet;
|
||||||
|
import org.apache.http.impl.client.HttpClientBuilder;
|
||||||
|
import org.apache.http.util.EntityUtils;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用ModerateContent服务开发的检测器.<br/>
|
||||||
|
* ModerateContent: www.moderatecontent.com
|
||||||
|
*/
|
||||||
|
public class ModerateContentDetector implements AdultContentDetector {
|
||||||
|
|
||||||
|
private final static HttpClient httpClient = HttpClientBuilder.create().build();
|
||||||
|
private final static Gson gson = new Gson();
|
||||||
|
private final String requestUrl;
|
||||||
|
|
||||||
|
private final static String API_URL = "https://www.moderatecontent.com/api/v2?key={key}&url=https://pixiv.cat/";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建一个使用ModerateContent鉴黄服务的检测器
|
||||||
|
* @param apiKey API密钥
|
||||||
|
*/
|
||||||
|
public ModerateContentDetector(String apiKey) {
|
||||||
|
requestUrl = API_URL.replace("{key}", apiKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
private JsonObject accessInterface(int illustId, boolean isUgoira, int pageIndex) throws IOException {
|
||||||
|
HttpResponse response;
|
||||||
|
if(pageIndex <= 0) {
|
||||||
|
response = httpClient.execute(new HttpGet(requestUrl + illustId + (isUgoira ? ".gif" : ".jpg")));
|
||||||
|
} else {
|
||||||
|
response = httpClient.execute(new HttpGet(requestUrl + illustId + "-" + pageIndex + (isUgoira ? ".gif" : ".jpg")));
|
||||||
|
}
|
||||||
|
if(response.getStatusLine().getStatusCode() != 200) {
|
||||||
|
throw new IOException("Http response error: " + response.getStatusLine());
|
||||||
|
}
|
||||||
|
|
||||||
|
JsonObject result = gson.fromJson(EntityUtils.toString(response.getEntity()), JsonObject.class);
|
||||||
|
if (result.get("error_code").getAsInt() != 0) {
|
||||||
|
throw new IOException("Interface result error: " + (result.has("error") ? result.get("error").getAsString() : "(error message is empty)"));
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double detect(int illustId, boolean isUgoira, int pageIndex) throws IOException {
|
||||||
|
return accessInterface(illustId, isUgoira, pageIndex).getAsJsonObject("predictions").get("adult").getAsDouble();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isAdultContent(int illustId, boolean isUgoira, int pageIndex) throws IOException {
|
||||||
|
return accessInterface(illustId, isUgoira, pageIndex).get("rating_index").getAsInt() == 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isAdultContent(int illustId, boolean isUgoira, int pageIndex, double threshold) throws IOException {
|
||||||
|
return detect(illustId, isUgoira, pageIndex) >= threshold;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
package net.lamgc.cgj.pixiv;
|
||||||
|
|
||||||
|
import org.junit.Ignore;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
@Ignore
|
||||||
|
public class ModerateContentDetectorTest {
|
||||||
|
|
||||||
|
private final static AdultContentDetector acd = new ModerateContentDetector("d91b6c3fa2bba9ee8f9e68827ba0d937");
|
||||||
|
private final static Logger log = LoggerFactory.getLogger(ModerateContentDetector.class);
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void checkTest() throws Exception {
|
||||||
|
log.info("Detect: {}, isAdult: {}",
|
||||||
|
acd.detect(80840411, false, 0),
|
||||||
|
acd.isAdultContent(80840411, false, 0));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user