Compare commits

..

9 Commits

Author SHA1 Message Date
f976017a89 [Update] 使用搜索推荐接口优化搜索功能对搜索内容的相关性;
[Add] PixivURL 增加搜索推荐接口Url;
[Add] PixivSearchLinkBuilder 增加`getContent`方法以允许外部获取搜索原始内容;
[Add] CacheStoreCentral 增加`getSearchBody(PixivSearchLinkBuilder)`方法以允许直接提供builder进行请求;
[Change] CacheStoreCentral, PixivUtils 移除`getSearchBody(String, ...)`方法对PixivSearchLinkBuilder的构建, 将该部分转移至`PixivUtils.buildSearchLinkBuilder`方法;
[Update] BotCommandProcess 增加`addRecommendKeywords(PixivSearchLinkBuilder, int)`方法以针对PixivSearchLinkBuilder的搜索原始内容增加指定数量的搜索推荐关键词;
2020-07-15 09:00:00 +08:00
575dc0c7fb [Fix] Dockerfile.sample 修复了容器无法正常启动的问题; 2020-07-14 11:01:21 +08:00
56ef463c63 [Change] MessageEvent 调整toString输出中hashCode的格式; 2020-07-13 20:35:39 +08:00
4387da37f5 [Change] 调整群禁言管理职责;
[Delete] BotEventHandler 移除禁言状态管理相关功能;
[Add] GroupMuteManager, GroupMuteManagerTest 增加群禁言状态管理类;
[Change] MiraiMain 将禁言状态对接由使用BotEventHandler改为独立管理;
2020-07-13 09:47:36 +08:00
0fc3e3ab48 [Change] 调整应用内事件线程池参数;
[Change] BotEventHandler 调整线程池参数;
2020-07-13 09:47:31 +08:00
a606ec0423 [Fix] IssueTemplates 修复模板错误的问题; 2020-07-12 11:18:29 +08:00
210aa84ed5 [Update] IssueTemplates 增加功能模板并更新Bug模板; 2020-07-12 11:12:21 +08:00
6fbbe522db [Fix #22] PixivDownload 修复因InputStream意外关闭导致的异常误判; 2020-07-12 01:12:58 +08:00
6d55325fc7 [Fix] PixivUgoiraBuilder 修复ZipInputStream在读取完第一帧图像后被意外关闭的问题; 2020-07-10 09:53:41 +08:00
15 changed files with 315 additions and 144 deletions

View File

@ -1,26 +1,35 @@
---
name: Bug Report
about: Use this template to feedback bugs.
name: Bug 反馈
about: 使用这个模板反馈应用问题。
title: ''
labels: bug
assignees: ''
---
## Environmental information ##
OS(e.g: Windows 10 1909):
Java(e.g: Oracle Jdk 8.242):
Issue version(versionTag or commitId):
## 环境信息 ##
系统(例如: Windows 10 1909): ``
Java版本(例如: Oracle Jdk 8.242): ``
<!-- 如果直接使用发布版那么就填写发布版版本号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.
## 相关信息 ##
### 日志 ###
<!-- 如果日志涉及了问题,请务必将日志一同提交,这对排查问题非常有用 -->
```
```

View File

@ -0,0 +1,11 @@
---
name: 功能/想法 提议
about: 使用这个模板将你对应用的想法提出来,或许我们会采纳!
title: ''
labels: function, question
assignees: ''
---
<!-- 如果可以,尽可能的清晰、详细的表达你的想法 -->
<!-- 没关系的,我们会进一步向你交流以尝试了解你的想法! -->

View File

@ -5,6 +5,7 @@ ENV jarFileName=ContentGrabbingJi-exec.jar
ENV CGJ_REDIS_URI="127.0.0.1:6379"
ENV CGJ_PROXY=""
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

View File

@ -11,12 +11,17 @@ import net.lamgc.cgj.bot.cache.JsonRedisCacheStore;
import net.lamgc.cgj.bot.event.BufferedMessageSender;
import net.lamgc.cgj.bot.sort.PreLoadDataAttribute;
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.PageQuality;
import net.lamgc.cgj.pixiv.PixivSearchLinkBuilder;
import net.lamgc.cgj.pixiv.PixivURL;
import net.lamgc.utils.base.runner.Argument;
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.LoggerFactory;
@ -315,6 +320,16 @@ public class BotCommandProcess {
@Argument(name = "p", force = false, defaultValue = "1") int pagesIndex
) throws IOException, InterruptedException {
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()
.getSearchBody(content, type, area, includeKeywords, excludeKeywords, contentOption, pagesIndex);
@ -525,6 +540,43 @@ public class BotCommandProcess {
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

View File

@ -8,6 +8,7 @@ import net.lamgc.cgj.bot.BotCode;
import net.lamgc.cgj.bot.BotCommandProcess;
import net.lamgc.cgj.bot.SettingProperties;
import net.lamgc.cgj.bot.boot.BotGlobal;
import net.lamgc.cgj.bot.util.PixivUtils;
import net.lamgc.cgj.exception.HttpRequestException;
import net.lamgc.cgj.pixiv.PixivDownload;
import net.lamgc.cgj.pixiv.PixivSearchLinkBuilder;
@ -405,73 +406,11 @@ public final class CacheStoreCentral {
return PixivDownload.getRanking(result, start - 1, range);
}
/**
* 获取搜索结果
* @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());
public JsonObject getSearchBody(PixivSearchLinkBuilder searchLinkBuilder) throws IOException {
log.debug("正在搜索作品, 条件: {}", searchLinkBuilder.getSearchCondition());
Locker<String> locker
= buildSyncKey(searchBuilder.buildURL());
= buildSyncKey(searchLinkBuilder.buildURL());
String requestUrl = locker.getKey();
log.debug("RequestUrl: {}", requestUrl);
JsonObject resultBody = null;
@ -524,6 +463,31 @@ public final class CacheStoreCentral {
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) {
String cacheKey = illustId + ":" + pageIndex;
if(!imageChecksumCache.exists(cacheKey)) {

View File

@ -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 ? "已禁言" : "已解除");
}
}

View File

@ -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() + '\'' +

View File

@ -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.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;
@ -30,7 +31,9 @@ public class MiraiMain implements Closeable {
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() {
Runtime.getRuntime().addShutdownHook(new Thread(this::close));
@ -73,9 +76,9 @@ public class MiraiMain implements Closeable {
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();
@ -90,9 +93,12 @@ public class MiraiMain implements Closeable {
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));

View 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);
}
}
}

View 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;
}
}

View File

@ -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");

View File

@ -161,6 +161,10 @@ public class PixivSearchLinkBuilder {
'}';
}
public String getContent() {
return content;
}
public PixivSearchLinkBuilder setSearchArea(SearchArea searchArea) {
this.searchArea = Objects.requireNonNull(searchArea);
return this;

View File

@ -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}";
/**
* 搜索推荐接口.
* <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, 则为每天

View File

@ -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()) {

View File

@ -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即可
}
}