From 017a01a0b8a371ad2fe3c4326668354574c69b71 Mon Sep 17 00:00:00 2001 From: yutou Date: Thu, 13 May 2021 11:31:38 +0800 Subject: [PATCH] =?UTF-8?q?=E7=9B=B4=E6=92=ADLive.list=E6=94=B9=E6=88=90Ha?= =?UTF-8?q?shMap?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/UpInfoController.java | 2 +- .../com/yutou/bilibili/BiliBili/Live.java | 36 +++++++++---------- .../yutou/bilibili/BiliBili/LiveUtils.java | 8 +++-- .../yutou/bilibili/BilibiliApplication.java | 2 +- .../yutou/bilibili/QQBot/QQBotManager.java | 8 +++-- .../yutou/bilibili/Test/TestController.java | 2 +- .../yutou/bilibili/Tools/ApplicationInit.java | 2 +- 7 files changed, 30 insertions(+), 30 deletions(-) diff --git a/src/main/java/com/yutou/bilibili/BiliBili/Controllers/UpInfoController.java b/src/main/java/com/yutou/bilibili/BiliBili/Controllers/UpInfoController.java index 2cb2bf4..f1d9f53 100644 --- a/src/main/java/com/yutou/bilibili/BiliBili/Controllers/UpInfoController.java +++ b/src/main/java/com/yutou/bilibili/BiliBili/Controllers/UpInfoController.java @@ -139,7 +139,7 @@ public class UpInfoController { info.add(JSON.toJSON(up)); } } - for (Live live : Live.lives) { + for (Live live : Live.lives.values()) { if(live.geData().getLive()==1){ liveArray.add(JSON.toJSON(live.geData())); } diff --git a/src/main/java/com/yutou/bilibili/BiliBili/Live.java b/src/main/java/com/yutou/bilibili/BiliBili/Live.java index f286260..dbeaae2 100644 --- a/src/main/java/com/yutou/bilibili/BiliBili/Live.java +++ b/src/main/java/com/yutou/bilibili/BiliBili/Live.java @@ -1,10 +1,10 @@ package com.yutou.bilibili.BiliBili; import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; import com.yutou.bilibili.BiliBili.Datas.DanmuData; import com.yutou.bilibili.BiliBili.Datas.GiftData; import com.yutou.bilibili.BiliBili.Datas.LiveData; -import com.alibaba.fastjson.JSONObject; import com.yutou.bilibili.BiliBili.Services.IBiliBiliLiveService; import com.yutou.bilibili.BiliBili.Tools.SaveLive; import com.yutou.bilibili.QQBot.QQBotManager; @@ -18,13 +18,12 @@ import com.yutou.bilibili.mybatis.Bili.mybatis.model.BilibiliUpInfo; import com.yutou.bilibili.sqlite.BiliBiliLiveDatabasesManager; import org.java_websocket.client.WebSocketClient; import org.java_websocket.handshake.ServerHandshake; +import org.jetbrains.annotations.NotNull; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; -import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; -import javax.annotation.Resource; import java.io.ByteArrayOutputStream; import java.io.File; import java.net.URI; @@ -37,8 +36,7 @@ public class Live implements ApplicationContextAware { IBiliBiliLiveService service; - public static List lives = new ArrayList<>(); - public static List liveOfRoomId = new ArrayList<>(); + public static Map lives = new HashMap<>(); private int roomId; @@ -75,7 +73,7 @@ public class Live implements ApplicationContextAware { * @param isLogin 是否登录用户 */ public void add(int roomId, boolean isLogin) { - if(Live.lives.contains(this)){ + if(Live.lives.containsKey(roomId)){ Log.i("已经在统计:"+roomId); return; } @@ -87,7 +85,7 @@ public class Live implements ApplicationContextAware { info.setGiftuser(0); info.setVipuserindex(0); info.setUserindex(0); - Live.lives.add(this); + Live.lives.put(roomId,this); updateUpInfo(); com.yutou.bilibili.Tools.Log.i("roomId = " + roomId + ", isLogin = " + isLogin); checkLiveTimer = new Timer(); @@ -125,10 +123,6 @@ public class Live implements ApplicationContextAware { * @throws Exception 发生异常 */ public void start() throws Exception { - if(liveOfRoomId.contains(roomId+"")){ - return; - } - liveOfRoomId.add(roomId+""); run = true; if (LiveUtils.isLivePlayer(roomId)) { upData.setLive(1); @@ -139,7 +133,7 @@ public class Live implements ApplicationContextAware { return; } startTime=new Date(); - HashMap header = new HashMap(); + HashMap header = new HashMap<>(); header.put("Sec-WebSocket-Extensions", "permessage-deflate; client_max_window_bits"); header.put("Sec-WebSocket-Key", "tORCZd8AI6xIyvqvgvI1Vw=="); header.put("Sec-WebSocket-Version", "13"); @@ -191,7 +185,6 @@ public class Live implements ApplicationContextAware { init = true; heartBeattimer.cancel(); client.close(); - liveOfRoomId.remove(roomId + ""); start(); } catch (Exception e) { Log.e(e); @@ -230,14 +223,13 @@ public class Live implements ApplicationContextAware { if (danmuManager != null) { danmuManager.close(); } - liveOfRoomId.remove(roomId+""); if (SaveLive.getInstance().checkLive(roomId)) { SaveLive.getInstance().stop(roomId); } if (checkLiveTimer != null) { checkLiveTimer.cancel(); } - Live.lives.remove(this); + Live.lives.remove(roomId); com.yutou.bilibili.Tools.Log.i("退出" + roomId + "直播间"); } @@ -248,7 +240,7 @@ public class Live implements ApplicationContextAware { */ private void likeLive() throws Exception { JSONObject tmp = LiveUtils.http_get("https://api.bilibili.com/x/web-interface/nav"); - if (tmp.getInteger("code") == -101) { + if (tmp!=null&&tmp.getInteger("code") == -101) { if (isLogin) { new File("cookies.json").deleteOnExit(); upData.setLive(0); @@ -258,10 +250,14 @@ public class Live implements ApplicationContextAware { } else { userId = 0; } - } else if (tmp.getInteger("code") == 0) { + } else if (tmp!=null&&tmp.getInteger("code") == 0) { userId = tmp.getJSONObject("data").getInteger("mid"); } JSONObject http = LiveUtils.http_get("https://api.live.bilibili.com/xlive/web-room/v1/index/getDanmuInfo?id=" + roomId + "&type=0"); + if(http==null){ + stop(); + return; + } JSONObject json = new JSONObject(); json.put("uid", userId); json.put("roomid", roomId); @@ -339,7 +335,7 @@ public class Live implements ApplicationContextAware { JSONObject json = JSONObject.parseObject(msg); JSONObject data; BilibiliLiveData liveData = new BilibiliLiveData(); - String danmu = null; + String danmu; GiftData giftData; switch (json.getString("cmd")) { @@ -403,7 +399,7 @@ public class Live implements ApplicationContextAware { case "COMBO_SEND"://礼物连击 data = json.getJSONObject("data"); String gift = data.getString("giftName"); - if (gift == null || gift.equals("null")) { + if (gift == null || "null".equals(gift)) { gift = data.getString("gift_name"); } danmu = data.getString("uname") + " " + data.getString("action") + " " + gift + "x" + data.getInteger("batch_combo_num"); @@ -565,7 +561,7 @@ public class Live implements ApplicationContextAware { private static ApplicationContext applicationContext = null; @Override - public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + public void setApplicationContext(@NotNull ApplicationContext applicationContext) throws BeansException { if (Live.applicationContext == null) { Live.applicationContext = applicationContext; } diff --git a/src/main/java/com/yutou/bilibili/BiliBili/LiveUtils.java b/src/main/java/com/yutou/bilibili/BiliBili/LiveUtils.java index c613406..ad9ce2d 100644 --- a/src/main/java/com/yutou/bilibili/BiliBili/LiveUtils.java +++ b/src/main/java/com/yutou/bilibili/BiliBili/LiveUtils.java @@ -32,8 +32,9 @@ public class LiveUtils { public static String getLiveUrl(int roomId) { JSONObject json = http_get("https://api.live.bilibili.com/xlive/web-room/v1/index/getDanmuInfo?id=" + roomId + "&type=0"); - if (json != null) + if (json != null) { return "wss://" + json.getJSONObject("data").getJSONArray("host_list").getJSONObject(0).getString("host") + "/sub"; + } return null; } @@ -251,8 +252,9 @@ public class LiveUtils { return json; } finally { stream.close(); - if (connectionOutputStream != null) + if (connectionOutputStream != null) { connectionOutputStream.close(); + } connection.disconnect(); } @@ -332,7 +334,7 @@ public class LiveUtils { } public static Live liveContains(BilibiliUpInfo data) { - for (Live live : Live.lives) { + for (Live live : Live.lives.values()) { if (live.getInfo().getRoomid().equals(data.getRoomid())) { return live; } diff --git a/src/main/java/com/yutou/bilibili/BilibiliApplication.java b/src/main/java/com/yutou/bilibili/BilibiliApplication.java index 1da145c..7702d51 100644 --- a/src/main/java/com/yutou/bilibili/BilibiliApplication.java +++ b/src/main/java/com/yutou/bilibili/BilibiliApplication.java @@ -13,7 +13,7 @@ import org.springframework.context.annotation.Import; @SpringBootApplication public class BilibiliApplication { - public static String version="0.6"; + public static String version="0.7"; public static void main(String[] args) { QQBotManager.getInstance().init(); diff --git a/src/main/java/com/yutou/bilibili/QQBot/QQBotManager.java b/src/main/java/com/yutou/bilibili/QQBot/QQBotManager.java index a481e9a..bfba0ef 100644 --- a/src/main/java/com/yutou/bilibili/QQBot/QQBotManager.java +++ b/src/main/java/com/yutou/bilibili/QQBot/QQBotManager.java @@ -38,8 +38,9 @@ public class QQBotManager implements ApplicationContextAware { @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { - if (QQBotManager.applicationContext == null) + if (QQBotManager.applicationContext == null) { QQBotManager.applicationContext = applicationContext; + } } private T getBean(Class tClass) { @@ -262,7 +263,7 @@ public class QQBotManager implements ApplicationContextAware { case QQCommands.QQ_LIVE_LIST: builder.append("当前正在记录数据的直播间:"); builder.append("\n"); - for (Live live : Live.lives) { + for (Live live : Live.lives.values()) { JSONObject liveJson = LiveUtils.LiveInfoManager.getInstance().getInfo(live.getInfo().getRoomid()); if (LiveUtils.isLivePlayer(live.getInfo().getRoomid())) { builder.append("【直播中】"); @@ -375,8 +376,9 @@ public class QQBotManager implements ApplicationContextAware { if (endTime == null) { endTime = AppTools.getToDayNowTime(); } - if (getInstance().realTimeDataController == null) + if (getInstance().realTimeDataController == null) { getInstance().realTimeDataController = getInstance().getBean(RealTimeDataController.class); + } JSONObject json = getInstance().realTimeDataController.queryToDayLiveData(roomId, startTime, endTime); System.out.println(json); builder.append("当前人气:").append(json.getJSONObject("data").getInteger("popular")).append("\n"); diff --git a/src/main/java/com/yutou/bilibili/Test/TestController.java b/src/main/java/com/yutou/bilibili/Test/TestController.java index c709c31..9428f0c 100644 --- a/src/main/java/com/yutou/bilibili/Test/TestController.java +++ b/src/main/java/com/yutou/bilibili/Test/TestController.java @@ -25,7 +25,7 @@ public class TestController { JSONObject json=new JSONObject(); JSONArray array=new JSONArray(); json.put("size", Live.lives.size()); - for (Live live :Live.lives) { + for (Live live :Live.lives.values()) { JSONObject item=new JSONObject(); item.put("info", JSON.toJSON(live.getInfo())); item.put("data",JSON.toJSON(live.geData())); diff --git a/src/main/java/com/yutou/bilibili/Tools/ApplicationInit.java b/src/main/java/com/yutou/bilibili/Tools/ApplicationInit.java index 98596ad..76c4421 100644 --- a/src/main/java/com/yutou/bilibili/Tools/ApplicationInit.java +++ b/src/main/java/com/yutou/bilibili/Tools/ApplicationInit.java @@ -59,7 +59,7 @@ public class ApplicationInit implements ApplicationRunner { oldTime = time; switch (time) { case "00:00": - for (Live live : Live.lives) { + for (Live live : Live.lives.values()) { live.clearInfo(); } break;