mirror of
https://github.com/LamGC/ContentGrabbingJi.git
synced 2025-07-05 06:47:26 +00:00
Merge branch 'master' of github.com:LamGC/ContentGrabbingJi into add-framework-interface
This commit is contained in:
@ -4,7 +4,6 @@ import com.google.common.base.Throwables;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
@ -23,7 +22,7 @@ public class RandomIntervalSendTimer extends TimerTask {
|
||||
private final long time;
|
||||
private final int floatTime;
|
||||
private final AtomicBoolean loop = new AtomicBoolean();
|
||||
private final AtomicBoolean start = new AtomicBoolean();
|
||||
private final AtomicBoolean running = new AtomicBoolean();
|
||||
private final String hashId = Integer.toHexString(this.hashCode());
|
||||
|
||||
|
||||
@ -88,7 +87,6 @@ public class RandomIntervalSendTimer extends TimerTask {
|
||||
this.sender = sender;
|
||||
this.time = time;
|
||||
this.floatTime = floatTime;
|
||||
timerMap.put(timerId, this);
|
||||
if(startNow) {
|
||||
start(loop);
|
||||
}
|
||||
@ -108,21 +106,18 @@ public class RandomIntervalSendTimer extends TimerTask {
|
||||
Date nextDate = new Date();
|
||||
nextDate.setTime(nextDate.getTime() + nextDelay);
|
||||
log.info("定时器 {} 下一延迟: {}ms ({})", hashId, nextDelay, nextDate);
|
||||
if(start.get()) {
|
||||
try {
|
||||
Field state = this.getClass().getSuperclass().getDeclaredField("state");
|
||||
state.setAccessible(true);
|
||||
state.setInt(this, 0);
|
||||
state.setAccessible(false);
|
||||
} catch (NoSuchFieldException | IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
if(running.get()) {
|
||||
reset();
|
||||
return;
|
||||
}
|
||||
start.set(true);
|
||||
running.set(true);
|
||||
timer.schedule(this, nextDelay);
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
timerMap.put(timerId, (RandomIntervalSendTimer) clone());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
log.info("定时器 {} 开始执行...(Sender: {}@{})", this.hashId, sender.getClass().getSimpleName(), sender.hashCode());
|
||||
@ -145,7 +140,7 @@ public class RandomIntervalSendTimer extends TimerTask {
|
||||
*/
|
||||
@Override
|
||||
public boolean cancel() {
|
||||
start.set(false);
|
||||
running.set(false);
|
||||
loop.set(false);
|
||||
return super.cancel();
|
||||
}
|
||||
@ -158,4 +153,18 @@ public class RandomIntervalSendTimer extends TimerTask {
|
||||
timerMap.remove(this.timerId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 克隆一个参数完全一样的TimerTask对象.
|
||||
* @return 返回对象不同, 参数相同的TimerTask对象.
|
||||
*/
|
||||
@Override
|
||||
@SuppressWarnings("MethodDoesntCallSuperMethod")
|
||||
public Object clone() {
|
||||
RandomIntervalSendTimer newTimerTask = new RandomIntervalSendTimer(
|
||||
this.timerId, this.sender,
|
||||
time, floatTime,
|
||||
running.get(), loop.get());
|
||||
this.destroy();
|
||||
return newTimerTask;
|
||||
}
|
||||
}
|
||||
|
@ -26,7 +26,6 @@ import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@ -40,15 +39,13 @@ public class BotEventHandler implements EventHandler {
|
||||
|
||||
private final static Logger log = LoggerFactory.getLogger(BotEventHandler.class);
|
||||
|
||||
private final static Map<Long, AtomicBoolean> muteStateMap = new Hashtable<>();
|
||||
|
||||
/**
|
||||
* 消息事件执行器
|
||||
*/
|
||||
private final static EventExecutor executor = new EventExecutor(new TimeLimitThreadPoolExecutor(
|
||||
180000, // 3minThr
|
||||
Math.max(Runtime.getRuntime().availableProcessors(), 4),
|
||||
Math.min(Math.max(Runtime.getRuntime().availableProcessors(), 4), 32),
|
||||
180000, // 3min limit
|
||||
Math.max(Runtime.getRuntime().availableProcessors(), 4), // 4 ~ processors
|
||||
Math.min(Math.max(Runtime.getRuntime().availableProcessors() * 2, 8), 32),// (8 ~ processors * 2) ~ 32
|
||||
30L,
|
||||
TimeUnit.SECONDS,
|
||||
new LinkedBlockingQueue<>(1536),
|
||||
@ -167,9 +164,6 @@ public class BotEventHandler implements EventHandler {
|
||||
log.debug(event.toString());
|
||||
if(mismatch(msg)) {
|
||||
return;
|
||||
} else if(isMute(event.getFromGroup())) {
|
||||
log.debug("机器人已被禁言, 忽略请求.");
|
||||
return;
|
||||
}
|
||||
|
||||
Pattern pattern = Pattern.compile("/\\s*(\".+?\"|[^:\\s])+((\\s*:\\s*(\".+?\"|[^\\s])+)|)|(\".+?\"|[^\"\\s])+");
|
||||
@ -236,7 +230,7 @@ public class BotEventHandler implements EventHandler {
|
||||
}
|
||||
}
|
||||
long processTime = System.currentTimeMillis() - time;
|
||||
if(!Objects.isNull(result) && result instanceof String && !isMute(event.getFromGroup())) {
|
||||
if(!Objects.isNull(result) && result instanceof String) {
|
||||
try {
|
||||
int sendResult = event.sendMessage((String) result);
|
||||
if (sendResult < 0) {
|
||||
@ -248,8 +242,6 @@ public class BotEventHandler implements EventHandler {
|
||||
} catch (Exception e) {
|
||||
log.error("发送消息时发生异常", e);
|
||||
}
|
||||
} else if(isMute(event.getFromGroup())) {
|
||||
log.warn("命令反馈时机器人已被禁言, 跳过反馈.");
|
||||
}
|
||||
long totalTime = System.currentTimeMillis() - time;
|
||||
log.info("命令反馈完成.(事件耗时: {}ms, P: {}%({}ms), R: {}%({}ms))", totalTime,
|
||||
@ -266,45 +258,4 @@ public class BotEventHandler implements EventHandler {
|
||||
return !message.startsWith(COMMAND_PREFIX) && !message.startsWith(ADMIN_COMMAND_PREFIX);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询某群组中Bot是否被禁言
|
||||
* @param groupId 待查询的群组号
|
||||
* @return 如果被禁言, 返回true, 如果未被禁言或禁言情况未知, 返回false
|
||||
*/
|
||||
private static boolean isMute(long groupId) {
|
||||
Boolean mute = isMute(groupId, false);
|
||||
return mute != null && mute;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询某群是否被禁言.
|
||||
* @param groupId 群组Id
|
||||
* @param rawValue 是否返回原始值(当没有该群状态, 且本参数为true时, 将返回null)
|
||||
* @return 返回状态值, 如无该群禁言记录且rawValue = true, 则返回null
|
||||
*/
|
||||
public static Boolean isMute(long groupId, boolean rawValue) {
|
||||
if(groupId <= 0) {
|
||||
return false;
|
||||
}
|
||||
AtomicBoolean state = muteStateMap.get(groupId);
|
||||
if(state == null && rawValue) {
|
||||
return null;
|
||||
}
|
||||
return state != null && state.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置机器人禁言状态.
|
||||
* <p>设置该项可防止因机器人在禁言期间反馈请求导致被封号.</p>
|
||||
* @param mute 如果被禁言, 传入true
|
||||
*/
|
||||
public static void setMuteState(long groupId, boolean mute) {
|
||||
if(!muteStateMap.containsKey(groupId)) {
|
||||
muteStateMap.put(groupId, new AtomicBoolean(mute));
|
||||
} else {
|
||||
muteStateMap.get(groupId).set(mute);
|
||||
}
|
||||
log.warn("群组 {} 机器人禁言状态已变更: {}", groupId, mute ? "已禁言" : "已解除");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -57,7 +57,7 @@ public abstract class MessageEvent implements EventObject, MessageSender {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.getClass().getSimpleName() + "@" + this.hashCode() + "{" +
|
||||
return this.getClass().getSimpleName() + "@" + Integer.toHexString(this.hashCode()) + "{" +
|
||||
"fromGroup=" + getFromGroup() +
|
||||
", fromQQ=" + getFromQQ() +
|
||||
", message='" + getMessage() + '\'' +
|
||||
|
@ -17,12 +17,12 @@ import org.springframework.stereotype.Component;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
@Component
|
||||
@SuppressWarnings("unused")
|
||||
public class CQPluginMain extends CQPlugin implements EventHandler {
|
||||
|
||||
private final static AtomicBoolean initialState = new AtomicBoolean();
|
||||
|
||||
public CQPluginMain() {
|
||||
|
||||
LoggerFactory.getLogger(CQPluginMain.class)
|
||||
.info("BotEventHandler.COMMAND_PREFIX = {}", BotEventHandler.COMMAND_PREFIX);
|
||||
}
|
||||
|
@ -8,6 +8,7 @@ import net.lamgc.cgj.bot.framework.FrameworkResources;
|
||||
import net.lamgc.cgj.bot.framework.mirai.message.MiraiMessageEvent;
|
||||
import net.lamgc.cgj.bot.framework.mirai.message.MiraiMessageSenderFactory;
|
||||
import net.lamgc.cgj.bot.message.MessageSenderBuilder;
|
||||
import net.lamgc.cgj.bot.util.GroupMuteManager;
|
||||
import net.mamoe.mirai.Bot;
|
||||
import net.mamoe.mirai.BotFactoryJvm;
|
||||
import net.mamoe.mirai.event.events.BotMuteEvent;
|
||||
@ -32,7 +33,9 @@ public class MiraiMain implements Framework {
|
||||
|
||||
private Bot bot;
|
||||
|
||||
private final static Properties botProperties = new Properties();
|
||||
private final Properties botProperties = new Properties();
|
||||
|
||||
private final GroupMuteManager muteManager = new GroupMuteManager();
|
||||
|
||||
@Override
|
||||
public void init(FrameworkResources resources) {
|
||||
@ -77,9 +80,9 @@ public class MiraiMain implements Framework {
|
||||
Events.subscribeAlways(FriendMessageEvent.class, this::executeMessageEvent);
|
||||
Events.subscribeAlways(TempMessageEvent.class, this::executeMessageEvent);
|
||||
Events.subscribeAlways(BotMuteEvent.class,
|
||||
event -> BotEventHandler.setMuteState(event.getGroup().getId(), true));
|
||||
event -> muteManager.setMuteState(event.getGroup().getId(), true));
|
||||
Events.subscribeAlways(BotUnmuteEvent.class,
|
||||
event -> BotEventHandler.setMuteState(event.getGroup().getId(), false));
|
||||
event -> muteManager.setMuteState(event.getGroup().getId(), false));
|
||||
bot.login();
|
||||
MessageSenderBuilder.setCurrentMessageSenderFactory(new MiraiMessageSenderFactory(bot));
|
||||
ApplicationBoot.initialBot();
|
||||
@ -100,9 +103,12 @@ public class MiraiMain implements Framework {
|
||||
log.debug("Mirai Message: {}", message);
|
||||
if(message instanceof GroupMessageEvent) {
|
||||
GroupMessageEvent GroupMessageEvent = (GroupMessageEvent) message;
|
||||
if(BotEventHandler.isMute(GroupMessageEvent.getGroup().getId(), true) == null) {
|
||||
BotEventHandler.setMuteState(GroupMessageEvent.getGroup().getId(),
|
||||
Boolean muteState = muteManager.isMute(GroupMessageEvent.getGroup().getId(), true);
|
||||
if(muteState == null) {
|
||||
muteManager.setMuteState(GroupMessageEvent.getGroup().getId(),
|
||||
((GroupMessageEvent) message).getGroup().getBotMuteRemaining() != 0);
|
||||
} else if(muteState) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
BotEventHandler.executeMessageEvent(MiraiMessageEvent.covertEventObject(message));
|
||||
|
48
src/main/java/net/lamgc/cgj/bot/util/GroupMuteManager.java
Normal file
48
src/main/java/net/lamgc/cgj/bot/util/GroupMuteManager.java
Normal file
@ -0,0 +1,48 @@
|
||||
package net.lamgc.cgj.bot.util;
|
||||
|
||||
import java.util.Hashtable;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
/**
|
||||
* 群禁言管理器.
|
||||
* <p>该管理器用于存取群组禁言状态.</p>
|
||||
*/
|
||||
public class GroupMuteManager {
|
||||
|
||||
private final Map<Long, AtomicBoolean> muteStateMap = new Hashtable<>();
|
||||
|
||||
/**
|
||||
* 查询某群是否被禁言.
|
||||
* @param groupId 群组Id
|
||||
* @param rawValue 是否返回原始值(当没有该群状态, 且本参数为true时, 将返回null)
|
||||
* @return 返回状态值, 如无该群禁言记录且rawValue = true, 则返回null
|
||||
*/
|
||||
public Boolean isMute(long groupId, boolean rawValue) {
|
||||
if(groupId <= 0) {
|
||||
return false;
|
||||
}
|
||||
AtomicBoolean state = muteStateMap.get(groupId);
|
||||
if(state == null && rawValue) {
|
||||
return null;
|
||||
}
|
||||
return state != null && state.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置机器人禁言状态.
|
||||
* <p>设置该项可防止因机器人在禁言期间反馈请求导致被封号.</p>
|
||||
* @param mute 如果被禁言, 传入true
|
||||
*/
|
||||
public void setMuteState(long groupId, boolean mute) {
|
||||
if(groupId <= 0) {
|
||||
return;
|
||||
}
|
||||
if(!muteStateMap.containsKey(groupId)) {
|
||||
muteStateMap.put(groupId, new AtomicBoolean(mute));
|
||||
} else {
|
||||
muteStateMap.get(groupId).set(mute);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -402,7 +402,7 @@ public class PixivDownload {
|
||||
if(resultObject.get("error").getAsBoolean()) {
|
||||
String message = resultObject.get("message").getAsString();
|
||||
log.warn("作品页面接口请求错误, 错误信息: {}", message);
|
||||
throw new HttpRequestException(response);
|
||||
throw new HttpRequestException(response.getStatusLine(), resultObject.toString());
|
||||
}
|
||||
|
||||
JsonArray linkArray = resultObject.getAsJsonArray("body");
|
||||
|
@ -1,5 +1,6 @@
|
||||
package net.lamgc.cgj.pixiv;
|
||||
|
||||
import com.google.common.io.ByteStreams;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonObject;
|
||||
@ -12,7 +13,6 @@ import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.apache.tomcat.util.http.fileupload.util.Streams;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@ -127,14 +127,13 @@ public final class PixivUgoiraBuilder {
|
||||
HashMap<String, InputStream> frameMap = new HashMap<>(frames.size());
|
||||
while((entry = zipInputStream.getNextEntry()) != null) {
|
||||
log.trace("ZipEntry {} 正在接收...", entry);
|
||||
Streams.copy(zipInputStream, cacheOutputStream, false);
|
||||
ByteStreams.copy(zipInputStream, cacheOutputStream);
|
||||
frameMap.put(entry.getName(), new ByteArrayInputStream(cacheOutputStream.toByteArray()));
|
||||
log.trace("ZipEntry {} 已接收完成.", entry);
|
||||
cacheOutputStream.reset();
|
||||
}
|
||||
|
||||
|
||||
InputStream firstFrameInput = frameMap.get("000000.jpg");
|
||||
InputStream firstFrameInput = frameMap.get(frames.get(0).getAsJsonObject().get("file").getAsString());
|
||||
BufferedImage firstFrame = ImageIO.read(firstFrameInput);
|
||||
firstFrameInput.reset();
|
||||
if(width != firstFrame.getWidth() || height != firstFrame.getHeight()) {
|
||||
|
Reference in New Issue
Block a user