diff --git a/src/main/java/com/yutou/bilibili/BiliBili/Live.java b/src/main/java/com/yutou/bilibili/BiliBili/Live.java index 7482750..cd4215d 100644 --- a/src/main/java/com/yutou/bilibili/BiliBili/Live.java +++ b/src/main/java/com/yutou/bilibili/BiliBili/Live.java @@ -294,7 +294,7 @@ public class Live implements ApplicationContextAware { processData(list.get(i), data); 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)); @@ -302,8 +302,14 @@ public class Live implements ApplicationContextAware { } catch (Exception e) { int popular = LiveUtils.bytesToInt2(bytes, 0); info.setPopular(popular); + if(popular>1){ + upData.setLive(1); + }else{ + upData.setLive(0); + } } } + } catch (Exception e) { Log.e(e); com.yutou.bilibili.Tools.Log.i("----------ERROR----------"); @@ -340,11 +346,10 @@ public class Live implements ApplicationContextAware { BilibiliLiveData liveData = new BilibiliLiveData(); String danmu; GiftData giftData; - + Log.i(AppTools.getToDayTimeToString(startTime),roomId,json.toJSONString()); switch (json.getString("cmd")) { case "INTERACT_WORD"://普通用户进直播间 danmu = json.getJSONObject("data").getString("uname") + " 进入到直播间"; - Log.i(AppTools.getToDayTimeToString(startTime), roomId, danmu); liveData.setUid(json.getJSONObject("data").getInteger("uid")); liveData.setType(LiveData.INTERACT_WORD); liveData.setMsg(danmu); @@ -370,7 +375,6 @@ public class Live implements ApplicationContextAware { danmuManager.addDanmu(danmuData); } } - Log.i(AppTools.getToDayTimeToString(startTime), roomId, danmu); break; case "SEND_GIFT"://送礼 data = json.getJSONObject("data"); @@ -397,7 +401,6 @@ public class Live implements ApplicationContextAware { liveData.setRoomid(roomId); info.setGiftuser(info.getGiftuser() + 1); service.addLiveData(liveData); - Log.i(AppTools.getToDayTimeToString(startTime), roomId, danmu); break; case "COMBO_SEND"://礼物连击 data = json.getJSONObject("data"); @@ -428,7 +431,6 @@ public class Live implements ApplicationContextAware { liveData.setRoomid(roomId); info.setGiftuser(info.getGiftuser() + 1); service.addLiveData(liveData); - Log.i(AppTools.getToDayTimeToString(startTime), roomId, danmu); break; case "ENTRY_EFFECT"://舰长进直播间 info.setVipuserindex(info.getVipuserindex() + 1); @@ -439,7 +441,6 @@ public class Live implements ApplicationContextAware { liveData.setRoomid(roomId); liveData.setSubtime(new Date()); service.addLiveData(liveData); - Log.i(AppTools.getToDayTimeToString(startTime), roomId, danmu); break; case "LIVE_INTERACTIVE_GAME"://彩色弹幕?通过游戏弹幕 break; @@ -459,7 +460,6 @@ public class Live implements ApplicationContextAware { liveData.setSubtime(new Date()); info.setGiftuser(info.getGiftuser() + 1); service.addLiveData(liveData); - Log.i(AppTools.getToDayTimeToString(startTime), roomId, danmu); break; case "USER_TOAST_MSG": break; @@ -477,7 +477,6 @@ public class Live implements ApplicationContextAware { liveData.setRoomid(roomId); info.setGiftuser(info.getGiftuser() + 1); service.addLiveData(liveData); - Log.i(AppTools.getToDayTimeToString(startTime), roomId, liveData.toString()); break; case "LIVE"://开始直播,不过有在心跳包上做检测了,所以也无所谓? if (upData.getLive() == 1) { @@ -505,7 +504,6 @@ public class Live implements ApplicationContextAware { QQBotManager.getInstance().sendMessage(file, builder.toString()); } }); - Log.i(AppTools.getToDayTimeToString(startTime), roomId, upData.getName() + "开播了!"); break; case "PREPARING":// case "SUPER_CHAT_MESSAGE_JPN": @@ -536,7 +534,6 @@ public class Live implements ApplicationContextAware { liveData.setRoomid(roomId); liveData.setSubtime(new Date()); service.addLiveData(liveData); - Log.i(AppTools.getToDayTimeToString(startTime), roomId, liveData.toString()); } } catch (Exception e) { Log.e(e); @@ -619,7 +616,6 @@ public class Live implements ApplicationContextAware { outputStream.write("[object Object]".getBytes(StandardCharsets.UTF_8)); outputStream.flush(); client.send(outputStream.toByteArray()); - checkLive(); } catch (Exception e) { Log.e(e); com.yutou.bilibili.Tools.Log.i(client.isClosed()); diff --git a/src/main/java/com/yutou/bilibili/BiliBili/LiveUtils.java b/src/main/java/com/yutou/bilibili/BiliBili/LiveUtils.java index 5b1b5fb..56005eb 100644 --- a/src/main/java/com/yutou/bilibili/BiliBili/LiveUtils.java +++ b/src/main/java/com/yutou/bilibili/BiliBili/LiveUtils.java @@ -25,6 +25,7 @@ import java.util.zip.Inflater; public class LiveUtils { private static String cookie = null; + private static Map liveStateMap=new HashMap<>(); public LiveUtils() { @@ -353,6 +354,12 @@ public class LiveUtils { private static JSONObject getLiveState(int roomId){ return http_get("https://api.live.bilibili.com/xlive/web-room/v2/index/getRoomPlayInfo?room_id=" + roomId); } + public static void reloadLiveState(int roomId){ + JSONObject json = getLiveState(roomId); + if(json!=null){ + liveStateMap.put(roomId,json.getJSONObject("data").getInteger("live_status") == 1); + } + } public static String getLiveTitle(int roomId) { JSONObject json =getLiveInfo(roomId); @@ -363,11 +370,12 @@ public class LiveUtils { } public static boolean isLivePlayer(int roomId) { - JSONObject json = getLiveState(roomId); - if (json == null) { - return false; + /* if (liveStateMap.containsKey(roomId)) { + return liveStateMap.get(roomId); } - return json.getJSONObject("data").getInteger("live_status") == 1; + reloadLiveState(roomId); + return false;*/ + return Live.lives.get(roomId).geData().getLive()==1; } private static void checkLiveSave(int roomId) { diff --git a/src/main/java/com/yutou/bilibili/BilibiliApplication.java b/src/main/java/com/yutou/bilibili/BilibiliApplication.java index 1376f5c..7816b80 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.7.4.1"; + public static String version="0.7.4.4"; public static void main(String[] args) { QQBotManager.getInstance().init(); diff --git a/src/main/java/com/yutou/bilibili/Controllers/UserController.java b/src/main/java/com/yutou/bilibili/Controllers/UserController.java index 57930df..02799f7 100644 --- a/src/main/java/com/yutou/bilibili/Controllers/UserController.java +++ b/src/main/java/com/yutou/bilibili/Controllers/UserController.java @@ -9,6 +9,7 @@ import com.yutou.bilibili.BiliBili.Tools.BiliTools; import com.yutou.bilibili.Services.IUserService; import com.yutou.bilibili.Tools.AppTools; import com.yutou.bilibili.Tools.Config; +import com.yutou.bilibili.Tools.RedisTools; import com.yutou.bilibili.mybatis.Bili.mybatis.model.BilibiliUpInfo; import com.yutou.bilibili.mybatis.model.UBiliUp; import com.yutou.bilibili.mybatis.model.UUser; @@ -50,6 +51,7 @@ public class UserController { user.setPower("[1,2,3,5,6,7]"); user.setSubtime(new Date()); user.setLogintoken(UUID.randomUUID().toString()); + RedisTools.set(user.getLogintoken(),user.getId()+"",30*24*60); AppTools.setCookie(response, "login", user.getLogintoken(), -1); boolean flag = service.reg(user); if (flag) { @@ -69,8 +71,8 @@ public class UserController { json.put("code", 0); if (service.login(user.getUser(), user.getPassword())) { user = service.getUser(user.getUser()); - user.setLogintoken(UUID.randomUUID().toString()); - service.update(user); + //user.setLogintoken(UUID.randomUUID().toString()); + RedisTools.set(user.getLogintoken(),user.getId()+"",30*24*60); AppTools.setCookie(response, "login", user.getLogintoken(), -1); json.put("msg", "登陆成功"); json.put("power", user.getPower()); @@ -92,6 +94,7 @@ public class UserController { } else { UUser user = service.getUserToToken(token); if (user != null) { + RedisTools.remove(user.getLogintoken()); user.setLogintoken(""); service.update(user); AppTools.deleteCookie(request, response, "login"); @@ -114,7 +117,7 @@ public class UserController { json.put("code", -1); json.put("msg", "未登录"); } else { - UUser loginUser = service.getUserToToken(token); + UUser loginUser = service.getUserToUid(Integer.parseInt(RedisTools.get(token))); if (loginUser != null) { user.setLogintoken(""); user.setId(loginUser.getId()); @@ -144,8 +147,7 @@ public class UserController { } else { json.put("code", 0); json.put("msg", "ok"); - UUser user = service.getUserToToken(token); - json.put("data", service.getUserUp(user.getId())); + json.put("data", service.getUserUp(Integer.parseInt(RedisTools.get(token)))); } return json; } @@ -159,7 +161,6 @@ public class UserController { json.put("code", "-1"); json.put("msg", "未登录"); } else { - UUser user = service.getUserToToken(token); int roomid = 0; if (!info.getUrl().startsWith("http")) { info.setUrl("https://live.bilibili.com/" + info.getUrl()); @@ -181,7 +182,7 @@ public class UserController { return json; } UBiliUp up = new UBiliUp(); - up.setUid(user.getId()); + up.setUid(Integer.parseInt(RedisTools.get(token))); up.setRoomid(roomid); json.put("code", 0); json.put("msg", service.addUp(up) ? "添加成功" : "添加失败"); @@ -198,7 +199,7 @@ public class UserController { json.put("code", -1); json.put("msg", "未登录"); } else { - UUser user = service.getUserToToken(token); + UUser user = service.getUserToUid(Integer.parseInt(RedisTools.get(token)));; if (user == null) { json.put("code", -2); json.put("msg", "未登录"); diff --git a/src/main/java/com/yutou/bilibili/QQBot/QQBotManager.java b/src/main/java/com/yutou/bilibili/QQBot/QQBotManager.java index d615276..813b889 100644 --- a/src/main/java/com/yutou/bilibili/QQBot/QQBotManager.java +++ b/src/main/java/com/yutou/bilibili/QQBot/QQBotManager.java @@ -235,8 +235,8 @@ public class QQBotManager implements ApplicationContextAware { } } - private void myGroup(String msg) { - msg = msg.replace("!", "!").toLowerCase(); + private void myGroup(String message) { + String msg = message.replace("!", "!").toLowerCase(); msg=msg.trim(); StringBuilder builder = new StringBuilder(); JSONObject json; @@ -346,19 +346,9 @@ public class QQBotManager implements ApplicationContextAware { } } else if (msg.startsWith(QQCommands.QQ_SYSTEM_LOG)) { try { - cmd = msg.split(" "); - switch (cmd[1]){ - case "true": - RedisTools.set(1,"live-log","true"); - getInstance().sendMessage("日志设为 true"); - break; - case "false": - RedisTools.set(1,"live-log","false"); - getInstance().sendMessage("日志设为 false"); - break; - default: - getInstance().sendMessage("设置错误,"+QQCommands.QQ_SYSTEM_LOG +" true|false"); - } + cmd = message.replace("!","!").replace(QQCommands.QQ_SYSTEM_LOG,"").split(" "); + RedisTools.set(1,"live-log",cmd[1].trim()); + getInstance().sendMessage("日志设为:"+cmd[1].trim()); } catch (Exception e) { e.printStackTrace(); } diff --git a/src/main/java/com/yutou/bilibili/Services/IUserService.java b/src/main/java/com/yutou/bilibili/Services/IUserService.java index 085fb74..dc37877 100644 --- a/src/main/java/com/yutou/bilibili/Services/IUserService.java +++ b/src/main/java/com/yutou/bilibili/Services/IUserService.java @@ -12,6 +12,7 @@ public interface IUserService { boolean removeUp(UUser user,UBiliUp up); boolean update(UUser user); UUser getUser(String uname); + UUser getUserToUid(Integer uid); UUser getUserToToken(String token); List getUserUp(int uid); } diff --git a/src/main/java/com/yutou/bilibili/Services/impl/UserServiceImpl.java b/src/main/java/com/yutou/bilibili/Services/impl/UserServiceImpl.java index 2150dc4..f208237 100644 --- a/src/main/java/com/yutou/bilibili/Services/impl/UserServiceImpl.java +++ b/src/main/java/com/yutou/bilibili/Services/impl/UserServiceImpl.java @@ -73,6 +73,11 @@ public class UserServiceImpl implements IUserService { } } + @Override + public UUser getUserToUid(Integer uid) { + return userDao.selectByPrimaryKey(uid); + } + @Override public List getUserUp(int uid) { UBiliUpExample example=new UBiliUpExample(); diff --git a/src/main/java/com/yutou/bilibili/Tools/APIFilter.java b/src/main/java/com/yutou/bilibili/Tools/APIFilter.java index 7fd8794..9c12d98 100644 --- a/src/main/java/com/yutou/bilibili/Tools/APIFilter.java +++ b/src/main/java/com/yutou/bilibili/Tools/APIFilter.java @@ -47,7 +47,7 @@ public class APIFilter implements Filter { ){ filterChain.doFilter(servletRequest,servletResponse); }else { - UUser user= service.getUserToToken(token); + UUser user= service.getUserToUid(Integer.parseInt(RedisTools.get(token)));; if(user==null){ response.sendRedirect("/"); }else{ diff --git a/src/main/java/com/yutou/bilibili/Tools/ApplicationInit.java b/src/main/java/com/yutou/bilibili/Tools/ApplicationInit.java index f3fb0f1..dfc757b 100644 --- a/src/main/java/com/yutou/bilibili/Tools/ApplicationInit.java +++ b/src/main/java/com/yutou/bilibili/Tools/ApplicationInit.java @@ -94,6 +94,7 @@ public class ApplicationInit implements ApplicationRunner { private void checkLive() { for (BilibiliUpInfo info : service.getUpInfo()) { + // LiveUtils.reloadLiveState(info.getRoomid()); if (info.getOfflinelistening() == 1) { if (info.getEnable() == 1) { Live live = LiveUtils.liveContains(info); @@ -111,7 +112,7 @@ public class ApplicationInit implements ApplicationRunner { } } } - }, 0, 60 * 1000); + }, 0, 2*60 * 1000); } } diff --git a/src/main/java/com/yutou/bilibili/Tools/Log.java b/src/main/java/com/yutou/bilibili/Tools/Log.java index d092d99..57120c2 100644 --- a/src/main/java/com/yutou/bilibili/Tools/Log.java +++ b/src/main/java/com/yutou/bilibili/Tools/Log.java @@ -9,6 +9,7 @@ import java.util.logging.Logger; public class Log { private static Logger logger; + public static void i(Object log) { if (!((boolean) ConfigTools.load(ConfigTools.CONFIG, "logout"))) { return; @@ -25,23 +26,30 @@ public class Log { return; } i(e.getMessage()); - e.printStackTrace(); + e.printStackTrace(); } - public static void i(String timer,int roomId,Object log){ - String logFlag=RedisTools.get("live-log",1); - if("true".equals(logFlag)) { - getLogger("[" + timer.replace(":", "_") + "]" + roomId).log(Level.INFO, log.toString()); + + public static void i(String timer, int roomId, Object log) { + String logFlag = RedisTools.get("live-log", 1); + if (logFlag.startsWith("true")) { + if (logFlag.contains("|")) { + if (log.toString().contains(logFlag.split("\\|")[1])) { + getLogger("[" + timer.replace(":", "_") + "]" + roomId).log(Level.INFO, log.toString()); + } + } else { + getLogger("[" + timer.replace(":", "_") + "]" + roomId).log(Level.INFO, log.toString()); + } } } - public static Logger getLogger(String fileName){ - if(logger==null){ + public static Logger getLogger(String fileName) { + if (logger == null) { try { - if(!new File("logs").exists()){ + if (!new File("logs").exists()) { new File("logs").mkdirs(); } - logger=Logger.getLogger("Live-Log"); - FileHandler handler=new FileHandler("logs"+ File.separator+fileName+".log"); + logger = Logger.getLogger("Live-Log"); + FileHandler handler = new FileHandler("logs" + File.separator + fileName + ".log"); logger.addHandler(handler); } catch (IOException e) { e.printStackTrace();