From 8afb8f8f0b6404eed84037add51c667cca04751d Mon Sep 17 00:00:00 2001 From: Yutousama <583819556@qq.com> Date: Thu, 28 Nov 2024 23:18:24 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E7=BB=9F=E4=B8=80=E7=AE=A1=E7=90=86Bi?= =?UTF-8?q?liLiveDatabase?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...lientManager.java => WebSocketServer.java} | 39 +++++++++---------- .../bilibili/services/LiveDanmuService.java | 23 ++++++----- .../services/LiveDatabasesService.java | 21 ++++++++++ .../yutou/bilibili/services/LiveService.java | 7 +++- .../services/LiveVideoDownloadService.java | 25 ++++++------ .../bilibili/services/SystemService.java | 1 + .../com/yutou/common/utils/RedisTools.java | 4 +- src/main/resources/application.properties | 7 +++- 8 files changed, 79 insertions(+), 48 deletions(-) rename src/main/java/com/yutou/biliapi/net/{WebSocketClientManager.java => WebSocketServer.java} (91%) create mode 100644 src/main/java/com/yutou/bilibili/services/LiveDatabasesService.java diff --git a/src/main/java/com/yutou/biliapi/net/WebSocketClientManager.java b/src/main/java/com/yutou/biliapi/net/WebSocketServer.java similarity index 91% rename from src/main/java/com/yutou/biliapi/net/WebSocketClientManager.java rename to src/main/java/com/yutou/biliapi/net/WebSocketServer.java index 11d3f82..9fbadc7 100644 --- a/src/main/java/com/yutou/biliapi/net/WebSocketClientManager.java +++ b/src/main/java/com/yutou/biliapi/net/WebSocketServer.java @@ -17,13 +17,16 @@ import com.yutou.biliapi.bean.websocket.live.WSData; import com.yutou.biliapi.databases.BiliBiliLoginDatabase; import com.yutou.biliapi.databases.BiliLiveDatabase; import com.yutou.biliapi.utils.BytesUtils; +import com.yutou.bilibili.services.LiveDatabasesService; import com.yutou.common.okhttp.HttpBody; import com.yutou.common.okhttp.HttpCallback; import com.yutou.common.utils.ConfigTools; import com.yutou.common.utils.Log; +import jakarta.annotation.Resource; import okhttp3.Headers; import org.java_websocket.client.WebSocketClient; import org.java_websocket.handshake.ServerHandshake; +import org.springframework.stereotype.Service; import retrofit2.Response; import java.io.ByteArrayOutputStream; @@ -38,24 +41,20 @@ import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; -public class WebSocketClientManager { +@Service +public class WebSocketServer { ThreadPoolExecutor executor; - private static WebSocketClientManager instance; + private static WebSocketServer instance; Map roomMap; private final List userStopList = new ArrayList<>();//手动停止列表 + @Resource + LiveDatabasesService liveDatabasesService; - private WebSocketClientManager() { + private WebSocketServer() { roomMap = new HashMap<>(); executor = new ThreadPoolExecutor(2, 4, Long.MAX_VALUE, TimeUnit.SECONDS, new ArrayBlockingQueue(100)); } - public static WebSocketClientManager getInstance() { - if (instance == null) { - instance = new WebSocketClientManager(); - } - return instance; - } - public boolean checkRoom(LiveRoomConfig roomConfig) { return roomMap.containsKey(roomConfig); } @@ -98,13 +97,13 @@ public class WebSocketClientManager { } } - private static class DanmuTask implements Runnable { + private class DanmuTask implements Runnable { LiveRoomConfig roomConfig; WebSocketClientTh client; public DanmuTask(LiveRoomConfig config) { this.roomConfig = config; - WebSocketClientManager.getInstance().roomMap.put(roomConfig, this); + roomMap.put(roomConfig, this); } @Override @@ -117,7 +116,7 @@ public class WebSocketClientManager { roomConfig.setRoomInfo(execute.body() != null ? execute.body().getData() : null); } } catch (IOException e) { - WebSocketClientManager.getInstance().roomMap.remove(roomConfig); + roomMap.remove(roomConfig); throw new RuntimeException(e); } api.getLiveRoomDanmuInfo(String.valueOf(roomConfig.getRoomId())).enqueue(new HttpCallback() { @@ -131,7 +130,7 @@ public class WebSocketClientManager { roomConfig.setLiveInfo(response); client = new WebSocketClientTh(new URI(url), roomConfig); } catch (URISyntaxException e) { - WebSocketClientManager.getInstance().roomMap.remove(roomConfig); + roomMap.remove(roomConfig); throw new RuntimeException(e); } } @@ -139,7 +138,7 @@ public class WebSocketClientManager { @Override public void onFailure(Throwable throwable) { - WebSocketClientManager.getInstance().roomMap.remove(roomConfig); + roomMap.remove(roomConfig); Log.e(throwable); } }); @@ -150,7 +149,7 @@ public class WebSocketClientManager { } } - private static class WebSocketClientTh extends WebSocketClient { + private class WebSocketClientTh extends WebSocketClient { private final LiveRoomConfig roomConfig; private final HeartbeatTask heartbeatTask; BiliLiveDatabase liveDatabase; @@ -159,7 +158,7 @@ public class WebSocketClientManager { super(serverUri); Log.i("WebSocketClientTh.WebSocketClientTh : " + serverUri); this.roomConfig = roomId; - liveDatabase = new BiliLiveDatabase(roomConfig); + liveDatabase = liveDatabasesService.getLiveDatabase(roomConfig.getRoomId()); Brotli4jLoader.ensureAvailability(); heartbeatTask = new HeartbeatTask(); addHeader("User-Agent", ConfigTools.getUserAgent()); @@ -190,8 +189,7 @@ public class WebSocketClientManager { @Override public void onClose(int i, String s, boolean b) { Log.e("WebSocketClientTh.onClose", "i = " + i + ", s = " + s + ", b = " + b, roomConfig.getRoomId(), heartbeatTask.socket.isOpen()); - WebSocketClientManager.getInstance().roomMap.remove(roomConfig); - liveDatabase.close(); + roomMap.remove(roomConfig); heartbeatTask.cancel(); } @@ -199,8 +197,7 @@ public class WebSocketClientManager { public void onError(Exception e) { Log.i("WebSocketClientTh.onError", roomConfig.getRoomId()); Log.e(e); - WebSocketClientManager.getInstance().roomMap.remove(roomConfig); - liveDatabase.close(); + roomMap.remove(roomConfig); heartbeatTask.cancel(); } diff --git a/src/main/java/com/yutou/bilibili/services/LiveDanmuService.java b/src/main/java/com/yutou/bilibili/services/LiveDanmuService.java index 9f3342b..1082ac0 100644 --- a/src/main/java/com/yutou/bilibili/services/LiveDanmuService.java +++ b/src/main/java/com/yutou/bilibili/services/LiveDanmuService.java @@ -10,11 +10,12 @@ import com.yutou.biliapi.bean.live.database.LiveVideoDatabaseBean; import com.yutou.biliapi.bean.websocket.live.WSData; import com.yutou.biliapi.databases.BiliLiveConfigDatabase; import com.yutou.biliapi.databases.BiliLiveDatabase; -import com.yutou.biliapi.net.WebSocketClientManager; +import com.yutou.biliapi.net.WebSocketServer; import com.yutou.bilibili.Tools.AssTools; import com.yutou.bilibili.Tools.Tools; import com.yutou.bilibili.datas.web.LiveVideoDanmu; import com.yutou.common.utils.Log; +import jakarta.annotation.Resource; import org.jetbrains.annotations.NotNull; import org.springframework.stereotype.Service; @@ -23,31 +24,35 @@ import java.util.List; @Service public class LiveDanmuService { + @Resource + LiveDatabasesService liveDatabasesService; + @Resource + WebSocketServer webSocketServer; public void start(String roomId, boolean isUser) { - WebSocketClientManager.getInstance().addRoom(LiveRoomConfig.buildConfig(roomId), isUser); + webSocketServer.addRoom(LiveRoomConfig.buildConfig(roomId), isUser); } public void start(LiveConfigDatabaseBean roomId, boolean isUser) { - WebSocketClientManager.getInstance().addRoom(LiveRoomConfig.buildConfig(roomId.getRoomId()), isUser); + webSocketServer.addRoom(LiveRoomConfig.buildConfig(roomId.getRoomId()), isUser); } public boolean check(String roomId) { LiveRoomConfig roomConfig = new LiveRoomConfig(); roomConfig.setRoomId(roomId); - return WebSocketClientManager.getInstance().checkRoom(roomConfig); + return webSocketServer.checkRoom(roomConfig); } public void stop(String roomId, boolean isUser) { - WebSocketClientManager.getInstance().stopRoom(roomId, isUser); + webSocketServer.stopRoom(roomId, isUser); } public JSONArray getLiveRoomList() { - return WebSocketClientManager.getInstance().getLiveRoomList(); + return webSocketServer.getLiveRoomList(); } public void clearUserList() { - WebSocketClientManager.getInstance().clearUserStopList(); + webSocketServer.clearUserStopList(); } public List getDanmuFileList(String roomId) { @@ -88,7 +93,7 @@ public class LiveDanmuService { public LiveVideoDanmu getDanmu(String roomId, String videoId, int page) { LiveVideoDanmu danmus = new LiveVideoDanmu(); - BiliLiveDatabase liveDatabase = new BiliLiveDatabase(LiveRoomConfig.buildConfig(roomId)); + BiliLiveDatabase liveDatabase = liveDatabasesService.getLiveDatabase(roomId); try { LiveVideoDatabaseBean videoBean = liveDatabase.getVideo(videoId); if (videoBean == null) { @@ -121,8 +126,6 @@ public class LiveDanmuService { } catch (Exception e) { Log.e(e); - } finally { - liveDatabase.close(); } return danmus; diff --git a/src/main/java/com/yutou/bilibili/services/LiveDatabasesService.java b/src/main/java/com/yutou/bilibili/services/LiveDatabasesService.java new file mode 100644 index 0000000..e698215 --- /dev/null +++ b/src/main/java/com/yutou/bilibili/services/LiveDatabasesService.java @@ -0,0 +1,21 @@ +package com.yutou.bilibili.services; + +import com.yutou.biliapi.bean.live.LiveRoomConfig; +import com.yutou.biliapi.databases.BiliLiveDatabase; +import org.springframework.stereotype.Service; + +import java.util.HashMap; +import java.util.Map; + +@Service +public class LiveDatabasesService { + private final Map liveDatabases=new HashMap<>(); + public BiliLiveDatabase getLiveDatabase(String roomId) { + if(liveDatabases.containsKey(roomId)) { + return liveDatabases.get(roomId); + } + BiliLiveDatabase liveDatabase = new BiliLiveDatabase(LiveRoomConfig.buildConfig(roomId)); + liveDatabases.put(roomId, liveDatabase); + return liveDatabase; + } +} diff --git a/src/main/java/com/yutou/bilibili/services/LiveService.java b/src/main/java/com/yutou/bilibili/services/LiveService.java index 5370007..f8a1cd4 100644 --- a/src/main/java/com/yutou/bilibili/services/LiveService.java +++ b/src/main/java/com/yutou/bilibili/services/LiveService.java @@ -33,8 +33,11 @@ public class LiveService { LiveVideoDownloadService videoDownloadService; @Resource LiveDanmuService danmuService; + @Resource + LiveDatabasesService databasesService; LiveApi api; + public LiveService() { liveConfigDatabase = new BiliLiveConfigDatabase(); api = BiliLiveNetApiManager.getInstance().getApi(null); @@ -108,7 +111,7 @@ public class LiveService { } public JSONObject getGiftInfo(String roomId, String videoId) { - BiliLiveDatabase database = new BiliLiveDatabase(LiveRoomConfig.buildConfig(roomId)); + BiliLiveDatabase database = databasesService.getLiveDatabase(roomId); LiveVideoDatabaseBean videoBean = database.getVideo(videoId); if (videoBean == null) { return null; @@ -124,4 +127,6 @@ public class LiveService { List data = service.getLiveList(1, 16); System.out.println(data.size()); } + + } diff --git a/src/main/java/com/yutou/bilibili/services/LiveVideoDownloadService.java b/src/main/java/com/yutou/bilibili/services/LiveVideoDownloadService.java index fd2e995..ab7c1fd 100644 --- a/src/main/java/com/yutou/bilibili/services/LiveVideoDownloadService.java +++ b/src/main/java/com/yutou/bilibili/services/LiveVideoDownloadService.java @@ -18,7 +18,7 @@ import com.yutou.biliapi.enums.LiveVideoCodec; import com.yutou.biliapi.enums.LiveVideoDefinition; import com.yutou.biliapi.enums.LiveVideoFormat; import com.yutou.biliapi.net.BiliLiveNetApiManager; -import com.yutou.biliapi.net.WebSocketClientManager; +import com.yutou.biliapi.net.WebSocketServer; import com.yutou.bilibili.Tools.DateFormatUtils; import com.yutou.bilibili.Tools.FileServerUtils; import com.yutou.bilibili.datas.VideoFilePath; @@ -29,6 +29,7 @@ import com.yutou.common.record.AbsVideoRecord; import com.yutou.common.utils.ConfigTools; import com.yutou.common.utils.FFmpegUtils; import com.yutou.common.utils.Log; +import jakarta.annotation.Resource; import okhttp3.Headers; import org.apache.commons.io.FileUtils; import org.springframework.stereotype.Service; @@ -48,6 +49,10 @@ public class LiveVideoDownloadService { private final ThreadPoolExecutor executor; private final List userStopList = new ArrayList<>();//手动停止列表 private final AbsVideoRecord videoRecord; + @Resource + LiveDatabasesService liveDatabasesService; + @Resource + WebSocketServer webSocketServer; public LiveVideoDownloadService() { Log.i("初始化下载服务"); @@ -176,7 +181,7 @@ public class LiveVideoDownloadService { config.setLogin(StringUtils.hasText(bean.getRecordUid())); config.setRoomInfo(roomInfo); config.setRootPath(bean.getRecordPath()); - database = new BiliLiveDatabase(config); + database = liveDatabasesService.getLiveDatabase(bean.getRoomId()); saveLiveInfo(roomInfo); api.getLiveRoomPlayInfo( bean.getRoomId(), @@ -355,14 +360,14 @@ public class LiveVideoDownloadService { //录制弹幕 private void recordDanmu() { - if (bean.isSyncDanmuForLive() && !WebSocketClientManager.getInstance().checkRoom(LiveRoomConfig.buildConfig(bean.getRoomId()))) { - WebSocketClientManager.getInstance().addRoom(LiveRoomConfig.buildConfig(bean.getRoomId()), true); + if (bean.isSyncDanmuForLive() && !webSocketServer.checkRoom(LiveRoomConfig.buildConfig(bean.getRoomId()))) { + webSocketServer.addRoom(LiveRoomConfig.buildConfig(bean.getRoomId()), true); } } private void stopRecordDanmu() { - if (bean.isSyncDanmuForLive() && WebSocketClientManager.getInstance().checkRoom(LiveRoomConfig.buildConfig(bean.getRoomId()))) { - WebSocketClientManager.getInstance().stopRoom(bean.getRoomId(), false); + if (bean.isSyncDanmuForLive() && webSocketServer.checkRoom(LiveRoomConfig.buildConfig(bean.getRoomId()))) { + webSocketServer.stopRoom(bean.getRoomId(), false); } } } @@ -381,13 +386,11 @@ public class LiveVideoDownloadService { private VideoFilePath getVideoFilePath(LiveConfigDatabaseBean configBean) { String recordPath = configBean.getRecordPath() + File.separator + configBean.getAnchorName(); File recordDir = new File(recordPath); - BiliLiveDatabase database = new BiliLiveDatabase(LiveRoomConfig.buildConfig(configBean.getRoomId()), recordDir.getAbsolutePath()); VideoFilePath path = createVideoRootFilePath(configBean, recordDir); if (recordDir.exists()) { - List infos = database.getLiveInfos(); + List infos = liveDatabasesService.getLiveDatabase(configBean.getRoomId()).getLiveInfos(); path.setChildren(getVideoInfo(infos)); } - database.close(); return path; } @@ -430,14 +433,12 @@ public class LiveVideoDownloadService { String recordPath = config.getRecordPath() + File.separator + config.getAnchorName(); configDatabase.close(); LiveVideoDatabaseBean videoInfo = null; - BiliLiveDatabase liveDatabase = new BiliLiveDatabase(LiveRoomConfig.buildConfig(roomId), new File(recordPath).getAbsolutePath()); - for (LiveVideoDatabaseBean info : liveDatabase.getLiveInfos()) { + for (LiveVideoDatabaseBean info : liveDatabasesService.getLiveDatabase(roomId).getLiveInfos()) { if (videoId.trim().equals(String.valueOf(info.getSql_time().getTime()))) { videoInfo = info; break; } } - liveDatabase.close(); if (videoInfo != null) { File videoFile = new File(videoInfo.getPath().replace("-%04d.ts", ".m3u8")); if (!videoFile.exists()) { diff --git a/src/main/java/com/yutou/bilibili/services/SystemService.java b/src/main/java/com/yutou/bilibili/services/SystemService.java index 5bd82fe..e935b6c 100644 --- a/src/main/java/com/yutou/bilibili/services/SystemService.java +++ b/src/main/java/com/yutou/bilibili/services/SystemService.java @@ -22,6 +22,7 @@ public class SystemService { LiveVideoDownloadService videoService; @Resource LiveDanmuService danmuService; + SystemConfigDatabases databases = new SystemConfigDatabases(); private ScheduledExecutorService timer; private ScheduledFuture scheduled; diff --git a/src/main/java/com/yutou/common/utils/RedisTools.java b/src/main/java/com/yutou/common/utils/RedisTools.java index bc84331..cdcfd3f 100644 --- a/src/main/java/com/yutou/common/utils/RedisTools.java +++ b/src/main/java/com/yutou/common/utils/RedisTools.java @@ -27,7 +27,7 @@ public class RedisTools { //Properties properties = PropertyUtil.loadProperties("jedis.properties"); //host = properties.getProperty("redis.host"); //port = Integer.valueOf(properties.getProperty("redis.port")); - host = "172.21.35.118"; + host = "192.168.31.148"; port = 6379; } @@ -41,7 +41,6 @@ public class RedisTools { String ret = jedis.set(key, value); jedis.close(); } catch (Exception e) { - // TODO: handle exception Log.e(e); return false; } @@ -71,7 +70,6 @@ public class RedisTools { } jedis.close(); } catch (Exception e) { - // TODO: handle exception Log.e(e); return false; } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 1274bf4..dc92337 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,3 +1,8 @@ -server.port=8080 +server.port=8880 logging.file.path=./logs logging.level.com.log.controller = trace +# 启用Hibernate SQL日志 +spring.jpa.show-sql=true +spring.jpa.properties.hibernate.format_sql=true +logging.level.org.hibernate.SQL=DEBUG +logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE \ No newline at end of file