mirror of
https://github.com/LamGC/ContentGrabbingJi.git
synced 2025-07-02 21:37:26 +00:00
[Add] AutoSender 增加自动发送器接口;
[Add] RandomIntervalSendTimer 增加随机延迟自动发送器; [Add] RecommendArtworksSender 增加随机排行榜作品发送器; [Change] PixivDownload 重新开发 getRanking 方法;
This commit is contained in:
30
src/main/java/net/lamgc/cgj/bot/AutoSender.java
Normal file
30
src/main/java/net/lamgc/cgj/bot/AutoSender.java
Normal file
@ -0,0 +1,30 @@
|
||||
package net.lamgc.cgj.bot;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 自动发送器
|
||||
*/
|
||||
public abstract class AutoSender {
|
||||
|
||||
private MessageSender messageSender;
|
||||
|
||||
/**
|
||||
* 构造一个自动发送器
|
||||
* @param messageSender 自动发送器所使用的消息发送器
|
||||
*/
|
||||
public AutoSender(MessageSender messageSender) {
|
||||
this.messageSender = Objects.requireNonNull(messageSender);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取设置等等消息发送器
|
||||
* @return 消息发送器
|
||||
*/
|
||||
MessageSender getMessageSender() {
|
||||
return this.messageSender;
|
||||
}
|
||||
|
||||
public abstract void send();
|
||||
|
||||
}
|
70
src/main/java/net/lamgc/cgj/bot/RandomIntervalSendTimer.java
Normal file
70
src/main/java/net/lamgc/cgj/bot/RandomIntervalSendTimer.java
Normal file
@ -0,0 +1,70 @@
|
||||
package net.lamgc.cgj.bot;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Random;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
/**
|
||||
* 随机间隔发送器
|
||||
*/
|
||||
public class RandomIntervalSendTimer extends TimerTask {
|
||||
|
||||
private final static Timer timer = new Timer("Thread-RIST");
|
||||
private final static Logger log = LoggerFactory.getLogger("RandomIntervalSendTimer");
|
||||
private final Random timeRandom = new Random();
|
||||
private final AutoSender sender;
|
||||
private final long time;
|
||||
private final int floatTime;
|
||||
private AtomicBoolean loop = new AtomicBoolean();
|
||||
private final AtomicBoolean start = new AtomicBoolean();
|
||||
|
||||
|
||||
public RandomIntervalSendTimer(AutoSender sender, long time, int floatTime) {
|
||||
this.sender = sender;
|
||||
this.time = time;
|
||||
this.floatTime = floatTime;
|
||||
}
|
||||
|
||||
public void start() {
|
||||
start(this.loop.get());
|
||||
}
|
||||
|
||||
public void start(boolean loop) {
|
||||
this.loop.set(loop);
|
||||
long nextDelay = time + timeRandom.nextInt(floatTime);
|
||||
log.info("定时器 {} 下一延迟: {}ms", Integer.toHexString(this.hashCode()), nextDelay);
|
||||
if(start.get()) {
|
||||
try {
|
||||
Field state = this.getClass().getSuperclass().getDeclaredField("state");
|
||||
state.setAccessible(true);
|
||||
state.setInt(this, 0);
|
||||
state.setAccessible(false);
|
||||
} catch (NoSuchFieldException | IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
}
|
||||
start.set(true);
|
||||
timer.schedule(this, nextDelay);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
sender.send();
|
||||
if (this.loop.get()) {
|
||||
start();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean cancel() {
|
||||
start.set(false);
|
||||
return super.cancel();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,85 @@
|
||||
package net.lamgc.cgj.bot;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import net.lamgc.cgj.pixiv.PixivDownload;
|
||||
import net.lamgc.cgj.pixiv.PixivURL;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* 推荐作品发送器
|
||||
*/
|
||||
public class RandomRankingArtworksSender extends AutoSender {
|
||||
|
||||
private final Logger log;
|
||||
private final int rankingStart;
|
||||
private final int rankingStop;
|
||||
private final PixivDownload.PageQuality quality;
|
||||
|
||||
/**
|
||||
* 构造一个推荐作品发送器
|
||||
* @param messageSender 消息发送器
|
||||
* @param rankingStart 排行榜开始范围(从1开始, 名次)
|
||||
* @param rankingStop 排名榜结束范围(包括该名次)
|
||||
* @param quality 图片质量, 详见{@link net.lamgc.cgj.pixiv.PixivDownload.PageQuality}
|
||||
*/
|
||||
public RandomRankingArtworksSender(MessageSender messageSender, int rankingStart, int rankingStop, PixivDownload.PageQuality quality) {
|
||||
super(messageSender);
|
||||
log = LoggerFactory.getLogger("RecommendArtworksSender@" + Integer.toHexString(this.hashCode()));
|
||||
this.rankingStart = rankingStart > 0 ? rankingStart : 1;
|
||||
this.rankingStop = rankingStop > 0 ? rankingStop : 150;
|
||||
this.quality = quality == null ? PixivDownload.PageQuality.REGULAR : quality;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send() {
|
||||
Date queryDate = new Date();
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.setTime(queryDate);
|
||||
if(calendar.get(Calendar.HOUR_OF_DAY) < 12) {
|
||||
calendar.add(Calendar.DAY_OF_YEAR, -2);
|
||||
} else {
|
||||
calendar.add(Calendar.DAY_OF_YEAR, -1);
|
||||
}
|
||||
queryDate = calendar.getTime();
|
||||
|
||||
int selectRanking = rankingStart + new Random().nextInt(rankingStop + 1);
|
||||
try {
|
||||
List<JsonObject> rankingList = BotCommandProcess.getRankingInfoByCache(
|
||||
PixivURL.RankingContentType.TYPE_ILLUST,
|
||||
PixivURL.RankingMode.MODE_DAILY,
|
||||
queryDate,
|
||||
selectRanking,
|
||||
1, false);
|
||||
|
||||
log.info("RankingResult.size: {}", rankingList.size());
|
||||
if(rankingList.size() != 1) {
|
||||
log.error("排行榜选取失败!(获取到了多个结果)");
|
||||
return;
|
||||
}
|
||||
|
||||
JsonObject rankingInfo = rankingList.get(0);
|
||||
int illustId = rankingInfo.get("illust_id").getAsInt();
|
||||
if(BotCommandProcess.isNoSafe(illustId, BotCommandProcess.globalProp, false)) {
|
||||
log.warn("作品为r18作品, 取消本次发送.");
|
||||
return;
|
||||
}
|
||||
|
||||
StringBuilder message = new StringBuilder();
|
||||
message.append("#美图推送 - 今日排行榜 第 ").append(rankingInfo.get("rank").getAsInt()).append(" 名\n");
|
||||
message.append("标题:").append(rankingInfo.get("title").getAsString()).append("(").append(illustId).append(")\n");
|
||||
message.append("作者:").append(rankingInfo.get("user_name").getAsString()).append("\n");
|
||||
message.append(BotCommandProcess.getImageById(illustId, quality, 1));
|
||||
|
||||
getMessageSender().sendMessage(message.toString());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user