From e956e36584a0d02d060a85e7d15b1e5d7782555d Mon Sep 17 00:00:00 2001 From: LamGC Date: Fri, 22 May 2020 20:23:46 +0800 Subject: [PATCH 1/4] =?UTF-8?q?[Fix]=20RankingUpdateTimer=20=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E9=A6=96=E6=AC=A1=E6=89=A7=E8=A1=8C=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E8=AE=A1=E7=AE=97=E9=94=99=E8=AF=AF=E7=9A=84=E9=97=AE=E9=A2=98?= =?UTF-8?q?;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/net/lamgc/cgj/bot/RankingUpdateTimer.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/net/lamgc/cgj/bot/RankingUpdateTimer.java b/src/main/java/net/lamgc/cgj/bot/RankingUpdateTimer.java index 0f35faf..1f02550 100644 --- a/src/main/java/net/lamgc/cgj/bot/RankingUpdateTimer.java +++ b/src/main/java/net/lamgc/cgj/bot/RankingUpdateTimer.java @@ -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 From 7aa00ff98bc850f67d8c40a7368538ce2f4d2312 Mon Sep 17 00:00:00 2001 From: LamGC Date: Fri, 22 May 2020 20:34:15 +0800 Subject: [PATCH 2/4] =?UTF-8?q?[Fix]=20BotCommandProcess=20=E4=BF=AE?= =?UTF-8?q?=E5=A4=8DImageStore=E4=B8=8D=E5=85=81=E8=AE=B8=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E8=BD=AF=E9=93=BE=E6=8E=A5=E7=9A=84=E9=97=AE=E9=A2=98?= =?UTF-8?q?;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../net/lamgc/cgj/bot/BotCommandProcess.java | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/lamgc/cgj/bot/BotCommandProcess.java b/src/main/java/net/lamgc/cgj/bot/BotCommandProcess.java index 77d593d..1dacdbf 100644 --- a/src/main/java/net/lamgc/cgj/bot/BotCommandProcess.java +++ b/src/main/java/net/lamgc/cgj/bot/BotCommandProcess.java @@ -25,6 +25,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; @@ -793,11 +794,19 @@ public class BotCommandProcess { } return result; } - + + /** + * 获取图片存储目录. + *

每次调用都会检查目录是否存在, 如不存在则会抛出异常

+ * @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; } From a3376e96eef8c51acd0e0189eadc80da7a2f7602 Mon Sep 17 00:00:00 2001 From: LamGC Date: Fri, 22 May 2020 20:39:49 +0800 Subject: [PATCH 3/4] =?UTF-8?q?[Add]=20BotCommandProcess,=20BotEventHandle?= =?UTF-8?q?r,=20PixivDownload=20=E5=AF=B9=E4=BD=9C=E5=93=81Id=E4=B8=8D?= =?UTF-8?q?=E5=AD=98=E5=9C=A8=E7=9A=84=E6=83=85=E5=86=B5=E5=81=9A=E5=87=BA?= =?UTF-8?q?=E5=8F=8D=E5=BA=94(=E8=80=8C=E4=B8=8D=E6=98=AF=E4=BD=9C?= =?UTF-8?q?=E4=B8=BA=E5=86=85=E9=83=A8=E5=BC=82=E5=B8=B8=E5=8F=8D=E9=A6=88?= =?UTF-8?q?);?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../net/lamgc/cgj/bot/BotCommandProcess.java | 22 ++++++++++--------- .../lamgc/cgj/bot/event/BotEventHandler.java | 13 +++++++---- .../net/lamgc/cgj/pixiv/PixivDownload.java | 5 +++-- 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/main/java/net/lamgc/cgj/bot/BotCommandProcess.java b/src/main/java/net/lamgc/cgj/bot/BotCommandProcess.java index c0c2254..84a90f3 100644 --- a/src/main/java/net/lamgc/cgj/bot/BotCommandProcess.java +++ b/src/main/java/net/lamgc/cgj/bot/BotCommandProcess.java @@ -149,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 { @@ -709,13 +709,6 @@ public class BotCommandProcess { return reportStore.exists(String.valueOf(illustId)); } - /* - 下一目标: - 添加定时发图 - 定时发图支持设置关注标签 - 标签....标签支持搜索吧 - */ - /** * 检查指定作品是否为r18 * @param illustId 作品Id @@ -723,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) { diff --git a/src/main/java/net/lamgc/cgj/bot/event/BotEventHandler.java b/src/main/java/net/lamgc/cgj/bot/event/BotEventHandler.java index 26b7583..2533d0c 100644 --- a/src/main/java/net/lamgc/cgj/bot/event/BotEventHandler.java +++ b/src/main/java/net/lamgc/cgj/bot/event/BotEventHandler.java @@ -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; diff --git a/src/main/java/net/lamgc/cgj/pixiv/PixivDownload.java b/src/main/java/net/lamgc/cgj/pixiv/PixivDownload.java index 21ebe55..8846d5a 100644 --- a/src/main/java/net/lamgc/cgj/pixiv/PixivDownload.java +++ b/src/main/java/net/lamgc/cgj/pixiv/PixivDownload.java @@ -542,8 +542,9 @@ public class PixivDownload { * } * * @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); } } From d89711b0b9200e634e35a25ad4d976f0e2d4aaba Mon Sep 17 00:00:00 2001 From: LamGC Date: Fri, 22 May 2020 20:42:29 +0800 Subject: [PATCH 4/4] =?UTF-8?q?[Version]=20=E6=9B=B4=E6=96=B0=E7=89=88?= =?UTF-8?q?=E6=9C=AC(2.5.2-20200520.1-SNAPSHOT=20->=202.5.2-20200522.1-SNA?= =?UTF-8?q?PSHOT);?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index e3d74a3..af075c3 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ net.lamgc ContentGrabbingJi - 2.5.2-20200520.1-SNAPSHOT + 2.5.2-20200522.1-SNAPSHOT