diff --git a/src/main/java/com/yutou/bilibili/services/LiveDanmuService.java b/src/main/java/com/yutou/bilibili/services/LiveDanmuService.java index ed7037d..9f3342b 100644 --- a/src/main/java/com/yutou/bilibili/services/LiveDanmuService.java +++ b/src/main/java/com/yutou/bilibili/services/LiveDanmuService.java @@ -89,36 +89,42 @@ public class LiveDanmuService { public LiveVideoDanmu getDanmu(String roomId, String videoId, int page) { LiveVideoDanmu danmus = new LiveVideoDanmu(); BiliLiveDatabase liveDatabase = new BiliLiveDatabase(LiveRoomConfig.buildConfig(roomId)); - LiveVideoDatabaseBean videoBean = liveDatabase.getVideo(videoId); - if (videoBean == null) { - return new LiveVideoDanmu(); - } - long startTime = videoBean.getStartTime().getTime(); - long endTime = videoBean.getStopTime() == null ? System.currentTimeMillis() : videoBean.getStopTime().getTime(); - List superChatList = liveDatabase.getOfTime(startTime, endTime, LiveSuperChatDatabaseBean.class); + try { + LiveVideoDatabaseBean videoBean = liveDatabase.getVideo(videoId); + if (videoBean == null) { + return new LiveVideoDanmu(); + } + long startTime = videoBean.getStartTime().getTime(); + long endTime = videoBean.getStopTime() == null ? System.currentTimeMillis() : videoBean.getStopTime().getTime(); + List superChatList = liveDatabase.getOfTime(startTime, endTime, LiveSuperChatDatabaseBean.class); - long count = liveDatabase.getCount(new LiveDanmuDatabaseBean().getTableName()); - int pageSize = 10000; - int pageCount = (int) Math.ceil((double) count / pageSize); + long count = liveDatabase.getCount(new LiveDanmuDatabaseBean().getTableName()); + int pageSize = 10000; + int pageCount = (int) Math.ceil((double) count / pageSize); - List danmuList = liveDatabase.getOfTime(startTime, endTime, page, pageSize, LiveDanmuDatabaseBean.class) - .stream() - .map(item -> createDanmu(item, startTime)) - .filter(item-> item.getTime()>=0) - .toList(); - danmus.getDanmu().addAll(danmuList); - danmus.setDanmuCount(count); - if (page < pageCount) { - danmus.setNextDanmu(true); + List danmuList = liveDatabase.getOfTime(startTime, endTime, page, pageSize, LiveDanmuDatabaseBean.class) + .stream() + .map(item -> createDanmu(item, startTime)) + .filter(item -> item.getTime() >= 0) + .toList(); + danmus.getDanmu().addAll(danmuList); + danmus.setDanmuCount(count); + if (page < pageCount) { + danmus.setNextDanmu(true); + } + + + for (LiveSuperChatDatabaseBean bean : superChatList) { + LiveVideoDanmu.SuperChat superChat = new LiveVideoDanmu.SuperChat(startTime, bean); + danmus.getSuperChat().add(superChat); + } + + } catch (Exception e) { + Log.e(e); + } finally { + liveDatabase.close(); } - - for (LiveSuperChatDatabaseBean bean : superChatList) { - LiveVideoDanmu.SuperChat superChat = new LiveVideoDanmu.SuperChat(startTime, bean); - danmus.getSuperChat().add(superChat); - } - - liveDatabase.close(); return danmus; }