mirror of
https://github.com/LamGC/ContentGrabbingJi.git
synced 2025-07-04 14:27:27 +00:00
Compare commits
9 Commits
v2.5.2-202
...
optimize-s
Author | SHA1 | Date | |
---|---|---|---|
f976017a89
|
|||
575dc0c7fb
|
|||
56ef463c63
|
|||
4387da37f5
|
|||
0fc3e3ab48
|
|||
a606ec0423
|
|||
210aa84ed5 | |||
6fbbe522db | |||
6d55325fc7 |
37
.github/ISSUE_TEMPLATE/Bug_Report.md
vendored
37
.github/ISSUE_TEMPLATE/Bug_Report.md
vendored
@ -1,26 +1,35 @@
|
|||||||
---
|
---
|
||||||
name: Bug Report
|
name: Bug 反馈
|
||||||
about: Use this template to feedback bugs.
|
about: 使用这个模板反馈应用问题。
|
||||||
title: ''
|
title: ''
|
||||||
labels: bug
|
labels: bug
|
||||||
assignees: ''
|
assignees: ''
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## Environmental information ##
|
## 环境信息 ##
|
||||||
OS(e.g: Windows 10 1909):
|
系统(例如: Windows 10 1909): ``
|
||||||
Java(e.g: Oracle Jdk 8.242):
|
Java版本(例如: Oracle Jdk 8.242): ``
|
||||||
Issue version(versionTag or commitId):
|
<!-- 如果直接使用发布版,那么就填写发布版版本号(v开头) -->
|
||||||
|
<!-- 如果你通过编译运行的方式运行开发版,请你填写运行所使用的Commit Id -->
|
||||||
|
发生问题所在的版本: ``
|
||||||
|
|
||||||
## Problem description ##
|
## 问题描述 ##
|
||||||
// Describe the problem in as much detail as possible here
|
<!-- 尽可能的清晰描述问题的信息 -->
|
||||||
|
|
||||||
## Expected behavior ##
|
## 预期行为 ##
|
||||||
// What will this function do under normal circumstances?
|
<!-- 你觉得正常情况下应该会发生什么? -->
|
||||||
|
|
||||||
## Actual behavior ##
|
## 实际行为 ##
|
||||||
// But what does this feature actually look like?
|
<!-- 实际上这个功能做了什么? -->
|
||||||
|
|
||||||
## Recurrence steps ##
|
## 复现步骤 ##
|
||||||
// What can we do to recreate this situation?
|
<!-- 将复现步骤详细的写出,如果是偶发问题,可以写已知的,触发几率高的方法 -->
|
||||||
1.
|
1.
|
||||||
|
|
||||||
|
## 相关信息 ##
|
||||||
|
### 日志 ###
|
||||||
|
<!-- 如果日志涉及了问题,请务必将日志一同提交,这对排查问题非常有用 -->
|
||||||
|
```
|
||||||
|
|
||||||
|
```
|
||||||
|
11
.github/ISSUE_TEMPLATE/Feature_Report.md
vendored
Normal file
11
.github/ISSUE_TEMPLATE/Feature_Report.md
vendored
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
---
|
||||||
|
name: 功能/想法 提议
|
||||||
|
about: 使用这个模板将你对应用的想法提出来,或许我们会采纳!
|
||||||
|
title: ''
|
||||||
|
labels: function, question
|
||||||
|
assignees: ''
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
<!-- 如果可以,尽可能的清晰、详细的表达你的想法 -->
|
||||||
|
<!-- 没关系的,我们会进一步向你交流以尝试了解你的想法! -->
|
@ -5,6 +5,7 @@ ENV jarFileName=ContentGrabbingJi-exec.jar
|
|||||||
ENV CGJ_REDIS_URI="127.0.0.1:6379"
|
ENV CGJ_REDIS_URI="127.0.0.1:6379"
|
||||||
ENV CGJ_PROXY=""
|
ENV CGJ_PROXY=""
|
||||||
RUN mkdir /data/
|
RUN mkdir /data/
|
||||||
CMD [java, "-Dcgj.logsPath=/data/logs", "-jar", "/CGJ.jar", "botMode", "-botDataDir=/data"]
|
ENTRYPOINT ["/usr/java/openjdk-14/bin/java", "-Duser.timezone=Aisa/Shanghai"]
|
||||||
|
CMD ["-Dcgj.logsPath=/data/logs", "-jar", "/CGJ.jar", "botMode", "-botDataDir=/data"]
|
||||||
|
|
||||||
COPY ${jarFileName} /CGJ.jar
|
COPY ${jarFileName} /CGJ.jar
|
||||||
|
@ -11,12 +11,17 @@ import net.lamgc.cgj.bot.cache.JsonRedisCacheStore;
|
|||||||
import net.lamgc.cgj.bot.event.BufferedMessageSender;
|
import net.lamgc.cgj.bot.event.BufferedMessageSender;
|
||||||
import net.lamgc.cgj.bot.sort.PreLoadDataAttribute;
|
import net.lamgc.cgj.bot.sort.PreLoadDataAttribute;
|
||||||
import net.lamgc.cgj.bot.sort.PreLoadDataAttributeComparator;
|
import net.lamgc.cgj.bot.sort.PreLoadDataAttributeComparator;
|
||||||
|
import net.lamgc.cgj.bot.util.PixivUtils;
|
||||||
import net.lamgc.cgj.pixiv.PixivDownload;
|
import net.lamgc.cgj.pixiv.PixivDownload;
|
||||||
import net.lamgc.cgj.pixiv.PixivDownload.PageQuality;
|
import net.lamgc.cgj.pixiv.PixivDownload.PageQuality;
|
||||||
import net.lamgc.cgj.pixiv.PixivSearchLinkBuilder;
|
import net.lamgc.cgj.pixiv.PixivSearchLinkBuilder;
|
||||||
import net.lamgc.cgj.pixiv.PixivURL;
|
import net.lamgc.cgj.pixiv.PixivURL;
|
||||||
import net.lamgc.utils.base.runner.Argument;
|
import net.lamgc.utils.base.runner.Argument;
|
||||||
import net.lamgc.utils.base.runner.Command;
|
import net.lamgc.utils.base.runner.Command;
|
||||||
|
import org.apache.http.HttpHeaders;
|
||||||
|
import org.apache.http.HttpResponse;
|
||||||
|
import org.apache.http.client.methods.HttpGet;
|
||||||
|
import org.apache.http.util.EntityUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@ -315,6 +320,16 @@ public class BotCommandProcess {
|
|||||||
@Argument(name = "p", force = false, defaultValue = "1") int pagesIndex
|
@Argument(name = "p", force = false, defaultValue = "1") int pagesIndex
|
||||||
) throws IOException, InterruptedException {
|
) throws IOException, InterruptedException {
|
||||||
log.debug("正在执行搜索...");
|
log.debug("正在执行搜索...");
|
||||||
|
PixivSearchLinkBuilder linkBuilder = PixivUtils.buildSearchLinkBuilder(content, type, area, includeKeywords,
|
||||||
|
excludeKeywords, contentOption, pagesIndex);
|
||||||
|
int recommendKeywordCount = 0;
|
||||||
|
try {
|
||||||
|
recommendKeywordCount = Integer.parseInt(SettingProperties.getProperties(fromGroup)
|
||||||
|
.getProperty("search.recommendKeywordCount", "0"));
|
||||||
|
} catch(NumberFormatException e) {
|
||||||
|
log.warn("配置项 search.recommendKeywordCount 的值无效");
|
||||||
|
}
|
||||||
|
addRecommendKeywords(linkBuilder, recommendKeywordCount);
|
||||||
JsonObject resultBody = CacheStoreCentral.getCentral()
|
JsonObject resultBody = CacheStoreCentral.getCentral()
|
||||||
.getSearchBody(content, type, area, includeKeywords, excludeKeywords, contentOption, pagesIndex);
|
.getSearchBody(content, type, area, includeKeywords, excludeKeywords, contentOption, pagesIndex);
|
||||||
|
|
||||||
@ -525,6 +540,43 @@ public class BotCommandProcess {
|
|||||||
return "色图姬收到了你的报告,将屏蔽该作品并对作品违规情况进行核实,感谢你的反馈!";
|
return "色图姬收到了你的报告,将屏蔽该作品并对作品违规情况进行核实,感谢你的反馈!";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据Pixiv搜索推荐列表补充关键词.
|
||||||
|
* <p>该操作可能会优化搜索效果.</p>
|
||||||
|
* @param searchLinkBuilder PixivSearchLinkBuilder对象
|
||||||
|
* @param includeKeywordsCount 需要添加的关键词数量
|
||||||
|
* @throws IOException 当获取推荐列表发生异常时抛出.
|
||||||
|
*/
|
||||||
|
private static void addRecommendKeywords(PixivSearchLinkBuilder searchLinkBuilder, int includeKeywordsCount)
|
||||||
|
throws IOException {
|
||||||
|
if(includeKeywordsCount <= 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
HttpGet request = BotGlobal.getGlobal().getPixivDownload()
|
||||||
|
.createHttpGetRequest(PixivURL.PIXIV_SEARCH_RECOMMENDS
|
||||||
|
.replace("{content}", searchLinkBuilder.getContent()));
|
||||||
|
request.addHeader(HttpHeaders.REFERER, "https://www.pixiv.net/");
|
||||||
|
|
||||||
|
HttpResponse response = BotGlobal.getGlobal().getPixivDownload().getHttpClient().execute(request);
|
||||||
|
String responseBody = EntityUtils.toString(response.getEntity());
|
||||||
|
if(response.getStatusLine().getStatusCode() != 200) {
|
||||||
|
throw new IOException("Interface request failure: " + response.getStatusLine() +
|
||||||
|
", response body: '" + responseBody + "'");
|
||||||
|
}
|
||||||
|
|
||||||
|
JsonObject resultObject = BotGlobal.getGlobal().getGson()
|
||||||
|
.fromJson(responseBody, JsonObject.class);
|
||||||
|
if(!resultObject.has("candidates")) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
JsonArray recommendsArr = resultObject.getAsJsonArray("candidates");
|
||||||
|
for (int count = 0; count < includeKeywordsCount && count < recommendsArr.size(); count++) {
|
||||||
|
searchLinkBuilder.addIncludeKeyword(
|
||||||
|
recommendsArr.get(count).getAsJsonObject().get("tag_name").getAsString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 检查某一作品是否被报告
|
* 检查某一作品是否被报告
|
||||||
* @param illustId 作品Id
|
* @param illustId 作品Id
|
||||||
|
@ -8,6 +8,7 @@ import net.lamgc.cgj.bot.BotCode;
|
|||||||
import net.lamgc.cgj.bot.BotCommandProcess;
|
import net.lamgc.cgj.bot.BotCommandProcess;
|
||||||
import net.lamgc.cgj.bot.SettingProperties;
|
import net.lamgc.cgj.bot.SettingProperties;
|
||||||
import net.lamgc.cgj.bot.boot.BotGlobal;
|
import net.lamgc.cgj.bot.boot.BotGlobal;
|
||||||
|
import net.lamgc.cgj.bot.util.PixivUtils;
|
||||||
import net.lamgc.cgj.exception.HttpRequestException;
|
import net.lamgc.cgj.exception.HttpRequestException;
|
||||||
import net.lamgc.cgj.pixiv.PixivDownload;
|
import net.lamgc.cgj.pixiv.PixivDownload;
|
||||||
import net.lamgc.cgj.pixiv.PixivSearchLinkBuilder;
|
import net.lamgc.cgj.pixiv.PixivSearchLinkBuilder;
|
||||||
@ -405,73 +406,11 @@ public final class CacheStoreCentral {
|
|||||||
return PixivDownload.getRanking(result, start - 1, range);
|
return PixivDownload.getRanking(result, start - 1, range);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public JsonObject getSearchBody(PixivSearchLinkBuilder searchLinkBuilder) throws IOException {
|
||||||
* 获取搜索结果
|
log.debug("正在搜索作品, 条件: {}", searchLinkBuilder.getSearchCondition());
|
||||||
* @param content 搜索内容
|
|
||||||
* @param type 类型
|
|
||||||
* @param area 范围
|
|
||||||
* @param includeKeywords 包含关键词
|
|
||||||
* @param excludeKeywords 排除关键词
|
|
||||||
* @param contentOption 内容类型
|
|
||||||
* @return 返回完整搜索结果
|
|
||||||
* @throws IOException 当请求发生异常, 或接口返回异常信息时抛出.
|
|
||||||
*/
|
|
||||||
public JsonObject getSearchBody(
|
|
||||||
String content,
|
|
||||||
String type,
|
|
||||||
String area,
|
|
||||||
String includeKeywords,
|
|
||||||
String excludeKeywords,
|
|
||||||
String contentOption,
|
|
||||||
int pageIndex
|
|
||||||
) throws IOException {
|
|
||||||
PixivSearchLinkBuilder searchBuilder = new PixivSearchLinkBuilder(Strings.isNullOrEmpty(content) ? "" : content);
|
|
||||||
if (type != null) {
|
|
||||||
try {
|
|
||||||
searchBuilder.setSearchType(PixivSearchLinkBuilder.SearchType.valueOf(type.toUpperCase()));
|
|
||||||
} catch (IllegalArgumentException e) {
|
|
||||||
log.warn("不支持的SearchType: {}", type);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (area != null) {
|
|
||||||
try {
|
|
||||||
searchBuilder.setSearchArea(PixivSearchLinkBuilder.SearchArea.valueOf(area));
|
|
||||||
} catch (IllegalArgumentException e) {
|
|
||||||
log.warn("不支持的SearchArea: {}", area);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (contentOption != null) {
|
|
||||||
try {
|
|
||||||
searchBuilder.setSearchContentOption(
|
|
||||||
PixivSearchLinkBuilder.SearchContentOption.valueOf(contentOption.trim().toUpperCase()));
|
|
||||||
} catch (IllegalArgumentException e) {
|
|
||||||
log.warn("不支持的SearchContentOption: {}", contentOption);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!Strings.isNullOrEmpty(includeKeywords)) {
|
|
||||||
for (String keyword : includeKeywords.split(";")) {
|
|
||||||
searchBuilder.removeExcludeKeyword(keyword.trim());
|
|
||||||
searchBuilder.addIncludeKeyword(keyword.trim());
|
|
||||||
log.trace("已添加关键字: {}", keyword);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!Strings.isNullOrEmpty(excludeKeywords)) {
|
|
||||||
for (String keyword : excludeKeywords.split(";")) {
|
|
||||||
searchBuilder.removeIncludeKeyword(keyword.trim());
|
|
||||||
searchBuilder.addExcludeKeyword(keyword.trim());
|
|
||||||
log.trace("已添加排除关键字: {}", keyword);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(pageIndex > 0) {
|
|
||||||
searchBuilder.setPage(pageIndex);
|
|
||||||
}
|
|
||||||
|
|
||||||
log.debug("正在搜索作品, 条件: {}", searchBuilder.getSearchCondition());
|
|
||||||
|
|
||||||
Locker<String> locker
|
Locker<String> locker
|
||||||
= buildSyncKey(searchBuilder.buildURL());
|
= buildSyncKey(searchLinkBuilder.buildURL());
|
||||||
String requestUrl = locker.getKey();
|
String requestUrl = locker.getKey();
|
||||||
log.debug("RequestUrl: {}", requestUrl);
|
log.debug("RequestUrl: {}", requestUrl);
|
||||||
JsonObject resultBody = null;
|
JsonObject resultBody = null;
|
||||||
@ -524,6 +463,31 @@ public final class CacheStoreCentral {
|
|||||||
return resultBody;
|
return resultBody;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取搜索结果
|
||||||
|
* @param content 搜索内容
|
||||||
|
* @param type 类型
|
||||||
|
* @param area 范围
|
||||||
|
* @param includeKeywords 包含关键词
|
||||||
|
* @param excludeKeywords 排除关键词
|
||||||
|
* @param contentOption 内容类型
|
||||||
|
* @return 返回完整搜索结果
|
||||||
|
* @throws IOException 当请求发生异常, 或接口返回异常信息时抛出.
|
||||||
|
*/
|
||||||
|
public JsonObject getSearchBody(
|
||||||
|
String content,
|
||||||
|
String type,
|
||||||
|
String area,
|
||||||
|
String includeKeywords,
|
||||||
|
String excludeKeywords,
|
||||||
|
String contentOption,
|
||||||
|
int pageIndex
|
||||||
|
) throws IOException {
|
||||||
|
return getSearchBody(PixivUtils.buildSearchLinkBuilder(content, type, area, includeKeywords,
|
||||||
|
excludeKeywords, contentOption, pageIndex));
|
||||||
|
}
|
||||||
|
|
||||||
protected ImageChecksum getImageChecksum(int illustId, int pageIndex) {
|
protected ImageChecksum getImageChecksum(int illustId, int pageIndex) {
|
||||||
String cacheKey = illustId + ":" + pageIndex;
|
String cacheKey = illustId + ":" + pageIndex;
|
||||||
if(!imageChecksumCache.exists(cacheKey)) {
|
if(!imageChecksumCache.exists(cacheKey)) {
|
||||||
|
@ -26,7 +26,6 @@ import java.text.SimpleDateFormat;
|
|||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.LinkedBlockingQueue;
|
import java.util.concurrent.LinkedBlockingQueue;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
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 Logger log = LoggerFactory.getLogger(BotEventHandler.class);
|
||||||
|
|
||||||
private final static Map<Long, AtomicBoolean> muteStateMap = new Hashtable<>();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 消息事件执行器
|
* 消息事件执行器
|
||||||
*/
|
*/
|
||||||
private final static EventExecutor executor = new EventExecutor(new TimeLimitThreadPoolExecutor(
|
private final static EventExecutor executor = new EventExecutor(new TimeLimitThreadPoolExecutor(
|
||||||
180000, // 3minThr
|
180000, // 3min limit
|
||||||
Math.max(Runtime.getRuntime().availableProcessors(), 4),
|
Math.max(Runtime.getRuntime().availableProcessors(), 4), // 4 ~ processors
|
||||||
Math.min(Math.max(Runtime.getRuntime().availableProcessors(), 4), 32),
|
Math.min(Math.max(Runtime.getRuntime().availableProcessors() * 2, 8), 32),// (8 ~ processors * 2) ~ 32
|
||||||
30L,
|
30L,
|
||||||
TimeUnit.SECONDS,
|
TimeUnit.SECONDS,
|
||||||
new LinkedBlockingQueue<>(1536),
|
new LinkedBlockingQueue<>(1536),
|
||||||
@ -167,9 +164,6 @@ public class BotEventHandler implements EventHandler {
|
|||||||
log.debug(event.toString());
|
log.debug(event.toString());
|
||||||
if(mismatch(msg)) {
|
if(mismatch(msg)) {
|
||||||
return;
|
return;
|
||||||
} else if(isMute(event.getFromGroup())) {
|
|
||||||
log.debug("机器人已被禁言, 忽略请求.");
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Pattern pattern = Pattern.compile("/\\s*(\".+?\"|[^:\\s])+((\\s*:\\s*(\".+?\"|[^\\s])+)|)|(\".+?\"|[^\"\\s])+");
|
Pattern pattern = Pattern.compile("/\\s*(\".+?\"|[^:\\s])+((\\s*:\\s*(\".+?\"|[^\\s])+)|)|(\".+?\"|[^\"\\s])+");
|
||||||
@ -236,7 +230,7 @@ public class BotEventHandler implements EventHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
long processTime = System.currentTimeMillis() - time;
|
long processTime = System.currentTimeMillis() - time;
|
||||||
if(!Objects.isNull(result) && result instanceof String && !isMute(event.getFromGroup())) {
|
if(!Objects.isNull(result) && result instanceof String) {
|
||||||
try {
|
try {
|
||||||
int sendResult = event.sendMessage((String) result);
|
int sendResult = event.sendMessage((String) result);
|
||||||
if (sendResult < 0) {
|
if (sendResult < 0) {
|
||||||
@ -248,8 +242,6 @@ public class BotEventHandler implements EventHandler {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("发送消息时发生异常", e);
|
log.error("发送消息时发生异常", e);
|
||||||
}
|
}
|
||||||
} else if(isMute(event.getFromGroup())) {
|
|
||||||
log.warn("命令反馈时机器人已被禁言, 跳过反馈.");
|
|
||||||
}
|
}
|
||||||
long totalTime = System.currentTimeMillis() - time;
|
long totalTime = System.currentTimeMillis() - time;
|
||||||
log.info("命令反馈完成.(事件耗时: {}ms, P: {}%({}ms), R: {}%({}ms))", totalTime,
|
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);
|
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
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return this.getClass().getSimpleName() + "@" + this.hashCode() + "{" +
|
return this.getClass().getSimpleName() + "@" + Integer.toHexString(this.hashCode()) + "{" +
|
||||||
"fromGroup=" + getFromGroup() +
|
"fromGroup=" + getFromGroup() +
|
||||||
", fromQQ=" + getFromQQ() +
|
", fromQQ=" + getFromQQ() +
|
||||||
", message='" + getMessage() + '\'' +
|
", message='" + getMessage() + '\'' +
|
||||||
|
@ -6,6 +6,7 @@ import net.lamgc.cgj.bot.event.BotEventHandler;
|
|||||||
import net.lamgc.cgj.bot.framework.mirai.message.MiraiMessageEvent;
|
import net.lamgc.cgj.bot.framework.mirai.message.MiraiMessageEvent;
|
||||||
import net.lamgc.cgj.bot.framework.mirai.message.MiraiMessageSenderFactory;
|
import net.lamgc.cgj.bot.framework.mirai.message.MiraiMessageSenderFactory;
|
||||||
import net.lamgc.cgj.bot.message.MessageSenderBuilder;
|
import net.lamgc.cgj.bot.message.MessageSenderBuilder;
|
||||||
|
import net.lamgc.cgj.bot.util.GroupMuteManager;
|
||||||
import net.mamoe.mirai.Bot;
|
import net.mamoe.mirai.Bot;
|
||||||
import net.mamoe.mirai.BotFactoryJvm;
|
import net.mamoe.mirai.BotFactoryJvm;
|
||||||
import net.mamoe.mirai.event.events.BotMuteEvent;
|
import net.mamoe.mirai.event.events.BotMuteEvent;
|
||||||
@ -30,7 +31,9 @@ public class MiraiMain implements Closeable {
|
|||||||
|
|
||||||
private Bot bot;
|
private Bot bot;
|
||||||
|
|
||||||
private final static Properties botProperties = new Properties();
|
private final Properties botProperties = new Properties();
|
||||||
|
|
||||||
|
private final GroupMuteManager muteManager = new GroupMuteManager();
|
||||||
|
|
||||||
public void init() {
|
public void init() {
|
||||||
Runtime.getRuntime().addShutdownHook(new Thread(this::close));
|
Runtime.getRuntime().addShutdownHook(new Thread(this::close));
|
||||||
@ -73,9 +76,9 @@ public class MiraiMain implements Closeable {
|
|||||||
Events.subscribeAlways(FriendMessageEvent.class, this::executeMessageEvent);
|
Events.subscribeAlways(FriendMessageEvent.class, this::executeMessageEvent);
|
||||||
Events.subscribeAlways(TempMessageEvent.class, this::executeMessageEvent);
|
Events.subscribeAlways(TempMessageEvent.class, this::executeMessageEvent);
|
||||||
Events.subscribeAlways(BotMuteEvent.class,
|
Events.subscribeAlways(BotMuteEvent.class,
|
||||||
event -> BotEventHandler.setMuteState(event.getGroup().getId(), true));
|
event -> muteManager.setMuteState(event.getGroup().getId(), true));
|
||||||
Events.subscribeAlways(BotUnmuteEvent.class,
|
Events.subscribeAlways(BotUnmuteEvent.class,
|
||||||
event -> BotEventHandler.setMuteState(event.getGroup().getId(), false));
|
event -> muteManager.setMuteState(event.getGroup().getId(), false));
|
||||||
bot.login();
|
bot.login();
|
||||||
MessageSenderBuilder.setCurrentMessageSenderFactory(new MiraiMessageSenderFactory(bot));
|
MessageSenderBuilder.setCurrentMessageSenderFactory(new MiraiMessageSenderFactory(bot));
|
||||||
ApplicationBoot.initialBot();
|
ApplicationBoot.initialBot();
|
||||||
@ -90,9 +93,12 @@ public class MiraiMain implements Closeable {
|
|||||||
log.debug("Mirai Message: {}", message);
|
log.debug("Mirai Message: {}", message);
|
||||||
if(message instanceof GroupMessageEvent) {
|
if(message instanceof GroupMessageEvent) {
|
||||||
GroupMessageEvent GroupMessageEvent = (GroupMessageEvent) message;
|
GroupMessageEvent GroupMessageEvent = (GroupMessageEvent) message;
|
||||||
if(BotEventHandler.isMute(GroupMessageEvent.getGroup().getId(), true) == null) {
|
Boolean muteState = muteManager.isMute(GroupMessageEvent.getGroup().getId(), true);
|
||||||
BotEventHandler.setMuteState(GroupMessageEvent.getGroup().getId(),
|
if(muteState == null) {
|
||||||
|
muteManager.setMuteState(GroupMessageEvent.getGroup().getId(),
|
||||||
((GroupMessageEvent) message).getGroup().getBotMuteRemaining() != 0);
|
((GroupMessageEvent) message).getGroup().getBotMuteRemaining() != 0);
|
||||||
|
} else if(muteState) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
BotEventHandler.executeMessageEvent(MiraiMessageEvent.covertEventObject(message));
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
86
src/main/java/net/lamgc/cgj/bot/util/PixivUtils.java
Normal file
86
src/main/java/net/lamgc/cgj/bot/util/PixivUtils.java
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
package net.lamgc.cgj.bot.util;
|
||||||
|
|
||||||
|
import com.google.common.base.Strings;
|
||||||
|
import net.lamgc.cgj.pixiv.PixivSearchLinkBuilder;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pixiv工具类
|
||||||
|
*/
|
||||||
|
public final class PixivUtils {
|
||||||
|
|
||||||
|
private final static Logger log = LoggerFactory.getLogger(PixivUtils.class);
|
||||||
|
|
||||||
|
private PixivUtils() {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 快速构造一个PixivSearchLinkBuilder
|
||||||
|
* @param content 搜索内容
|
||||||
|
* @param type 搜索类型
|
||||||
|
* @param area 搜索范围
|
||||||
|
* @param includeKeywords 包含关键词
|
||||||
|
* @param excludeKeywords 排除关键词
|
||||||
|
* @param contentOption 内容级别选项
|
||||||
|
* @param pageIndex 搜索页数
|
||||||
|
* @return 返回PixivSearchLinkBuilder对象
|
||||||
|
* @see PixivSearchLinkBuilder
|
||||||
|
*/
|
||||||
|
public static PixivSearchLinkBuilder buildSearchLinkBuilder(
|
||||||
|
String content,
|
||||||
|
String type,
|
||||||
|
String area,
|
||||||
|
String includeKeywords,
|
||||||
|
String excludeKeywords,
|
||||||
|
String contentOption,
|
||||||
|
int pageIndex
|
||||||
|
) {
|
||||||
|
PixivSearchLinkBuilder searchBuilder = new PixivSearchLinkBuilder(Strings.isNullOrEmpty(content) ? "" : content);
|
||||||
|
if (type != null) {
|
||||||
|
try {
|
||||||
|
searchBuilder.setSearchType(PixivSearchLinkBuilder.SearchType.valueOf(type.toUpperCase()));
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
log.warn("不支持的SearchType: {}", type);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (area != null) {
|
||||||
|
try {
|
||||||
|
searchBuilder.setSearchArea(PixivSearchLinkBuilder.SearchArea.valueOf(area));
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
log.warn("不支持的SearchArea: {}", area);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (contentOption != null) {
|
||||||
|
try {
|
||||||
|
searchBuilder.setSearchContentOption(
|
||||||
|
PixivSearchLinkBuilder.SearchContentOption.valueOf(contentOption.trim().toUpperCase()));
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
log.warn("不支持的SearchContentOption: {}", contentOption);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!Strings.isNullOrEmpty(includeKeywords)) {
|
||||||
|
for (String keyword : includeKeywords.split(";")) {
|
||||||
|
searchBuilder.removeExcludeKeyword(keyword.trim());
|
||||||
|
searchBuilder.addIncludeKeyword(keyword.trim());
|
||||||
|
log.trace("已添加关键字: {}", keyword);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!Strings.isNullOrEmpty(excludeKeywords)) {
|
||||||
|
for (String keyword : excludeKeywords.split(";")) {
|
||||||
|
searchBuilder.removeIncludeKeyword(keyword.trim());
|
||||||
|
searchBuilder.addExcludeKeyword(keyword.trim());
|
||||||
|
log.trace("已添加排除关键字: {}", keyword);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(pageIndex > 0) {
|
||||||
|
searchBuilder.setPage(pageIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
return searchBuilder;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -402,7 +402,7 @@ public class PixivDownload {
|
|||||||
if(resultObject.get("error").getAsBoolean()) {
|
if(resultObject.get("error").getAsBoolean()) {
|
||||||
String message = resultObject.get("message").getAsString();
|
String message = resultObject.get("message").getAsString();
|
||||||
log.warn("作品页面接口请求错误, 错误信息: {}", message);
|
log.warn("作品页面接口请求错误, 错误信息: {}", message);
|
||||||
throw new HttpRequestException(response);
|
throw new HttpRequestException(response.getStatusLine(), resultObject.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonArray linkArray = resultObject.getAsJsonArray("body");
|
JsonArray linkArray = resultObject.getAsJsonArray("body");
|
||||||
|
@ -161,6 +161,10 @@ public class PixivSearchLinkBuilder {
|
|||||||
'}';
|
'}';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getContent() {
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
|
||||||
public PixivSearchLinkBuilder setSearchArea(SearchArea searchArea) {
|
public PixivSearchLinkBuilder setSearchArea(SearchArea searchArea) {
|
||||||
this.searchArea = Objects.requireNonNull(searchArea);
|
this.searchArea = Objects.requireNonNull(searchArea);
|
||||||
return this;
|
return this;
|
||||||
|
@ -194,6 +194,16 @@ public final class PixivURL {
|
|||||||
*/
|
*/
|
||||||
public final static String PIXIV_USER_COLLECTION_PAGE = "https://www.pixiv.net/bookmark.php?rest=show&p={pageIndex}";
|
public final static String PIXIV_USER_COLLECTION_PAGE = "https://www.pixiv.net/bookmark.php?rest=show&p={pageIndex}";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 搜索推荐接口.
|
||||||
|
* <p>可返回与搜索内容相关的标签信息, 以优化搜索效果.</p>
|
||||||
|
* 需要替换的文本:
|
||||||
|
* <ul>
|
||||||
|
* <li>{content} - 搜索内容</li>
|
||||||
|
* </ul>
|
||||||
|
*/
|
||||||
|
public final static String PIXIV_SEARCH_RECOMMENDS = "https://www.pixiv.net/rpc/cps.php?keywork={content}";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取排名榜
|
* 获取排名榜
|
||||||
* @param mode 查询类型, 详细信息看{@link RankingMode}, 如本参数为null, 则为每天
|
* @param mode 查询类型, 详细信息看{@link RankingMode}, 如本参数为null, 则为每天
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package net.lamgc.cgj.pixiv;
|
package net.lamgc.cgj.pixiv;
|
||||||
|
|
||||||
|
import com.google.common.io.ByteStreams;
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.JsonArray;
|
import com.google.gson.JsonArray;
|
||||||
import com.google.gson.JsonObject;
|
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.HttpClient;
|
||||||
import org.apache.http.client.methods.HttpGet;
|
import org.apache.http.client.methods.HttpGet;
|
||||||
import org.apache.http.util.EntityUtils;
|
import org.apache.http.util.EntityUtils;
|
||||||
import org.apache.tomcat.util.http.fileupload.util.Streams;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@ -127,14 +127,13 @@ public final class PixivUgoiraBuilder {
|
|||||||
HashMap<String, InputStream> frameMap = new HashMap<>(frames.size());
|
HashMap<String, InputStream> frameMap = new HashMap<>(frames.size());
|
||||||
while((entry = zipInputStream.getNextEntry()) != null) {
|
while((entry = zipInputStream.getNextEntry()) != null) {
|
||||||
log.trace("ZipEntry {} 正在接收...", entry);
|
log.trace("ZipEntry {} 正在接收...", entry);
|
||||||
Streams.copy(zipInputStream, cacheOutputStream, false);
|
ByteStreams.copy(zipInputStream, cacheOutputStream);
|
||||||
frameMap.put(entry.getName(), new ByteArrayInputStream(cacheOutputStream.toByteArray()));
|
frameMap.put(entry.getName(), new ByteArrayInputStream(cacheOutputStream.toByteArray()));
|
||||||
log.trace("ZipEntry {} 已接收完成.", entry);
|
log.trace("ZipEntry {} 已接收完成.", entry);
|
||||||
cacheOutputStream.reset();
|
cacheOutputStream.reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
InputStream firstFrameInput = frameMap.get(frames.get(0).getAsJsonObject().get("file").getAsString());
|
||||||
InputStream firstFrameInput = frameMap.get("000000.jpg");
|
|
||||||
BufferedImage firstFrame = ImageIO.read(firstFrameInput);
|
BufferedImage firstFrame = ImageIO.read(firstFrameInput);
|
||||||
firstFrameInput.reset();
|
firstFrameInput.reset();
|
||||||
if(width != firstFrame.getWidth() || height != firstFrame.getHeight()) {
|
if(width != firstFrame.getWidth() || height != firstFrame.getHeight()) {
|
||||||
|
@ -0,0 +1,30 @@
|
|||||||
|
package net.lamgc.cgj.bot.util;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class GroupMuteManagerTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void muteStateTest() {
|
||||||
|
GroupMuteManager manager = new GroupMuteManager();
|
||||||
|
Assert.assertNull(manager.isMute(1, true)); // 未设置的群组返回null
|
||||||
|
Assert.assertFalse(manager.isMute(1, false)); // 未设置就返回false
|
||||||
|
manager.setMuteState(1, true); // mute == true
|
||||||
|
Assert.assertNotNull(manager.isMute(1, true)); // 第一次设置后不为null
|
||||||
|
Assert.assertTrue(manager.isMute(1, false)); // 确保条件正常
|
||||||
|
manager.setMuteState(2, true); // 不能出现不同群号的冲突
|
||||||
|
manager.setMuteState(1, false);
|
||||||
|
Assert.assertTrue(manager.isMute(2, false));
|
||||||
|
Assert.assertNotNull(manager.isMute(1, true)); // 变更为false后依然不能返回null
|
||||||
|
Assert.assertFalse(manager.isMute(1, false));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void invalidGroupIdTest() {
|
||||||
|
GroupMuteManager manager = new GroupMuteManager();
|
||||||
|
manager.setMuteState(-1, true); // 设置应该是无效的
|
||||||
|
Assert.assertFalse(manager.isMute(-1, false)); // 由于设置无效, 返回false即可
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Reference in New Issue
Block a user