mirror of
https://github.com/LamGC/ContentGrabbingJi.git
synced 2025-07-04 06:17:26 +00:00
Compare commits
1 Commits
add-framew
...
optimize-s
Author | SHA1 | Date | |
---|---|---|---|
f976017a89
|
@ -5,7 +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/
|
||||||
ENTRYPOINT ["/usr/java/openjdk-14/bin/java", "-Duser.timezone=GMT+8"]
|
ENTRYPOINT ["/usr/java/openjdk-14/bin/java", "-Duser.timezone=Aisa/Shanghai"]
|
||||||
CMD ["-Dcgj.logsPath=/data/logs", "-jar", "/CGJ.jar", "botMode", "-botDataDir=/data"]
|
CMD ["-Dcgj.logsPath=/data/logs", "-jar", "/CGJ.jar", "botMode", "-botDataDir=/data"]
|
||||||
|
|
||||||
COPY ${jarFileName} /CGJ.jar
|
COPY ${jarFileName} /CGJ.jar
|
||||||
|
@ -54,14 +54,11 @@
|
|||||||
你应该注意到了,在部署过程中,你需要设置一个管理员QQ的配置,色图姬支持通过命令来管理色图姬的运行。
|
你应该注意到了,在部署过程中,你需要设置一个管理员QQ的配置,色图姬支持通过命令来管理色图姬的运行。
|
||||||
目前支持的管理员命令:
|
目前支持的管理员命令:
|
||||||
```bash
|
```bash
|
||||||
# 清除缓存(慎用)
|
# 清除缓存
|
||||||
# 该操作将会清除Redis服务器内的所有数据, 以及色图姬下载到本地的所有图片缓存.
|
|
||||||
.cgjadmin cleanCache
|
.cgjadmin cleanCache
|
||||||
|
|
||||||
# 设置配置项
|
# 设置配置项
|
||||||
# 如果不使用group参数, 则设置全局配置
|
# 如果不使用group参数, 则设置全局配置
|
||||||
# 注意: 配置项设置后需要使用`.cgjadmin saveProperties`才会保存到文件中,
|
|
||||||
# 如不保存, 则仅能在本次运行中生效(或使用`.cgjadmin loadProperties`重新加载后失效).
|
|
||||||
.cgjadmin setProperty <-key 配置项名> <-value 配置项新值> [-group 指定群组]
|
.cgjadmin setProperty <-key 配置项名> <-value 配置项新值> [-group 指定群组]
|
||||||
|
|
||||||
# 查询配置项
|
# 查询配置项
|
||||||
@ -82,8 +79,7 @@
|
|||||||
# 增加群组作品推送
|
# 增加群组作品推送
|
||||||
# 如果增加了original参数, 则图片为原图发送
|
# 如果增加了original参数, 则图片为原图发送
|
||||||
# 如果不指定group参数, 则群组为命令发送所在群组
|
# 如果不指定group参数, 则群组为命令发送所在群组
|
||||||
# 最长发送时间 = 最短发送时间 + 随机时间范围
|
.cgjadmin addPushGroup [-group 指定群组号] [-minTime 最小] [-floatTime 随机时间范围] [-rankingStart 排行榜起始排名]
|
||||||
.cgjadmin addPushGroup [-group 指定群组号] [-minTime 最短发送时间] [-floatTime 随机时间范围] [-rankingStart 排行榜起始排名]
|
|
||||||
[-rankingStop 排行榜结束排名] [-mode 排行榜模式] [-type 排行榜类型] [-original]
|
[-rankingStop 排行榜结束排名] [-mode 排行榜模式] [-type 排行榜类型] [-original]
|
||||||
|
|
||||||
# 删除群组推送功能
|
# 删除群组推送功能
|
||||||
|
@ -7,7 +7,6 @@ import com.google.gson.JsonElement;
|
|||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import net.lamgc.cgj.bot.boot.ApplicationBoot;
|
import net.lamgc.cgj.bot.boot.ApplicationBoot;
|
||||||
import net.lamgc.cgj.bot.boot.BotGlobal;
|
import net.lamgc.cgj.bot.boot.BotGlobal;
|
||||||
import net.lamgc.cgj.bot.framework.FrameworkManager;
|
|
||||||
import net.lamgc.cgj.bot.framework.cli.ConsoleMain;
|
import net.lamgc.cgj.bot.framework.cli.ConsoleMain;
|
||||||
import net.lamgc.cgj.bot.framework.coolq.SpringCQApplication;
|
import net.lamgc.cgj.bot.framework.coolq.SpringCQApplication;
|
||||||
import net.lamgc.cgj.bot.framework.mirai.MiraiMain;
|
import net.lamgc.cgj.bot.framework.mirai.MiraiMain;
|
||||||
@ -89,29 +88,19 @@ public class Main {
|
|||||||
|
|
||||||
@Command
|
@Command
|
||||||
public static void botMode(@Argument(name = "args", force = false) String argsStr) {
|
public static void botMode(@Argument(name = "args", force = false) String argsStr) {
|
||||||
try {
|
MiraiMain main = new MiraiMain();
|
||||||
FrameworkManager.registerFramework(new MiraiMain()).join();
|
main.init();
|
||||||
} catch (InterruptedException ignored) {
|
main.close();
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Command
|
@Command
|
||||||
public static void consoleMode() {
|
public static void consoleMode() throws IOException {
|
||||||
try {
|
ConsoleMain.start();
|
||||||
FrameworkManager.registerFramework(new ConsoleMain()).join();
|
|
||||||
} catch (InterruptedException ignored) {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Command
|
@Command
|
||||||
public static void pluginMode(@Argument(name = "args", force = false) String argsStr) {
|
public static void pluginMode(@Argument(name = "args", force = false) String argsStr) {
|
||||||
try {
|
new SpringCQApplication().start(argsStr);
|
||||||
FrameworkManager.registerFramework(new SpringCQApplication()).join();
|
|
||||||
} catch (InterruptedException ignored) {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Command
|
@Command
|
||||||
|
@ -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)) {
|
||||||
|
@ -1,41 +0,0 @@
|
|||||||
package net.lamgc.cgj.bot.framework;
|
|
||||||
|
|
||||||
public interface Framework {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 框架初始化方法
|
|
||||||
* @param resources 框架所分配到的资源.
|
|
||||||
* @throws Exception 当框架抛出异常时, 将不会继续运行框架.
|
|
||||||
* @see FrameworkResources
|
|
||||||
*/
|
|
||||||
void init(FrameworkResources resources) throws Exception;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 框架运行方法
|
|
||||||
* @throws Exception 当框架抛出异常时, 将会终止框架的所有活动.
|
|
||||||
*/
|
|
||||||
void run() throws Exception;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 关闭框架
|
|
||||||
* @throws Exception 即使该方法抛出异常, {@link FrameworkManager}依然会尝试向框架所属的线程发起中断, 以试图清除框架资源.
|
|
||||||
*/
|
|
||||||
void close() throws Exception;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取框架标识名.
|
|
||||||
* <p>可根据需要自行调整框架标识名.</p>
|
|
||||||
* @return 返回标识名.
|
|
||||||
*/
|
|
||||||
default String getIdentify() {
|
|
||||||
return this.getClass().getSimpleName() + "@" + Integer.toHexString(this.hashCode());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取框架名称.
|
|
||||||
* <p>框架名称不可更改.</p>
|
|
||||||
* @return 返回框架名称.
|
|
||||||
*/
|
|
||||||
String getFrameworkName();
|
|
||||||
|
|
||||||
}
|
|
@ -1,75 +0,0 @@
|
|||||||
package net.lamgc.cgj.bot.framework;
|
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
|
|
||||||
import java.util.*;
|
|
||||||
import java.util.regex.Pattern;
|
|
||||||
|
|
||||||
public final class FrameworkManager {
|
|
||||||
|
|
||||||
private FrameworkManager() {}
|
|
||||||
|
|
||||||
private final static Map<Framework, FrameworkResources> resourcesMap = new HashMap<>();
|
|
||||||
|
|
||||||
private final static ThreadGroup frameworkRootGroup = new ThreadGroup("FrameworkRootGroup");
|
|
||||||
|
|
||||||
static {
|
|
||||||
Runtime.getRuntime()
|
|
||||||
.addShutdownHook(new Thread(FrameworkManager::shutdownAllFramework, "FrameworkManager-Shutdown"));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Thread registerFramework(Framework framework) {
|
|
||||||
checkFramework(framework);
|
|
||||||
FrameworkResources resources = new FrameworkResources(framework);
|
|
||||||
resourcesMap.put(framework, resources);
|
|
||||||
Thread frameworkThread = new Thread(resources.getFrameworkThreadGroup(),
|
|
||||||
() -> FrameworkManager.runFramework(framework), "FrameworkThread-" + framework.getIdentify());
|
|
||||||
|
|
||||||
frameworkThread.start();
|
|
||||||
return frameworkThread;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static final Pattern FRAMEWORK_NAME_CHECK_PATTERN = Pattern.compile("^[A-Za-z0-9_\\-$]+$");
|
|
||||||
private static void checkFramework(Framework framework) {
|
|
||||||
if(!FRAMEWORK_NAME_CHECK_PATTERN.matcher(framework.getFrameworkName()).matches()) {
|
|
||||||
throw new IllegalStateException("Invalid Framework Name: " + framework.getFrameworkName());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Set<Framework> frameworkSet() {
|
|
||||||
return new HashSet<>(resourcesMap.keySet());
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void shutdownAllFramework() {
|
|
||||||
for (Framework framework : resourcesMap.keySet()) {
|
|
||||||
FrameworkResources frameworkResources = resourcesMap.get(framework);
|
|
||||||
Logger frameworkLogger = frameworkResources.getLogger();
|
|
||||||
try {
|
|
||||||
frameworkLogger.info("正在关闭框架...");
|
|
||||||
framework.close();
|
|
||||||
frameworkLogger.info("框架已关闭.");
|
|
||||||
frameworkResources.getFrameworkThreadGroup().interrupt();
|
|
||||||
resourcesMap.remove(framework);
|
|
||||||
} catch(Throwable e) {
|
|
||||||
frameworkLogger.error("退出框架时发生异常", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static ThreadGroup getFrameworkRootGroup() {
|
|
||||||
return frameworkRootGroup;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void runFramework(Framework framework) {
|
|
||||||
FrameworkResources frameworkResources = resourcesMap.get(framework);
|
|
||||||
try {
|
|
||||||
framework.init(frameworkResources);
|
|
||||||
framework.run();
|
|
||||||
} catch(Throwable e) {
|
|
||||||
frameworkResources.getLogger().error("框架未捕获异常, 导致异常退出.", e);
|
|
||||||
} finally {
|
|
||||||
frameworkResources.getFrameworkThreadGroup().interrupt();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,35 +0,0 @@
|
|||||||
package net.lamgc.cgj.bot.framework;
|
|
||||||
|
|
||||||
import net.lamgc.cgj.bot.boot.BotGlobal;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
|
|
||||||
public class FrameworkResources {
|
|
||||||
|
|
||||||
private final static File frameworkDataStoreRootDir = new File(BotGlobal.getGlobal().getDataStoreDir(),
|
|
||||||
"frameworks/");
|
|
||||||
|
|
||||||
private final ThreadGroup frameworkThreadGroup;
|
|
||||||
|
|
||||||
private final Logger logger;
|
|
||||||
|
|
||||||
private final File frameworkDataStoreDir;
|
|
||||||
|
|
||||||
public FrameworkResources(Framework framework) {
|
|
||||||
frameworkThreadGroup = new ThreadGroup(FrameworkManager.getFrameworkRootGroup(),
|
|
||||||
"Framework-" + framework.getIdentify());
|
|
||||||
frameworkDataStoreDir = new File(frameworkDataStoreRootDir, framework.getClass().getSimpleName());
|
|
||||||
logger = LoggerFactory.getLogger("Framework-" + framework.getIdentify());
|
|
||||||
}
|
|
||||||
|
|
||||||
ThreadGroup getFrameworkThreadGroup() {
|
|
||||||
return frameworkThreadGroup;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Logger getLogger() {
|
|
||||||
return logger;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -2,9 +2,6 @@ package net.lamgc.cgj.bot.framework.cli;
|
|||||||
|
|
||||||
import net.lamgc.cgj.bot.boot.ApplicationBoot;
|
import net.lamgc.cgj.bot.boot.ApplicationBoot;
|
||||||
import net.lamgc.cgj.bot.event.BotEventHandler;
|
import net.lamgc.cgj.bot.event.BotEventHandler;
|
||||||
import net.lamgc.cgj.bot.framework.Framework;
|
|
||||||
import net.lamgc.cgj.bot.framework.FrameworkManager;
|
|
||||||
import net.lamgc.cgj.bot.framework.FrameworkResources;
|
|
||||||
import net.lamgc.cgj.bot.framework.cli.message.ConsoleMessageEvent;
|
import net.lamgc.cgj.bot.framework.cli.message.ConsoleMessageEvent;
|
||||||
import net.lamgc.cgj.bot.framework.cli.message.ConsoleMessageSenderFactory;
|
import net.lamgc.cgj.bot.framework.cli.message.ConsoleMessageSenderFactory;
|
||||||
import net.lamgc.cgj.bot.message.MessageSenderBuilder;
|
import net.lamgc.cgj.bot.message.MessageSenderBuilder;
|
||||||
@ -15,18 +12,13 @@ import org.jline.terminal.TerminalBuilder;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.io.IOException;
|
||||||
|
|
||||||
public class ConsoleMain implements Framework {
|
public class ConsoleMain {
|
||||||
|
|
||||||
private final static Logger log = LoggerFactory.getLogger(ConsoleMain.class);
|
private final static Logger log = LoggerFactory.getLogger(ConsoleMain.class);
|
||||||
private final AtomicBoolean quitState = new AtomicBoolean();
|
|
||||||
|
|
||||||
@Override
|
public static void start() throws IOException {
|
||||||
public void init(FrameworkResources resources) { }
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void run() throws Exception {
|
|
||||||
MessageSenderBuilder.setCurrentMessageSenderFactory(new ConsoleMessageSenderFactory());
|
MessageSenderBuilder.setCurrentMessageSenderFactory(new ConsoleMessageSenderFactory());
|
||||||
ApplicationBoot.initialBot();
|
ApplicationBoot.initialBot();
|
||||||
LineReader lineReader = LineReaderBuilder.builder()
|
LineReader lineReader = LineReaderBuilder.builder()
|
||||||
@ -53,22 +45,7 @@ public class ConsoleMain implements Framework {
|
|||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
log.error("执行时发生中断", e);
|
log.error("执行时发生中断", e);
|
||||||
}
|
}
|
||||||
} while(!quitState.get());
|
} while(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void close() {
|
|
||||||
quitState.set(true);
|
|
||||||
Thread.currentThread().getThreadGroup().interrupt();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getIdentify() {
|
|
||||||
return this.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getFrameworkName() {
|
|
||||||
return "console";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
package net.lamgc.cgj.bot.framework.coolq;
|
package net.lamgc.cgj.bot.framework.coolq;
|
||||||
|
|
||||||
|
import com.google.common.base.Strings;
|
||||||
import net.lamgc.cgj.bot.boot.BotGlobal;
|
import net.lamgc.cgj.bot.boot.BotGlobal;
|
||||||
import net.lamgc.cgj.bot.framework.Framework;
|
|
||||||
import net.lamgc.cgj.bot.framework.FrameworkResources;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
import org.springframework.boot.context.event.ApplicationFailedEvent;
|
import org.springframework.boot.context.event.ApplicationFailedEvent;
|
||||||
@ -12,21 +12,28 @@ import org.springframework.context.ConfigurableApplicationContext;
|
|||||||
import org.springframework.context.event.ContextClosedEvent;
|
import org.springframework.context.event.ContextClosedEvent;
|
||||||
import org.springframework.context.event.ContextStoppedEvent;
|
import org.springframework.context.event.ContextStoppedEvent;
|
||||||
|
|
||||||
@SpringBootApplication
|
import java.util.ArrayList;
|
||||||
public class SpringCQApplication implements Framework {
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
private Logger log;
|
@SpringBootApplication
|
||||||
|
public class SpringCQApplication {
|
||||||
|
|
||||||
|
private final static Logger log = LoggerFactory.getLogger(SpringCQApplication.class);
|
||||||
|
|
||||||
private final Object quitLock = new Object();
|
private final Object quitLock = new Object();
|
||||||
|
|
||||||
@Override
|
public void start(String argsStr) {
|
||||||
public void init(FrameworkResources resources) {
|
|
||||||
this.log = resources.getLogger();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void run() {
|
|
||||||
log.info("酷Q机器人根目录: {}", BotGlobal.getGlobal().getDataStoreDir().getPath());
|
log.info("酷Q机器人根目录: {}", BotGlobal.getGlobal().getDataStoreDir().getPath());
|
||||||
ConfigurableApplicationContext context = SpringApplication.run(SpringCQApplication.class);
|
Pattern pattern = Pattern.compile("/\\s*(\".+?\"|[^:\\s])+((\\s*:\\s*(\".+?\"|[^\\s])+)|)|(\".+?\"|[^\"\\s])+");
|
||||||
|
Matcher matcher = pattern.matcher(Strings.nullToEmpty(argsStr));
|
||||||
|
ArrayList<String> argsList = new ArrayList<>();
|
||||||
|
while (matcher.find()) {
|
||||||
|
argsList.add(matcher.group());
|
||||||
|
}
|
||||||
|
String[] args = new String[argsList.size()];
|
||||||
|
argsList.toArray(args);
|
||||||
|
ConfigurableApplicationContext context = SpringApplication.run(SpringCQApplication.class, args);
|
||||||
registerShutdownHook(context);
|
registerShutdownHook(context);
|
||||||
try {
|
try {
|
||||||
synchronized (quitLock) {
|
synchronized (quitLock) {
|
||||||
@ -35,30 +42,22 @@ public class SpringCQApplication implements Framework {
|
|||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
log.warn("发生中断, 退出SpringCQ...", e);
|
log.warn("发生中断, 退出SpringCQ...", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
context.stop();
|
|
||||||
context.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void registerShutdownHook(ConfigurableApplicationContext context) {
|
private void registerShutdownHook(ConfigurableApplicationContext context) {
|
||||||
context.addApplicationListener((ApplicationListener<ApplicationFailedEvent>)
|
context.addApplicationListener((ApplicationListener<ApplicationFailedEvent>)
|
||||||
event -> close());
|
event -> notifyThread());
|
||||||
context.addApplicationListener((ApplicationListener<ContextClosedEvent>)
|
context.addApplicationListener((ApplicationListener<ContextClosedEvent>)
|
||||||
event -> close());
|
event -> notifyThread());
|
||||||
context.addApplicationListener((ApplicationListener<ContextStoppedEvent>)
|
context.addApplicationListener((ApplicationListener<ContextStoppedEvent>)
|
||||||
event -> close());
|
event -> notifyThread());
|
||||||
Runtime.getRuntime().addShutdownHook(new Thread(this::close));
|
Runtime.getRuntime().addShutdownHook(new Thread(this::notifyThread));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void close() {
|
private void notifyThread() {
|
||||||
synchronized (quitLock) {
|
synchronized (quitLock) {
|
||||||
quitLock.notify();
|
quitLock.notify();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getFrameworkName() {
|
|
||||||
return "SpringCoolQ";
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -3,8 +3,6 @@ package net.lamgc.cgj.bot.framework.mirai;
|
|||||||
import net.lamgc.cgj.bot.boot.ApplicationBoot;
|
import net.lamgc.cgj.bot.boot.ApplicationBoot;
|
||||||
import net.lamgc.cgj.bot.boot.BotGlobal;
|
import net.lamgc.cgj.bot.boot.BotGlobal;
|
||||||
import net.lamgc.cgj.bot.event.BotEventHandler;
|
import net.lamgc.cgj.bot.event.BotEventHandler;
|
||||||
import net.lamgc.cgj.bot.framework.Framework;
|
|
||||||
import net.lamgc.cgj.bot.framework.FrameworkResources;
|
|
||||||
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;
|
||||||
@ -27,7 +25,7 @@ import java.io.*;
|
|||||||
import java.util.Base64;
|
import java.util.Base64;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
public class MiraiMain implements Framework {
|
public class MiraiMain implements Closeable {
|
||||||
|
|
||||||
private final Logger log = LoggerFactory.getLogger(MiraiMain.class);
|
private final Logger log = LoggerFactory.getLogger(MiraiMain.class);
|
||||||
|
|
||||||
@ -37,8 +35,7 @@ public class MiraiMain implements Framework {
|
|||||||
|
|
||||||
private final GroupMuteManager muteManager = new GroupMuteManager();
|
private final GroupMuteManager muteManager = new GroupMuteManager();
|
||||||
|
|
||||||
@Override
|
public void init() {
|
||||||
public void init(FrameworkResources resources) {
|
|
||||||
Runtime.getRuntime().addShutdownHook(new Thread(this::close));
|
Runtime.getRuntime().addShutdownHook(new Thread(this::close));
|
||||||
try {
|
try {
|
||||||
Class.forName(BotEventHandler.class.getName());
|
Class.forName(BotEventHandler.class.getName());
|
||||||
@ -75,7 +72,6 @@ public class MiraiMain implements Framework {
|
|||||||
|
|
||||||
bot = BotFactoryJvm.newBot(Long.parseLong(botProperties.getProperty("bot.qq", "0")),
|
bot = BotFactoryJvm.newBot(Long.parseLong(botProperties.getProperty("bot.qq", "0")),
|
||||||
Base64.getDecoder().decode(botProperties.getProperty("bot.password", "")), configuration);
|
Base64.getDecoder().decode(botProperties.getProperty("bot.password", "")), configuration);
|
||||||
// TODO: 看看能不能单独订阅某个Bot?
|
|
||||||
Events.subscribeAlways(GroupMessageEvent.class, this::executeMessageEvent);
|
Events.subscribeAlways(GroupMessageEvent.class, this::executeMessageEvent);
|
||||||
Events.subscribeAlways(FriendMessageEvent.class, this::executeMessageEvent);
|
Events.subscribeAlways(FriendMessageEvent.class, this::executeMessageEvent);
|
||||||
Events.subscribeAlways(TempMessageEvent.class, this::executeMessageEvent);
|
Events.subscribeAlways(TempMessageEvent.class, this::executeMessageEvent);
|
||||||
@ -89,12 +85,6 @@ public class MiraiMain implements Framework {
|
|||||||
bot.join();
|
bot.join();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
bot.login();
|
|
||||||
bot.join();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 处理消息事件
|
* 处理消息事件
|
||||||
* @param message 消息事件对象
|
* @param message 消息事件对象
|
||||||
@ -117,7 +107,6 @@ public class MiraiMain implements Framework {
|
|||||||
/**
|
/**
|
||||||
* 关闭机器人
|
* 关闭机器人
|
||||||
*/
|
*/
|
||||||
@Override
|
|
||||||
public synchronized void close() {
|
public synchronized void close() {
|
||||||
if(bot == null) {
|
if(bot == null) {
|
||||||
return;
|
return;
|
||||||
@ -128,9 +117,4 @@ public class MiraiMain implements Framework {
|
|||||||
log.warn("机器人已关闭.");
|
log.warn("机器人已关闭.");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getFrameworkName() {
|
|
||||||
return "MiraiQQ";
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -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, 则为每天
|
||||||
|
Reference in New Issue
Block a user