[Add] AutoSender 增加自动发送器接口;

[Add] RandomIntervalSendTimer 增加随机延迟自动发送器;
[Add] RecommendArtworksSender 增加随机排行榜作品发送器;
[Change] PixivDownload 重新开发 getRanking 方法;
This commit is contained in:
2020-04-20 01:32:11 +08:00
parent dd88f2acab
commit 9dfc20a525
4 changed files with 217 additions and 65 deletions

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