From 9de83f27aa49ff663ea390c42d7ddfd3f15f8bee Mon Sep 17 00:00:00 2001 From: Yutousama <583819556@qq.com> Date: Mon, 5 Apr 2021 17:40:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=BC=B9=E5=B9=95=E4=BF=9D?= =?UTF-8?q?=E5=AD=98=20=E5=AE=8C=E5=96=84FFmpeg=E8=BD=AC=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/BiliUserController.java | 2 +- .../Controllers/BiliVideoController.java | 8 + .../bilibili/BiliBili/Datas/DanmuData.java | 25 + .../com/yutou/bilibili/BiliBili/Live.java | 57 +- .../yutou/bilibili/BiliBili/LiveUtils.java | 45 +- .../com/yutou/bilibili/Tools/FFmpegUtils.java | 7 +- .../sqlite/BiliBiliLiveDatabasesManager.java | 529 ++---------------- .../yutou/bilibili/sqlite/SQLiteManager.java | 7 + 8 files changed, 166 insertions(+), 514 deletions(-) create mode 100644 src/main/java/com/yutou/bilibili/BiliBili/Datas/DanmuData.java diff --git a/src/main/java/com/yutou/bilibili/BiliBili/Controllers/BiliUserController.java b/src/main/java/com/yutou/bilibili/BiliBili/Controllers/BiliUserController.java index ca71417..c31c033 100644 --- a/src/main/java/com/yutou/bilibili/BiliBili/Controllers/BiliUserController.java +++ b/src/main/java/com/yutou/bilibili/BiliBili/Controllers/BiliUserController.java @@ -76,7 +76,7 @@ public class BiliUserController { cancel(); } } - },0,1000); + },0,3000); } } diff --git a/src/main/java/com/yutou/bilibili/BiliBili/Controllers/BiliVideoController.java b/src/main/java/com/yutou/bilibili/BiliBili/Controllers/BiliVideoController.java index b916546..10ecddb 100644 --- a/src/main/java/com/yutou/bilibili/BiliBili/Controllers/BiliVideoController.java +++ b/src/main/java/com/yutou/bilibili/BiliBili/Controllers/BiliVideoController.java @@ -16,6 +16,7 @@ import com.yutou.bilibili.mybatis.model.UUser; import org.springframework.core.io.FileSystemResource; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; +import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @@ -122,11 +123,18 @@ public class BiliVideoController { json.put("msg","ok"); String time = fileName.split(" ")[0].substring(1); File file = new File("live" + File.separator + time + File.separator + fileName); + System.out.println("file "+file.getAbsolutePath()+" "+file.exists()); + String ffmpeg=configService.getConfig(Config.SYSTEM_VIDEO_FFMPEG); + if(StringUtils.isEmpty(ffmpeg)||!new File(ffmpeg).exists()){ + json.put("msg","FFmpeg未设置,请联系管理员"); + return json; + } int i= FFmpegUtils.add(configService.getConfig(Config.SYSTEM_VIDEO_FFMPEG),file,new File("ffmpeg_out"+File.separator+time)); json.put("model",i); switch (i){ case 0: json.put("msg","已添加到后台转码"); + break; case 1: json.put("msg","已经转码,无须二次转码"); break; diff --git a/src/main/java/com/yutou/bilibili/BiliBili/Datas/DanmuData.java b/src/main/java/com/yutou/bilibili/BiliBili/Datas/DanmuData.java new file mode 100644 index 0000000..0295685 --- /dev/null +++ b/src/main/java/com/yutou/bilibili/BiliBili/Datas/DanmuData.java @@ -0,0 +1,25 @@ +package com.yutou.bilibili.BiliBili.Datas; + +import lombok.Data; + +import java.util.Date; + +@Data +public class DanmuData { + private int id; + private int model;//1~3 滚动弹幕 4 底端弹幕 5 顶端弹幕 6 逆向弹幕 7 精准定位 8 高级弹幕 + private int fontSize; + private int fontColor; + private long time; + private String uCode; + private String danmu; + private int uid; + private String uname; + + public Date getTimeDate(){ + return new Date(time); + } + public String getFontColorHex(){ + return Integer.toHexString(fontColor); + } +} diff --git a/src/main/java/com/yutou/bilibili/BiliBili/Live.java b/src/main/java/com/yutou/bilibili/BiliBili/Live.java index 11eb790..3692643 100644 --- a/src/main/java/com/yutou/bilibili/BiliBili/Live.java +++ b/src/main/java/com/yutou/bilibili/BiliBili/Live.java @@ -1,11 +1,14 @@ package com.yutou.bilibili.BiliBili; +import com.alibaba.fastjson.JSONArray; +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.Tools.AppTools; +import com.yutou.bilibili.Tools.Log; import com.yutou.bilibili.mybatis.Bili.mybatis.model.BilibiliLiveData; import com.yutou.bilibili.mybatis.Bili.mybatis.model.BilibiliLiveInfo; import com.yutou.bilibili.mybatis.Bili.mybatis.model.BilibiliUpInfo; @@ -39,11 +42,10 @@ public class Live implements ApplicationContextAware { private boolean run; private int userId; private WebSocketClient client; - private int popular; private BilibiliLiveInfo info; private BilibiliUpInfo upData; private Timer heartBeattimer; - private String danmuFileName; + public BiliBiliLiveDatabasesManager danmuManager=null; @PostConstruct public void init() { @@ -69,7 +71,6 @@ public class Live implements ApplicationContextAware { public void add(int roomId, boolean isLogin) { this.roomId = roomId; this.isLogin = isLogin; - this.danmuFileName = "[" + AppTools.getToDayTime() + "]_" + roomId + "_" + System.currentTimeMillis(); upData = new BilibiliUpInfo(); upData.setRoomid(roomId); info.setRoomid(roomId); @@ -85,6 +86,15 @@ public class Live implements ApplicationContextAware { try { upData = service.queryUp(upData); upData.setLive(1); + if (upData.getSavedanmu() == 1) { + danmuManager = new BiliBiliLiveDatabasesManager(); + danmuManager.init("[" + AppTools.getToDayTime() + "]" + upData.getRoomid()); + } else { + if (danmuManager != null) { + danmuManager.close(); + danmuManager = null; + } + } } catch (Exception e) { e.printStackTrace(); } @@ -103,7 +113,7 @@ public class Live implements ApplicationContextAware { upData.setLive(1); } String url = LiveUtils.getLiveUrl(roomId); - if(url==null){ + if (url == null) { stop(); return; } @@ -190,11 +200,14 @@ public class Live implements ApplicationContextAware { public void stop() { run = false; client.close(); + if(danmuManager!=null){ + danmuManager.close(); + } if (SaveLive.getInstance().checkLive(roomId)) { SaveLive.getInstance().stop(roomId); } Live.lives.remove(this); - com.yutou.bilibili.Tools.Log.i("退出"+roomId+"直播间"); + com.yutou.bilibili.Tools.Log.i("退出" + roomId + "直播间"); } /** @@ -258,7 +271,7 @@ public class Live implements ApplicationContextAware { JSONObject json = JSONObject.parseObject(new String(bytes, StandardCharsets.UTF_8)); com.yutou.bilibili.Tools.Log.i(json.toJSONString()); } catch (Exception e) { - popular = LiveUtils.bytesToInt2(bytes, 0); + int popular = LiveUtils.bytesToInt2(bytes, 0); info.setPopular(popular); } } @@ -271,7 +284,7 @@ public class Live implements ApplicationContextAware { } } - private void checkLive() { + public void checkLive() { boolean isLive = LiveUtils.isLivePlayer(roomId); upData.setLive(isLive ? 1 : 0); if (isLive && upData.getSavelive() == 1) { @@ -279,7 +292,6 @@ public class Live implements ApplicationContextAware { } } - public BiliBiliLiveDatabasesManager danmuManager; /** * 处理数据 @@ -307,21 +319,24 @@ public class Live implements ApplicationContextAware { service.addLiveData(liveData); break; case "DANMU_MSG"://普通弹幕 - danmu = json.getJSONArray("info").getJSONArray(2).getString(1) + ":" + json.getJSONArray("info").getString(1); - liveData.setUid(json.getJSONArray("info").getJSONArray(2).getInteger(0)); - liveData.setType(LiveData.DANMU_MSG); - liveData.setMsg(danmu); - liveData.setRoomid(roomId); - liveData.setSubtime(new Date()); + 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) { - // service.addLiveData(liveData); - /* if(danmuManager==null){ - danmuManager=BiliBiliLiveDatabasesManager.getInstance(); - danmuManager.init(danmuFileName); - } - danmuManager.addLiveData(liveData);*/ + Log.i(danmuData.toString()); + if (danmuManager != null) + danmuManager.addDanmu(danmuData); } - // com.yutou.bilibili.Tools.Log.i(json.toJSONString()); + // com.yutou.bilibili.Tools.Log.i(json.toJSONString()); break; case "SEND_GIFT"://送礼 data = json.getJSONObject("data"); diff --git a/src/main/java/com/yutou/bilibili/BiliBili/LiveUtils.java b/src/main/java/com/yutou/bilibili/BiliBili/LiveUtils.java index 2d80b39..db87438 100644 --- a/src/main/java/com/yutou/bilibili/BiliBili/LiveUtils.java +++ b/src/main/java/com/yutou/bilibili/BiliBili/LiveUtils.java @@ -175,9 +175,17 @@ public class LiveUtils { ,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){ + try { + Thread.sleep(500); + } catch (InterruptedException e) { + e.printStackTrace(); + } + oldBiliBiliHttpTime=System.currentTimeMillis(); + } HttpURLConnection connection = getBiliHttpGet(url, getCookie()); BufferedInputStream stream = new BufferedInputStream(connection.getInputStream()); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); @@ -210,6 +218,14 @@ public class LiveUtils { public static JSONObject http_post(String url, String body) { try { + if(System.currentTimeMillis()-oldBiliBiliHttpTime<1000){ + try { + Thread.sleep(500); + } catch (InterruptedException e) { + e.printStackTrace(); + } + oldBiliBiliHttpTime=System.currentTimeMillis(); + } HttpURLConnection connection = getBiliHttpPost(url, getCookie()); OutputStream connectionOutputStream = null; if (!StringUtils.isEmpty(body)) { @@ -250,6 +266,14 @@ public class LiveUtils { } public static HttpURLConnection getBiliHttpPost(String url, String cookie) throws Exception { + if(System.currentTimeMillis()-oldBiliBiliHttpTime<1000){ + try { + Thread.sleep(500); + } catch (InterruptedException e) { + e.printStackTrace(); + } + oldBiliBiliHttpTime=System.currentTimeMillis(); + } HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); connection.setRequestMethod("POST"); connection.setDoOutput(true); @@ -266,6 +290,14 @@ public class LiveUtils { } public static HttpURLConnection getBiliHttpGet(String url, String cookie) throws IOException { + if(System.currentTimeMillis()-oldBiliBiliHttpTime<1000){ + try { + Thread.sleep(500); + } catch (InterruptedException e) { + e.printStackTrace(); + } + 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"); connection.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8"); @@ -315,10 +347,21 @@ public class LiveUtils { 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)); 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(); + } + + } + public static class LiveGiftConfig { @Resource IBiliBiliLiveService service; diff --git a/src/main/java/com/yutou/bilibili/Tools/FFmpegUtils.java b/src/main/java/com/yutou/bilibili/Tools/FFmpegUtils.java index b697e6a..c381791 100644 --- a/src/main/java/com/yutou/bilibili/Tools/FFmpegUtils.java +++ b/src/main/java/com/yutou/bilibili/Tools/FFmpegUtils.java @@ -12,8 +12,7 @@ public class FFmpegUtils { if (nowFFmpegList.contains(file)) { return 2; } - File tmp = new File(file.getAbsolutePath().replace(".mp4", "_ffmpeg.mp4")); - if (tmp.exists()) { + if (file.getName().contains("ffmpeg")) { return 1; } else { startFFmpeg(ffmpeg, file, out); @@ -52,8 +51,8 @@ public class FFmpegUtils { } inputStream.close(); AppTools.copyFileToName(out.getAbsolutePath()+File.separator+file.getName(), file.getParent() + File.separator, file.getName().replace(".mp4", "_ffmpeg.mp4"), true); - // file.delete(); - out.delete(); + file.delete(); + new File(out.getAbsolutePath()+File.separator+file.getName()).delete(); } catch (Exception e) { e.printStackTrace(); } diff --git a/src/main/java/com/yutou/bilibili/sqlite/BiliBiliLiveDatabasesManager.java b/src/main/java/com/yutou/bilibili/sqlite/BiliBiliLiveDatabasesManager.java index 2d2f638..7bb22a8 100644 --- a/src/main/java/com/yutou/bilibili/sqlite/BiliBiliLiveDatabasesManager.java +++ b/src/main/java/com/yutou/bilibili/sqlite/BiliBiliLiveDatabasesManager.java @@ -1,11 +1,13 @@ package com.yutou.bilibili.sqlite; import com.yutou.bilibili.BiliBili.Datas.BiliBiliUpData; +import com.yutou.bilibili.BiliBili.Datas.DanmuData; import com.yutou.bilibili.BiliBili.Datas.LiveData; import com.yutou.bilibili.BiliBili.Datas.LiveInfo; import com.alibaba.fastjson.JSONObject; import com.yutou.bilibili.BiliBili.LiveUtils; import com.yutou.bilibili.Tools.AppTools; +import com.yutou.bilibili.Tools.Log; import com.yutou.bilibili.mybatis.Bili.mybatis.model.BilibiliLiveData; import java.sql.ResultSet; @@ -15,522 +17,75 @@ import java.text.SimpleDateFormat; import java.util.*; public class BiliBiliLiveDatabasesManager extends SQLiteManager { - private static BiliBiliLiveDatabasesManager manager; - private Statement statement; - public static BiliBiliLiveDatabasesManager getInstance() { - if (manager == null) { - manager = new BiliBiliLiveDatabasesManager(); - } - return manager; - } - private BiliBiliLiveDatabasesManager() { + public BiliBiliLiveDatabasesManager() { super(); } public void init(String fileName){ - JSONObject json = JSONObject.parseObject("{\"file\":\"bilibili.db\",\"table\":[{\"name\":\"up_info\",\"item\":[{\"name\":\"id\",\"type\":\"int\",\"isNull\":false,\"isKey\":true},{\"name\":\"name\",\"type\":\"String\",\"isNull\":false,\"isKey\":false},{\"name\":\"url\",\"type\":\"String\",\"isNull\":false,\"isKey\":false},{\"name\":\"roomid\",\"type\":\"int\",\"isNull\":false,\"isKey\":false},{\"name\":\"offlineListening\",\"type\":\"int\",\"isNull\":false,\"isKey\":false},{\"name\":\"saveDanmu\",\"type\":\"int\",\"isNull\":false,\"isKey\":false},{\"name\":\"enable\",\"type\":\"int\",\"isNull\":false,\"isKey\":false}]},{\"name\":\"live_data\",\"item\":[{\"name\":\"id\",\"type\":\"int\",\"isNull\":false,\"isKey\":true},{\"name\":\"uid\",\"type\":\"int\",\"isNull\":false,\"isKey\":false},{\"name\":\"roomid\",\"type\":\"int\",\"isNull\":false,\"isKey\":false},{\"name\":\"type\",\"type\":\"String\",\"isNull\":false,\"isKey\":false},{\"name\":\"msg\",\"type\":\"String\",\"isNull\":true,\"isKey\":false},{\"name\":\"giftId\",\"type\":\"int\",\"isNull\":true,\"isKey\":false},{\"name\":\"giftName\",\"type\":\"String\",\"isNull\":true,\"isKey\":false},{\"name\":\"giftIndex\",\"type\":\"int\",\"isNull\":true,\"isKey\":false},{\"name\":\"price\",\"type\":\"int\",\"isNull\":true,\"isKey\":false},{\"name\":\"priceOfcommission\",\"type\":\"int\",\"isNull\":true,\"isKey\":false},{\"name\":\"subtime\",\"type\":\"TIME\",\"isNull\":true,\"isKey\":false}]},{\"name\":\"live_info\",\"item\":[{\"name\":\"id\",\"type\":\"int\",\"isNull\":false,\"isKey\":true},{\"name\":\"roomid\",\"type\":\"int\",\"isNull\":false,\"isKey\":false},{\"name\":\"popular\",\"type\":\"int\",\"isNull\":true,\"isKey\":false},{\"name\":\"userIndex\",\"type\":\"int\",\"isNull\":true,\"isKey\":false},{\"name\":\"vipUserIndex\",\"type\":\"int\",\"isNull\":true,\"isKey\":false},{\"name\":\"giftUser\",\"type\":\"int\",\"isNull\":true,\"isKey\":false},{\"name\":\"subtime\",\"type\":\"TIME\",\"isNull\":false,\"isKey\":false}]}]}"); + JSONObject json = JSONObject.parseObject("{\"file\":\"bilibili.db\",\"table\":[{\"name\":\"danmu\",\"item\":[{\"name\":\"id\",\"type\":\"int\",\"isNull\":false,\"isKey\":true},{\"name\":\"model\",\"type\":\"int\",\"isNull\":false,\"isKey\":false},{\"name\":\"fontSize\",\"type\":\"int\",\"isNull\":false,\"isKey\":false},{\"name\":\"fontColor\",\"type\":\"int\",\"isNull\":false,\"isKey\":false},{\"name\":\"time\",\"type\":\"int\",\"isNull\":false,\"isKey\":false},{\"name\":\"uCode\",\"type\":\"String\",\"isNull\":false,\"isKey\":false},{\"name\":\"danmu\",\"type\":\"String\",\"isNull\":false,\"isKey\":false},{\"name\":\"uid\",\"type\":\"int\",\"isNull\":false,\"isKey\":false},{\"name\":\"uname\",\"type\":\"String\",\"isNull\":false,\"isKey\":false}]}]}"); json.put("file",fileName+".db"); build(json); } /** - * 获取所有up信息 - * @return up列表 + * 添加弹幕 + * @param data 弹幕参数 */ - public List getUpInfo() { - return getUpInfo("select * from `up_info`;"); - } - - /** - * 根据RoomId查询UP信息 - * @param data roomId - * @return UP信息 - */ - public BiliBiliUpData queryUp(BiliBiliUpData data) { - String sql = "select * from `up_info` where `roomid`=" + data.getRoomId() + ";"; - List list = getUpInfo(sql); - if (list.isEmpty()) { - return null; - } else { - return list.get(0); - } - } - - /** - * 查询UP信息 - * @param sql sql - * @return 列表 - */ - private List getUpInfo(String sql) { - List list = new ArrayList<>(); - try { + public void addDanmu(DanmuData data){ + try{ Statement statement = conn.createStatement(); - ResultSet set = statement.executeQuery(sql); - while (set.next()) { - BiliBiliUpData upData = new BiliBiliUpData(); - upData.setId(set.getInt("id")); - upData.setName(set.getString("name")); - upData.setUrl(set.getString("url")); - upData.setRoomId(set.getInt("roomid")); - upData.setOfflineListening(set.getInt("offlineListening")); - upData.setEnable(set.getInt("enable")); - upData.setSaveDanmu(set.getInt("saveDanmu")); - list.add(upData); - } + String sql=String.format("insert into `danmu` (`model`,`fontSize`,`fontColor`,`time`,`uCode`,`danmu`,`uid`,`uname`) " + + "values (%d,%d,%d,%d,'%s','%s',%d,'%s')" + ,data.getModel() + ,data.getFontSize() + ,data.getFontColor() + ,data.getTime() + ,data.getUCode() + ,data.getDanmu() + ,data.getUid() + ,data.getUname() + ); + Log.i(sql); + statement.execute(sql); statement.closeOnCompletion(); - } catch (SQLException e) { + }catch (Exception e){ e.printStackTrace(); } - return list; - } + } /** - * 获取所有直播数据 - * @return 直播数据列表 + * 获取弹幕 + * @param startTime 弹幕范围开始时间 + * @param endTime 弹幕范围结束时间 + * @return 弹幕列表 */ - public List getLiveData() { - return getLiveData("select * from `live_data`;"); - } - - /** - * 获取直播数据 - * @param sql sql - * @return 直播数据 - */ - private List getLiveData(String sql) { - List list = new ArrayList<>(); + public List queryDanmu(Date startTime,Date endTime){ + List list=new ArrayList<>(); try { + String sql=String.format("select * from `danmu` where time >%d and time <%d",startTime.getTime(),endTime.getTime()); Statement statement = conn.createStatement(); ResultSet set = statement.executeQuery(sql); - while (set.next()) { - LiveData data = new LiveData(); + while (set.next()){ + DanmuData data=new DanmuData(); data.setId(set.getInt("id")); - data.setRoomId(set.getInt("roomid")); + data.setModel(set.getInt("model")); + data.setFontSize(set.getInt("fontSize")); + data.setFontColor(set.getInt("fontColor")); + data.setTime(set.getInt("time")); + data.setUCode(set.getString("uCode")); + data.setDanmu(set.getString("danmu")); data.setUid(set.getInt("uid")); - data.setType(set.getString("type")); - data.setMsg(set.getString("msg")); - data.setGiftName(set.getString("giftName")); - data.setGiftIndex(set.getInt("giftIndex")); - data.setPrice(set.getInt("price")); - data.setPriceOfCommission(set.getInt("priceOfcommission")); - data.setSubTime(new Date(Long.parseLong(set.getString("subtime")))); - data.setGiftId(set.getInt("giftId")); + data.setUname(set.getString("uname")); list.add(data); } - statement.closeOnCompletion(); - } catch (SQLException e) { + set.close(); + statement.closeOnCompletion();; + }catch (Exception e){ e.printStackTrace(); } return list; } - /** - * 获取所有直播统计 - * @return 直播统计列表 - */ - public List getLiveInfo() { - return getLiveInfo("select * from `live_info`;"); - } - - /** - * 获取直播统计 - * @param sql sql - * @return 直播统计列表 - */ - private List getLiveInfo(String sql) { - List list = new ArrayList<>(); - try { - Statement statement = conn.createStatement(); - ResultSet set = statement.executeQuery(sql); - while (set.next()) { - LiveInfo data = new LiveInfo(); - data.setId(set.getInt("id")); - data.setRoomId(set.getInt("roomid")); - data.setPopular(set.getInt("popular")); - data.setUserIndex(set.getInt("userIndex")); - data.setVipUserIndex(set.getInt("vipUserIndex")); - data.setSubTime(new Date(Long.parseLong(set.getString("subtime")))); - list.add(data); - } - statement.closeOnCompletion(); - } catch (SQLException e) { - e.printStackTrace(); - } - return list; - } - - /** - * 添加up信息 - * @param upData up信息 - * @return 是否成功 - */ - public boolean addUpInfo(BiliBiliUpData upData) { - if (queryUp(upData) != null) { - return false; - } - try { - Statement statement = conn.createStatement(); - String sql = "insert into `up_info` (`name`,`url`,`roomid`,`offlineListening`,`enable`,`saveDanmu`)" + - " values ('" - + upData.getName() - + "','" + upData.getUrl() - + "'," + upData.getRoomId() - + " ," + (upData.isOfflineListening() ? 1 : 0) - + ", " + (upData.isEnable() ? 1 : 0) - + ", " + (upData.isSaveDanmu() ? 1 : 0) + - ");"; - com.yutou.bilibili.Tools.Log.i(sql); - statement.execute(sql); - statement.closeOnCompletion(); - return true; - } catch (Exception e) { - e.printStackTrace(); - } - return false; - } - - /** - * 更新UP信息 - * @param upData up信息 - * @return 是否成功 - */ - public boolean updateUpInfo(BiliBiliUpData upData) { - try { - Statement statement = conn.createStatement(); - String sql = "update `up_info` set `name`='" + upData.getName() + "'" + - ",`url`='" + upData.getUrl() + "'" + - ", `roomid` = " + upData.getRoomId() + "" + - ",`offlineListening` = " + (upData.isOfflineListening() ? 1 : 0) + "" + - ", `enable` = " + (upData.isEnable() ? 1 : 0) - + " where `id` = " + upData.getId(); - statement.execute(sql); - statement.closeOnCompletion(); - return true; - } catch (Exception e) { - e.printStackTrace(); - } - return false; - } - - /** - * 删除指定up信息 - * @param upData roomId - * @return 是否成功 - */ - public boolean deleteUp(BiliBiliUpData upData) { - try { - Statement statement = conn.createStatement(); - String sql = "DELETE from `up_info` where `roomid`=" + upData.getRoomId(); - statement.execute(sql); - statement.closeOnCompletion(); - return true; - } catch (Exception e) { - e.printStackTrace(); - } - return false; - } - - /** - * 新增直播数据 - * @param data 直播数据 - * @return 是否成功 - */ - public boolean addLiveData(BilibiliLiveData data) { - String sql = "insert into `live_data` (`type`,`msg`,`giftName`,`giftIndex`,`price`,`priceOfcommission`,`subtime`,`giftId`,`roomid`,`uid`) values (" + - "'" + data.getType() + "'" + - ",'" + data.getMsg() + "'" + - ",'" + data.getGiftname() + "'" + - "," + data.getGiftindex() + "" + - "," + data.getPrice() + "" + - "," + data.getPriceofcommission() + "" + - "," + data.getSubtime().getTime() + "" + - "," + data.getGiftid() + - "," + data.getRoomid() + - "," + data.getUid() + - ")"; - try { - Statement statement = conn.createStatement(); - statement.execute(sql); - statement.closeOnCompletion(); - return true; - } catch (Exception e) { - e.printStackTrace(); - com.yutou.bilibili.Tools.Log.i(sql); - } - return false; - } - - /** - * 添加直播统计 - * @param data 直播统计 - * @return 是否成功 - */ - public boolean addLiveInfo(LiveInfo data) { - try { - Statement statement = conn.createStatement(); - String sql = "insert into `live_info` (`popular`,`userIndex`,`vipUserIndex`,`subtime`,`roomid`) values (" + - "" + data.getPopular() + "" + - "," + data.getUserIndex() + "" + - "," + data.getVipUserIndex() + "" + - "," + data.getSubTime().getTime() + - "," + data.getRoomId() + - ")"; - statement.execute(sql); - statement.closeOnCompletion(); - return true; - } catch (Exception e) { - e.printStackTrace(); - } - return false; - } - - /** - * 查询指定时段直播数据 - * @param roomId roomId - * @param startTime 开始时间,为空表示为当天00:00:00 - * @param endTime 结束时间,为空表示为当时 - * @param type 类型 - * @return 列表 - */ - public List queryLiveData(int roomId, Date startTime, Date endTime, String[] type) { - String typeSql = ""; - for (String t : type) { - typeSql += "or `type` ='" + t + "' "; - } - typeSql = typeSql.substring(2); - String sql = String.format("select * from `live_data` where `subtime`>=%d and `subtime`<=%d and `roomid`=%d and (" + typeSql + ")" - , startTime.getTime(), endTime.getTime(), roomId); - return getLiveData(sql); - } - - /** - * 查询指定时段直播某个礼物总数 - * @param roomId roomId - * @param giftId 礼物id,-1则为全部 - * @param startTime 开始时间 - * @param endTime 结束时间 - * @return 数量 - */ - public int queryGiftSize(int roomId, int giftId, Date startTime, Date endTime) { - try { - if (startTime == null || endTime == null) { - startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(AppTools.getToDayTime() + " " + "00:00"); - endTime = new Date(); - } - String sql; - if(giftId!=-1) { - sql = String.format("select sum(data.giftIndex) as size from `live_data` as data where data.giftId =%d and data.roomid=%d and data.subtime >=%d and data.subtime <%d;" - , giftId, roomId, startTime.getTime(), endTime.getTime()); - }else{ - sql = String.format("select sum(data.giftIndex) as size from `live_data` as data where data.roomid=%d and data.subtime >=%d and data.subtime <%d;" - , roomId, startTime.getTime(), endTime.getTime()); - } - Statement statement = conn.createStatement(); - ResultSet set = statement.executeQuery(sql); - if (set.next()) { - statement.closeOnCompletion(); - return set.getInt("size"); - } - statement.closeOnCompletion(); - return 0; - } catch (Exception e) { - e.printStackTrace(); - } - return -1; - } - - /** - * 返回指定时段各个礼物总数 - * @param roomId roomId - * @param startTime 开始时间 - * @param endTime 结束时间 - * @return {giftName:size} - */ - public Map queryGiftSize(int roomId, Date startTime, Date endTime) { - Map map = new HashMap<>(); - try { - if (startTime == null || endTime == null) { - startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(AppTools.getToDayTime() + " " + "00:00"); - endTime = new Date(); - } - String sql = String.format("select giftName,sum(giftIndex) as size from `live_data` where subtime >=%d and subtime <=%d and roomid=%d group by giftName;" - , startTime.getTime(), endTime.getTime(), roomId); - Statement statement = conn.createStatement(); - ResultSet set = statement.executeQuery(sql); - while (set.next()) { - map.put(set.getString("giftName"), set.getInt("size")); - } - statement.closeOnCompletion(); - } catch (Exception e) { - e.printStackTrace(); - } - return map; - } - - /** - * 查询指定时段收益情况 - * @param roomId roomId - * @param startTime 开始时间 - * @param endTime 结束时间 - * @return 未扣除收成比例的金瓜子数量 - */ - public int queryPriceSize(int roomId, Date startTime, Date endTime) { - try { - if (startTime == null || endTime == null) { - startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(AppTools.getToDayTime() + " " + "00:00"); - endTime = new Date(); - } - String sql = String.format("select sum(data.price) as size from `live_data` as data where data.subtime >=%d and data.subtime <=%d and data.roomid=%d;" - , startTime.getTime(), endTime.getTime(), roomId); - Statement statement = conn.createStatement(); - ResultSet set = statement.executeQuery(sql); - if (set.next()) { - statement.closeOnCompletion(); - return set.getInt("size"); - } - statement.closeOnCompletion(); - return 0; - } catch (Exception e) { - e.printStackTrace(); - } - return -1; - } - - /** - * 根据礼物id查询礼物信息,该方法主要用于阿B接口未收录的礼物信息 - * @param giftId 礼物id - * @return 直播信息 - */ - public LiveData queryGiftOfId(int giftId) { - String sql = String.format("select * from `live_data` where `giftId` = %d;", giftId); - List list = getLiveData(sql); - if (list.isEmpty()) { - return null; - } - return list.get(0); - } - - /** - * 查询指定时段去重送礼人数 - * - * @param upData up信息 - * @param startTime 开始时间 - * @param endTime 结束时间 - * @param type 礼物类型 - * @return 人数 - */ - public int queryGiftUserToDistinct(BiliBiliUpData upData, Date startTime, Date endTime, String[] type) { - try { - if (startTime == null || endTime == null) { - startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(AppTools.getToDayTime() + " " + "00:00"); - endTime = new Date(); - } - String typeSql = ""; - for (String t : type) { - typeSql += "or `type` ='" + t + "' "; - } - typeSql = typeSql.substring(2); - String sql = String.format("select count(distinct uid) as size from `live_data` where subtime>=%d and subtime <%d and roomid=%d and(%s);" - , startTime.getTime(), endTime.getTime(), upData.getRoomId(), typeSql); - return queryGiftCount(sql); - } catch (Exception e) { - e.printStackTrace(); - } - return 0; - } - - /** - * 查询礼物数量 - * @param sql sql - * @return 礼物数量 - */ - private int queryGiftCount(String sql) { - int count = 0; - try { - - Statement statement = conn.createStatement(); - ResultSet set = statement.executeQuery(sql); - if (set.next()) { - count = set.getInt("size"); - } - statement.closeOnCompletion(); - } catch (Exception e) { - e.printStackTrace(); - } - return count; - } - - /** - * 查询时段内人气数量 - * @param upData up信息 - * @param startTime 开始时间 - * @param endTime 结束时间 - * @return 人气 - */ - public int queryPopularCount(BiliBiliUpData upData,Date startTime, Date endTime){ - int count = 0; - try{ - if (startTime == null || endTime == null) { - startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(AppTools.getToDayTime() + " " + "00:00"); - endTime = new Date(); - } - String sql=String.format("select sum(data.popular) as size from `live_info` as data where data.subtime >=%d and data.subtime <%d and data.roomid=%d;" - ,startTime.getTime(),endTime.getTime(),upData.getRoomId()); - Statement statement = conn.createStatement(); - ResultSet set = statement.executeQuery(sql); - if (set.next()) { - count = set.getInt("size"); - } - statement.closeOnCompletion(); - }catch (Exception e){ - e.printStackTrace(); - } - return count; - } - - /** - * 查询时段内进场人数 - * @param upData up信息 - * @param startTime 开始时间 - * @param endTime 结束时间 - * @return 人数 - */ - public int queryUserCount(BiliBiliUpData upData,Date startTime, Date endTime){ - int count=0; - try{ - if (startTime == null || endTime == null) { - startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(AppTools.getToDayTime() + " " + "00:00"); - endTime = new Date(); - } - String sql=String.format("select sum(userIndex+vipUserIndex) as size from `live_info` where `subtime`>=%d and `subtime`<=%d and `roomid`=%d;" - ,startTime.getTime(),endTime.getTime(),upData.getRoomId()); - Statement statement = conn.createStatement(); - ResultSet set = statement.executeQuery(sql); - if (set.next()) { - count = set.getInt("size"); - } - statement.closeOnCompletion(); - }catch (Exception e){ - e.printStackTrace(); - } - return count; - } - public Map queryUserGiftCount(BiliBiliUpData upData,Date startTime,Date endTime){ - Map map=new HashMap<>(); - try{ - if (startTime == null || endTime == null) { - startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(AppTools.getToDayTime() + " " + "00:00"); - endTime = new Date(); - } - String sql=String.format("select * from `live_info` where subtime >=%d and subtime <%d and roomid=%d;" - ,startTime.getTime() - ,endTime.getTime() - ,upData.getRoomId()); - }catch (Exception e){ - e.printStackTrace(); - } - return map; - } } diff --git a/src/main/java/com/yutou/bilibili/sqlite/SQLiteManager.java b/src/main/java/com/yutou/bilibili/sqlite/SQLiteManager.java index c096370..0b50668 100644 --- a/src/main/java/com/yutou/bilibili/sqlite/SQLiteManager.java +++ b/src/main/java/com/yutou/bilibili/sqlite/SQLiteManager.java @@ -30,6 +30,13 @@ public class SQLiteManager { e.printStackTrace(); } } + public void close(){ + try { + conn.close(); + } catch (SQLException throwables) { + throwables.printStackTrace(); + } + } public void commit() { try {