From 9b7633e8a9a6c507cff78e7cad83084bedde7bdb Mon Sep 17 00:00:00 2001 From: Yutousama <583819556@qq.com> Date: Thu, 8 Apr 2021 22:03:47 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=9C=BA=E5=99=A8=E4=BA=BA?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=20=E4=BD=BF=E7=94=A8=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E6=9D=A5=E5=AD=98=E5=82=A8=E7=9B=B4=E6=92=AD=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=EF=BC=8C=E9=98=B2=E6=AD=A2=E9=A2=91=E7=B9=81=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E5=AF=BC=E8=87=B4412=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .mvn/wrapper/MavenWrapperDownloader.java | 2 +- .../Controllers/RealTimeDataController.java | 3 +- .../com/yutou/bilibili/BiliBili/Live.java | 43 ++++-- .../bilibili/BiliBili/LiveController.java | 2 +- .../yutou/bilibili/BiliBili/LiveUtils.java | 142 ++++++++++++------ .../bilibili/BiliBili/Tools/SaveLive.java | 22 ++- .../yutou/bilibili/BilibiliApplication.java | 2 +- .../Controllers/SystemConfigController.java | 10 ++ .../yutou/bilibili/QQBot/QQBotManager.java | 54 +++---- .../com/yutou/bilibili/Tools/APIFilter.java | 2 +- .../com/yutou/bilibili/Tools/AppTools.java | 8 +- .../yutou/bilibili/Tools/ApplicationInit.java | 5 +- .../com/yutou/bilibili/Tools/ConfigTools.java | 86 +++++++++++ .../com/yutou/bilibili/Tools/ExcelUtils.java | 2 +- .../com/yutou/bilibili/Tools/FFmpegUtils.java | 4 +- .../com/yutou/bilibili/Tools/HttpTools.java | 6 +- .../java/com/yutou/bilibili/Tools/Log.java | 11 +- .../yutou/bilibili/Tools/ServiceTools.java | 2 +- .../com/yutou/bilibili/Tools/TestMain.java | 2 +- .../java/com/yutou/bilibili/Tools/Tools.java | 4 +- .../sqlite/BiliBiliLiveDatabasesManager.java | 4 +- .../yutou/bilibili/sqlite/SQLiteManager.java | 14 +- .../resources/mappers/BilibiliLiveDataDao.xml | 4 +- 23 files changed, 305 insertions(+), 129 deletions(-) create mode 100644 src/main/java/com/yutou/bilibili/Tools/ConfigTools.java diff --git a/.mvn/wrapper/MavenWrapperDownloader.java b/.mvn/wrapper/MavenWrapperDownloader.java index e8786e7..49adb17 100644 --- a/.mvn/wrapper/MavenWrapperDownloader.java +++ b/.mvn/wrapper/MavenWrapperDownloader.java @@ -90,7 +90,7 @@ public class MavenWrapperDownloader { System.exit(0); } catch (Throwable e) { com.yutou.bilibili.Tools.Log.i("- Error downloading"); - e.printStackTrace(); + com.yutou.bilibili.Tools.Log.e(e); System.exit(1); } } diff --git a/src/main/java/com/yutou/bilibili/BiliBili/Controllers/RealTimeDataController.java b/src/main/java/com/yutou/bilibili/BiliBili/Controllers/RealTimeDataController.java index bc41183..3218103 100644 --- a/src/main/java/com/yutou/bilibili/BiliBili/Controllers/RealTimeDataController.java +++ b/src/main/java/com/yutou/bilibili/BiliBili/Controllers/RealTimeDataController.java @@ -7,6 +7,7 @@ import com.yutou.bilibili.BiliBili.Live; import com.yutou.bilibili.BiliBili.LiveUtils; import com.yutou.bilibili.BiliBili.Services.IBiliBiliLiveService; import com.yutou.bilibili.Tools.AppTools; +import com.yutou.bilibili.Tools.Log; import com.yutou.bilibili.mybatis.Bili.mybatis.model.BilibiliLiveInfo; import com.yutou.bilibili.mybatis.Bili.mybatis.model.BilibiliUpInfo; import org.springframework.stereotype.Controller; @@ -28,7 +29,7 @@ public class RealTimeDataController { IBiliBiliLiveService service; @ResponseBody @RequestMapping("get/query.do") - public JSONObject queryToDayLiveData(int roomid,Date startTime,Date endTime) throws ParseException { + public JSONObject queryToDayLiveData(int roomid,Date startTime,Date endTime){ JSONObject json = new JSONObject(); BilibiliUpInfo info = new BilibiliUpInfo(); info.setRoomid(roomid); diff --git a/src/main/java/com/yutou/bilibili/BiliBili/Live.java b/src/main/java/com/yutou/bilibili/BiliBili/Live.java index 608b3ab..616b6cc 100644 --- a/src/main/java/com/yutou/bilibili/BiliBili/Live.java +++ b/src/main/java/com/yutou/bilibili/BiliBili/Live.java @@ -38,6 +38,7 @@ public class Live implements ApplicationContextAware { public static List lives = new ArrayList<>(); + public static List liveOfRoomId = new ArrayList<>(); private int roomId; @@ -119,6 +120,10 @@ 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); @@ -196,6 +201,8 @@ public class Live implements ApplicationContextAware { public void onClosing(int code, String reason, boolean remote) { super.onClosing(code, reason, remote); com.yutou.bilibili.Tools.Log.i("code = " + code + ", reason = " + reason + ", remote = " + remote); + run = false; + client.close(); } @Override @@ -214,6 +221,9 @@ public class Live implements ApplicationContextAware { if (danmuManager != null) { danmuManager.close(); } + if(liveOfRoomId.contains(roomId+"")){ + liveOfRoomId.remove(roomId+""); + } if (SaveLive.getInstance().checkLive(roomId)) { SaveLive.getInstance().stop(roomId); } @@ -318,7 +328,7 @@ public class Live implements ApplicationContextAware { * @param msg 数据json * @param bytes 原始hex数据 */ - public void processData(String msg, byte[] bytes) { + public synchronized void processData(String msg, byte[] bytes) { try { JSONObject json = JSONObject.parseObject(msg); JSONObject data; @@ -328,8 +338,8 @@ public class Live implements ApplicationContextAware { switch (json.getString("cmd")) { case "INTERACT_WORD"://普通用户进直播间 - // com.yutou.bilibili.Tools.Log.i(json.getJSONObject("data").getString("uname") + " 进入到直播间"); danmu = json.getJSONObject("data").getString("uname") + " 进入到直播间"; + //com.yutou.bilibili.Tools.Log.i(danmu); liveData.setUid(json.getJSONObject("data").getInteger("uid")); liveData.setType(LiveData.INTERACT_WORD); liveData.setMsg(danmu); @@ -460,17 +470,13 @@ public class Live implements ApplicationContextAware { info.setGiftuser(info.getGiftuser() + 1); service.addLiveData(liveData); break; - 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 "LIVE"://开始直播,不过有在心跳包上做检测了,所以也无所谓? - JSONObject liveInfo = LiveUtils.getLiveInfo(roomId); + if (upData.getLive() == 1) { + break; + } + upData.setLive(1); + LiveUtils.LiveInfoManager.getInstance().check(roomId); + JSONObject liveInfo = LiveUtils.LiveInfoManager.getInstance().getInfo(roomId); StringBuilder builder = new StringBuilder(); builder.append(upData.getName()).append("开播了!").append("\n"); builder.append(liveInfo.getJSONObject("data").getJSONObject("room_info").getString("title")); @@ -482,6 +488,19 @@ public class Live implements ApplicationContextAware { } }); break; + case "PREPARING": + upData.setLive(0); + QQBotManager.getInstance().sendMessage(upData.getName() + " 已下播"); + break; + 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": diff --git a/src/main/java/com/yutou/bilibili/BiliBili/LiveController.java b/src/main/java/com/yutou/bilibili/BiliBili/LiveController.java index 97360cf..0d6b808 100644 --- a/src/main/java/com/yutou/bilibili/BiliBili/LiveController.java +++ b/src/main/java/com/yutou/bilibili/BiliBili/LiveController.java @@ -49,7 +49,7 @@ public class LiveController { live.add(data.getRoomid(), !StringUtils.isEmpty(AppTools.readFile(new File("cookies.json")))); live.start(); } catch (Exception e) { - e.printStackTrace(); + com.yutou.bilibili.Tools.Log.e(e); } } } else { diff --git a/src/main/java/com/yutou/bilibili/BiliBili/LiveUtils.java b/src/main/java/com/yutou/bilibili/BiliBili/LiveUtils.java index f9d587f..e851f61 100644 --- a/src/main/java/com/yutou/bilibili/BiliBili/LiveUtils.java +++ b/src/main/java/com/yutou/bilibili/BiliBili/LiveUtils.java @@ -6,6 +6,7 @@ import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.yutou.bilibili.BiliBili.Services.IBiliBiliLiveService; +import com.yutou.bilibili.BiliBili.Tools.SaveLive; import com.yutou.bilibili.Tools.AppTools; import com.yutou.bilibili.Tools.Log; import com.yutou.bilibili.mybatis.Bili.mybatis.model.BilibiliUpInfo; @@ -66,7 +67,7 @@ public class LiveUtils { } output = o.toByteArray(); } catch (Exception e) { - // e.printStackTrace(); + // com.yutou.bilibili.Tools.Log.e(e); try { JSONObject json = JSONObject.parseObject(new String(data, StandardCharsets.UTF_8)); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); @@ -81,7 +82,7 @@ public class LiveUtils { try { o.close(); } catch (IOException e) { - e.printStackTrace(); + com.yutou.bilibili.Tools.Log.e(e); } } @@ -110,7 +111,7 @@ public class LiveUtils { list.add(new String(bytes, StandardCharsets.UTF_8)); return list; } catch (Exception e) { - e.printStackTrace(); + com.yutou.bilibili.Tools.Log.e(e); } } datas.clear(); @@ -161,29 +162,31 @@ public class LiveUtils { } return builder.toString(); } - /* public static String createCookie(){ - String bid="AUTO"+AppTools.randomString(16,new String[]{AppTools.numbers}); - bid="AUTO6616152607312876"; - return String.format("LIVE_BUVID=%s; _uuid=%sinfoc; buvid3=%sinfoc; sid=%s; fingerprint=%s; buvid_fp=%sinfoc; buvid_fp_plain=%sinfoc; PVID=%d;" - ,bid - ,UUID.randomUUID().toString() - ,UUID.randomUUID().toString() - ,"bm3byv4p" - ,"074eae35a01a56c40558a54ab33179e5" - ,UUID.randomUUID().toString() - ,UUID.randomUUID().toString() - ,AppTools.randomCommon(0,9,1)[0]); - }*/ - private static long oldBiliBiliHttpTime=0; + + /* public static String createCookie(){ + String bid="AUTO"+AppTools.randomString(16,new String[]{AppTools.numbers}); + bid="AUTO6616152607312876"; + return String.format("LIVE_BUVID=%s; _uuid=%sinfoc; buvid3=%sinfoc; sid=%s; fingerprint=%s; buvid_fp=%sinfoc; buvid_fp_plain=%sinfoc; PVID=%d;" + ,bid + ,UUID.randomUUID().toString() + ,UUID.randomUUID().toString() + ,"bm3byv4p" + ,"074eae35a01a56c40558a54ab33179e5" + ,UUID.randomUUID().toString() + ,UUID.randomUUID().toString() + ,AppTools.randomCommon(0,9,1)[0]); + }*/ + private static long oldBiliBiliHttpTime = 0; + public static JSONObject http_get(String url) { try { - if(System.currentTimeMillis()-oldBiliBiliHttpTime<1000){ + if (System.currentTimeMillis() - oldBiliBiliHttpTime < 1000) { try { Thread.sleep(500); } catch (InterruptedException e) { - e.printStackTrace(); + com.yutou.bilibili.Tools.Log.e(e); } - oldBiliBiliHttpTime=System.currentTimeMillis(); + oldBiliBiliHttpTime = System.currentTimeMillis(); } HttpURLConnection connection = getBiliHttpGet(url, getCookie()); BufferedInputStream stream = new BufferedInputStream(connection.getInputStream()); @@ -210,21 +213,22 @@ public class LiveUtils { } } catch (IOException e) { - //e.printStackTrace(); - Log.i("412 in "+url); + //com.yutou.bilibili.Tools.Log.e(e); + Log.e(e); + Log.i("412 in " + url); } return null; } public static JSONObject http_post(String url, String body) { try { - if(System.currentTimeMillis()-oldBiliBiliHttpTime<1000){ + if (System.currentTimeMillis() - oldBiliBiliHttpTime < 1000) { try { Thread.sleep(500); } catch (InterruptedException e) { - e.printStackTrace(); + com.yutou.bilibili.Tools.Log.e(e); } - oldBiliBiliHttpTime=System.currentTimeMillis(); + oldBiliBiliHttpTime = System.currentTimeMillis(); } HttpURLConnection connection = getBiliHttpPost(url, getCookie()); OutputStream connectionOutputStream = null; @@ -260,19 +264,19 @@ public class LiveUtils { } } catch (Exception e) { - e.printStackTrace(); + com.yutou.bilibili.Tools.Log.e(e); } return new JSONObject(); } public static HttpURLConnection getBiliHttpPost(String url, String cookie) throws Exception { - if(System.currentTimeMillis()-oldBiliBiliHttpTime<1000){ + if (System.currentTimeMillis() - oldBiliBiliHttpTime < 1000) { try { Thread.sleep(500); } catch (InterruptedException e) { - e.printStackTrace(); + com.yutou.bilibili.Tools.Log.e(e); } - oldBiliBiliHttpTime=System.currentTimeMillis(); + oldBiliBiliHttpTime = System.currentTimeMillis(); } HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); connection.setRequestMethod("POST"); @@ -290,13 +294,13 @@ public class LiveUtils { } public static HttpURLConnection getBiliHttpGet(String url, String cookie) throws IOException { - if(System.currentTimeMillis()-oldBiliBiliHttpTime<1000){ + if (System.currentTimeMillis() - oldBiliBiliHttpTime < 1000) { try { Thread.sleep(500); } catch (InterruptedException e) { - e.printStackTrace(); + com.yutou.bilibili.Tools.Log.e(e); } - oldBiliBiliHttpTime=System.currentTimeMillis(); + oldBiliBiliHttpTime = System.currentTimeMillis(); } HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); connection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"); @@ -340,34 +344,46 @@ public class LiveUtils { } return null; } - public static JSONObject getLiveInfo(int roomId){ + + private static JSONObject getLiveInfo(int roomId) { return http_get("https://api.live.bilibili.com/xlive/web-room/v1/index/getInfoByRoom?room_id=" + roomId); } - public static String getLiveTitle(int roomId){ - JSONObject json=getLiveInfo(roomId); - if(json!=null){ + + public static String getLiveTitle(int roomId) { + JSONObject json = LiveInfoManager.getInstance().getInfo(roomId); + if (json != null) { return json.getJSONObject("data").getJSONObject("room_info").getString("title"); } return null; } + public static boolean isLivePlayer(int roomId) { - JSONObject json =getLiveInfo(roomId); - if (json == null) + JSONObject json = LiveInfoManager.getInstance().getInfo(roomId); + if (json == null) { return false; + } BilibiliUpInfo upData = new BilibiliUpInfo(); upData.setRoomid(roomId); - checkLiveSave(roomId); - Log.i("直播检测:" + roomId + " > " + (json.getJSONObject("data").getJSONObject("room_info").getInteger("live_status") == 1) + " 录播器:" + liveContains(upData)); + //Log.i("直播检测:" + roomId + " > " + (json.getJSONObject("data").getJSONObject("room_info").getInteger("live_status") == 1) + " 录播器:" + liveContains(upData)); return json.getJSONObject("data").getJSONObject("room_info").getInteger("live_status") == 1; } private static void checkLiveSave(int roomId) { BilibiliUpInfo upData = new BilibiliUpInfo(); upData.setRoomid(roomId); - Live live=liveContains(upData); - if(live!=null){ - live.checkLive(); + Live live = liveContains(upData); + if (live == null) { + live = new Live(); + live.add(roomId, !StringUtils.isEmpty(AppTools.readFile(new File("cookies.json")))); + try { + live.start(); + } catch (Exception e) { + com.yutou.bilibili.Tools.Log.e(e); + } + } + if (!SaveLive.getInstance().checkLive(roomId)) { + SaveLive.getInstance().addLive(roomId); } } @@ -408,5 +424,45 @@ public class LiveUtils { } + public static class LiveInfoManager { + private static LiveInfoManager manager; + private Map infoMap; + + public static LiveInfoManager getInstance() { + if (manager == null) { + manager = new LiveInfoManager(); + } + return manager; + } + + private LiveInfoManager() { + infoMap = new HashMap<>(); + } + + public synchronized void check(List list) { + for (BilibiliUpInfo upInfo : list) { + JSONObject json = getLiveInfo(upInfo.getRoomid()); + if (json != null) { + infoMap.put(upInfo.getRoomid(), json); + } + } + } + + public synchronized void check(int roomId) { + JSONObject json = getLiveInfo(roomId); + if (json != null) { + Log.i("登记直播:"+roomId); + infoMap.put(roomId, json); + } + } + + public synchronized JSONObject getInfo(int roomId) { + if (!infoMap.containsKey(roomId)) { + //check(roomId); + return null; + } + return infoMap.get(roomId); + } + } } diff --git a/src/main/java/com/yutou/bilibili/BiliBili/Tools/SaveLive.java b/src/main/java/com/yutou/bilibili/BiliBili/Tools/SaveLive.java index 8054fdf..5269008 100644 --- a/src/main/java/com/yutou/bilibili/BiliBili/Tools/SaveLive.java +++ b/src/main/java/com/yutou/bilibili/BiliBili/Tools/SaveLive.java @@ -36,11 +36,12 @@ public class SaveLive { public void addLive(Live live) { addLive(live.geData().getRoomid()); } - public void startLive(int roomId){ - saveList.add(roomId + ""); + + public void startLive(int roomId) { start(roomId); - QQBotManager.getInstance().sendMessage(roomId+" 已启动录制"); + saveList.add(roomId + ""); } + public void addLive(int roomId) { if (saveList.contains(roomId + "")) { return; @@ -48,15 +49,15 @@ public class SaveLive { if (!LiveUtils.isLivePlayer(roomId)) { return; } - saveList.add(roomId + ""); start(roomId); - QQBotManager.getInstance().sendMessage(roomId+" 已启动录制"); + saveList.add(roomId + ""); } public boolean checkLive(int roomId) { return saveList.contains(roomId + ""); } - public File getLiveFile(int roomId){ + + public File getLiveFile(int roomId) { return downloads.get(roomId).liveFile; } @@ -75,10 +76,14 @@ public class SaveLive { heartbeats.get(roomId).cancel(); heartbeats.remove(roomId); } + LiveUtils.LiveInfoManager.getInstance().check(roomId); } private void start(int roomId) { + if (checkLive(roomId)) { + return; + } timer = System.currentTimeMillis(); DownloadThread thread = new DownloadThread(roomId); downloads.put(roomId, thread); @@ -118,6 +123,7 @@ public class SaveLive { String url = getLiveUrl(roomId); try { com.yutou.bilibili.Tools.Log.i("开始录制:" + roomId); + QQBotManager.getInstance().sendMessage(roomId + " 已启动录制"); HttpURLConnection connection = LiveUtils.getBiliHttpGet(url, LiveUtils.getCookie()); heartbeat = new Timer(); //Heartbeat beat = new Heartbeat(); @@ -146,7 +152,7 @@ public class SaveLive { inputStream.close(); com.yutou.bilibili.Tools.Log.i("录制完成:" + roomId + " save = " + isSave + " len = " + len); } catch (Exception e) { - e.printStackTrace(); + com.yutou.bilibili.Tools.Log.e(e); } SaveLive.this.stop(roomId); @@ -173,7 +179,7 @@ public class SaveLive { hearBeat = LiveUtils.http_post("https://api.live.bilibili.com/User/userOnlineHeart", HttpTools.toUrlParams(userHear)); com.yutou.bilibili.Tools.Log.i("[" + AppTools.getToDayNowTimeToString() + "]" + hearBeat); } catch (Exception e) { - e.printStackTrace(); + com.yutou.bilibili.Tools.Log.e(e); } } } diff --git a/src/main/java/com/yutou/bilibili/BilibiliApplication.java b/src/main/java/com/yutou/bilibili/BilibiliApplication.java index 639fd3f..1da145c 100644 --- a/src/main/java/com/yutou/bilibili/BilibiliApplication.java +++ b/src/main/java/com/yutou/bilibili/BilibiliApplication.java @@ -16,8 +16,8 @@ public class BilibiliApplication { public static String version="0.6"; public static void main(String[] args) { - SpringApplication.run(BilibiliApplication.class, args); QQBotManager.getInstance().init(); + SpringApplication.run(BilibiliApplication.class, args); } } diff --git a/src/main/java/com/yutou/bilibili/Controllers/SystemConfigController.java b/src/main/java/com/yutou/bilibili/Controllers/SystemConfigController.java index e86cf75..68a55bb 100644 --- a/src/main/java/com/yutou/bilibili/Controllers/SystemConfigController.java +++ b/src/main/java/com/yutou/bilibili/Controllers/SystemConfigController.java @@ -1,6 +1,7 @@ package com.yutou.bilibili.Controllers; import com.alibaba.fastjson.JSONObject; +import com.yutou.bilibili.QQBot.QQBotManager; import com.yutou.bilibili.Services.ISystemConfigService; import com.yutou.bilibili.Tools.Config; import org.springframework.stereotype.Controller; @@ -84,4 +85,13 @@ public class SystemConfigController { json.put("data",data); return json; } + @ResponseBody + @RequestMapping("/system/qq/login.do") + public JSONObject loginQQ(){ + QQBotManager.getInstance().init(); + JSONObject json=new JSONObject(); + json.put("code",0); + json.put("msg","ok"); + return json; + } } diff --git a/src/main/java/com/yutou/bilibili/QQBot/QQBotManager.java b/src/main/java/com/yutou/bilibili/QQBot/QQBotManager.java index ca1d9b7..6ae3b62 100644 --- a/src/main/java/com/yutou/bilibili/QQBot/QQBotManager.java +++ b/src/main/java/com/yutou/bilibili/QQBot/QQBotManager.java @@ -6,10 +6,7 @@ import com.yutou.bilibili.BiliBili.Live; import com.yutou.bilibili.BiliBili.LiveUtils; import com.yutou.bilibili.BiliBili.Tools.SaveLive; import com.yutou.bilibili.BilibiliApplication; -import com.yutou.bilibili.Tools.AppTools; -import com.yutou.bilibili.Tools.HttpTools; -import com.yutou.bilibili.Tools.Log; -import com.yutou.bilibili.Tools.Tools; +import com.yutou.bilibili.Tools.*; import com.yutou.bilibili.interfaces.DownloadInterface; import com.yutou.bilibili.mybatis.Bili.mybatis.model.BilibiliUpInfo; import net.mamoe.mirai.Bot; @@ -54,7 +51,7 @@ public class QQBotManager implements ApplicationContextAware { private final static String QQ_HELP = "!help"; private final static String QQ_GET_IP = "!ip"; private final static String QQ_GET_VERSION = "!version"; - private final static String QQ_LIVE_LIST = "!直播列表"; + private final static String QQ_LIVE_LIST = "!列表"; private final static String QQ_LIVE_SAVE = "!录播列表"; private final static String QQ_LIVE_TO_DAY_DATE = "!今日数据"; private final static String QQ_LIVE_DATE = "!数据"; @@ -63,31 +60,32 @@ public class QQBotManager implements ApplicationContextAware { private static QQBotManager botManager = null; private Bot bot; - private static final long qqGroup = 891655174L; + private static long qqGroup ; private boolean isLogin = false; private static boolean isInit = false; - private static final boolean debug = true; @Resource RealTimeDataController realTimeDataController; private QQBotManager() { - + qqGroup = Long.parseLong((String) ConfigTools.load(ConfigTools.CONFIG, "qq_group")); } public void init() { + if(!((boolean) ConfigTools.load(ConfigTools.CONFIG,"qq_bot"))){ + return; + } new Thread(new Runnable() { @Override public void run() { - long qq = 3620756944L; - String password = "UAs6YBYMyxJU"; - System.out.println("调用机器人"); + long qq = Long.parseLong((String) ConfigTools.load(ConfigTools.CONFIG, "qq_number")); + String password = (String) ConfigTools.load(ConfigTools.CONFIG, "qq_password"); bot = BotFactory.INSTANCE.newBot(qq, password, new BotConfiguration() { { setProtocol(MiraiProtocol.ANDROID_PAD); fileBasedDeviceInfo("qq_bot_devices_info.json"); - if (debug) { + if (!((boolean) ConfigTools.load(ConfigTools.CONFIG, "qq_debug"))) { noBotLog(); noNetworkLog(); } @@ -95,9 +93,9 @@ public class QQBotManager implements ApplicationContextAware { }); //Events.registerEvents(bot, new MessageListener()); GlobalEventChannel.INSTANCE.subscribeAlways(GroupMessageEvent.class, new MessageListener()); - System.out.println("准备登陆"); + Log.i("准备登陆"); bot.login(); - System.out.println("登陆成功"); + Log.i("登陆成功"); isLogin = true; isInit = true; new Thread(new Runnable() { @@ -106,7 +104,7 @@ public class QQBotManager implements ApplicationContextAware { try { Thread.sleep(1000); } catch (InterruptedException e) { - e.printStackTrace(); + com.yutou.bilibili.Tools.Log.e(e); } String str = sendMessage("姬妻酱上线拉~"); Log.i(str); @@ -148,7 +146,7 @@ public class QQBotManager implements ApplicationContextAware { try { return Objects.requireNonNull(bot.getGroup(qqGroup)).sendMessage(text).toString(); } catch (Exception e) { - e.printStackTrace(); + com.yutou.bilibili.Tools.Log.e(e); } } return getNotLoginQQ(); @@ -159,7 +157,7 @@ public class QQBotManager implements ApplicationContextAware { try { return Objects.requireNonNull(bot.getGroup(group)).sendMessage(text).toString(); } catch (Exception e) { - e.printStackTrace(); + com.yutou.bilibili.Tools.Log.e(e); } } return getNotLoginQQ(); @@ -184,7 +182,7 @@ public class QQBotManager implements ApplicationContextAware { return Objects.requireNonNull(bot.getGroup(qqGroup)).sendMessage(builder.asMessageChain()).toString(); } } catch (Exception e) { - e.printStackTrace(); + com.yutou.bilibili.Tools.Log.e(e); } return getNotLoginQQ(); @@ -225,10 +223,8 @@ public class QQBotManager implements ApplicationContextAware { @Override public void accept(GroupMessageEvent event) { String msg = event.getMessage().contentToString(); - switch (event.getGroup().getId() + "") { - case qqGroup + "": - myGroup(msg); - + if (qqGroup == event.getGroup().getId()) { + myGroup(msg); } } @@ -251,7 +247,7 @@ public class QQBotManager implements ApplicationContextAware { builder.append("当前正在记录数据的直播间:"); builder.append("\n"); for (Live live : Live.lives) { - JSONObject liveJson = LiveUtils.getLiveInfo(live.getInfo().getRoomid()); + JSONObject liveJson = LiveUtils.LiveInfoManager.getInstance().getInfo(live.getInfo().getRoomid()); builder .append("名字:").append(live.geData().getName()).append(" ") .append(" roomId:").append(live.geData().getRoomid()).append(" "); @@ -271,7 +267,7 @@ public class QQBotManager implements ApplicationContextAware { data.setRoomid(Integer.parseInt(roomId)); Live live = LiveUtils.liveContains(data); if (live != null) { - File file = SaveLive.getInstance().getLiveFile(_roomId); + File file = SaveLive.getInstance().getLiveFile(Integer.parseInt(roomId)); builder .append("名字:").append(live.geData().getName()).append(" ") .append("文件大小(字节):").append(file.length()).append(" ") @@ -287,13 +283,13 @@ public class QQBotManager implements ApplicationContextAware { field.setAccessible(true); builder.append(field.get(null)).append("\n"); } catch (IllegalAccessException e) { - e.printStackTrace(); + com.yutou.bilibili.Tools.Log.e(e); } } getInstance().sendMessage(builder.toString()); break; default: - if(msg.startsWith(QQCommands.QQ_LIVE_TO_DAY_DATE)){ + if (msg.startsWith(QQCommands.QQ_LIVE_TO_DAY_DATE)) { try { cmd = msg.split(" "); _roomId = Integer.parseInt(cmd[1]); @@ -302,7 +298,7 @@ public class QQBotManager implements ApplicationContextAware { } catch (Exception e) { getInstance().sendMessage("参数错误。\n使用方式: " + QQCommands.QQ_LIVE_TO_DAY_DATE + "+空格+roomId"); } - }else if(msg.startsWith(QQCommands.QQ_LIVE_DATE)){ + } else if (msg.startsWith(QQCommands.QQ_LIVE_DATE)) { try { cmd = msg.split(" "); _roomId = Integer.parseInt(cmd[1]); @@ -316,7 +312,7 @@ public class QQBotManager implements ApplicationContextAware { "\n时间格式:年-月-日_时:分:秒 例:2021-4-1_12:00:00" + "\n时分秒不可省略"); } - }else if(msg.startsWith(QQCommands.QQ_LIVE_USE_SAVE)){ + } else if (msg.startsWith(QQCommands.QQ_LIVE_USE_SAVE)) { try { cmd = msg.split(" "); _roomId = Integer.parseInt(cmd[1]); @@ -332,7 +328,7 @@ public class QQBotManager implements ApplicationContextAware { } } - public void sendGiftData(int roomId, Date startTime, Date endTime) throws ParseException { + public void sendGiftData(int roomId, Date startTime, Date endTime) { StringBuilder builder = new StringBuilder(); if (startTime == null) { startTime = AppTools.getToDayStartTime(); diff --git a/src/main/java/com/yutou/bilibili/Tools/APIFilter.java b/src/main/java/com/yutou/bilibili/Tools/APIFilter.java index 4a1f23c..7fd8794 100644 --- a/src/main/java/com/yutou/bilibili/Tools/APIFilter.java +++ b/src/main/java/com/yutou/bilibili/Tools/APIFilter.java @@ -55,7 +55,7 @@ public class APIFilter implements Filter { try { url = tmp.split(tmp.split("/")[tmp.split("/").length - 1])[0]; } catch (Exception e) { - e.printStackTrace(); + com.yutou.bilibili.Tools.Log.e(e); com.yutou.bilibili.Tools.Log.i("无权限请求:"+tmp); return; } diff --git a/src/main/java/com/yutou/bilibili/Tools/AppTools.java b/src/main/java/com/yutou/bilibili/Tools/AppTools.java index 006874e..9e731c8 100644 --- a/src/main/java/com/yutou/bilibili/Tools/AppTools.java +++ b/src/main/java/com/yutou/bilibili/Tools/AppTools.java @@ -26,7 +26,7 @@ public class AppTools { writer.close(); return true; } catch (IOException e) { - e.printStackTrace(); + com.yutou.bilibili.Tools.Log.e(e); return false; } } @@ -244,7 +244,7 @@ public class AppTools { digest.update(data.getBytes()); return DatatypeConverter.printHexBinary(digest.digest()); } catch (Exception e) { - e.printStackTrace(); + com.yutou.bilibili.Tools.Log.e(e); } return ""; } @@ -297,10 +297,10 @@ public class AppTools { in.close(); return true; } catch (FileNotFoundException e) { - e.printStackTrace(); + com.yutou.bilibili.Tools.Log.e(e); return false; } catch (IOException e) { - e.printStackTrace(); + com.yutou.bilibili.Tools.Log.e(e); return false; } } diff --git a/src/main/java/com/yutou/bilibili/Tools/ApplicationInit.java b/src/main/java/com/yutou/bilibili/Tools/ApplicationInit.java index 1931d90..493d386 100644 --- a/src/main/java/com/yutou/bilibili/Tools/ApplicationInit.java +++ b/src/main/java/com/yutou/bilibili/Tools/ApplicationInit.java @@ -41,7 +41,7 @@ public class ApplicationInit implements ApplicationRunner { live.add(data.getRoomid(), !StringUtils.isEmpty(AppTools.readFile(new File("cookies.json")))); live.start(); } catch (Exception e) { - e.printStackTrace(); + com.yutou.bilibili.Tools.Log.e(e); } } } @@ -96,6 +96,7 @@ public class ApplicationInit implements ApplicationRunner { saveData(time); break; } + LiveUtils.LiveInfoManager.getInstance().check(service.getUpInfo()); } private void checkLive() { @@ -110,7 +111,7 @@ public class ApplicationInit implements ApplicationRunner { live.start(); } catch (Exception e) { - e.printStackTrace(); + com.yutou.bilibili.Tools.Log.e(e); } } } diff --git a/src/main/java/com/yutou/bilibili/Tools/ConfigTools.java b/src/main/java/com/yutou/bilibili/Tools/ConfigTools.java new file mode 100644 index 0000000..4d8fca0 --- /dev/null +++ b/src/main/java/com/yutou/bilibili/Tools/ConfigTools.java @@ -0,0 +1,86 @@ +package com.yutou.bilibili.Tools; + +import com.alibaba.fastjson.JSONObject; + +import java.io.*; + +/** + * 配置和参数 + */ +public class ConfigTools { + public static final String CONFIG="config.json"; + public static final String DATA="data.json"; + public static final String SQLITE="sqlite.json"; + static { + try { + File file=new File(CONFIG); + if(!file.exists()){ + file.createNewFile(); + } + file=new File(DATA); + if(!file.exists()){ + file.createNewFile(); + } + file=null; + }catch (Exception e){ + com.yutou.bilibili.Tools.Log.e(e); + } + + } + public static Object load(String type,String key){ + File file=new File(type); + //com.yutou.nas.utils.Log.i(type+"配置文件地址:"+file.getAbsolutePath()); + String src=readFile(file); + if(src!=null){ + try { + JSONObject json=JSONObject.parseObject(src); + if(json==null){ + json=new JSONObject(); + saveFile(file,json.toJSONString()); + } + return json.getOrDefault(key, ""); + }catch (Exception e){ + return ""; + } + } + return ""; + } + public static boolean save(String type,String key,Object data){ + File file=new File(type); + String src=readFile(file); + if(src==null){ + src="{}"; + } + JSONObject json=JSONObject.parseObject(src); + json.put(key,data); + saveFile(file,json.toJSONString()); + return false; + } + public static boolean saveFile(File file,String data){ + try { + FileWriter writer=new FileWriter(file); + writer.write(data); + writer.flush(); + writer.close(); + return true; + } catch (IOException e) { + com.yutou.bilibili.Tools.Log.e(e); + return false; + } + } + public static String readFile(File file){ + try { + BufferedReader reader=new BufferedReader(new FileReader(file)); + String tmp; + StringBuilder str= new StringBuilder(); + while ((tmp=reader.readLine())!=null){ + str.append(tmp); + } + reader.close(); + return str.toString(); + } catch (Exception e) { + com.yutou.bilibili.Tools.Log.e(e); + } + return null; + } +} diff --git a/src/main/java/com/yutou/bilibili/Tools/ExcelUtils.java b/src/main/java/com/yutou/bilibili/Tools/ExcelUtils.java index 5c5b2ee..cce6ae6 100644 --- a/src/main/java/com/yutou/bilibili/Tools/ExcelUtils.java +++ b/src/main/java/com/yutou/bilibili/Tools/ExcelUtils.java @@ -117,7 +117,7 @@ public class ExcelUtils implements ApplicationContextAware { file.renameTo(new File("excel"+File.separator+fileName)); return file; } catch (Exception e) { - e.printStackTrace(); + com.yutou.bilibili.Tools.Log.e(e); } return null; diff --git a/src/main/java/com/yutou/bilibili/Tools/FFmpegUtils.java b/src/main/java/com/yutou/bilibili/Tools/FFmpegUtils.java index c381791..7febd92 100644 --- a/src/main/java/com/yutou/bilibili/Tools/FFmpegUtils.java +++ b/src/main/java/com/yutou/bilibili/Tools/FFmpegUtils.java @@ -54,7 +54,7 @@ public class FFmpegUtils { file.delete(); new File(out.getAbsolutePath()+File.separator+file.getName()).delete(); } catch (Exception e) { - e.printStackTrace(); + com.yutou.bilibili.Tools.Log.e(e); } nowFFmpegList.remove(file); } @@ -70,7 +70,7 @@ public class FFmpegUtils { try { Thread.sleep(300); } catch (InterruptedException e) { - e.printStackTrace(); + com.yutou.bilibili.Tools.Log.e(e); } } com.yutou.bilibili.Tools.Log.i("转码完成"); diff --git a/src/main/java/com/yutou/bilibili/Tools/HttpTools.java b/src/main/java/com/yutou/bilibili/Tools/HttpTools.java index bae889f..ef6f3cc 100644 --- a/src/main/java/com/yutou/bilibili/Tools/HttpTools.java +++ b/src/main/java/com/yutou/bilibili/Tools/HttpTools.java @@ -28,7 +28,7 @@ public class HttpTools { connection.disconnect(); return str.toString(); } catch (Exception e) { - e.printStackTrace(); + com.yutou.bilibili.Tools.Log.e(e); } return null; } @@ -69,7 +69,7 @@ public class HttpTools { try { networkInterface.httpGetData(str.toString(), connection.getResponseCode()); } catch (IOException e) { - e.printStackTrace(); + com.yutou.bilibili.Tools.Log.e(e); } networkInterface.onCookie(connection.getHeaderField("Set-Cookie")); } @@ -98,7 +98,7 @@ public class HttpTools { try { string.append("&").append(key).append("=").append(URLEncoder.encode(json.getString(key), "UTF-8")); } catch (Exception e) { - e.printStackTrace(); + com.yutou.bilibili.Tools.Log.e(e); try { string.append("&").append(URLEncoder.encode(key, "UTF-8")).append("="); // string += "&" + key + "="; diff --git a/src/main/java/com/yutou/bilibili/Tools/Log.java b/src/main/java/com/yutou/bilibili/Tools/Log.java index c838f49..3afa1ff 100644 --- a/src/main/java/com/yutou/bilibili/Tools/Log.java +++ b/src/main/java/com/yutou/bilibili/Tools/Log.java @@ -1,8 +1,8 @@ package com.yutou.bilibili.Tools; public class Log { - public static void i(Object log){ - if(true) + public static void i(Object log) { + if (!((boolean) ConfigTools.load(ConfigTools.CONFIG, "logout"))) return; System.out.printf("[%s]%s%n", AppTools.getToDayNowTimeToString(), @@ -10,9 +10,10 @@ public class Log { ); } - public static void e(Exception e){ - if(true) + + public static void e(Exception e) { + if (!((boolean) ConfigTools.load(ConfigTools.CONFIG, "logout"))) return; - e.printStackTrace(); + com.yutou.bilibili.Tools.Log.e(e); } } diff --git a/src/main/java/com/yutou/bilibili/Tools/ServiceTools.java b/src/main/java/com/yutou/bilibili/Tools/ServiceTools.java index a087d79..ea2d9cf 100644 --- a/src/main/java/com/yutou/bilibili/Tools/ServiceTools.java +++ b/src/main/java/com/yutou/bilibili/Tools/ServiceTools.java @@ -36,7 +36,7 @@ public class ServiceTools implements ApplicationContextAware { try { url = tmp.split(tmp.split("/")[tmp.split("/").length - 1])[0]; } catch (Exception e) { - e.printStackTrace(); + com.yutou.bilibili.Tools.Log.e(e); com.yutou.bilibili.Tools.Log.i("无权限请求:" + tmp); return false; } diff --git a/src/main/java/com/yutou/bilibili/Tools/TestMain.java b/src/main/java/com/yutou/bilibili/Tools/TestMain.java index d7cd2aa..c4077a9 100644 --- a/src/main/java/com/yutou/bilibili/Tools/TestMain.java +++ b/src/main/java/com/yutou/bilibili/Tools/TestMain.java @@ -48,7 +48,7 @@ public class TestMain { com.yutou.bilibili.Tools.Log.i("["+AppTools.getToDayNowTimeToString()+"]"+hearBeat); } catch (Exception e) { - e.printStackTrace(); + com.yutou.bilibili.Tools.Log.e(e); } } public static int byteArrayToInt(byte[] bytes) { diff --git a/src/main/java/com/yutou/bilibili/Tools/Tools.java b/src/main/java/com/yutou/bilibili/Tools/Tools.java index 16eb299..0531d3d 100644 --- a/src/main/java/com/yutou/bilibili/Tools/Tools.java +++ b/src/main/java/com/yutou/bilibili/Tools/Tools.java @@ -111,7 +111,7 @@ public class Tools { inputStream.close(); downloadInterface.onDownload(file); } catch (IOException e) { - e.printStackTrace(); + com.yutou.bilibili.Tools.Log.e(e); downloadInterface.onError(e); } } @@ -153,7 +153,7 @@ public class Tools { } in.close(); } catch (Exception e) { - e.printStackTrace(); + com.yutou.bilibili.Tools.Log.e(e); return null; } return bytesToHexString(digest.digest()); diff --git a/src/main/java/com/yutou/bilibili/sqlite/BiliBiliLiveDatabasesManager.java b/src/main/java/com/yutou/bilibili/sqlite/BiliBiliLiveDatabasesManager.java index 7bb22a8..6aa0e1f 100644 --- a/src/main/java/com/yutou/bilibili/sqlite/BiliBiliLiveDatabasesManager.java +++ b/src/main/java/com/yutou/bilibili/sqlite/BiliBiliLiveDatabasesManager.java @@ -51,7 +51,7 @@ public class BiliBiliLiveDatabasesManager extends SQLiteManager { statement.execute(sql); statement.closeOnCompletion(); }catch (Exception e){ - e.printStackTrace(); + com.yutou.bilibili.Tools.Log.e(e); } } @@ -83,7 +83,7 @@ public class BiliBiliLiveDatabasesManager extends SQLiteManager { set.close(); statement.closeOnCompletion();; }catch (Exception e){ - e.printStackTrace(); + com.yutou.bilibili.Tools.Log.e(e); } return list; } diff --git a/src/main/java/com/yutou/bilibili/sqlite/SQLiteManager.java b/src/main/java/com/yutou/bilibili/sqlite/SQLiteManager.java index 0b50668..e718300 100644 --- a/src/main/java/com/yutou/bilibili/sqlite/SQLiteManager.java +++ b/src/main/java/com/yutou/bilibili/sqlite/SQLiteManager.java @@ -19,7 +19,7 @@ public class SQLiteManager { try { conn.setAutoCommit(false); } catch (SQLException e) { - e.printStackTrace(); + com.yutou.bilibili.Tools.Log.e(e); } } @@ -27,7 +27,7 @@ public class SQLiteManager { try { conn.setAutoCommit(true); } catch (SQLException e) { - e.printStackTrace(); + com.yutou.bilibili.Tools.Log.e(e); } } public void close(){ @@ -42,7 +42,7 @@ public class SQLiteManager { try { conn.commit(); } catch (SQLException e) { - e.printStackTrace(); + com.yutou.bilibili.Tools.Log.e(e); } } @@ -55,7 +55,7 @@ public class SQLiteManager { sql.delete(); conn = DriverManager.getConnection(url + sql.getAbsolutePath()); }catch (Exception e){ - e.printStackTrace(); + com.yutou.bilibili.Tools.Log.e(e); } startBatch(); JSONArray array=json.getJSONArray("table"); @@ -102,7 +102,7 @@ public class SQLiteManager { statement.execute(sql.toString().replace(",);", ");")); statement.closeOnCompletion(); } catch (Exception e) { - e.printStackTrace(); + com.yutou.bilibili.Tools.Log.e(e); } } @@ -116,7 +116,7 @@ public class SQLiteManager { conn = DriverManager.getConnection(url + sql.getAbsolutePath()); } } catch (Exception e) { - e.printStackTrace(); + com.yutou.bilibili.Tools.Log.e(e); } } @@ -132,7 +132,7 @@ public class SQLiteManager { return true; } } catch (Exception e) { - e.printStackTrace(); + com.yutou.bilibili.Tools.Log.e(e); } return false; } diff --git a/src/main/resources/mappers/BilibiliLiveDataDao.xml b/src/main/resources/mappers/BilibiliLiveDataDao.xml index 44d06dd..43ea14c 100644 --- a/src/main/resources/mappers/BilibiliLiveDataDao.xml +++ b/src/main/resources/mappers/BilibiliLiveDataDao.xml @@ -355,7 +355,7 @@ ;