直播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));
}
}
for (Live live : Live.lives) {
for (Live live : Live.lives.values()) {
if(live.geData().getLive()==1){
liveArray.add(JSON.toJSON(live.geData()));
}

View File

@ -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<Live> lives = new ArrayList<>();
public static List<String> liveOfRoomId = new ArrayList<>();
public static Map<Integer,Live> 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<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-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;
}

View File

@ -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;
}

View File

@ -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();

View File

@ -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> T getBean(Class<T> 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");

View File

@ -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()));

View File

@ -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;