This commit is contained in:
yutou 2021-06-04 17:19:50 +08:00
parent be3e3bbf9d
commit 57ec517608
4 changed files with 218 additions and 192 deletions

View File

@ -152,6 +152,10 @@ public class Live implements ApplicationContextAware {
header.put("Pragma", "no-cache");
header.put("Upgrade", "websocket");
header.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36");
if (client != null && !client.isClosed()) {
stop();
return;
}
client = new WebSocketClient(new URI(url), header) {
private long time = 0;
private boolean init = true;
@ -223,6 +227,14 @@ public class Live implements ApplicationContextAware {
}
};
client.connect();
new Timer().schedule(new TimerTask() {
@Override
public void run() {
if (client == null || client.isClosed()) {
stop();
}
}
}, 0, 60 * 1000);
}
public void stop() {
@ -294,11 +306,11 @@ public class Live implements ApplicationContextAware {
if (data.length > 32) {
List<String> list = LiveUtils.getMsgList(LiveUtils.dec(bytes), new ArrayList<>(), true);
String tmp = "";
for (int i=0;i<list.size();i++) {
for (int i = 0; i < list.size(); i++) {
processData(list.get(i), data);
tmp +="|||"+i+"[start] "+ list.get(i)+ " [end]||| ";
tmp += "|||" + i + "[start] " + list.get(i) + " [end]||| ";
}
// Log.i(AppTools.getToDayTimeToString(startTime), roomId, "list size = " + list.size() + " data = " + tmp);
// Log.i(AppTools.getToDayTimeToString(startTime), roomId, "list size = " + list.size() + " data = " + tmp);
} else {
try {
JSONObject json = JSONObject.parseObject(new String(bytes, StandardCharsets.UTF_8));
@ -306,14 +318,14 @@ public class Live implements ApplicationContextAware {
} catch (Exception e) {
int popular = LiveUtils.bytesToInt2(bytes, 0);
info.setPopular(popular);
if(popular>1){
if (popular > 1) {
upData.setLive(1);
}else{
} else {
upData.setLive(0);
}
}
}
} catch (Exception e) {
Log.e(e);
com.yutou.bilibili.Tools.Log.i("----------ERROR----------");
@ -350,194 +362,198 @@ public class Live implements ApplicationContextAware {
BilibiliLiveData liveData = new BilibiliLiveData();
String danmu;
GiftData giftData;
Log.i(AppTools.getToDayTime(),roomId,json.toJSONString());
switch (json.getString("cmd")) {
case "INTERACT_WORD"://普通用户进直播间
danmu = json.getJSONObject("data").getString("uname") + " 进入到直播间";
liveData.setUid(json.getJSONObject("data").getInteger("uid"));
liveData.setType(LiveData.INTERACT_WORD);
liveData.setMsg(danmu);
liveData.setRoomid(roomId);
liveData.setSubtime(new Date());
service.addLiveData(liveData);
break;
case "DANMU_MSG"://普通弹幕
JSONArray infoData = json.getJSONArray("info");
danmu = json.getJSONArray("info").getString(1);
DanmuData danmuData = new DanmuData();
danmuData.setModel(infoData.getJSONArray(0).getInteger(1));
danmuData.setFontSize(infoData.getJSONArray(0).getInteger(2));
danmuData.setFontColor(infoData.getJSONArray(0).getInteger(3));
danmuData.setTime(infoData.getJSONArray(0).getLong(4));
danmuData.setUCode(infoData.getJSONArray(0).getString(7));
danmuData.setDanmu(danmu);
danmuData.setUid(infoData.getJSONArray(2).getInteger(0));
danmuData.setUname(infoData.getJSONArray(2).getString(1));
Log.i(AppTools.getToDayTime(), roomId, json.toJSONString());
if (json.getString("cmd").startsWith("DANMU_MSG")) {//普通弹幕
JSONArray infoData = json.getJSONArray("info");
danmu = json.getJSONArray("info").getString(1);
DanmuData danmuData = new DanmuData();
danmuData.setModel(infoData.getJSONArray(0).getInteger(1));
danmuData.setFontSize(infoData.getJSONArray(0).getInteger(2));
danmuData.setFontColor(infoData.getJSONArray(0).getInteger(3));
danmuData.setTime(infoData.getJSONArray(0).getLong(4));
danmuData.setUCode(infoData.getJSONArray(0).getString(7));
danmuData.setDanmu(danmu);
danmuData.setUid(infoData.getJSONArray(2).getInteger(0));
danmuData.setUname(infoData.getJSONArray(2).getString(1));
if (upData != null && upData.getSavedanmu() == 1) {
if (danmuManager != null) {
danmuManager.addDanmu(danmuData);
}
if (upData != null && upData.getSavedanmu() == 1) {
if (danmuManager != null) {
danmuManager.addDanmu(danmuData);
}
break;
case "SEND_GIFT"://送礼
data = json.getJSONObject("data");
giftData = getGiftData(data.getInteger("giftId"));
if (giftData == null) {
giftData = new GiftData();
giftData.setName(data.getString("giftName"));
giftData.setId(data.getInteger("giftId"));
giftData.setPrice(data.getInteger("price"));
giftData.setIcon("");
giftData.setDesc("这是阿B没有收录的礼物金额可能不准无法判别为免费礼物");
giftData.setRights("?");
}
danmu = data.getString("uname") + " " + data.getString("action") + " " + giftData.getName();
liveData.setType(LiveData.SEND_GIFT);
liveData.setUid(data.getInteger("uid"));
liveData.setGiftid(giftData.getId());
liveData.setGiftindex(data.getInteger("num"));
liveData.setGiftname(giftData.getName());
liveData.setMsg(danmu);
liveData.setPrice(giftData.getPrice());
liveData.setPriceofcommission(giftData.getPrice() == 0 ? 0 : giftData.getPrice() / 2);
liveData.setSubtime(new Date());
liveData.setRoomid(roomId);
info.setGiftuser(info.getGiftuser() + 1);
service.addLiveData(liveData);
break;
case "COMBO_SEND"://礼物连击
data = json.getJSONObject("data");
String gift = data.getString("giftName");
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");
giftData = getGiftData(data.getInteger("gift_id"));
if (giftData == null) {
giftData = new GiftData();
giftData.setName(data.getString("giftName"));
giftData.setId(data.getInteger("giftId"));
giftData.setPrice(data.getInteger("price"));
giftData.setIcon("");
giftData.setDesc("这是阿B没有收录的礼物金额可能不准无法判别为免费礼物");
giftData.setRights("?");
}
liveData.setType(LiveData.COMBO_SEND);
liveData.setUid(data.getInteger("uid"));
liveData.setGiftid(giftData.getId());
liveData.setGiftindex(data.getInteger("batch_combo_num"));
liveData.setGiftname(giftData.getName());
liveData.setMsg(danmu);
liveData.setPrice(giftData.getPrice() * liveData.getGiftindex());
liveData.setPriceofcommission(giftData.getPrice() == 0 ? 0 : giftData.getPrice() / 2);
liveData.setSubtime(new Date());
liveData.setRoomid(roomId);
info.setGiftuser(info.getGiftuser() + 1);
service.addLiveData(liveData);
break;
case "ENTRY_EFFECT"://舰长进直播间
info.setVipuserindex(info.getVipuserindex() + 1);
danmu = "[舰长]" + json.getJSONObject("data").getString("uid") + " 进入到直播间";
liveData.setUid(json.getJSONObject("data").getInteger("uid"));
liveData.setType(LiveData.ENTRY_EFFECT);
liveData.setMsg(danmu);
liveData.setRoomid(roomId);
liveData.setSubtime(new Date());
service.addLiveData(liveData);
break;
case "LIVE_INTERACTIVE_GAME"://彩色弹幕通过游戏弹幕
break;
case "SUPER_CHAT_MESSAGE"://SC
data = json.getJSONObject("data");
danmu = data.getJSONObject("user_info").getString("uname") + " "
+ data.getJSONObject("gift").getString("gift_name") + " " + data.getInteger("price") + "元: " + data.getString("message");
liveData.setType(LiveData.SUPER_CHAT_MESSAGE);
liveData.setUid(data.getInteger("uid"));
liveData.setMsg(danmu);
liveData.setGiftid(0);
liveData.setRoomid(roomId);
liveData.setGiftname(data.getJSONObject("gift").getString("gift_name"));
liveData.setGiftindex(1);
liveData.setPrice(data.getInteger("price") * 1000);
liveData.setPriceofcommission(liveData.getPrice() == 0 ? 0 : liveData.getPrice() / 2);
liveData.setSubtime(new Date());
info.setGiftuser(info.getGiftuser() + 1);
service.addLiveData(liveData);
break;
case "USER_TOAST_MSG":
break;
case "GUARD_BUY"://开通/续费 牛逼的东西
data = json.getJSONObject("data");
liveData.setType(LiveData.GUARD_BUY);
liveData.setUid(data.getInteger("uid"));
liveData.setMsg(data.getString("gift_name"));
liveData.setGiftid(-data.getInteger("guard_level"));
liveData.setGiftindex(data.getInteger("num"));
liveData.setGiftname(data.getString("gift_name"));
liveData.setPrice(data.getInteger("price"));
liveData.setPriceofcommission(liveData.getPrice() == 0 ? 0 : liveData.getPrice() / 2);
liveData.setSubtime(new Date());
liveData.setRoomid(roomId);
info.setGiftuser(info.getGiftuser() + 1);
service.addLiveData(liveData);
break;
case "LIVE"://开始直播,不过有在心跳包上做检测了所以也无所谓
if (upData.getLive() == 1) {
}
} else {
switch (json.getString("cmd")) {
case "INTERACT_WORD"://普通用户进直播间
danmu = json.getJSONObject("data").getString("uname") + " 进入到直播间";
liveData.setUid(json.getJSONObject("data").getInteger("uid"));
liveData.setType(LiveData.INTERACT_WORD);
liveData.setMsg(danmu);
liveData.setRoomid(roomId);
liveData.setSubtime(new Date());
service.addLiveData(liveData);
break;
}
upData.setLive(1);
JSONObject liveInfo = LiveUtils.getLiveInfo(roomId);
if (liveInfo == null) {
case "DANMU_MSG":
break;
}
LiveInfo info = new LiveInfo();
info.setTitle(liveInfo.getJSONObject("data").getJSONObject("room_info").getString("title"));
info.setKeyframe(liveInfo.getJSONObject("data").getJSONObject("room_info").getString("keyframe"));
info.setRoomId(liveInfo.getJSONObject("data").getJSONObject("room_info").getInteger("room_id"));
info.setShortId(liveInfo.getJSONObject("data").getJSONObject("room_info").getInteger("short_id"));
info.setInfo(liveInfo);
setLiveInfo(info);
StringBuilder builder = new StringBuilder();
builder.append(upData.getName()).append("开播了!").append("\n");
builder.append(info.getTitle());
Tools.download(info.getKeyframe(), new DownloadInterface() {
@Override
public void onDownload(File file) {
super.onDownload(file);
QQBotManager.getInstance().sendMessage(file, builder.toString());
case "SEND_GIFT"://送礼
data = json.getJSONObject("data");
giftData = getGiftData(data.getInteger("giftId"));
if (giftData == null) {
giftData = new GiftData();
giftData.setName(data.getString("giftName"));
giftData.setId(data.getInteger("giftId"));
giftData.setPrice(data.getInteger("price"));
giftData.setIcon("");
giftData.setDesc("这是阿B没有收录的礼物金额可能不准无法判别为免费礼物");
giftData.setRights("?");
}
});
break;
case "PREPARING"://
case "SUPER_CHAT_MESSAGE_JPN":
case "NOTICE_MSG":
case "HOT_RANK_CHANGED"://榜单更新等无用信息
case "ONLINE_RANK_COUNT":
case "ONLINE_RANK_V2":
case "ONLINE_RANK_TOP3":
case "ROOM_REAL_TIME_MESSAGE_UPDATE":
case "WIDGET_BANNER"://鬼知道是啥
case "HOT_RANK_SETTLEMENT":
case "PK_BATTLE_SETTLE_V2":
case "PK_BATTLE_END":
case "PK_BATTLE_SETTLE":
case "PK_BATTLE_PRE_NEW":
case "PK_BATTLE_PRE":
case "STOP_LIVE_ROOM_LIST"://不知道是啥停止直播
danmu = data.getString("uname") + " " + data.getString("action") + " " + giftData.getName();
liveData.setType(LiveData.SEND_GIFT);
liveData.setUid(data.getInteger("uid"));
liveData.setGiftid(giftData.getId());
liveData.setGiftindex(data.getInteger("num"));
liveData.setGiftname(giftData.getName());
liveData.setMsg(danmu);
liveData.setPrice(giftData.getPrice());
liveData.setPriceofcommission(giftData.getPrice() == 0 ? 0 : giftData.getPrice() / 2);
liveData.setSubtime(new Date());
liveData.setRoomid(roomId);
info.setGiftuser(info.getGiftuser() + 1);
service.addLiveData(liveData);
break;
case "COMBO_SEND"://礼物连击
data = json.getJSONObject("data");
String gift = data.getString("giftName");
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");
giftData = getGiftData(data.getInteger("gift_id"));
if (giftData == null) {
giftData = new GiftData();
giftData.setName(data.getString("giftName"));
giftData.setId(data.getInteger("giftId"));
giftData.setPrice(data.getInteger("price"));
giftData.setIcon("");
giftData.setDesc("这是阿B没有收录的礼物金额可能不准无法判别为免费礼物");
giftData.setRights("?");
}
liveData.setType(LiveData.COMBO_SEND);
liveData.setUid(data.getInteger("uid"));
liveData.setGiftid(giftData.getId());
liveData.setGiftindex(data.getInteger("batch_combo_num"));
liveData.setGiftname(giftData.getName());
liveData.setMsg(danmu);
liveData.setPrice(giftData.getPrice() * liveData.getGiftindex());
liveData.setPriceofcommission(giftData.getPrice() == 0 ? 0 : giftData.getPrice() / 2);
liveData.setSubtime(new Date());
liveData.setRoomid(roomId);
info.setGiftuser(info.getGiftuser() + 1);
service.addLiveData(liveData);
break;
case "ENTRY_EFFECT"://舰长进直播间
info.setVipuserindex(info.getVipuserindex() + 1);
danmu = "[舰长]" + json.getJSONObject("data").getString("uid") + " 进入到直播间";
liveData.setUid(json.getJSONObject("data").getInteger("uid"));
liveData.setType(LiveData.ENTRY_EFFECT);
liveData.setMsg(danmu);
liveData.setRoomid(roomId);
liveData.setSubtime(new Date());
service.addLiveData(liveData);
break;
case "LIVE_INTERACTIVE_GAME"://彩色弹幕通过游戏弹幕
break;
case "SUPER_CHAT_MESSAGE"://SC
data = json.getJSONObject("data");
danmu = data.getJSONObject("user_info").getString("uname") + " "
+ data.getJSONObject("gift").getString("gift_name") + " " + data.getInteger("price") + "元: " + data.getString("message");
liveData.setType(LiveData.SUPER_CHAT_MESSAGE);
liveData.setUid(data.getInteger("uid"));
liveData.setMsg(danmu);
liveData.setGiftid(0);
liveData.setRoomid(roomId);
liveData.setGiftname(data.getJSONObject("gift").getString("gift_name"));
liveData.setGiftindex(1);
liveData.setPrice(data.getInteger("price") * 1000);
liveData.setPriceofcommission(liveData.getPrice() == 0 ? 0 : liveData.getPrice() / 2);
liveData.setSubtime(new Date());
info.setGiftuser(info.getGiftuser() + 1);
service.addLiveData(liveData);
break;
case "USER_TOAST_MSG":
break;
case "GUARD_BUY"://开通/续费 牛逼的东西
data = json.getJSONObject("data");
liveData.setType(LiveData.GUARD_BUY);
liveData.setUid(data.getInteger("uid"));
liveData.setMsg(data.getString("gift_name"));
liveData.setGiftid(-data.getInteger("guard_level"));
liveData.setGiftindex(data.getInteger("num"));
liveData.setGiftname(data.getString("gift_name"));
liveData.setPrice(data.getInteger("price"));
liveData.setPriceofcommission(liveData.getPrice() == 0 ? 0 : liveData.getPrice() / 2);
liveData.setSubtime(new Date());
liveData.setRoomid(roomId);
info.setGiftuser(info.getGiftuser() + 1);
service.addLiveData(liveData);
break;
case "LIVE"://开始直播,不过有在心跳包上做检测了所以也无所谓
if (upData.getLive() == 1) {
break;
}
upData.setLive(1);
JSONObject liveInfo = LiveUtils.getLiveInfo(roomId);
if (liveInfo == null) {
break;
}
LiveInfo info = new LiveInfo();
info.setTitle(liveInfo.getJSONObject("data").getJSONObject("room_info").getString("title"));
info.setKeyframe(liveInfo.getJSONObject("data").getJSONObject("room_info").getString("keyframe"));
info.setRoomId(liveInfo.getJSONObject("data").getJSONObject("room_info").getInteger("room_id"));
info.setShortId(liveInfo.getJSONObject("data").getJSONObject("room_info").getInteger("short_id"));
info.setInfo(liveInfo);
setLiveInfo(info);
StringBuilder builder = new StringBuilder();
builder.append(upData.getName()).append("开播了!").append("\n");
builder.append(info.getTitle());
Tools.download(info.getKeyframe(), new DownloadInterface() {
@Override
public void onDownload(File file) {
super.onDownload(file);
QQBotManager.getInstance().sendMessage(file, builder.toString());
}
});
break;
case "PREPARING"://
case "SUPER_CHAT_MESSAGE_JPN":
case "NOTICE_MSG":
case "HOT_RANK_CHANGED"://榜单更新等无用信息
case "ONLINE_RANK_COUNT":
case "ONLINE_RANK_V2":
case "ONLINE_RANK_TOP3":
case "ROOM_REAL_TIME_MESSAGE_UPDATE":
case "WIDGET_BANNER"://鬼知道是啥
case "HOT_RANK_SETTLEMENT":
case "PK_BATTLE_SETTLE_V2":
case "PK_BATTLE_END":
case "PK_BATTLE_SETTLE":
case "PK_BATTLE_PRE_NEW":
case "PK_BATTLE_PRE":
case "STOP_LIVE_ROOM_LIST"://不知道是啥停止直播
//com.yutou.bilibili.Tools.Log.i(msg);
break;
default:
com.yutou.bilibili.Tools.Log.i("["+roomId+"]"+msg);
liveData = new BilibiliLiveData();
liveData.setType(LiveData.UNKNOWN_MESSAGE);
liveData.setUid(-1);
liveData.setMsg(msg);
liveData.setRoomid(roomId);
liveData.setSubtime(new Date());
service.addLiveData(liveData);
//com.yutou.bilibili.Tools.Log.i(msg);
break;
default:
Log.i("[" + roomId + "]" + msg);
liveData = new BilibiliLiveData();
liveData.setType(LiveData.UNKNOWN_MESSAGE);
liveData.setUid(-1);
liveData.setMsg(msg);
liveData.setRoomid(roomId);
liveData.setSubtime(new Date());
service.addLiveData(liveData);
}
}
} catch (Exception e) {
Log.e(e);

View File

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

View File

@ -58,6 +58,7 @@ public class QQBotManager implements ApplicationContextAware {
private final static String QQ_LIVE_TO_DAY_DATE = "!今日数据";
private final static String QQ_LIVE_DATE = "!数据";
private final static String QQ_LIVE_USE_SAVE = "!启动录播";
private final static String QQ_LIVE_RELINK="!relink";
}
private static QQBotManager botManager = null;
@ -308,6 +309,11 @@ public class QQBotManager implements ApplicationContextAware {
}
getInstance().sendMessage(builder.toString());
break;
case QQCommands.QQ_LIVE_RELINK:
for (Live live : Live.lives.values()) {
live.stop();
}
break;
default:
if (msg.startsWith(QQCommands.QQ_LIVE_TO_DAY_DATE)) {
try {

View File

@ -33,26 +33,30 @@ public class BilibiliLiveInfo implements Serializable {
}
public Integer getPopular() {
if(popular==null)
if(popular==null) {
popular=0;
}
return popular;
}
public Integer getUserindex() {
if(userindex==null)
if(userindex==null) {
userindex=0;
}
return userindex;
}
public Integer getVipuserindex() {
if(vipuserindex==null)
if(vipuserindex==null) {
vipuserindex=0;
}
return vipuserindex;
}
public Integer getGiftuser() {
if(giftuser==null)
if(giftuser==null) {
giftuser=0;
}
return giftuser;
}
}