95 lines
3.1 KiB
Java
95 lines
3.1 KiB
Java
package com.yutou.bilibili.services;
|
|
|
|
import com.yutou.biliapi.api.LiveApi;
|
|
import com.yutou.biliapi.bean.live.LiveRoomConfig;
|
|
import com.yutou.biliapi.bean.live.LiveRoomInfo;
|
|
import com.yutou.biliapi.bean.live.database.LiveConfigDatabaseBean;
|
|
import com.yutou.biliapi.databases.BiliLiveConfigDatabase;
|
|
import com.yutou.biliapi.net.BiliLiveNetApiManager;
|
|
import com.yutou.biliapi.net.WebSocketManager;
|
|
import com.yutou.bilibili.databases.SystemConfigDatabases;
|
|
import com.yutou.bilibili.datas.SystemConfigDatabaseBean;
|
|
import com.yutou.common.okhttp.HttpBody;
|
|
import jakarta.annotation.Resource;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.util.StringUtils;
|
|
import retrofit2.Response;
|
|
|
|
import java.io.IOException;
|
|
import java.util.Date;
|
|
import java.util.List;
|
|
import java.util.Timer;
|
|
import java.util.TimerTask;
|
|
import java.util.concurrent.Executors;
|
|
import java.util.concurrent.ScheduledExecutorService;
|
|
import java.util.concurrent.ScheduledFuture;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
@Service
|
|
public class SystemService {
|
|
@Resource
|
|
LiveVideoService videoService;
|
|
@Resource
|
|
LiveDanmuService danmuService;
|
|
SystemConfigDatabases databases = new SystemConfigDatabases();
|
|
private ScheduledExecutorService timer;
|
|
private ScheduledFuture<?> scheduled;
|
|
BiliLiveConfigDatabase liveConfigDatabase = new BiliLiveConfigDatabase();
|
|
|
|
public long getLoopTimer() {
|
|
SystemConfigDatabaseBean config = databases.getConfig();
|
|
if (config == null) {
|
|
return databases.getDefaultConfig().getTimerLoop();
|
|
}
|
|
return config.getTimerLoop();
|
|
}
|
|
|
|
public void start() {
|
|
if (timer == null) {
|
|
timer = Executors.newScheduledThreadPool(1);
|
|
}
|
|
System.out.println("执行任务");
|
|
if (scheduled != null) {
|
|
scheduled.cancel(true);
|
|
}
|
|
scheduled = timer.scheduleAtFixedRate(() -> {
|
|
List<LiveConfigDatabaseBean> list = liveConfigDatabase.getAllConfig();
|
|
System.out.println("循环任务:" + list.size());
|
|
for (LiveConfigDatabaseBean bean : list) {
|
|
if (bean.isRecordDanmu() && bean.checkRecordDanmuTime()) {
|
|
recordDanmu(bean);
|
|
}
|
|
if (bean.isRecordLive() && bean.checkRecordLiveTime()) {
|
|
recordVideo(bean);
|
|
}
|
|
}
|
|
}, 0, getLoopTimer(), TimeUnit.MILLISECONDS);
|
|
}
|
|
|
|
public void stop() {
|
|
scheduled.cancel(true);
|
|
videoService.stopAll();
|
|
}
|
|
|
|
// 录制弹幕
|
|
private void recordDanmu(LiveConfigDatabaseBean bean) {
|
|
danmuService.start(bean.getRoomId().toString(),false);
|
|
}
|
|
|
|
// 录制视频
|
|
private void recordVideo(LiveConfigDatabaseBean bean) {
|
|
videoService.start(bean, false);
|
|
}
|
|
|
|
public static void main(String[] args) throws InterruptedException {
|
|
SystemService service = new SystemService();
|
|
System.out.println(new Date());
|
|
service.start();
|
|
Thread.sleep(15000);
|
|
System.out.println(new Date());
|
|
service.start();
|
|
}
|
|
|
|
|
|
}
|