Merge branch 'master' of github.com:LamGC/ContentGrabbingJi

This commit is contained in:
LamGC 2020-05-25 09:25:49 +08:00
commit cb8b01fd74
5 changed files with 42 additions and 24 deletions

View File

@ -6,7 +6,7 @@
<groupId>net.lamgc</groupId>
<artifactId>ContentGrabbingJi</artifactId>
<version>2.5.2-20200520.1-SNAPSHOT</version>
<version>2.5.2-20200522.1-SNAPSHOT</version>
<repositories>
<repository>

View File

@ -29,6 +29,7 @@ import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
@ -148,7 +149,7 @@ public class BotCommandProcess {
@Command(commandName = "info")
public static String artworkInfo(@Argument(name = "$fromGroup") long fromGroup, @Argument(name = "id") int illustId) {
if(illustId <= 0) {
return "错误的作品id";
return "这个作品Id是错误的!";
}
try {
@ -708,13 +709,6 @@ public class BotCommandProcess {
return reportStore.exists(String.valueOf(illustId));
}
/*
下一目标
添加定时发图
定时发图支持设置关注标签
标签....标签支持搜索吧
*/
/**
* 检查指定作品是否为r18
* @param illustId 作品Id
@ -722,13 +716,22 @@ public class BotCommandProcess {
* @param returnRaw 是否返回原始值
* @return 如果为true, 则不为全年龄
* @throws IOException 获取数据时发生异常时抛出
* @throws NoSuchElementException 当作品不存在时抛出
*/
public static boolean isNoSafe(int illustId, Properties settingProp, boolean returnRaw) throws IOException {
public static boolean isNoSafe(int illustId, Properties settingProp, boolean returnRaw) throws IOException, NoSuchElementException {
boolean rawValue = getIllustInfo(illustId, false).getAsJsonArray("tags").contains(new JsonPrimitive("R-18"));
return returnRaw || settingProp == null ? rawValue : rawValue && !settingProp.getProperty("image.allowR18", "false").equalsIgnoreCase("true");
}
private static JsonObject getIllustInfo(int illustId, boolean flushCache) throws IOException {
/**
* 获取作品信息
* @param illustId 作品Id
* @param flushCache 强制刷新缓存
* @return 返回作品信息
* @throws IOException 当Http请求发生异常时抛出
* @throws NoSuchElementException 当作品未找到时抛出
*/
private static JsonObject getIllustInfo(int illustId, boolean flushCache) throws IOException, NoSuchElementException {
String illustIdStr = buildSyncKey(Integer.toString(illustId));
JsonObject illustInfoObj = null;
if (!illustInfoCache.exists(illustIdStr) || flushCache) {
@ -809,11 +812,19 @@ public class BotCommandProcess {
}
return result;
}
/**
* 获取图片存储目录.
* <p>每次调用都会检查目录是否存在, 如不存在则会抛出异常</p>
* @return 返回File对象
* @throws RuntimeException 当目录创建失败时将包装{@link IOException}异常并抛出.
*/
private static File getImageStoreDir() {
if(!imageStoreDir.exists() && !imageStoreDir.mkdirs()) {
log.warn("酷Q图片缓存目录失效(Path: {} )", imageStoreDir.getAbsolutePath());
throw new RuntimeException(new IOException("文件夹创建失败!"));
if(!imageStoreDir.exists() && !Files.isSymbolicLink(imageStoreDir.toPath())) {
if(!imageStoreDir.mkdirs()) {
log.warn("酷Q图片缓存目录失效(Path: {} )", imageStoreDir.getAbsolutePath());
throw new RuntimeException(new IOException("文件夹创建失败!"));
}
}
return imageStoreDir;
}

View File

@ -27,8 +27,9 @@ public class RankingUpdateTimer {
Calendar cal = Calendar.getInstance();
cal.setTime(firstRunDate == null ? new Date() : firstRunDate);
LocalDate currentLocalDate = LocalDate.now();
if(cal.get(Calendar.DAY_OF_YEAR) <= currentLocalDate.getDayOfYear() &&
cal.get(Calendar.HOUR_OF_DAY) >= 11 && cal.get(Calendar.MINUTE) >= 30) {
if(cal.get(Calendar.DAY_OF_YEAR) < currentLocalDate.getDayOfYear() || (
cal.get(Calendar.DAY_OF_YEAR) == currentLocalDate.getDayOfYear() &&
(cal.get(Calendar.HOUR_OF_DAY) * 60 + cal.get(Calendar.MINUTE) >= 690))) {
cal.set(Calendar.DAY_OF_YEAR, currentLocalDate.getDayOfYear() + 1);
}
cal.set(Calendar.HOUR_OF_DAY, 11);
@ -36,7 +37,7 @@ public class RankingUpdateTimer {
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
long delay = cal.getTime().getTime() - (System.currentTimeMillis());
long delay = cal.getTime().getTime() - System.currentTimeMillis();
log.warn("已设置排行榜定时更新, 首次运行时间: {} ({}min)", cal.getTime(), delay / 1000 / 60);
timer.schedule(new TimerTask() {
@Override

View File

@ -215,12 +215,17 @@ public class BotEventHandler implements EventHandler {
} catch(ParameterNoFoundException e) {
result = "命令缺少参数: " + e.getParameterName();
} catch(DeveloperRunnerException e) {
if (!(e.getCause() instanceof InterruptedException)) {
log.error("执行命令时发生异常", e);
result = "色图姬在执行命令时遇到了一个错误!";
} else {
Throwable cause = e.getCause();
if (cause instanceof InterruptedException) {
log.error("命令执行超时, 终止执行.");
result = "色图姬发现这个命令的处理时间太久了!所以打断了这个命令。";
} else if(cause instanceof NoSuchElementException && cause.getMessage().startsWith("No work found: ")) {
String message = cause.getMessage();
log.error("指定作品不存在.(Id: {})", message.substring(message.lastIndexOf(": ") + 2));
result = "色图姬找不到这个作品!";
} else {
log.error("执行命令时发生异常", e);
result = "色图姬在执行命令时遇到了一个错误!";
}
}
long processTime = System.currentTimeMillis() - time;

View File

@ -542,8 +542,9 @@ public class PixivDownload {
* }
* </pre>
* @throws IOException 当请求发生异常, 或接口返回错误信息时抛出.
* @throws NoSuchElementException 当该作品不存在时抛出异常
*/
public JsonObject getIllustInfoByIllustId(int illustId) throws IOException {
public JsonObject getIllustInfoByIllustId(int illustId) throws IOException, NoSuchElementException {
HttpGet request = createHttpGetRequest(PixivURL.getPixivIllustInfoAPI(illustId));
HttpResponse response = httpClient.execute(request);
String responseStr = EntityUtils.toString(response.getEntity());
@ -558,7 +559,7 @@ public class PixivDownload {
if(illustsArray.size() == 1) {
return illustsArray.get(0).getAsJsonObject();
} else {
return null;
throw new NoSuchElementException("No work found: " + illustId);
}
}