mirror of
https://github.com/LamGC/ContentGrabbingJi.git
synced 2025-07-02 21:37:26 +00:00
[Change] 调整包路径;
[Change] 修改 CQPluginMain 功能, 将命令处理的具体细节迁移;
This commit is contained in:
27
src/main/java/net/lamgc/cgj/bot/CQConfig.java
Normal file
27
src/main/java/net/lamgc/cgj/bot/CQConfig.java
Normal file
@ -0,0 +1,27 @@
|
||||
package net.lamgc.cgj.bot;
|
||||
|
||||
import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
||||
import net.lz1998.cq.CQGlobal;
|
||||
import net.lz1998.cq.EnableCQ;
|
||||
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@EnableCQ
|
||||
public class CQConfig {
|
||||
|
||||
public static void init() {
|
||||
CQGlobal.pluginList.add(CQPluginMain.class);
|
||||
CQGlobal.executor = new ThreadPoolExecutor(
|
||||
(int) Math.ceil(Runtime.getRuntime().availableProcessors() / 2F),
|
||||
Runtime.getRuntime().availableProcessors(),
|
||||
25, TimeUnit.SECONDS,
|
||||
new LinkedBlockingQueue<>(512),
|
||||
new ThreadFactoryBuilder()
|
||||
.setNameFormat("Plugin-ProcessThread-%d")
|
||||
.build()
|
||||
);
|
||||
}
|
||||
|
||||
}
|
49
src/main/java/net/lamgc/cgj/bot/CQPluginMain.java
Normal file
49
src/main/java/net/lamgc/cgj/bot/CQPluginMain.java
Normal file
@ -0,0 +1,49 @@
|
||||
package net.lamgc.cgj.bot;
|
||||
|
||||
import net.lamgc.cgj.bot.event.BotEventHandler;
|
||||
import net.lamgc.cgj.bot.event.SpringCQMessageEvent;
|
||||
import net.lamgc.utils.event.EventHandler;
|
||||
import net.lz1998.cq.event.message.CQDiscussMessageEvent;
|
||||
import net.lz1998.cq.event.message.CQGroupMessageEvent;
|
||||
import net.lz1998.cq.event.message.CQMessageEvent;
|
||||
import net.lz1998.cq.event.message.CQPrivateMessageEvent;
|
||||
import net.lz1998.cq.robot.CQPlugin;
|
||||
import net.lz1998.cq.robot.CoolQ;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class CQPluginMain extends CQPlugin implements EventHandler {
|
||||
|
||||
public CQPluginMain() {
|
||||
LoggerFactory.getLogger(this.toString())
|
||||
.info("BotEventHandler.COMMAND_PREFIX = {}", BotEventHandler.COMMAND_PREFIX);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int onPrivateMessage(CoolQ cq, CQPrivateMessageEvent event) {
|
||||
//log.info("私聊消息到达: 发送者[{}], 消息内容: {}", event.getSender().getUserId(), event.getMessage());
|
||||
return processMessage(cq, event);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int onGroupMessage(CoolQ cq, CQGroupMessageEvent event) {
|
||||
//log.info("群消息到达: 群[{}], 发送者[{}], 消息内容: {}", event.getGroupId(), event.getSender().getUserId(), event.getMessage());
|
||||
return processMessage(cq, event);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int onDiscussMessage(CoolQ cq, CQDiscussMessageEvent event) {
|
||||
//log.info("讨论组消息到达: 群[{}], 发送者[{}], 消息内容: {}", event.getDiscussId(), event.getSender().getUserId(), event.getMessage());
|
||||
return processMessage(cq, event);
|
||||
}
|
||||
|
||||
public int processMessage(CoolQ cq, CQMessageEvent event) {
|
||||
if(!BotEventHandler.match(event.getMessage())) {
|
||||
return MESSAGE_IGNORE;
|
||||
}
|
||||
BotEventHandler.executor.executor(new SpringCQMessageEvent(cq, event));
|
||||
return MESSAGE_BLOCK;
|
||||
}
|
||||
|
||||
}
|
86
src/main/java/net/lamgc/cgj/bot/RankingUpdateTimer.java
Normal file
86
src/main/java/net/lamgc/cgj/bot/RankingUpdateTimer.java
Normal file
@ -0,0 +1,86 @@
|
||||
package net.lamgc.cgj.bot;
|
||||
|
||||
import net.lamgc.cgj.pixiv.PixivURL;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.LocalDate;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
|
||||
public class RankingUpdateTimer {
|
||||
|
||||
private final Timer timer = new Timer("PixivRankingUpdate@" + Integer.toHexString(this.hashCode()), true);
|
||||
private final Logger log = LoggerFactory.getLogger(this.toString());
|
||||
|
||||
/**
|
||||
* 启动定时任务.
|
||||
* 本方法在设置后立即返回
|
||||
* @param firstRunDate 首次运行时间, 只需要设置日期, 时间为自动设置.
|
||||
*/
|
||||
public void schedule(Date firstRunDate) {
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.setTime(firstRunDate == null ? new Date() : firstRunDate);
|
||||
LocalDate currentLocalDate = LocalDate.now();
|
||||
if(cal.get(Calendar.DAY_OF_YEAR) <= currentLocalDate.getDayOfYear() && cal.get(Calendar.HOUR_OF_DAY) >= 12) {
|
||||
cal.set(Calendar.DAY_OF_YEAR, cal.get(Calendar.DAY_OF_YEAR) + 1);
|
||||
}
|
||||
cal.set(Calendar.HOUR_OF_DAY, 12);
|
||||
cal.set(Calendar.MINUTE, 30);
|
||||
cal.set(Calendar.SECOND, 0);
|
||||
cal.set(Calendar.MILLISECOND, 0);
|
||||
|
||||
log.warn("已设置排行榜定时更新, 首次运行时间: {}", cal.getTime());
|
||||
timer.schedule(new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
now(null);
|
||||
}
|
||||
}, cal.getTime(), 86400000); // 1 Day
|
||||
}
|
||||
|
||||
public void now(Date queryDate) {
|
||||
log.info("当前时间 {}, 定时任务开始执行...", new Date());
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.setTime(queryDate == null ? new Date() : queryDate);
|
||||
|
||||
LocalDate currentLocalDate = LocalDate.now();
|
||||
if(calendar.get(Calendar.DAY_OF_YEAR) == currentLocalDate.getDayOfYear() ||
|
||||
calendar.get(Calendar.DAY_OF_YEAR) == currentLocalDate.getDayOfYear() - 1) {
|
||||
if(calendar.get(Calendar.HOUR_OF_DAY) < 12) {
|
||||
calendar.add(Calendar.DAY_OF_YEAR, -2);
|
||||
} else {
|
||||
calendar.add(Calendar.DAY_OF_YEAR, -1);
|
||||
}
|
||||
}
|
||||
|
||||
log.info("正在获取 {} 期排行榜数据...", calendar.getTime());
|
||||
for (PixivURL.RankingMode rankingMode : PixivURL.RankingMode.values()) {
|
||||
for (PixivURL.RankingContentType contentType : PixivURL.RankingContentType.values()) {
|
||||
if(!contentType.isSupportedMode(rankingMode)) {
|
||||
log.debug("不支持的类型, 填空值跳过...(类型: {}.{})", rankingMode.name(), contentType.name());
|
||||
}
|
||||
log.info("当前排行榜类型: {}.{}, 正在更新...", rankingMode.name(), contentType.name());
|
||||
try {
|
||||
BotCommandProcess.getRankingInfoByCache(contentType, rankingMode, calendar.getTime(), 1, 0, true);
|
||||
log.info("排行榜 {}.{} 更新完成.", rankingMode.name(), contentType.name());
|
||||
} catch (IOException e) {
|
||||
log.error("排行榜 {}.{} 更新时发生异常", rankingMode.name(), contentType.name());
|
||||
log.error("异常信息如下", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消任务.
|
||||
*/
|
||||
public void stop() {
|
||||
timer.cancel();
|
||||
log.warn("排行榜更新任务已取消.");
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user