From 553bb135ad3571e01be2513d32465e123e72a369 Mon Sep 17 00:00:00 2001 From: ShrBox Date: Mon, 11 Jan 2021 19:36:56 +0800 Subject: [PATCH] bugfix & new functions --- README.md | 3 +- src/main/java/shrbox/github/acgpro/Main.java | 59 +++++++++++-------- .../java/shrbox/github/acgpro/Thread.java | 34 +++++++---- 3 files changed, 59 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index ce5a835..8226860 100644 --- a/README.md +++ b/README.md @@ -12,9 +12,10 @@ apikey: '' //填写apikey r18: false //是否开启r18 r18-groups: //允许r18的群 - 1145141919810 -limit-mode: false //限制模式,只能同时存在一个任务 FlashImageMode: true //闪照模式,发送图片为闪照 AutoRecall: 0 //自动撤回时间,0为禁用 +MaxThread: 10 //最大线程数 +OriginalImages: false //原图模式 ``` # Download diff --git a/src/main/java/shrbox/github/acgpro/Main.java b/src/main/java/shrbox/github/acgpro/Main.java index 2b4f3b5..5e98aa1 100644 --- a/src/main/java/shrbox/github/acgpro/Main.java +++ b/src/main/java/shrbox/github/acgpro/Main.java @@ -14,64 +14,77 @@ class Main extends PluginBase { public static Config config; - public static boolean isPulling = false; - short count = 0; - public boolean limitMode; - public static boolean flashImageMode; - public static int autoRecall; - public void load_Config() { //配置文件加载 + short pullCount = 0; + public static short threadRunning = 0; + + short maxPullCount = 10; + public boolean limitMode = false; + public static boolean flashImageMode = false; + public static int autoRecall = 0; + short maxThread = 10; + public static boolean originalImages = false; + + public void loadConfig() { //配置文件加载 config = loadConfig("config.yml"); + config.setIfAbsent("apikey", ""); config.setIfAbsent("r18", false); + config.setIfAbsent("MaxPullCount", 10); //每分钟最多请求的次数 + config.setIfAbsent("FlashImageMode", false); //闪照模式 + config.setIfAbsent("AutoRecall", 0); //自动撤回 + config.setIfAbsent("MaxThread", 10); //最大线程数 + config.setIfAbsent("OriginalImages", false); //原图模式 + List r18Groups = new ArrayList<>(); Collections.addAll(r18Groups, 1145141919L, 123123123L); config.setIfAbsent("r18-groups", r18Groups); - config.setIfAbsent("limit-mode", false); - config.setIfAbsent("FlashImageMode", false); - config.setIfAbsent("AutoRecall", 0); - config.save(); r18Groups.clear(); + + config.save(); + + maxPullCount = (short) config.getInt("MaxPullCount"); flashImageMode = config.getBoolean("FlashImageMode"); autoRecall = config.getInt("AutoRecall"); limitMode = config.getBoolean("limit-mode"); + maxThread = (short) config.getInt("MaxThread"); + originalImages = config.getBoolean("OriginalImages"); } - public void onEnable() { - load_Config(); - + public void registerCommands() { JCommandManager.getInstance().register(this, new BlockingCommand( //注册命令 "acgreload", new ArrayList<>(), "重载ACGPro配置文件", "/acghreload" ) { @Override public boolean onCommandBlocking(@NotNull CommandSender commandSender, @NotNull List list) { - load_Config(); + loadConfig(); commandSender.sendMessageBlocking("重载成功"); return true; } }); + } + + public void onEnable() { + loadConfig(); + registerCommands(); new Timer().schedule(new TimerTask() { @Override public void run() { - count = 0; + pullCount = 0; } - }, 60 * 1000, 60 * 1000); //一分钟自动重置变量count + }, 60 * 1000, 60 * 1000); //一分钟自动重置变量 System.setProperty("http.agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; QQBrowser/7.0.3698.400)"); getEventListener().subscribeAlways(GroupMessageEvent.class, (GroupMessageEvent e) -> {//监听群消息 if (e.getMessage().contentToString().toLowerCase().contains("acg")) { - if (limitMode && Main.isPulling) {//如果limit-mode为true,则同时只能存在一个图片下载任务 - e.getGroup().sendMessage("[ACGPro] 正在下载图片,请稍后再试"); - return; - } - if (count > 15) { + if (pullCount > maxPullCount || threadRunning >= maxThread) { e.getGroup().sendMessage(MessageUtils.newChain(new At(e.getSender())).plus("[ACGPro] 请先喝口水再尝试")); return; } - count++; - new Thread().boot(e); + pullCount++; + new Thread().newThread(e); } }); } diff --git a/src/main/java/shrbox/github/acgpro/Thread.java b/src/main/java/shrbox/github/acgpro/Thread.java index 5280cda..d618ea2 100644 --- a/src/main/java/shrbox/github/acgpro/Thread.java +++ b/src/main/java/shrbox/github/acgpro/Thread.java @@ -15,7 +15,7 @@ public class Thread extends java.lang.Thread { GroupMessageEvent e; - public void boot(GroupMessageEvent event) { + public void newThread(GroupMessageEvent event) { this.e = event; this.start(); } @@ -83,11 +83,15 @@ public void run() { Data data = json.data.get(index); e.getGroup().sendMessage("[ACGPro] 正在从服务器下载图片..."); Image image = null; - Main.isPulling = true; + Main.threadRunning++; String imageURL = data.url.replace("i.pixiv.cat", "pixivi.sakuralo.top"); - String imageURL_ss = imageURL.replace("img-original", "c/540x540_70/img-master") - .replace(".jpg", "_master1200.jpg") - .replace(".png", "_master1200.jpg"); + String imageURL_ss = imageURL; + if (!Main.originalImages) { + imageURL_ss = imageURL.replace("img-original", "c/540x540_70/img-master") + .replace(".jpg", "_master1200.jpg") + .replace(".png", "_master1200.jpg"); + } + try { image = e.getGroup().uploadImage(new URL(imageURL_ss)); } catch (Exception e) { @@ -95,7 +99,7 @@ public void run() { } if (image == null) { e.getGroup().sendMessage("[ACGPro] 图片下载错误"); - Main.isPulling = false; + Main.threadRunning--; return; } e.getGroup().sendMessage("作品标题: " + data.title @@ -123,19 +127,23 @@ public void run() { for (short a = 0; a < pigNum; a++) { Data data = json.data.get(a); Image image = null; - Main.isPulling = true; + Main.threadRunning++; String imageURL = data.url.replace("i.pixiv.cat", "pixivi.sakuralo.top"); - String imageURL_ss = imageURL.replace("img-original", "c/540x540_70/img-master") - .replace(".jpg", "_master1200.jpg") - .replace(".png", "_master1200.jpg"); + String imageURL_ss = imageURL; + if (!Main.originalImages) { + imageURL_ss = imageURL.replace("img-original", "c/540x540_70/img-master") + .replace(".jpg", "_master1200.jpg") + .replace(".png", "_master1200.jpg"); + } + try { - image = e.getGroup().uploadImage(new URL(imageURL)); + image = e.getGroup().uploadImage(new URL(imageURL_ss)); } catch (Exception e) { e.printStackTrace(); } if (image == null) { e.getGroup().sendMessage("[ACGPro] 图片下载错误"); - Main.isPulling = false; + Main.threadRunning--; continue; } e.getGroup().sendMessage("作品标题: " + data.title + "\nPid: " @@ -160,6 +168,6 @@ public void run() { } } //timerTask.cancel(); - Main.isPulling = false; + Main.threadRunning--; } } \ No newline at end of file