完善机器人功能

使用缓存来存储直播信息,防止频繁请求导致412问题
This commit is contained in:
Yutousama 2021-04-08 22:03:47 +08:00
parent ab6bfbfd42
commit 9b7633e8a9
23 changed files with 305 additions and 129 deletions

View File

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

View File

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

View File

@ -38,6 +38,7 @@ public class Live implements ApplicationContextAware {
public static List<Live> lives = new ArrayList<>();
public static List<String> 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":

View File

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

View File

@ -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<Integer, JSONObject> infoMap;
public static LiveInfoManager getInstance() {
if (manager == null) {
manager = new LiveInfoManager();
}
return manager;
}
private LiveInfoManager() {
infoMap = new HashMap<>();
}
public synchronized void check(List<BilibiliUpInfo> 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);
}
}
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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("转码完成");

View File

@ -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 + "=";

View File

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

View File

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

View File

@ -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) {

View File

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

View File

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

View File

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

View File

@ -355,7 +355,7 @@
;
</select>
<select id="queryPriceTimeGroup" resultType="map">
select sum(price)as price,group_concat(distinct DATE_FORMAT(subtime,'%d/%H')) as `time` from DDOB_db.bilibili_live_data
select sum(price)as price,group_concat(distinct DATE_FORMAT(subtime,'%d/%H')) as `time` from `bilibili_live_data`
<where>
<if test="roomid!=-1">
roomid=#{roomid,jdbcType=INTEGER}
@ -370,7 +370,7 @@
group by subtime-subtime%(2.8*60*60);
</select>
<select id="queryGiftTimeGroup" resultType="map">
select sum(giftIndex) as price,group_concat(distinct giftName) as giftName ,group_concat(distinct DATE_FORMAT(subtime,'%d/%H')) as `time` from DDOB_db.bilibili_live_data
select sum(giftIndex) as price,group_concat(distinct giftName) as giftName ,group_concat(distinct DATE_FORMAT(subtime,'%d/%H')) as `time` from `bilibili_live_data`
<where>
<if test="roomid!=-1">
roomid=#{roomid,jdbcType=INTEGER}