直播Live.list改成HashMap

This commit is contained in:
yutou 2021-05-13 11:31:38 +08:00
parent dd1103e870
commit 017a01a0b8
7 changed files with 30 additions and 30 deletions

View File

@ -139,7 +139,7 @@ public class UpInfoController {
info.add(JSON.toJSON(up)); info.add(JSON.toJSON(up));
} }
} }
for (Live live : Live.lives) { for (Live live : Live.lives.values()) {
if(live.geData().getLive()==1){ if(live.geData().getLive()==1){
liveArray.add(JSON.toJSON(live.geData())); liveArray.add(JSON.toJSON(live.geData()));
} }

View File

@ -1,10 +1,10 @@
package com.yutou.bilibili.BiliBili; package com.yutou.bilibili.BiliBili;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yutou.bilibili.BiliBili.Datas.DanmuData; import com.yutou.bilibili.BiliBili.Datas.DanmuData;
import com.yutou.bilibili.BiliBili.Datas.GiftData; import com.yutou.bilibili.BiliBili.Datas.GiftData;
import com.yutou.bilibili.BiliBili.Datas.LiveData; import com.yutou.bilibili.BiliBili.Datas.LiveData;
import com.alibaba.fastjson.JSONObject;
import com.yutou.bilibili.BiliBili.Services.IBiliBiliLiveService; import com.yutou.bilibili.BiliBili.Services.IBiliBiliLiveService;
import com.yutou.bilibili.BiliBili.Tools.SaveLive; import com.yutou.bilibili.BiliBili.Tools.SaveLive;
import com.yutou.bilibili.QQBot.QQBotManager; 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 com.yutou.bilibili.sqlite.BiliBiliLiveDatabasesManager;
import org.java_websocket.client.WebSocketClient; import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake; import org.java_websocket.handshake.ServerHandshake;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.net.URI; import java.net.URI;
@ -37,8 +36,7 @@ public class Live implements ApplicationContextAware {
IBiliBiliLiveService service; IBiliBiliLiveService service;
public static List<Live> lives = new ArrayList<>(); public static Map<Integer,Live> lives = new HashMap<>();
public static List<String> liveOfRoomId = new ArrayList<>();
private int roomId; private int roomId;
@ -75,7 +73,7 @@ public class Live implements ApplicationContextAware {
* @param isLogin 是否登录用户 * @param isLogin 是否登录用户
*/ */
public void add(int roomId, boolean isLogin) { public void add(int roomId, boolean isLogin) {
if(Live.lives.contains(this)){ if(Live.lives.containsKey(roomId)){
Log.i("已经在统计:"+roomId); Log.i("已经在统计:"+roomId);
return; return;
} }
@ -87,7 +85,7 @@ public class Live implements ApplicationContextAware {
info.setGiftuser(0); info.setGiftuser(0);
info.setVipuserindex(0); info.setVipuserindex(0);
info.setUserindex(0); info.setUserindex(0);
Live.lives.add(this); Live.lives.put(roomId,this);
updateUpInfo(); updateUpInfo();
com.yutou.bilibili.Tools.Log.i("roomId = " + roomId + ", isLogin = " + isLogin); com.yutou.bilibili.Tools.Log.i("roomId = " + roomId + ", isLogin = " + isLogin);
checkLiveTimer = new Timer(); checkLiveTimer = new Timer();
@ -125,10 +123,6 @@ public class Live implements ApplicationContextAware {
* @throws Exception 发生异常 * @throws Exception 发生异常
*/ */
public void start() throws Exception { public void start() throws Exception {
if(liveOfRoomId.contains(roomId+"")){
return;
}
liveOfRoomId.add(roomId+"");
run = true; run = true;
if (LiveUtils.isLivePlayer(roomId)) { if (LiveUtils.isLivePlayer(roomId)) {
upData.setLive(1); upData.setLive(1);
@ -139,7 +133,7 @@ public class Live implements ApplicationContextAware {
return; return;
} }
startTime=new Date(); startTime=new Date();
HashMap<String, String> header = new HashMap<String, String>(); HashMap<String, String> header = new HashMap<>();
header.put("Sec-WebSocket-Extensions", "permessage-deflate; client_max_window_bits"); header.put("Sec-WebSocket-Extensions", "permessage-deflate; client_max_window_bits");
header.put("Sec-WebSocket-Key", "tORCZd8AI6xIyvqvgvI1Vw=="); header.put("Sec-WebSocket-Key", "tORCZd8AI6xIyvqvgvI1Vw==");
header.put("Sec-WebSocket-Version", "13"); header.put("Sec-WebSocket-Version", "13");
@ -191,7 +185,6 @@ public class Live implements ApplicationContextAware {
init = true; init = true;
heartBeattimer.cancel(); heartBeattimer.cancel();
client.close(); client.close();
liveOfRoomId.remove(roomId + "");
start(); start();
} catch (Exception e) { } catch (Exception e) {
Log.e(e); Log.e(e);
@ -230,14 +223,13 @@ public class Live implements ApplicationContextAware {
if (danmuManager != null) { if (danmuManager != null) {
danmuManager.close(); danmuManager.close();
} }
liveOfRoomId.remove(roomId+"");
if (SaveLive.getInstance().checkLive(roomId)) { if (SaveLive.getInstance().checkLive(roomId)) {
SaveLive.getInstance().stop(roomId); SaveLive.getInstance().stop(roomId);
} }
if (checkLiveTimer != null) { if (checkLiveTimer != null) {
checkLiveTimer.cancel(); checkLiveTimer.cancel();
} }
Live.lives.remove(this); Live.lives.remove(roomId);
com.yutou.bilibili.Tools.Log.i("退出" + roomId + "直播间"); com.yutou.bilibili.Tools.Log.i("退出" + roomId + "直播间");
} }
@ -248,7 +240,7 @@ public class Live implements ApplicationContextAware {
*/ */
private void likeLive() throws Exception { private void likeLive() throws Exception {
JSONObject tmp = LiveUtils.http_get("https://api.bilibili.com/x/web-interface/nav"); 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) { if (isLogin) {
new File("cookies.json").deleteOnExit(); new File("cookies.json").deleteOnExit();
upData.setLive(0); upData.setLive(0);
@ -258,10 +250,14 @@ public class Live implements ApplicationContextAware {
} else { } else {
userId = 0; userId = 0;
} }
} else if (tmp.getInteger("code") == 0) { } else if (tmp!=null&&tmp.getInteger("code") == 0) {
userId = tmp.getJSONObject("data").getInteger("mid"); 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"); 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(); JSONObject json = new JSONObject();
json.put("uid", userId); json.put("uid", userId);
json.put("roomid", roomId); json.put("roomid", roomId);
@ -339,7 +335,7 @@ public class Live implements ApplicationContextAware {
JSONObject json = JSONObject.parseObject(msg); JSONObject json = JSONObject.parseObject(msg);
JSONObject data; JSONObject data;
BilibiliLiveData liveData = new BilibiliLiveData(); BilibiliLiveData liveData = new BilibiliLiveData();
String danmu = null; String danmu;
GiftData giftData; GiftData giftData;
switch (json.getString("cmd")) { switch (json.getString("cmd")) {
@ -403,7 +399,7 @@ public class Live implements ApplicationContextAware {
case "COMBO_SEND"://礼物连击 case "COMBO_SEND"://礼物连击
data = json.getJSONObject("data"); data = json.getJSONObject("data");
String gift = data.getString("giftName"); String gift = data.getString("giftName");
if (gift == null || gift.equals("null")) { if (gift == null || "null".equals(gift)) {
gift = data.getString("gift_name"); gift = data.getString("gift_name");
} }
danmu = data.getString("uname") + " " + data.getString("action") + " " + gift + "x" + data.getInteger("batch_combo_num"); 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; private static ApplicationContext applicationContext = null;
@Override @Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { public void setApplicationContext(@NotNull ApplicationContext applicationContext) throws BeansException {
if (Live.applicationContext == null) { if (Live.applicationContext == null) {
Live.applicationContext = applicationContext; Live.applicationContext = applicationContext;
} }

View File

@ -32,8 +32,9 @@ public class LiveUtils {
public static String getLiveUrl(int roomId) { 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"); 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 "wss://" + json.getJSONObject("data").getJSONArray("host_list").getJSONObject(0).getString("host") + "/sub";
}
return null; return null;
} }
@ -251,8 +252,9 @@ public class LiveUtils {
return json; return json;
} finally { } finally {
stream.close(); stream.close();
if (connectionOutputStream != null) if (connectionOutputStream != null) {
connectionOutputStream.close(); connectionOutputStream.close();
}
connection.disconnect(); connection.disconnect();
} }
@ -332,7 +334,7 @@ public class LiveUtils {
} }
public static Live liveContains(BilibiliUpInfo data) { public static Live liveContains(BilibiliUpInfo data) {
for (Live live : Live.lives) { for (Live live : Live.lives.values()) {
if (live.getInfo().getRoomid().equals(data.getRoomid())) { if (live.getInfo().getRoomid().equals(data.getRoomid())) {
return live; return live;
} }

View File

@ -13,7 +13,7 @@ import org.springframework.context.annotation.Import;
@SpringBootApplication @SpringBootApplication
public class BilibiliApplication { public class BilibiliApplication {
public static String version="0.6"; public static String version="0.7";
public static void main(String[] args) { public static void main(String[] args) {
QQBotManager.getInstance().init(); QQBotManager.getInstance().init();

View File

@ -38,9 +38,10 @@ public class QQBotManager implements ApplicationContextAware {
@Override @Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
if (QQBotManager.applicationContext == null) if (QQBotManager.applicationContext == null) {
QQBotManager.applicationContext = applicationContext; QQBotManager.applicationContext = applicationContext;
} }
}
private <T> T getBean(Class<T> tClass) { private <T> T getBean(Class<T> tClass) {
return applicationContext.getBean(tClass); return applicationContext.getBean(tClass);
@ -262,7 +263,7 @@ public class QQBotManager implements ApplicationContextAware {
case QQCommands.QQ_LIVE_LIST: case QQCommands.QQ_LIVE_LIST:
builder.append("当前正在记录数据的直播间:"); builder.append("当前正在记录数据的直播间:");
builder.append("\n"); builder.append("\n");
for (Live live : Live.lives) { for (Live live : Live.lives.values()) {
JSONObject liveJson = LiveUtils.LiveInfoManager.getInstance().getInfo(live.getInfo().getRoomid()); JSONObject liveJson = LiveUtils.LiveInfoManager.getInstance().getInfo(live.getInfo().getRoomid());
if (LiveUtils.isLivePlayer(live.getInfo().getRoomid())) { if (LiveUtils.isLivePlayer(live.getInfo().getRoomid())) {
builder.append("【直播中】"); builder.append("【直播中】");
@ -375,8 +376,9 @@ public class QQBotManager implements ApplicationContextAware {
if (endTime == null) { if (endTime == null) {
endTime = AppTools.getToDayNowTime(); endTime = AppTools.getToDayNowTime();
} }
if (getInstance().realTimeDataController == null) if (getInstance().realTimeDataController == null) {
getInstance().realTimeDataController = getInstance().getBean(RealTimeDataController.class); getInstance().realTimeDataController = getInstance().getBean(RealTimeDataController.class);
}
JSONObject json = getInstance().realTimeDataController.queryToDayLiveData(roomId, startTime, endTime); JSONObject json = getInstance().realTimeDataController.queryToDayLiveData(roomId, startTime, endTime);
System.out.println(json); System.out.println(json);
builder.append("当前人气:").append(json.getJSONObject("data").getInteger("popular")).append("\n"); builder.append("当前人气:").append(json.getJSONObject("data").getInteger("popular")).append("\n");

View File

@ -25,7 +25,7 @@ public class TestController {
JSONObject json=new JSONObject(); JSONObject json=new JSONObject();
JSONArray array=new JSONArray(); JSONArray array=new JSONArray();
json.put("size", Live.lives.size()); json.put("size", Live.lives.size());
for (Live live :Live.lives) { for (Live live :Live.lives.values()) {
JSONObject item=new JSONObject(); JSONObject item=new JSONObject();
item.put("info", JSON.toJSON(live.getInfo())); item.put("info", JSON.toJSON(live.getInfo()));
item.put("data",JSON.toJSON(live.geData())); item.put("data",JSON.toJSON(live.geData()));

View File

@ -59,7 +59,7 @@ public class ApplicationInit implements ApplicationRunner {
oldTime = time; oldTime = time;
switch (time) { switch (time) {
case "00:00": case "00:00":
for (Live live : Live.lives) { for (Live live : Live.lives.values()) {
live.clearInfo(); live.clearInfo();
} }
break; break;