新版改版:
模块化各个功能 改为okhttp网络请求 改为sqlite作为存储 预设QQ机器人API
This commit is contained in:
@@ -1,82 +0,0 @@
|
||||
package com.yutou.bilibili.BiliBili.Controllers;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.yutou.bilibili.BiliBili.LiveUtils;
|
||||
import com.yutou.bilibili.Tools.AppTools;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Map;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/bili/")
|
||||
public class BiliUserController {
|
||||
@ResponseBody
|
||||
@RequestMapping("/login/get/test.do")
|
||||
public JSONObject testLogin(){
|
||||
JSONObject json=new JSONObject();
|
||||
if(StringUtils.isEmpty(AppTools.readFile(new File("cookies.json")))){
|
||||
json.put("code",-1);
|
||||
json.put("msg","未登录");
|
||||
}else{
|
||||
JSONObject login= LiveUtils.getUserLoginInfo();
|
||||
JSONObject data=new JSONObject();
|
||||
json.put("code",login.getInteger("code"));
|
||||
data.put("uname",login.getJSONObject("data").getString("uname"));
|
||||
data.put("icon",login.getJSONObject("data").getString("face"));
|
||||
json.put("data",data);
|
||||
}
|
||||
return json;
|
||||
}
|
||||
@ResponseBody
|
||||
@RequestMapping("/login/set/login.do")
|
||||
public JSONObject login(){
|
||||
JSONObject login=LiveUtils.http_get("https://passport.bilibili.com/qrcode/getLoginUrl");
|
||||
JSONObject json=new JSONObject();
|
||||
json.put("code",login.getInteger("code"));
|
||||
json.put("url",login.getJSONObject("data").getString("url"));
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
waitLogin(login.getJSONObject("data").getString("oauthKey"));
|
||||
}
|
||||
}).start();
|
||||
return json;
|
||||
}
|
||||
public void waitLogin(String oauthKey){
|
||||
long time=System.currentTimeMillis();
|
||||
String bd="gourl=https%3A%2F%2Fpassport.bilibili.com%2Fajax%2FminiLogin%2Fredirect&oauthKey="+oauthKey;
|
||||
new Timer().schedule(new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
if((System.currentTimeMillis()-time)>5*60*1000){
|
||||
cancel();
|
||||
return;
|
||||
}
|
||||
JSONObject json=LiveUtils.http_post("https://passport.bilibili.com/qrcode/getLoginInfo",bd);
|
||||
if(json.containsKey("code")&&json.getInteger("code")==0){
|
||||
String _url=json.getJSONObject("data").getString("url");
|
||||
Map<String,String> map=AppTools.getUrlParams(_url);
|
||||
JSONObject tmp=LiveUtils.http_post(_url,"");
|
||||
JSONObject cookie=new JSONObject();
|
||||
for (String key : map.keySet()) {
|
||||
cookie.put(key,map.get(key));
|
||||
}
|
||||
String sid=tmp.getString("cookie");
|
||||
sid=sid.split("sid=")[1];
|
||||
sid=sid.split(";")[0];
|
||||
cookie.put("sid",sid);
|
||||
cookie.put("Domain",".bilibili.com");
|
||||
AppTools.saveFile(new File("cookies.json"),cookie.toJSONString());
|
||||
cancel();
|
||||
}
|
||||
}
|
||||
},0,3000);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,149 +0,0 @@
|
||||
package com.yutou.bilibili.BiliBili.Controllers;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.yutou.bilibili.BiliBili.LiveUtils;
|
||||
import com.yutou.bilibili.BiliBili.Services.IBiliBiliLiveService;
|
||||
import com.yutou.bilibili.BiliBili.Tools.BiliTools;
|
||||
import com.yutou.bilibili.Services.ISystemConfigService;
|
||||
import com.yutou.bilibili.Services.IUserService;
|
||||
import com.yutou.bilibili.Tools.*;
|
||||
import com.yutou.bilibili.mybatis.Bili.mybatis.model.BilibiliUpInfo;
|
||||
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;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.File;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLDecoder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@Controller
|
||||
public class BiliVideoController {
|
||||
@Resource
|
||||
IUserService service;
|
||||
@Resource
|
||||
IBiliBiliLiveService liveService;
|
||||
@Resource
|
||||
ISystemConfigService configService;
|
||||
|
||||
@ResponseBody
|
||||
@RequestMapping("/bili/video/get/list.do")
|
||||
public JSONObject videoList(HttpServletRequest request) {
|
||||
JSONObject json = new JSONObject();
|
||||
String token = AppTools.getLoginToken(request);
|
||||
UUser user = service.getUserToToken(token);
|
||||
if (user == null) {
|
||||
json.put("code", -1);
|
||||
json.put("msg", "未登录");
|
||||
json.put("data", new JSONObject());
|
||||
return json;
|
||||
}
|
||||
if (ServiceTools.getInstance().auth(request, user.getUser(), "/bili/video/get/")) {
|
||||
File file = new File(ConfigTools.load(ConfigTools.CONFIG,"liveSavePath",String.class));
|
||||
Log.i(file.getAbsolutePath());
|
||||
if (!file.exists() || Objects.requireNonNull(file.listFiles()).length == 0) {
|
||||
json.put("code", 0);
|
||||
json.put("msg", "文件夹为空");
|
||||
json.put("data", new JSONObject());
|
||||
} else {
|
||||
List<File> list = AppTools.scanFilePath(file.getAbsolutePath()+File.separator+"live");
|
||||
JSONArray array = new JSONArray();
|
||||
for (File f : list) {
|
||||
if(!f.getName().endsWith(".mp4")){
|
||||
continue;
|
||||
}
|
||||
JSONObject item = new JSONObject();
|
||||
String _time = f.getName().split(" ")[0].substring(1);
|
||||
File mpeg = new File("ffmpeg_out" + File.separator + _time + File.separator + f.getName());
|
||||
if (mpeg.exists()) {
|
||||
f = mpeg;
|
||||
item.put("ffmpeg", true);
|
||||
} else {
|
||||
item.put("ffmpeg", false);
|
||||
}
|
||||
String date = f.getName().split(" ")[0].substring(1);
|
||||
String time = f.getName().split(" ")[1].split("\\]")[0];
|
||||
String roomid = f.getName().split("\\]")[1].replace(".mp4", "").replace("_ffmpeg", "").trim();
|
||||
|
||||
BilibiliUpInfo info = liveService.queryUpToRoomId(Integer.parseInt(roomid));
|
||||
long size = f.length();
|
||||
String _size;
|
||||
if (size < 1024) {
|
||||
_size = size + "B";
|
||||
} else if (size < 1048576) {
|
||||
_size = (size / 1024) + "KB";
|
||||
} else if (size < 1073741824) {
|
||||
_size = (size / 1048576) + "MB";
|
||||
} else {
|
||||
_size = (size / 1073741824) + "GB";
|
||||
}
|
||||
|
||||
item.put("fileName", f.getName());
|
||||
item.put("fileSize", _size);
|
||||
item.put("date", date);
|
||||
item.put("time", time);
|
||||
item.put("name", info == null ? roomid : info.getName());
|
||||
array.add(item);
|
||||
}
|
||||
json.put("code", 0);
|
||||
json.put("msg", "ok");
|
||||
json.put("data", array);
|
||||
}
|
||||
} else {
|
||||
json.put("code", -2);
|
||||
json.put("msg", "未授权");
|
||||
json.put("data", new JSONObject());
|
||||
}
|
||||
return json;
|
||||
}
|
||||
|
||||
@RequestMapping("/bili/video/download/get.do")
|
||||
public ResponseEntity<FileSystemResource> download(String fileName) {
|
||||
String time = fileName.split(" ")[0].substring(1);
|
||||
File file = new File("ffmpeg_out" + File.separator + time + File.separator + fileName);
|
||||
if (!file.exists()) {
|
||||
file = new File(ConfigTools.load(ConfigTools.CONFIG,"liveSavePath",String.class)+File.separator+"live" + File.separator + time + File.separator + fileName);
|
||||
}
|
||||
Log.i(file.getAbsolutePath());
|
||||
return AppTools.getFile(file);
|
||||
}
|
||||
@ResponseBody
|
||||
@RequestMapping("/bili/video/set/ffmpeg.do")
|
||||
public JSONObject ffmpegOut(String fileName) throws UnsupportedEncodingException {
|
||||
fileName= URLDecoder.decode(fileName,"UTF-8");
|
||||
JSONObject json=new JSONObject();
|
||||
json.put("code",0);
|
||||
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;
|
||||
case 2:
|
||||
json.put("msg","正在转码");
|
||||
break;
|
||||
}
|
||||
return json;
|
||||
}
|
||||
}
|
||||
@@ -1,107 +0,0 @@
|
||||
package com.yutou.bilibili.BiliBili.Controllers;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.yutou.bilibili.BiliBili.Datas.LiveData;
|
||||
import com.yutou.bilibili.BiliBili.Services.IBiliBiliLiveService;
|
||||
import com.yutou.bilibili.Tools.AppTools;
|
||||
import com.yutou.bilibili.Tools.ExcelUtils;
|
||||
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;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.File;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Controller
|
||||
public class OverviewController {
|
||||
@Resource
|
||||
IBiliBiliLiveService manager;
|
||||
|
||||
@RequestMapping("/overview/get/query.do")
|
||||
@ResponseBody
|
||||
public JSONObject queryOverview(Date startTime, Date endTime, BilibiliUpInfo upData) {
|
||||
JSONObject json = new JSONObject();
|
||||
JSONObject data = new JSONObject();
|
||||
//BiliBiliLiveDatabasesManager manager = BiliBiliLiveDatabasesManager.getInstance();
|
||||
data.put("giftCount", manager.queryGiftSize(upData.getRoomid(), -1, startTime, endTime));
|
||||
data.put("giftUserCount", manager.queryGiftUserToDistinct(upData, startTime, endTime, new String[]{
|
||||
LiveData.SEND_GIFT
|
||||
, LiveData.COMBO_SEND
|
||||
, LiveData.GUARD_BUY}));
|
||||
data.put("price", manager.queryPriceSize(upData.getRoomid(), startTime, endTime));
|
||||
long h = (endTime.getTime() - startTime.getTime()) % (1000 * 24 * 60 * 60) / (1000 * 60 * 60);
|
||||
com.yutou.bilibili.Tools.Log.i("相差:" + h + "小时");
|
||||
try {
|
||||
data.put("hprice", data.getInteger("price") / h);
|
||||
} catch (Exception e) {
|
||||
data.put("hprice", 0);
|
||||
}
|
||||
|
||||
|
||||
JSONArray array=new JSONArray();
|
||||
List<BilibiliLiveInfo> infos=manager.queryLiveInfo(upData,startTime,endTime);
|
||||
int maxUser=0;
|
||||
for (BilibiliLiveInfo info : infos) {
|
||||
com.yutou.bilibili.Tools.Log.i(info);
|
||||
maxUser+=info.getUserindex()+info.getVipuserindex();
|
||||
JSONObject item=new JSONObject();
|
||||
String nowTime = new SimpleDateFormat("HH").format(info.getSubtime());
|
||||
item.put("time",nowTime);
|
||||
item.put("popular",info.getPopular());
|
||||
item.put("user",info.getUserindex()+info.getVipuserindex());
|
||||
array.add(item);
|
||||
}
|
||||
try {
|
||||
Map<String, Integer> map = manager.queryUserGiftCount(upData, startTime, endTime);
|
||||
int huserGift = 0;
|
||||
for (String s : map.keySet()) {
|
||||
huserGift += map.get(s);
|
||||
}
|
||||
data.put("userGift", huserGift / maxUser);
|
||||
} catch (Exception e) {
|
||||
data.put("userGift", 0);
|
||||
}
|
||||
data.put("map",array);
|
||||
json.put("data",data);
|
||||
json.put("code",0);
|
||||
return json;
|
||||
}
|
||||
|
||||
@ResponseBody
|
||||
@RequestMapping("/overview/get/down.do")
|
||||
public JSONObject downExcel(Date startTime, Date endTime, BilibiliUpInfo upData){
|
||||
final String fileName=System.currentTimeMillis()+".xlsx";
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
ExcelUtils.getInstance(upData.getRoomid(),startTime,endTime,fileName);
|
||||
}
|
||||
}).start();
|
||||
JSONObject json=new JSONObject();
|
||||
json.put("code",0);
|
||||
json.put("data",fileName);
|
||||
return json;
|
||||
|
||||
}
|
||||
|
||||
@RequestMapping("/overview/get/download.do")
|
||||
public ResponseEntity<FileSystemResource> downloadExcel(HttpServletRequest request, String fname){
|
||||
File file=new File("excel"+File.separator+fname);
|
||||
if(file.exists()) {
|
||||
return AppTools.getFile(file);
|
||||
}else{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,120 +0,0 @@
|
||||
package com.yutou.bilibili.BiliBili.Controllers;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.yutou.bilibili.BiliBili.Datas.LiveData;
|
||||
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.BilibiliLiveData;
|
||||
import com.yutou.bilibili.mybatis.Bili.mybatis.model.BilibiliLiveInfo;
|
||||
import com.yutou.bilibili.mybatis.Bili.mybatis.model.BilibiliUpInfo;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.math.BigDecimal;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/realTimeData/")
|
||||
public class RealTimeDataController {
|
||||
@Resource
|
||||
IBiliBiliLiveService service;
|
||||
@ResponseBody
|
||||
@RequestMapping("get/query.do")
|
||||
public JSONObject queryToDayLiveData(int roomid,Date startTime,Date endTime){
|
||||
JSONObject json = new JSONObject();
|
||||
BilibiliUpInfo info = new BilibiliUpInfo();
|
||||
info.setRoomid(roomid);
|
||||
Live live = LiveUtils.liveContains(info);
|
||||
if(startTime==null){
|
||||
startTime=AppTools.getToDayStartTime();
|
||||
}
|
||||
if(endTime==null){
|
||||
endTime=AppTools.getToDayNowTime();
|
||||
}
|
||||
int userCount = service.queryLiveData(info.getRoomid(), startTime, endTime, new String[]{LiveData.INTERACT_WORD}).size();
|
||||
int vipCount = service.queryLiveData(info.getRoomid(), startTime, endTime, new String[]{LiveData.ENTRY_EFFECT}).size();
|
||||
JSONObject data = new JSONObject();
|
||||
data.put("roomid", roomid);
|
||||
data.put("popular",live==null?0:live.getInfo().getPopular());
|
||||
data.put("userLength", userCount);
|
||||
data.put("vipLength", vipCount);
|
||||
|
||||
JSONArray price = new JSONArray();
|
||||
//改写这个sql可以按时段统计数据
|
||||
List<Map<String, BigDecimal>> ps=service.queryTimeGroup(roomid, startTime, endTime);
|
||||
for (Map<String, BigDecimal> map : ps) {
|
||||
JSONObject pr = new JSONObject();
|
||||
pr.put("price", map.containsKey("price") ? map.get("price") : 0);
|
||||
pr.put("time", map.get("time"));
|
||||
price.add(pr);
|
||||
}
|
||||
|
||||
JSONArray giftArray = new JSONArray();
|
||||
Map<String, Integer> map = service.queryGiftSize(roomid,startTime,endTime);
|
||||
for (String key : map.keySet()) {
|
||||
JSONObject item = new JSONObject();
|
||||
if(key==null|| "null".equals(key)) {
|
||||
continue;
|
||||
}
|
||||
item.put("giftName",key);
|
||||
item.put("size",map.get(key));
|
||||
giftArray.add(item);
|
||||
}
|
||||
data.put("price", price);
|
||||
data.put("gift", giftArray);
|
||||
json.put("data", data);
|
||||
json.put("code",0);
|
||||
return json;
|
||||
}
|
||||
@ResponseBody
|
||||
@RequestMapping("data.do")
|
||||
public JSONObject queryRoomData(int rid,int model){
|
||||
Date startTime = AppTools.getToDayStartTime();
|
||||
Date endTime=AppTools.getToDayNowTime();
|
||||
List<BilibiliLiveData> giftList = service.queryLiveData(rid, startTime, endTime, new String[]{
|
||||
LiveData.SEND_GIFT
|
||||
, LiveData.COMBO_SEND
|
||||
, LiveData.GUARD_BUY
|
||||
});
|
||||
List<BilibiliLiveData> userList = service.queryLiveData(rid, startTime, endTime, new String[]{
|
||||
LiveData.INTERACT_WORD
|
||||
, LiveData.ENTRY_EFFECT
|
||||
});
|
||||
JSONObject json=new JSONObject();
|
||||
JSONArray retData=new JSONArray();
|
||||
if(model==1) {
|
||||
for (BilibiliLiveData data : giftList) {
|
||||
JSONObject item = new JSONObject();
|
||||
item.put("uid", data.getUid());
|
||||
item.put("msg", data.getMsg());
|
||||
json.put("size", data.getGiftindex());
|
||||
json.put("money", data.getPrice());
|
||||
item.put("time", AppTools.getToDayTimeToString(data.getSubtime()));
|
||||
retData.add(item);
|
||||
}
|
||||
}else if(model==2) {
|
||||
for (BilibiliLiveData data : userList) {
|
||||
JSONObject item = new JSONObject();
|
||||
item.put("uid", data.getUid());
|
||||
item.put("msg", data.getMsg());
|
||||
item.put("time", AppTools.getToDayTimeToString(data.getSubtime()));
|
||||
retData.add(item);
|
||||
}
|
||||
}
|
||||
json.put("code",0);
|
||||
json.put("count",retData.size());
|
||||
json.put("data",retData);
|
||||
json.put("msg","");
|
||||
return json;
|
||||
}
|
||||
}
|
||||
@@ -1,163 +0,0 @@
|
||||
package com.yutou.bilibili.BiliBili.Controllers;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.yutou.bilibili.BiliBili.Live;
|
||||
import com.yutou.bilibili.BiliBili.LiveController;
|
||||
import com.yutou.bilibili.BiliBili.LiveUtils;
|
||||
import com.yutou.bilibili.BiliBili.Services.IBiliBiliLiveService;
|
||||
import com.yutou.bilibili.BiliBili.Tools.BiliTools;
|
||||
import com.yutou.bilibili.Services.IUserService;
|
||||
import com.yutou.bilibili.Tools.AppTools;
|
||||
import com.yutou.bilibili.mybatis.Bili.mybatis.model.BilibiliUpInfo;
|
||||
import com.yutou.bilibili.mybatis.model.UUser;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.List;
|
||||
|
||||
@Controller
|
||||
public class UpInfoController {
|
||||
@Resource
|
||||
IBiliBiliLiveService service;
|
||||
@Resource
|
||||
IUserService userService;
|
||||
|
||||
@RequestMapping("/upinfo/get/list.do")
|
||||
@ResponseBody
|
||||
public JSONObject upInfoList(HttpServletRequest request) {
|
||||
String token = AppTools.getLoginToken(request);
|
||||
UUser user = userService.getUserToToken(token);
|
||||
JSONObject json = new JSONObject();
|
||||
if (user == null) {
|
||||
json.put("code", -1);
|
||||
json.put("msg", "未登录");
|
||||
json.put("data", "[]");
|
||||
return json;
|
||||
}
|
||||
List<BilibiliUpInfo> upDataList = service.getUpInfo(user.getId(), user.getPower());
|
||||
com.yutou.bilibili.Tools.Log.i("UP用户数:" + upDataList.size());
|
||||
json.put("code", 0);
|
||||
json.put("data", JSONArray.toJSON(upDataList));
|
||||
return json;
|
||||
}
|
||||
|
||||
@ResponseBody
|
||||
@RequestMapping("/upinfo/set/add.do")
|
||||
public JSONObject addUpInfo(BilibiliUpInfo upData) {
|
||||
JSONObject json = new JSONObject();
|
||||
if (BiliTools.checkout(upData) != null) {
|
||||
upData.setEnable(0);
|
||||
upData = BiliTools.checkout(upData);
|
||||
com.yutou.bilibili.Tools.Log.i(upData);
|
||||
boolean flag = service.addUpInfo(upData);
|
||||
if (flag) {
|
||||
json.put("code", 0);
|
||||
json.put("msg", "添加成功");
|
||||
} else {
|
||||
json.put("code", -1);
|
||||
json.put("msg", "添加失败,该UP已经存在");
|
||||
}
|
||||
} else {
|
||||
json.put("code", -2);
|
||||
json.put("msg", "添加失败,可能参数有问题或该UP已经存在");
|
||||
}
|
||||
return json;
|
||||
}
|
||||
|
||||
@ResponseBody
|
||||
@RequestMapping("/upinfo/update.do")
|
||||
public JSONObject updateV1(BilibiliUpInfo upData) {
|
||||
return update(upData);
|
||||
}
|
||||
@ResponseBody
|
||||
@RequestMapping("/upinfo/set/update.do")
|
||||
public JSONObject update(BilibiliUpInfo upData) {
|
||||
JSONObject json = new JSONObject();
|
||||
com.yutou.bilibili.Tools.Log.i("old data =" + upData);
|
||||
BilibiliUpInfo old = service.queryUp(upData);
|
||||
if (!old.getUrl().equals(upData.getUrl())) {
|
||||
upData = BiliTools.checkout(upData);
|
||||
if (upData != null) {
|
||||
upData.setEnable(0);
|
||||
upData.setSavedanmu(0);
|
||||
upData.setOfflinelistening(0);
|
||||
}
|
||||
}
|
||||
com.yutou.bilibili.Tools.Log.i("save data = " + upData);
|
||||
boolean flag = service.updateUpInfo(upData);
|
||||
if (flag) {
|
||||
LiveController.getInstance().updateUpInfo(upData);
|
||||
json.put("code", 0);
|
||||
json.put("msg", "更新成功");
|
||||
} else {
|
||||
json.put("code", -1);
|
||||
json.put("msg", "更新失败,可能参数有问题");
|
||||
}
|
||||
return json;
|
||||
}
|
||||
|
||||
@ResponseBody
|
||||
@RequestMapping("/upinfo/set/delete.do")
|
||||
public JSONObject delete(BilibiliUpInfo upData) {
|
||||
JSONObject json = new JSONObject();
|
||||
com.yutou.bilibili.Tools.Log.i(upData);
|
||||
boolean flag = service.deleteUp(upData);
|
||||
if (flag) {
|
||||
json.put("code", 0);
|
||||
json.put("msg", "删除成功");
|
||||
} else {
|
||||
json.put("code", -1);
|
||||
json.put("msg", "删除失败,可能参数有问题");
|
||||
}
|
||||
return json;
|
||||
}
|
||||
|
||||
@ResponseBody
|
||||
@RequestMapping("/upinfo/get/query.do")
|
||||
public JSONObject query(BilibiliUpInfo upData) {
|
||||
upData = service.queryUp(upData);
|
||||
return (JSONObject) JSON.toJSON(upData);
|
||||
}
|
||||
|
||||
@ResponseBody
|
||||
@RequestMapping("/upinfo/get/liveInfo.do")
|
||||
public JSONObject liveInfo() {
|
||||
List<BilibiliUpInfo> list = service.getUpInfo();
|
||||
JSONObject json = new JSONObject();
|
||||
JSONObject data = new JSONObject();
|
||||
JSONArray info = new JSONArray();
|
||||
JSONArray liveArray = new JSONArray();
|
||||
int online = 0;
|
||||
JSONObject infoByUid = LiveUtils.getStatusInfoByUid(list);
|
||||
JSONObject infoData = infoByUid.getJSONObject("data");
|
||||
for (BilibiliUpInfo up : list) {
|
||||
JSONObject user = infoData.getJSONObject(up.getMid() + "");
|
||||
if(user==null){
|
||||
continue;
|
||||
}
|
||||
up.setLive(user.getInteger("live_status"));
|
||||
if (up.checkLiveStatus() == 1) {
|
||||
online++;
|
||||
}
|
||||
JSONObject item= (JSONObject) JSON.toJSON(up);
|
||||
item.put("listen",Live.lives.get(up.getRoomid())!=null);
|
||||
info.add(JSON.toJSON(item));
|
||||
}
|
||||
for (Live live : Live.lives.values()) {
|
||||
if (live.geData().getLive() == 1) {
|
||||
liveArray.add(JSON.toJSON(live.geData()));
|
||||
}
|
||||
}
|
||||
data.put("online", online);
|
||||
data.put("info", info);
|
||||
data.put("live", liveArray);
|
||||
json.put("code", 0);
|
||||
json.put("data", data);
|
||||
return json;
|
||||
}
|
||||
}
|
||||
@@ -1,670 +0,0 @@
|
||||
package com.yutou.bilibili.BiliBili;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.yutou.bilibili.BiliBili.Datas.DanmuData;
|
||||
import com.yutou.bilibili.BiliBili.Datas.GiftData;
|
||||
import com.yutou.bilibili.BiliBili.Datas.LiveData;
|
||||
import com.yutou.bilibili.BiliBili.Services.IBiliBiliLiveService;
|
||||
import com.yutou.bilibili.BiliBili.Tools.SaveLive;
|
||||
import com.yutou.bilibili.QQBot.QQBotManager;
|
||||
import com.yutou.bilibili.Tools.AppTools;
|
||||
import com.yutou.bilibili.Tools.Log;
|
||||
import com.yutou.bilibili.Tools.RedisTools;
|
||||
import com.yutou.bilibili.Tools.Tools;
|
||||
import com.yutou.bilibili.interfaces.DownloadInterface;
|
||||
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;
|
||||
import com.yutou.bilibili.sqlite.BiliBiliLiveDatabasesManager;
|
||||
import lombok.Data;
|
||||
import org.java_websocket.client.WebSocketClient;
|
||||
import org.java_websocket.handshake.ServerHandshake;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.net.URI;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
|
||||
public class Live implements ApplicationContextAware {
|
||||
IBiliBiliLiveService service;
|
||||
|
||||
|
||||
public static Map<Integer, Live> lives = new HashMap<>();
|
||||
|
||||
|
||||
private int roomId;
|
||||
private boolean isLogin;
|
||||
private boolean run;
|
||||
private int userId;
|
||||
private WebSocketClient client;
|
||||
private BilibiliLiveInfo info;
|
||||
private BilibiliUpInfo upData;
|
||||
private Timer heartBeattimer;
|
||||
public BiliBiliLiveDatabasesManager danmuManager = null;
|
||||
private LiveInfo liveInfo;
|
||||
|
||||
public LiveInfo getLiveInfo() {
|
||||
return liveInfo;
|
||||
}
|
||||
|
||||
public void setLiveInfo(LiveInfo liveInfo) {
|
||||
this.liveInfo = liveInfo;
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
|
||||
}
|
||||
|
||||
public Live() {
|
||||
if (applicationContext != null) {
|
||||
service = getBean(IBiliBiliLiveService.class);
|
||||
}
|
||||
info = new BilibiliLiveInfo();
|
||||
info.setGiftuser(0);
|
||||
info.setPopular(0);
|
||||
info.setUserindex(0);
|
||||
info.setVipuserindex(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造方法
|
||||
*
|
||||
* @param roomId 直播间roomId,不是房间号,有可能是临时房间号,得通过 {@link com.yutou.bilibili.BiliBili.Tools.BiliTools#getBiliUpInfo(String)} 来获取roomId
|
||||
* @param isLogin 是否登录用户
|
||||
*/
|
||||
public void add(int roomId, boolean isLogin) {
|
||||
if (Live.lives.containsKey(roomId)) {
|
||||
Log.i("已经在统计:" + roomId);
|
||||
return;
|
||||
}
|
||||
this.roomId = roomId;
|
||||
this.isLogin = isLogin;
|
||||
upData = new BilibiliUpInfo();
|
||||
upData.setRoomid(roomId);
|
||||
info.setRoomid(roomId);
|
||||
info.setGiftuser(0);
|
||||
info.setVipuserindex(0);
|
||||
info.setUserindex(0);
|
||||
Live.lives.put(roomId, this);
|
||||
updateUpInfo();
|
||||
com.yutou.bilibili.Tools.Log.i("roomId = " + roomId + ", isLogin = " + isLogin);
|
||||
try {
|
||||
start();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void updateUpInfo() {
|
||||
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) {
|
||||
Log.e(e);
|
||||
}
|
||||
}
|
||||
|
||||
private Date startTime;
|
||||
|
||||
/**
|
||||
* 开始监听
|
||||
*
|
||||
* @throws Exception 发生异常
|
||||
*/
|
||||
private void start() throws Exception {
|
||||
run = true;
|
||||
String url = LiveUtils.getLiveUrl(roomId);
|
||||
if (url == null) {
|
||||
stop();
|
||||
return;
|
||||
}
|
||||
upData.checkLiveStatus();
|
||||
startTime = new Date();
|
||||
HashMap<String, String> header = new HashMap<>();
|
||||
header.put("Sec-WebSocket-Extensions", "permessage-deflate; client_max_window_bits");
|
||||
header.put("Sec-WebSocket-Key", "tORCZd8AI6xIyvqvgvI1Vw==");
|
||||
header.put("Sec-WebSocket-Version", "13");
|
||||
header.put("Cache-Control", "no-cache");
|
||||
header.put("Connection", "Upgrade");
|
||||
header.put("Host", "tx-gz-live-comet-02.chat.bilibili.com");
|
||||
header.put("Origin", "https://live.bilibili.com");
|
||||
header.put("Pragma", "no-cache");
|
||||
header.put("Upgrade", "websocket");
|
||||
header.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36");
|
||||
if (client != null && !client.isClosed()) {
|
||||
stop();
|
||||
return;
|
||||
}
|
||||
client = new WebSocketClient(new URI(url), header) {
|
||||
private long time = 0;
|
||||
private boolean init = true;
|
||||
|
||||
@Override
|
||||
public void onOpen(ServerHandshake serverHandshake) {
|
||||
time = System.currentTimeMillis();
|
||||
com.yutou.bilibili.Tools.Log.i("ws: ok");
|
||||
try {
|
||||
likeLive();
|
||||
} catch (Exception e) {
|
||||
Log.e(e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessage(String s) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessage(ByteBuffer bytes) {
|
||||
super.onMessage(bytes);
|
||||
if (init) {
|
||||
heartBeattimer = new Timer();
|
||||
heartBeattimer.schedule(new sendHeartbeat(), 2000, 20000);
|
||||
init = false;
|
||||
}
|
||||
decompress(bytes.array());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClose(int i, String s, boolean b) {
|
||||
com.yutou.bilibili.Tools.Log.i("连接时间:" + (System.currentTimeMillis() - time));
|
||||
if (upData.getOfflinelistening() == 1) {
|
||||
Log.i(roomId + " 断开连接,重连...");
|
||||
try {
|
||||
init = true;
|
||||
if(heartBeattimer!=null) {
|
||||
heartBeattimer.cancel();
|
||||
}
|
||||
if(client!=null) {
|
||||
client.close();
|
||||
}
|
||||
start();
|
||||
} catch (Exception e) {
|
||||
Log.e(e);
|
||||
}
|
||||
return;
|
||||
}
|
||||
stop();
|
||||
client.close();
|
||||
run = false;
|
||||
throw new RuntimeException("连接关闭: code = " + i + " msg = " + s + " b = " + b + " roomId=" + roomId);
|
||||
}
|
||||
|
||||
@Override
|
||||
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();
|
||||
stop();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Exception e) {
|
||||
Log.e(e);
|
||||
run = false;
|
||||
client.close();
|
||||
stop();
|
||||
}
|
||||
};
|
||||
client.connect();
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
run = false;
|
||||
client.close();
|
||||
if (danmuManager != null) {
|
||||
danmuManager.close();
|
||||
}
|
||||
if (SaveLive.getInstance().checkLive(roomId)) {
|
||||
SaveLive.getInstance().stop(roomId);
|
||||
}
|
||||
Live.lives.remove(roomId);
|
||||
com.yutou.bilibili.Tools.Log.i("退出" + roomId + "直播间");
|
||||
}
|
||||
|
||||
/**
|
||||
* 连接房间
|
||||
*
|
||||
* @throws Exception e
|
||||
*/
|
||||
private void likeLive() throws Exception {
|
||||
JSONObject tmp = LiveUtils.http_get("https://api.bilibili.com/x/web-interface/nav");
|
||||
if (tmp != null && tmp.getInteger("code") == -101) {
|
||||
if (isLogin) {
|
||||
new File("cookies.json").deleteOnExit();
|
||||
upData.setLive(0);
|
||||
service.updateUpInfo(upData);
|
||||
stop();
|
||||
return;
|
||||
} else {
|
||||
userId = 0;
|
||||
}
|
||||
} else if (tmp != null && tmp.getInteger("code") == 0) {
|
||||
userId = tmp.getJSONObject("data").getInteger("mid");
|
||||
}
|
||||
JSONObject http = LiveUtils.http_get("https://api.live.bilibili.com/xlive/web-room/v1/index/getDanmuInfo?id=" + roomId + "&type=0");
|
||||
if (http == null) {
|
||||
stop();
|
||||
return;
|
||||
}
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("uid", userId);
|
||||
json.put("roomid", roomId);
|
||||
json.put("protover", 2);
|
||||
json.put("platform", "web");
|
||||
json.put("clientver", "2.6.36");
|
||||
json.put("type", 2);
|
||||
json.put("key", http.getJSONObject("data").getString("token"));
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
outputStream.write(LiveUtils.toLH(json.toJSONString().length() + 16));
|
||||
outputStream.write(new byte[]{0, 16, 0, 1, 0, 0, 0, 7, 0, 0, 0, 1});
|
||||
outputStream.write(json.toJSONString().getBytes(StandardCharsets.UTF_8));
|
||||
outputStream.flush();
|
||||
// LiveUtils.printHex(outputStream.toByteArray());
|
||||
client.send(outputStream.toByteArray());
|
||||
checkLive();
|
||||
}
|
||||
|
||||
/**
|
||||
* 解压缩
|
||||
*
|
||||
* @param data 待压缩的数据
|
||||
*/
|
||||
public void decompress(byte[] data) {
|
||||
try {
|
||||
byte[] bytes = new byte[data.length - 16];
|
||||
System.arraycopy(data, 16, bytes, 0, data.length - 16);
|
||||
|
||||
if (data.length > 32) {
|
||||
List<String> list = LiveUtils.getMsgList(LiveUtils.dec(bytes), new ArrayList<>(), true);
|
||||
String tmp = "";
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
processData(list.get(i), data);
|
||||
tmp += "|||" + i + "[start] " + list.get(i) + " [end]||| ";
|
||||
}
|
||||
// Log.i(AppTools.getToDayTimeToString(startTime), roomId, "list size = " + list.size() + " data = " + tmp);
|
||||
} else {
|
||||
try {
|
||||
JSONObject json = JSONObject.parseObject(new String(bytes, StandardCharsets.UTF_8));
|
||||
System.out.println("json = " + json);
|
||||
com.yutou.bilibili.Tools.Log.i(json.toJSONString());
|
||||
} catch (Exception e) {
|
||||
int popular = LiveUtils.bytesToInt2(bytes, 0);
|
||||
info.setPopular(popular);
|
||||
}
|
||||
}
|
||||
upData.setLive(LiveUtils.getLiveState(roomId) ? 1 : 0);
|
||||
|
||||
} catch (Exception e) {
|
||||
Log.e(e);
|
||||
com.yutou.bilibili.Tools.Log.i("----------ERROR----------");
|
||||
com.yutou.bilibili.Tools.Log.i(new String(data, StandardCharsets.UTF_8));
|
||||
LiveUtils.printHex(LiveUtils.dec(data));
|
||||
com.yutou.bilibili.Tools.Log.i(new SimpleDateFormat("HH:mm:ss.SSS").format(new Date()));
|
||||
}
|
||||
}
|
||||
|
||||
public void checkLive() {
|
||||
boolean isLive = upData.checkLiveStatus() == 1;
|
||||
if (SaveLive.getInstance().checkLive(roomId)) {
|
||||
return;
|
||||
}
|
||||
if (isLive && upData.getSavelive() == 1) {
|
||||
SaveLive.getInstance().addLive(this);
|
||||
}
|
||||
if (!isLive || upData.getSavelive() == 0) {
|
||||
if (SaveLive.getInstance().checkLive(roomId)) {
|
||||
SaveLive.getInstance().stop(roomId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 处理数据
|
||||
*
|
||||
* @param msg 数据json
|
||||
* @param bytes 原始hex数据
|
||||
*/
|
||||
public void processData(String msg, byte[] bytes) {
|
||||
if (msg.contains("[object Object]")) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
JSONObject json = JSONObject.parseObject(msg);
|
||||
JSONObject data;
|
||||
BilibiliLiveData liveData = new BilibiliLiveData();
|
||||
String danmu;
|
||||
GiftData giftData;
|
||||
Log.i(AppTools.getToDayTime(), roomId, json.toJSONString());
|
||||
switch (json.getString("cmd")) {
|
||||
case "INTERACT_WORD"://普通用户进直播间
|
||||
danmu = json.getJSONObject("data").getString("uname") + " 进入到直播间";
|
||||
liveData.setUid(json.getJSONObject("data").getInteger("uid"));
|
||||
liveData.setType(LiveData.INTERACT_WORD);
|
||||
liveData.setMsg(danmu);
|
||||
liveData.setRoomid(roomId);
|
||||
liveData.setSubtime(new Date());
|
||||
service.addLiveData(liveData);
|
||||
break;
|
||||
case "DANMU_MSG"://普通弹幕
|
||||
JSONArray infoData = json.getJSONArray("info");
|
||||
danmu = json.getJSONArray("info").getString(1);
|
||||
DanmuData danmuData = new DanmuData();
|
||||
danmuData.setModel(infoData.getJSONArray(0).getInteger(1));
|
||||
danmuData.setFontSize(infoData.getJSONArray(0).getInteger(2));
|
||||
danmuData.setFontColor(infoData.getJSONArray(0).getInteger(3));
|
||||
danmuData.setTime(infoData.getJSONArray(0).getLong(4));
|
||||
danmuData.setUCode(infoData.getJSONArray(0).getString(7));
|
||||
danmuData.setDanmu(danmu);
|
||||
danmuData.setUid(infoData.getJSONArray(2).getInteger(0));
|
||||
danmuData.setUname(infoData.getJSONArray(2).getString(1));
|
||||
|
||||
if (upData != null && upData.getSavedanmu() == 1) {
|
||||
if (danmuManager != null) {
|
||||
danmuManager.addDanmu(danmuData);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case "SEND_GIFT"://送礼
|
||||
data = json.getJSONObject("data");
|
||||
giftData = getGiftData(data.getInteger("giftId"));
|
||||
if (giftData == null) {
|
||||
giftData = new GiftData();
|
||||
giftData.setName(data.getString("giftName"));
|
||||
giftData.setId(data.getInteger("giftId"));
|
||||
giftData.setPrice(data.getInteger("price"));
|
||||
giftData.setIcon("");
|
||||
giftData.setDesc("这是阿B没有收录的礼物,金额可能不准(无法判别为免费礼物)");
|
||||
giftData.setRights("?");
|
||||
}
|
||||
danmu = data.getString("uname") + " " + data.getString("action") + " " + giftData.getName();
|
||||
liveData.setType(LiveData.SEND_GIFT);
|
||||
liveData.setUid(data.getInteger("uid"));
|
||||
liveData.setGiftid(giftData.getId());
|
||||
liveData.setGiftindex(data.getInteger("num"));
|
||||
liveData.setGiftname(giftData.getName());
|
||||
liveData.setMsg(danmu);
|
||||
liveData.setPrice(giftData.getPrice());
|
||||
liveData.setPriceofcommission(giftData.getPrice() == 0 ? 0 : giftData.getPrice() / 2);
|
||||
liveData.setSubtime(new Date());
|
||||
liveData.setRoomid(roomId);
|
||||
info.setGiftuser(info.getGiftuser() + 1);
|
||||
service.addLiveData(liveData);
|
||||
break;
|
||||
case "COMBO_SEND"://礼物连击
|
||||
data = json.getJSONObject("data");
|
||||
String gift = data.getString("giftName");
|
||||
if (gift == null || "null".equals(gift)) {
|
||||
gift = data.getString("gift_name");
|
||||
}
|
||||
danmu = data.getString("uname") + " " + data.getString("action") + " " + gift + "x" + data.getInteger("batch_combo_num");
|
||||
giftData = getGiftData(data.getInteger("gift_id"));
|
||||
if (giftData == null) {
|
||||
giftData = new GiftData();
|
||||
giftData.setName(data.getString("giftName"));
|
||||
giftData.setId(data.getInteger("giftId"));
|
||||
giftData.setPrice(data.getInteger("price"));
|
||||
giftData.setIcon("");
|
||||
giftData.setDesc("这是阿B没有收录的礼物,金额可能不准(无法判别为免费礼物)");
|
||||
giftData.setRights("?");
|
||||
}
|
||||
liveData.setType(LiveData.COMBO_SEND);
|
||||
liveData.setUid(data.getInteger("uid"));
|
||||
liveData.setGiftid(giftData.getId());
|
||||
liveData.setGiftindex(data.getInteger("batch_combo_num"));
|
||||
liveData.setGiftname(giftData.getName());
|
||||
liveData.setMsg(danmu);
|
||||
liveData.setPrice(giftData.getPrice() * liveData.getGiftindex());
|
||||
liveData.setPriceofcommission(giftData.getPrice() == 0 ? 0 : giftData.getPrice() / 2);
|
||||
liveData.setSubtime(new Date());
|
||||
liveData.setRoomid(roomId);
|
||||
info.setGiftuser(info.getGiftuser() + 1);
|
||||
service.addLiveData(liveData);
|
||||
break;
|
||||
case "ENTRY_EFFECT"://舰长进直播间
|
||||
info.setVipuserindex(info.getVipuserindex() + 1);
|
||||
danmu = "[舰长]" + json.getJSONObject("data").getString("uid") + " 进入到直播间";
|
||||
liveData.setUid(json.getJSONObject("data").getInteger("uid"));
|
||||
liveData.setType(LiveData.ENTRY_EFFECT);
|
||||
liveData.setMsg(danmu);
|
||||
liveData.setRoomid(roomId);
|
||||
liveData.setSubtime(new Date());
|
||||
service.addLiveData(liveData);
|
||||
break;
|
||||
case "LIVE_INTERACTIVE_GAME"://彩色弹幕?通过游戏弹幕
|
||||
break;
|
||||
case "SUPER_CHAT_MESSAGE"://SC
|
||||
data = json.getJSONObject("data");
|
||||
danmu = data.getJSONObject("user_info").getString("uname") + " "
|
||||
+ data.getJSONObject("gift").getString("gift_name") + " " + data.getInteger("price") + "元: " + data.getString("message");
|
||||
liveData.setType(LiveData.SUPER_CHAT_MESSAGE);
|
||||
liveData.setUid(data.getInteger("uid"));
|
||||
liveData.setMsg(danmu);
|
||||
liveData.setGiftid(0);
|
||||
liveData.setRoomid(roomId);
|
||||
liveData.setGiftname(data.getJSONObject("gift").getString("gift_name"));
|
||||
liveData.setGiftindex(1);
|
||||
liveData.setPrice(data.getInteger("price") * 1000);
|
||||
liveData.setPriceofcommission(liveData.getPrice() == 0 ? 0 : liveData.getPrice() / 2);
|
||||
liveData.setSubtime(new Date());
|
||||
info.setGiftuser(info.getGiftuser() + 1);
|
||||
service.addLiveData(liveData);
|
||||
break;
|
||||
case "USER_TOAST_MSG":
|
||||
break;
|
||||
case "GUARD_BUY"://开通/续费 牛逼的东西
|
||||
data = json.getJSONObject("data");
|
||||
liveData.setType(LiveData.GUARD_BUY);
|
||||
liveData.setUid(data.getInteger("uid"));
|
||||
liveData.setMsg(data.getString("gift_name"));
|
||||
liveData.setGiftid(-data.getInteger("guard_level"));
|
||||
liveData.setGiftindex(data.getInteger("num"));
|
||||
liveData.setGiftname(data.getString("gift_name"));
|
||||
liveData.setPrice(data.getInteger("price"));
|
||||
liveData.setPriceofcommission(liveData.getPrice() == 0 ? 0 : liveData.getPrice() / 2);
|
||||
liveData.setSubtime(new Date());
|
||||
liveData.setRoomid(roomId);
|
||||
info.setGiftuser(info.getGiftuser() + 1);
|
||||
service.addLiveData(liveData);
|
||||
break;
|
||||
case "LIVE"://开始直播,不过有在心跳包上做检测了,所以也无所谓?
|
||||
|
||||
JSONObject liveInfo = LiveUtils.getLiveInfo(upData);
|
||||
if (liveInfo == null) {
|
||||
break;
|
||||
}
|
||||
upData.setLive(liveInfo.getInteger("live_status"));
|
||||
LiveInfo info = new LiveInfo();
|
||||
info.setTitle(liveInfo.getString("title"));
|
||||
info.setKeyframe(liveInfo.getString("keyframe"));
|
||||
info.setRoomId(liveInfo.getInteger("room_id"));
|
||||
info.setShortId(liveInfo.getInteger("short_id"));
|
||||
info.setInfo(liveInfo);
|
||||
setLiveInfo(info);
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append(upData.getName()).append("开播了!").append("\n");
|
||||
builder.append(info.getTitle());
|
||||
Tools.download(info.getKeyframe(), new DownloadInterface() {
|
||||
@Override
|
||||
public void onDownload(File file) {
|
||||
super.onDownload(file);
|
||||
QQBotManager.getInstance().sendMessage(file, builder.toString());
|
||||
}
|
||||
});
|
||||
break;
|
||||
case "PREPARING"://直播结束
|
||||
upData.setLive(0);
|
||||
break;
|
||||
case "WATCHED_CHANGE"://人气值
|
||||
try {
|
||||
Live.this.info.setPopular(Integer.getInteger(json.getJSONObject("data").getString("text_small")));
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
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":
|
||||
case "PK_BATTLE_PRE_NEW":
|
||||
case "PK_BATTLE_PRE":
|
||||
case "STOP_LIVE_ROOM_LIST"://不知道是啥,停止直播?
|
||||
|
||||
|
||||
//com.yutou.bilibili.Tools.Log.i(msg);
|
||||
break;
|
||||
default:
|
||||
com.yutou.bilibili.Tools.Log.i("[" + roomId + "]" + msg);
|
||||
liveData = new BilibiliLiveData();
|
||||
liveData.setType(LiveData.UNKNOWN_MESSAGE);
|
||||
liveData.setUid(-1);
|
||||
liveData.setMsg(msg);
|
||||
liveData.setRoomid(roomId);
|
||||
liveData.setSubtime(new Date());
|
||||
service.addLiveData(liveData);
|
||||
}
|
||||
checkLive();
|
||||
} catch (Exception e) {
|
||||
Log.e(e);
|
||||
Log.i("msg = " + msg);
|
||||
// Log.i(AppTools.getToDayTimeToString(startTime), roomId, e.getLocalizedMessage());
|
||||
/*try {
|
||||
JSONObject.parseObject(new String(bytes, StandardCharsets.UTF_8));
|
||||
processData(new String(bytes, StandardCharsets.UTF_8), null);
|
||||
} catch (Exception e2) {
|
||||
try {
|
||||
JSONObject.parseObject(msg);
|
||||
processData(msg, bytes);
|
||||
} catch (Exception e3) {
|
||||
Log.e(e2);
|
||||
com.yutou.bilibili.Tools.Log.i("---------ERROR !! start-----");
|
||||
com.yutou.bilibili.Tools.Log.i(msg);
|
||||
com.yutou.bilibili.Tools.Log.i("---------ERROR !! end-----");
|
||||
// LiveUtils.printHex(bytes);
|
||||
}
|
||||
}*/
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void clearInfo() {
|
||||
info = new BilibiliLiveInfo();
|
||||
}
|
||||
|
||||
public BilibiliLiveInfo getInfo() {
|
||||
return info;
|
||||
}
|
||||
|
||||
public void setSaveDanmo(boolean saveDanmu) {
|
||||
upData.setSavedanmu(saveDanmu ? 1 : 0);
|
||||
}
|
||||
|
||||
private static ApplicationContext applicationContext = null;
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(@NotNull ApplicationContext applicationContext) throws BeansException {
|
||||
if (Live.applicationContext == null) {
|
||||
Live.applicationContext = applicationContext;
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> T getBean(Class<T> clazz) {
|
||||
return applicationContext.getBean(clazz);
|
||||
}
|
||||
|
||||
public BilibiliUpInfo geData() {
|
||||
return upData;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送心跳包
|
||||
*/
|
||||
public class sendHeartbeat extends TimerTask {
|
||||
|
||||
public sendHeartbeat() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if (!run) {
|
||||
cancel();
|
||||
return;
|
||||
}
|
||||
updateUpInfo();
|
||||
if (upData.getOfflinelistening() != 1) {
|
||||
if (upData.getLive() == 0) {
|
||||
stop();
|
||||
}
|
||||
}
|
||||
try {
|
||||
// com.yutou.bilibili.Tools.Log.i("-------发送心跳--------");
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
outputStream.write(LiveUtils.toLH("[object Object]".length() + 16));
|
||||
outputStream.write(new byte[]{0, 16, 0, 1, 0, 0, 0, 2, 0, 0, 0, 1});
|
||||
outputStream.write("[object Object]".getBytes(StandardCharsets.UTF_8));
|
||||
outputStream.flush();
|
||||
client.send(outputStream.toByteArray());
|
||||
} catch (Exception e) {
|
||||
Log.e(e);
|
||||
com.yutou.bilibili.Tools.Log.i(client.isClosed());
|
||||
com.yutou.bilibili.Tools.Log.i(client.isOpen());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public GiftData getGiftData(int id) {
|
||||
JSONObject item = JSONObject.parseObject(RedisTools.get("bili_gift_" + id, 2));
|
||||
GiftData data = new GiftData();
|
||||
data.setPrice(item.getInteger("price"));
|
||||
data.setName(item.getString("name"));
|
||||
data.setId(item.getInteger("id"));
|
||||
data.setRights(item.getString("rights"));
|
||||
data.setIcon(item.getString("img_basic"));
|
||||
data.setDesc(item.getString("desc"));
|
||||
return data;
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class LiveInfo {
|
||||
private String title;
|
||||
private String keyframe;
|
||||
private int roomId;
|
||||
private int shortId;
|
||||
private JSONObject info;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
package com.yutou.bilibili.BiliBili;
|
||||
|
||||
import com.yutou.bilibili.BiliBili.Tools.SaveLive;
|
||||
import com.yutou.bilibili.Tools.AppTools;
|
||||
import com.yutou.bilibili.mybatis.Bili.mybatis.model.BilibiliUpInfo;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Timer;
|
||||
|
||||
public class LiveController {
|
||||
private static LiveController controller;
|
||||
private Timer timer;
|
||||
|
||||
public static LiveController getInstance() {
|
||||
if (controller == null) {
|
||||
controller = new LiveController();
|
||||
}
|
||||
return controller;
|
||||
}
|
||||
|
||||
private LiveController() {
|
||||
init();
|
||||
}
|
||||
|
||||
private void init() {
|
||||
|
||||
}
|
||||
|
||||
public void updateUpInfo(BilibiliUpInfo data) {
|
||||
Live live = LiveUtils.liveContains(data);
|
||||
if (live != null) {
|
||||
live.setSaveDanmo(data.getSavedanmu() == 1);
|
||||
boolean saveLive=SaveLive.getInstance().checkLive(data.getRoomid());
|
||||
if(data.getSavelive()==1){
|
||||
if(!saveLive){
|
||||
SaveLive.getInstance().addLive(live);
|
||||
}
|
||||
}else{
|
||||
if(saveLive){
|
||||
SaveLive.getInstance().stop(data.getRoomid());
|
||||
}
|
||||
}
|
||||
}
|
||||
if (data.getEnable()==1||data.getOfflinelistening()==1) {
|
||||
if (live == null) {
|
||||
live=new Live();
|
||||
live.add(data.getRoomid(), !StringUtils.isEmpty(AppTools.readFile(new File("cookies.json"))));
|
||||
}
|
||||
} else {
|
||||
if (live != null) {
|
||||
live.stop();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,450 +0,0 @@
|
||||
package com.yutou.bilibili.BiliBili;
|
||||
|
||||
import com.yutou.bilibili.BiliBili.Datas.BiliBiliUpData;
|
||||
import com.yutou.bilibili.BiliBili.Datas.GiftData;
|
||||
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.HttpTools;
|
||||
import com.yutou.bilibili.Tools.Log;
|
||||
import com.yutou.bilibili.Tools.RedisTools;
|
||||
import com.yutou.bilibili.mybatis.Bili.mybatis.model.BilibiliUpInfo;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.xml.bind.DatatypeConverter;
|
||||
import java.io.*;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.*;
|
||||
import java.util.zip.Inflater;
|
||||
|
||||
public class LiveUtils {
|
||||
private static String cookie = null;
|
||||
private static Map<Integer, Boolean> liveStateMap = new HashMap<>();
|
||||
|
||||
public LiveUtils() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static String getLiveUrl(int roomId) {
|
||||
JSONObject json = http_get("https://api.live.bilibili.com/xlive/web-room/v1/index/getDanmuInfo?id=" + roomId + "&type=0");
|
||||
if (json != null) {
|
||||
return "wss://" + json.getJSONObject("data").getJSONArray("host_list").getJSONObject(0).getString("host") + "/sub";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void printHex(byte[] bytes) {
|
||||
String str = DatatypeConverter.printHexBinary(bytes);
|
||||
for (int i = 0; i < str.length(); i = i + 4) {
|
||||
if (i % 32 == 0 && i != 0) {
|
||||
com.yutou.bilibili.Tools.Log.i("\n");
|
||||
}
|
||||
if (str.length() - i > 4) {
|
||||
com.yutou.bilibili.Tools.Log.i(str.substring(i, i + 4));
|
||||
} else {
|
||||
com.yutou.bilibili.Tools.Log.i(str.substring(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static String getHex(byte[] bytes) {
|
||||
return DatatypeConverter.printHexBinary(bytes);
|
||||
}
|
||||
|
||||
public static byte[] dec(byte[] data) {
|
||||
byte[] output = new byte[0];
|
||||
|
||||
Inflater decompresser = new Inflater();
|
||||
decompresser.reset();
|
||||
decompresser.setInput(data);
|
||||
|
||||
ByteArrayOutputStream o = new ByteArrayOutputStream(data.length);
|
||||
try {
|
||||
byte[] buf = new byte[1024];
|
||||
while (!decompresser.finished()) {
|
||||
int i = decompresser.inflate(buf);
|
||||
o.write(buf, 0, i);
|
||||
}
|
||||
output = o.toByteArray();
|
||||
} catch (Exception e) {
|
||||
// com.yutou.bilibili.Tools.Log.e(e);
|
||||
try {
|
||||
JSONObject json = JSONObject.parseObject(new String(data, StandardCharsets.UTF_8));
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
// outputStream.write(toLH(json.toJSONString().length() + 16));
|
||||
//outputStream.write(new byte[]{0, 16, 0, 1, 0, 0, 0, 5, 0, 0, 0, 1});
|
||||
outputStream.write(json.toJSONString().getBytes(StandardCharsets.UTF_8));
|
||||
output = outputStream.toByteArray();
|
||||
} catch (Exception e1) {
|
||||
output = data;
|
||||
}
|
||||
} finally {
|
||||
try {
|
||||
o.close();
|
||||
} catch (IOException e) {
|
||||
com.yutou.bilibili.Tools.Log.e(e);
|
||||
}
|
||||
}
|
||||
|
||||
decompresser.end();
|
||||
return output;
|
||||
}
|
||||
|
||||
public static List<String> getMsgList(byte[] bytes, List<String> list, boolean isOne) {
|
||||
int len = 16;
|
||||
ByteBuffer datas = ByteBuffer.allocate(bytes.length - len);
|
||||
byte[] heads = new byte[4];
|
||||
//获取到头信息
|
||||
System.arraycopy(bytes, 0, heads, 0, 4);
|
||||
int size = bytesToInt2(heads, 0) - 16;
|
||||
|
||||
//datas=MainApp.dec(datas);
|
||||
if (size == datas.array().length) {
|
||||
System.arraycopy(bytes, len, datas.array(), 0, bytes.length - len);
|
||||
list.add(new String(datas.array(), StandardCharsets.UTF_8));
|
||||
} else {
|
||||
try {
|
||||
// com.yutou.bilibili.Tools.Log.i("datas size = " + size);
|
||||
if (size > 1000000000) {
|
||||
try {
|
||||
JSONObject.parseObject(new String(bytes));
|
||||
list.add(new String(bytes, StandardCharsets.UTF_8));
|
||||
return list;
|
||||
} catch (Exception e) {
|
||||
com.yutou.bilibili.Tools.Log.e(e);
|
||||
}
|
||||
}
|
||||
datas.clear();
|
||||
datas = ByteBuffer.allocate(size);
|
||||
System.arraycopy(bytes, len, datas.array(), 0, size);
|
||||
list.add(new String(datas.array(), StandardCharsets.UTF_8));
|
||||
byte[] tmps = new byte[bytes.length - size - 16];
|
||||
int length = bytes.length - (size + len);
|
||||
System.arraycopy(bytes, size + (len), tmps, 0, length);
|
||||
|
||||
getMsgList(tmps, list, false);
|
||||
} catch (Exception e) {
|
||||
list.add(new String(bytes, StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public static byte[] toLH(int n) {
|
||||
byte[] b = new byte[4];
|
||||
b[3] = (byte) (n & 0xff);
|
||||
b[2] = (byte) (n >> 8 & 0xff);
|
||||
b[1] = (byte) (n >> 16 & 0xff);
|
||||
b[0] = (byte) (n >> 24 & 0xff);
|
||||
return b;
|
||||
}
|
||||
|
||||
public static int bytesToInt2(byte[] src, int offset) {
|
||||
int value;
|
||||
value = (int) (((src[offset] & 0xFF) << 24)
|
||||
| ((src[offset + 1] & 0xFF) << 16)
|
||||
| ((src[offset + 2] & 0xFF) << 8)
|
||||
| (src[offset + 3] & 0xFF));
|
||||
return value;
|
||||
}
|
||||
|
||||
public static String getCookie() {
|
||||
if (StringUtils.isEmpty(getFile("cookies.json"))) {
|
||||
|
||||
return "";
|
||||
}
|
||||
JSONObject json = JSONObject.parseObject(getFile("cookies.json"));
|
||||
StringBuilder builder = new StringBuilder();
|
||||
for (String s : json.keySet()) {
|
||||
builder.append(s).append("=").append(json.getString(s)).append(";");
|
||||
|
||||
}
|
||||
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 synchronized static JSONObject http_get(String url) {
|
||||
try {
|
||||
// Log.i("调用url = "+url);
|
||||
HttpURLConnection connection = getBiliHttpGet(url, getCookie());
|
||||
BufferedInputStream stream = new BufferedInputStream(connection.getInputStream());
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
byte[] bytes = new byte[1024];
|
||||
int len = 0, size;
|
||||
while ((len = stream.read(bytes)) != -1) {
|
||||
outputStream.write(bytes, 0, len);
|
||||
outputStream.flush();
|
||||
}
|
||||
String str = new String(outputStream.toByteArray(), StandardCharsets.UTF_8);
|
||||
outputStream.close();
|
||||
try {
|
||||
JSONObject json = JSON.parseObject(str);
|
||||
return json;
|
||||
} catch (Exception e) {
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("html", str);
|
||||
return json;
|
||||
} finally {
|
||||
|
||||
stream.close();
|
||||
connection.disconnect();
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
//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) {
|
||||
try {
|
||||
Thread.sleep(500);
|
||||
} catch (InterruptedException e) {
|
||||
com.yutou.bilibili.Tools.Log.e(e);
|
||||
}
|
||||
oldBiliBiliHttpTime = System.currentTimeMillis();
|
||||
}
|
||||
HttpURLConnection connection = getBiliHttpPost(url, getCookie());
|
||||
OutputStream connectionOutputStream = null;
|
||||
if (!StringUtils.isEmpty(body)) {
|
||||
connectionOutputStream = connection.getOutputStream();
|
||||
connectionOutputStream.write(body.getBytes(StandardCharsets.UTF_8));
|
||||
connectionOutputStream.flush();
|
||||
}
|
||||
BufferedInputStream stream = new BufferedInputStream(connection.getInputStream());
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
byte[] bytes = new byte[1024];
|
||||
int len = 0, size;
|
||||
while ((len = stream.read(bytes)) != -1) {
|
||||
outputStream.write(bytes, 0, len);
|
||||
outputStream.flush();
|
||||
}
|
||||
String str = new String(outputStream.toByteArray(), StandardCharsets.UTF_8);
|
||||
outputStream.close();
|
||||
try {
|
||||
JSONObject json = JSON.parseObject(str);
|
||||
json.put("cookie", connection.getHeaderField("Set-Cookie"));
|
||||
return json;
|
||||
} catch (Exception e) {
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("html", str);
|
||||
json.put("cookie", connection.getHeaderField("Set-Cookie"));
|
||||
return json;
|
||||
} finally {
|
||||
stream.close();
|
||||
if (connectionOutputStream != null) {
|
||||
connectionOutputStream.close();
|
||||
}
|
||||
connection.disconnect();
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
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) {
|
||||
try {
|
||||
Thread.sleep(500);
|
||||
} catch (InterruptedException e) {
|
||||
com.yutou.bilibili.Tools.Log.e(e);
|
||||
}
|
||||
oldBiliBiliHttpTime = System.currentTimeMillis();
|
||||
}
|
||||
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
|
||||
connection.setRequestMethod("POST");
|
||||
connection.setDoOutput(true);
|
||||
connection.setDoInput(true);
|
||||
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");
|
||||
connection.setRequestProperty("Cache-Control", "max-age=0");
|
||||
connection.setRequestProperty("Referer", "https://live.bilibili.com");
|
||||
connection.setRequestProperty("Connection", "keep-alive");
|
||||
connection.setRequestProperty("Upgrade-Insecure-Requests", "1");
|
||||
connection.setRequestProperty("Cookie", cookie);
|
||||
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36");
|
||||
return connection;
|
||||
}
|
||||
|
||||
public static HttpURLConnection getBiliHttpGet(String url, String cookie) throws IOException {
|
||||
if (System.currentTimeMillis() - oldBiliBiliHttpTime < 1000) {
|
||||
try {
|
||||
Thread.sleep(500);
|
||||
} catch (InterruptedException e) {
|
||||
com.yutou.bilibili.Tools.Log.e(e);
|
||||
}
|
||||
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");
|
||||
connection.setRequestProperty("Cache-Control", "max-age=0");
|
||||
connection.setRequestProperty("Referer", "https://live.bilibili.com");
|
||||
connection.setRequestProperty("Connection", "keep-alive");
|
||||
connection.setRequestProperty("Upgrade-Insecure-Requests", "1");
|
||||
connection.setRequestProperty("Cookie", cookie);
|
||||
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36");
|
||||
connection.setReadTimeout(5000);
|
||||
connection.setConnectTimeout(5000);
|
||||
return connection;
|
||||
}
|
||||
|
||||
|
||||
public static String getFile(String filePath) {
|
||||
File file = new File(filePath);
|
||||
try {
|
||||
BufferedReader reader = new BufferedReader(new FileReader(file));
|
||||
String str = "", tmp;
|
||||
while ((tmp = reader.readLine()) != null) {
|
||||
str += tmp;
|
||||
}
|
||||
reader.close();
|
||||
return str;
|
||||
} catch (Exception e) {
|
||||
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public static JSONObject getUserLoginInfo() {
|
||||
return LiveUtils.http_get("https://api.bilibili.com/x/web-interface/nav");
|
||||
}
|
||||
|
||||
public static Live liveContains(BilibiliUpInfo data) {
|
||||
for (Live live : Live.lives.values()) {
|
||||
if (live.getInfo().getRoomid().equals(data.getRoomid())) {
|
||||
return live;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static long getMid(int roomId) {
|
||||
JSONObject http_get = http_get("https://api.live.bilibili.com/room/v1/Room/room_init?id=" + roomId);
|
||||
if (http_get == null) {
|
||||
return -1;
|
||||
}
|
||||
if (http_get.getInteger("code") == 0) {
|
||||
return http_get.getJSONObject("data").getLong("uid");
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static JSONObject getLiveInfo(BilibiliUpInfo info) {
|
||||
List<BilibiliUpInfo> list = new ArrayList<>();
|
||||
list.add(info);
|
||||
JSONObject infoByUid = getStatusInfoByUid(list);
|
||||
return infoByUid.getJSONObject("data").getJSONObject(info.getMid() + "");
|
||||
}
|
||||
|
||||
public static JSONObject getLiveRoomInfo(int roomId) {
|
||||
String url = "https://api.live.bilibili.com/xlive/web-room/v2/index/getRoomPlayInfo?room_id=" + roomId + "&protocol=0,1&format=0,1,2&codec=0,1&qn=10000&platform=web&ptype=8";
|
||||
return http_get(url);
|
||||
}
|
||||
|
||||
public static boolean getLiveState(int roomId) {
|
||||
return liveStateMap.getOrDefault(roomId, false);
|
||||
}
|
||||
|
||||
public static JSONObject getStatusInfoByUid(List<BilibiliUpInfo> list) {
|
||||
JSONArray userId = new JSONArray();
|
||||
for (BilibiliUpInfo info : list) {
|
||||
userId.add(info.getMid());
|
||||
}
|
||||
JSONObject json=new JSONObject();
|
||||
json.put("uids",userId);
|
||||
return JSONObject.parseObject(HttpTools.post("https://api.live.bilibili.com/room/v1/Room/get_status_info_by_uids", json.toJSONString().getBytes(StandardCharsets.UTF_8)));
|
||||
}
|
||||
|
||||
public static void reloadLiveState(List<BilibiliUpInfo> list) {
|
||||
JSONObject json = getStatusInfoByUid(list);
|
||||
try {
|
||||
if (json.getInteger("code") == 0) {
|
||||
JSONObject data = json.getJSONObject("data");
|
||||
for (BilibiliUpInfo info : list) {
|
||||
if (data.containsKey(info.getMid() + "")) {
|
||||
liveStateMap.put(info.getRoomid(), data.getJSONObject(info.getMid()+"").getInteger("live_status") == 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.i("live state = " + json);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static boolean isLivePlayer(int roomId) {
|
||||
return getLiveState(roomId);
|
||||
}
|
||||
|
||||
|
||||
public static class LiveGiftConfig {
|
||||
@Resource
|
||||
IBiliBiliLiveService service;
|
||||
private static LiveGiftConfig config;
|
||||
|
||||
public static LiveGiftConfig getInstance() {
|
||||
if (config == null) {
|
||||
config = new LiveGiftConfig();
|
||||
}
|
||||
return config;
|
||||
}
|
||||
|
||||
private LiveGiftConfig() {
|
||||
init();
|
||||
}
|
||||
|
||||
public void init() {
|
||||
JSONObject json = http_get("https://api.live.bilibili.com/xlive/web-room/v1/giftPanel/giftConfig?platform=pc");
|
||||
if (json == null) {
|
||||
return;
|
||||
}
|
||||
JSONArray list = json.getJSONObject("data").getJSONArray("list");
|
||||
for (Object o : list) {
|
||||
JSONObject item = (JSONObject) o;
|
||||
RedisTools.set(2, "bili_gift_" + item.getInteger("id"), item.toJSONString());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
List<BilibiliUpInfo> list=new ArrayList<>();
|
||||
BilibiliUpInfo info=new BilibiliUpInfo();
|
||||
info.setMid(402417817L);
|
||||
list.add(info);
|
||||
JSONObject infoByUid = getStatusInfoByUid(list);
|
||||
System.out.println(infoByUid);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,156 +0,0 @@
|
||||
package com.yutou.bilibili.BiliBili.Services;
|
||||
|
||||
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;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public interface IBiliBiliLiveService {
|
||||
|
||||
/**
|
||||
* 获取所有up信息
|
||||
* @return up列表
|
||||
*/
|
||||
List<BilibiliUpInfo> getUpInfo();
|
||||
List<BilibiliUpInfo> getUpInfo(int uid,String power);
|
||||
/**
|
||||
* 根据RoomId查询UP信息
|
||||
* @param data roomId
|
||||
* @return UP信息
|
||||
*/
|
||||
BilibiliUpInfo queryUp(BilibiliUpInfo data);
|
||||
BilibiliUpInfo queryUpToUrl(String url);
|
||||
|
||||
/**
|
||||
* 根据RoomId查询UP信息
|
||||
* @param roomId roomId
|
||||
* @return UP信息
|
||||
*/
|
||||
BilibiliUpInfo queryUpToRoomId(int roomId);
|
||||
/**
|
||||
* 获取所有直播数据
|
||||
* @return 直播数据列表
|
||||
*/
|
||||
List<BilibiliLiveData> getLiveData();
|
||||
/**
|
||||
* 获取所有直播统计
|
||||
* @return 直播统计列表
|
||||
*/
|
||||
List<BilibiliLiveInfo> getLiveInfo();
|
||||
/**
|
||||
* 添加up信息
|
||||
* @param upData up信息
|
||||
* @return 是否成功
|
||||
*/
|
||||
boolean addUpInfo(BilibiliUpInfo upData);
|
||||
/**
|
||||
* 更新UP信息
|
||||
* @param upData up信息
|
||||
* @return 是否成功
|
||||
*/
|
||||
boolean updateUpInfo(BilibiliUpInfo upData);
|
||||
/**
|
||||
* 删除指定up信息
|
||||
* @param upData roomId
|
||||
* @return 是否成功
|
||||
*/
|
||||
boolean deleteUp(BilibiliUpInfo upData);
|
||||
/**
|
||||
* 新增直播数据
|
||||
* @param data 直播数据
|
||||
* @return 是否成功
|
||||
*/
|
||||
boolean addLiveData(BilibiliLiveData data);
|
||||
/**
|
||||
* 添加直播统计
|
||||
* @param data 直播统计
|
||||
* @return 是否成功
|
||||
*/
|
||||
boolean addLiveInfo(BilibiliLiveInfo data);
|
||||
|
||||
/**
|
||||
* 查询指定时段直播数据
|
||||
* @param roomId roomId
|
||||
* @param startTime 开始时间,为空表示为当天00:00:00
|
||||
* @param endTime 结束时间,为空表示为当时
|
||||
* @param type 类型
|
||||
* @return 列表
|
||||
*/
|
||||
List<BilibiliLiveData> queryLiveData(int roomId, Date startTime, Date endTime, String[] type);
|
||||
/**
|
||||
* 查询指定时段直播某个礼物总数
|
||||
* @param roomId roomId
|
||||
* @param giftId 礼物id,-1则为全部
|
||||
* @param startTime 开始时间
|
||||
* @param endTime 结束时间
|
||||
* @return 数量
|
||||
*/
|
||||
int queryGiftSize(int roomId, int giftId, Date startTime, Date endTime);
|
||||
/**
|
||||
* 返回指定时段各个礼物总数
|
||||
* @param roomId roomId
|
||||
* @param startTime 开始时间
|
||||
* @param endTime 结束时间
|
||||
* @return {giftName:size}
|
||||
*/
|
||||
Map<String, Integer> queryGiftSize(int roomId, Date startTime, Date endTime);
|
||||
/**
|
||||
* 查询指定时段收益情况
|
||||
* @param roomId roomId
|
||||
* @param startTime 开始时间
|
||||
* @param endTime 结束时间
|
||||
* @return 未扣除收成比例的金瓜子数量
|
||||
*/
|
||||
int queryPriceSize(int roomId, Date startTime, Date endTime);
|
||||
/**
|
||||
* 根据礼物id查询礼物信息,该方法主要用于阿B接口未收录的礼物信息
|
||||
* @param giftId 礼物id
|
||||
* @return 直播信息
|
||||
*/
|
||||
BilibiliLiveData queryGiftOfId(int giftId);
|
||||
/**
|
||||
* 查询指定时段去重送礼人数
|
||||
*
|
||||
* @param upData up信息
|
||||
* @param startTime 开始时间
|
||||
* @param endTime 结束时间
|
||||
* @param type 礼物类型
|
||||
* @return 人数
|
||||
*/
|
||||
int queryGiftUserToDistinct(BilibiliUpInfo upData, Date startTime, Date endTime, String[] type);
|
||||
/**
|
||||
* 查询时段内人气数量
|
||||
* @param upData up信息
|
||||
* @param startTime 开始时间
|
||||
* @param endTime 结束时间
|
||||
* @return 人气
|
||||
*/
|
||||
int queryPopularCount(BilibiliUpInfo upData,Date startTime, Date endTime);
|
||||
/**
|
||||
* 查询时段内进场人数
|
||||
* @param upData up信息
|
||||
* @param startTime 开始时间
|
||||
* @param endTime 结束时间
|
||||
* @return 人数
|
||||
*/
|
||||
int queryUserCount(BilibiliUpInfo upData,Date startTime, Date endTime);
|
||||
/**
|
||||
* 查询时段内礼物详情
|
||||
* @param upData up信息
|
||||
* @param startTime 开始时间
|
||||
* @param endTime 结束时间
|
||||
* @return 人数
|
||||
*/
|
||||
Map<String, Integer> queryUserGiftCount(BilibiliUpInfo upData,Date startTime,Date endTime);
|
||||
|
||||
List<BilibiliLiveInfo> queryLiveInfo(BilibiliUpInfo upInfo,Date startTime,Date endTime);
|
||||
|
||||
List<Map<String, BigDecimal>>queryTimeGroup(int roomId, Date startTime, Date endTime);
|
||||
|
||||
List<Map<String, BigDecimal>>queryGiftTimeGroup(int roomId, Date startTime, Date endTime);
|
||||
}
|
||||
@@ -1,244 +0,0 @@
|
||||
package com.yutou.bilibili.BiliBili.Services.impl;
|
||||
|
||||
import com.yutou.bilibili.BiliBili.Services.IBiliBiliLiveService;
|
||||
import com.yutou.bilibili.Services.ISystemConfigService;
|
||||
import com.yutou.bilibili.Tools.Config;
|
||||
import com.yutou.bilibili.mybatis.Bili.mybatis.dao.BilibiliLiveDataDao;
|
||||
import com.yutou.bilibili.mybatis.Bili.mybatis.dao.BilibiliLiveInfoDao;
|
||||
import com.yutou.bilibili.mybatis.Bili.mybatis.dao.BilibiliUpInfoDao;
|
||||
import com.yutou.bilibili.mybatis.Bili.mybatis.model.*;
|
||||
import com.yutou.bilibili.mybatis.dao.UBiliUpDao;
|
||||
import com.yutou.bilibili.mybatis.model.UBiliUp;
|
||||
import com.yutou.bilibili.mybatis.model.UBiliUpExample;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.math.BigDecimal;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
|
||||
@Service("BiliBiliLiveService")
|
||||
public class BiliBiliLiveServiceImpl implements IBiliBiliLiveService {
|
||||
@Resource
|
||||
BilibiliUpInfoDao upInfoDao;
|
||||
@Resource
|
||||
BilibiliLiveDataDao liveDataDao;
|
||||
@Resource
|
||||
BilibiliLiveInfoDao liveInfoDao;
|
||||
@Resource
|
||||
UBiliUpDao biliUpDao;
|
||||
@Resource
|
||||
ISystemConfigService configService;
|
||||
|
||||
private <T> T getData(List<T> list) {
|
||||
if (list.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return list.get(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<BilibiliUpInfo> getUpInfo() {
|
||||
return upInfoDao.selectByExample(new BilibiliUpInfoExample());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<BilibiliUpInfo> getUpInfo(int uid, String power) {
|
||||
UBiliUpExample example = new UBiliUpExample();
|
||||
List<BilibiliUpInfo> upInfos = new ArrayList<>();
|
||||
if (power.equals("[-1]")) {
|
||||
upInfos = upInfoDao.selectByExample(new BilibiliUpInfoExample());
|
||||
return upInfos;
|
||||
}
|
||||
example.createCriteria().andUidEqualTo(uid);
|
||||
List<UBiliUp> list = biliUpDao.selectByExample(example);
|
||||
int[] ids = new int[list.size()];
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
ids[i] = list.get(i).getRoomid();
|
||||
}
|
||||
if (list.isEmpty()) {
|
||||
return upInfos;
|
||||
}
|
||||
upInfos = upInfoDao.queryToRoomIds(ids);
|
||||
com.yutou.bilibili.Tools.Log.i("list = " + upInfos.size());
|
||||
return upInfos;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BilibiliUpInfo queryUp(BilibiliUpInfo data) {
|
||||
if (data.getRoomid() == null) {
|
||||
return null;
|
||||
}
|
||||
return queryUpToRoomId(data.getRoomid());
|
||||
}
|
||||
|
||||
@Override
|
||||
public BilibiliUpInfo queryUpToUrl(String url) {
|
||||
BilibiliUpInfoExample example = new BilibiliUpInfoExample();
|
||||
example.createCriteria().andUrlEqualTo(url);
|
||||
return getData(upInfoDao.selectByExample(example));
|
||||
}
|
||||
|
||||
@Override
|
||||
public BilibiliUpInfo queryUpToRoomId(int roomId) {
|
||||
BilibiliUpInfoExample example = new BilibiliUpInfoExample();
|
||||
example.createCriteria().andRoomidEqualTo(roomId);
|
||||
return getData(upInfoDao.selectByExample(example));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public List<BilibiliLiveData> getLiveData() {
|
||||
return liveDataDao.selectByExample(new BilibiliLiveDataExample());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<BilibiliLiveInfo> getLiveInfo() {
|
||||
return liveInfoDao.selectByExample(new BilibiliLiveInfoExample());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addUpInfo(BilibiliUpInfo upData) {
|
||||
BilibiliUpInfoExample example = new BilibiliUpInfoExample();
|
||||
example.createCriteria().andRoomidEqualTo(upData.getRoomid());
|
||||
if (upInfoDao.selectByExample(example).isEmpty()) {
|
||||
return upInfoDao.insert(upData) > 0;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateUpInfo(BilibiliUpInfo upData) {
|
||||
return upInfoDao.updateByPrimaryKey(upData) > 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean deleteUp(BilibiliUpInfo upData) {
|
||||
BilibiliUpInfoExample example = new BilibiliUpInfoExample();
|
||||
example.createCriteria().andRoomidEqualTo(upData.getRoomid());
|
||||
return upInfoDao.deleteByExample(example) > 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addLiveData(BilibiliLiveData data) {
|
||||
if (!isLive()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return liveDataDao.insert(data) > 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addLiveInfo(BilibiliLiveInfo data) {
|
||||
if (!isLive()) {
|
||||
return false;
|
||||
}
|
||||
return liveInfoDao.insert(data) > 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<BilibiliLiveData> queryLiveData(int roomId, Date startTime, Date endTime, String[] type) {
|
||||
return liveDataDao.queryLiveData(roomId, startTime, endTime, type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int queryGiftSize(int roomId, int giftId, Date startTime, Date endTime) {
|
||||
int count = 0;
|
||||
for (BilibiliLiveData liveData : liveDataDao.queryTimeOfRoomid(roomId, giftId, startTime, endTime)) {
|
||||
count += liveData.getGiftindex() == null ? 0 : liveData.getGiftindex();
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Integer> queryGiftSize(int roomId, Date startTime, Date endTime) {
|
||||
Map<String, Integer> map = new HashMap<>();
|
||||
List<BilibiliLiveData> list = liveDataDao.queryTimeOfRoomid(roomId, -1, startTime, endTime);
|
||||
for (BilibiliLiveData liveData : list) {
|
||||
if (map.containsKey(liveData.getGiftname())) {
|
||||
map.put(liveData.getGiftname(), map.get(liveData.getGiftname()) + (liveData.getGiftindex() == null ? 0 : liveData.getGiftindex()));
|
||||
} else {
|
||||
map.put(liveData.getGiftname(), 1);
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int queryPriceSize(int roomId, Date startTime, Date endTime) {
|
||||
int count = 0;
|
||||
for (BilibiliLiveData liveData : liveDataDao.queryTimeOfRoomid(roomId, -1, startTime, endTime)) {
|
||||
count += liveData.getPrice() == null ? 0 : liveData.getPrice();
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BilibiliLiveData queryGiftOfId(int giftId) {
|
||||
BilibiliLiveDataExample example = new BilibiliLiveDataExample();
|
||||
example.createCriteria().andGiftidEqualTo(giftId);
|
||||
List<BilibiliLiveData> list = liveDataDao.selectByExample(example);
|
||||
if (list.isEmpty()) {
|
||||
return null;
|
||||
} else {
|
||||
return list.get(0);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int queryGiftUserToDistinct(BilibiliUpInfo upData, Date startTime, Date endTime, String[] type) {
|
||||
return liveDataDao.queryGiftUserToDistinct(upData.getRoomid(), -1, startTime, endTime, type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int queryPopularCount(BilibiliUpInfo upData, Date startTime, Date endTime) {
|
||||
int count = 0;
|
||||
for (BilibiliLiveInfo info : liveInfoDao.queryTimeOfRoomid(upData.getRoomid(), startTime, endTime)) {
|
||||
count += info.getPopular();
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int queryUserCount(BilibiliUpInfo upData, Date startTime, Date endTime) {
|
||||
int count = 0;
|
||||
for (BilibiliLiveInfo info : liveInfoDao.queryTimeOfRoomid(upData.getRoomid(), startTime, endTime)) {
|
||||
count += (info.getUserindex() + info.getVipuserindex());
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Integer> queryUserGiftCount(BilibiliUpInfo upData, Date startTime, Date endTime) {
|
||||
Map<String, Integer> map = new HashMap<>();
|
||||
List<BilibiliLiveInfo> list = liveInfoDao.queryTimeOfRoomid(upData.getRoomid(), startTime, endTime);
|
||||
for (BilibiliLiveInfo info : list) {
|
||||
map.put(new SimpleDateFormat("HH").format(info.getSubtime()), info.getGiftuser() == null ? 0 : info.getGiftuser());
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<BilibiliLiveInfo> queryLiveInfo(BilibiliUpInfo upInfo, Date startTime, Date endTime) {
|
||||
return liveInfoDao.queryTimeOfRoomid(upInfo.getRoomid(), startTime, endTime);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Map<String, BigDecimal>> queryTimeGroup(int roomId, Date startTime, Date endTime) {
|
||||
return liveDataDao.queryPriceTimeGroup(roomId, startTime, endTime);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Map<String, BigDecimal>> queryGiftTimeGroup(int roomId, Date startTime, Date endTime) {
|
||||
return liveDataDao.queryGiftTimeGroup(roomId, startTime, endTime);
|
||||
}
|
||||
|
||||
public boolean isLive() {
|
||||
String flag = configService.getConfig(Config.BILI_LIVE_FLAG);
|
||||
if (flag != null) {
|
||||
return !"0".equals(flag);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
package com.yutou.bilibili.BiliBili.Tools;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.yutou.bilibili.BiliBili.Datas.BiliBiliUpData;
|
||||
import com.yutou.bilibili.BiliBili.LiveUtils;
|
||||
import com.yutou.bilibili.mybatis.Bili.mybatis.model.BilibiliUpInfo;
|
||||
import com.yutou.bilibili.sqlite.BiliBiliLiveDatabasesManager;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
public class BiliTools {
|
||||
public static BilibiliUpInfo checkout(BilibiliUpInfo info) {
|
||||
if (StringUtils.isEmpty(info.getUrl())) {
|
||||
return null;
|
||||
}
|
||||
if (!info.getUrl().contains("http")) {
|
||||
info.setUrl("https://live.bilibili.com/" + info.getUrl());
|
||||
}
|
||||
BilibiliUpInfo data = getBiliUpInfo(info.getUrl());
|
||||
info.setUrl(data.getUrl());
|
||||
info.setRoomid(data.getRoomid());
|
||||
info.setName(data.getName());
|
||||
return info;
|
||||
}
|
||||
|
||||
public static BilibiliUpInfo getBiliUpInfo(String url) {
|
||||
if(!url.startsWith("http")){
|
||||
url="https://live.bilibili.com/"+url;
|
||||
}
|
||||
BilibiliUpInfo upData = new BilibiliUpInfo();
|
||||
JSONObject json = LiveUtils.http_get("https://api.live.bilibili.com/xlive/web-room/v1/index/getInfoByRoom?room_id=" + url.replace("https://live.bilibili.com/", "").split("\\?")[0]);
|
||||
com.yutou.bilibili.Tools.Log.i(json);
|
||||
String roomId = json.getJSONObject("data").getJSONObject("room_info").getInteger("room_id") + "";
|
||||
String name = json.getJSONObject("data").getJSONObject("anchor_info").getJSONObject("base_info").getString("uname");
|
||||
upData.setRoomid(Integer.parseInt(roomId));
|
||||
upData.setName(name);
|
||||
upData.setUrl(url);
|
||||
upData.setMid(LiveUtils.getMid(upData.getRoomid()));
|
||||
return upData;
|
||||
}
|
||||
}
|
||||
@@ -1,64 +0,0 @@
|
||||
package com.yutou.bilibili.BiliBili.Tools;
|
||||
|
||||
import com.yutou.bilibili.BiliBili.Services.IBiliBiliLiveService;
|
||||
import com.yutou.bilibili.mybatis.Bili.mybatis.model.BilibiliLiveInfo;
|
||||
import com.yutou.bilibili.mybatis.Bili.mybatis.model.BilibiliUpInfo;
|
||||
import org.java_websocket.client.WebSocketClient;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.Resource;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Timer;
|
||||
|
||||
public class LiveT implements ApplicationContextAware {
|
||||
private static ApplicationContext applicationContext = null;
|
||||
|
||||
IBiliBiliLiveService service;
|
||||
|
||||
|
||||
public static List<LiveT> list=new ArrayList<>();
|
||||
int v;
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
com.yutou.bilibili.Tools.Log.i("初始化2");
|
||||
}
|
||||
|
||||
public LiveT() {
|
||||
com.yutou.bilibili.Tools.Log.i("初始化");
|
||||
}
|
||||
|
||||
public void add(int a){
|
||||
|
||||
v=a;
|
||||
BilibiliUpInfo info=new BilibiliUpInfo();
|
||||
info.setRoomid(a);
|
||||
info.setOfflinelistening(0);
|
||||
info.setSavedanmu(0);
|
||||
info.setEnable(0);
|
||||
info.setLive(0);
|
||||
info.setSavelive(0);
|
||||
info.setName("a"+a);
|
||||
info.setUrl("#");
|
||||
service=getBean(IBiliBiliLiveService.class);
|
||||
service.addUpInfo(info);
|
||||
}
|
||||
public int get(){
|
||||
return v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||
if(LiveT.applicationContext==null){
|
||||
LiveT.applicationContext=applicationContext;
|
||||
}
|
||||
}
|
||||
public static <T> T getBean(Class<T> clazz){
|
||||
return applicationContext.getBean(clazz);
|
||||
}
|
||||
}
|
||||
@@ -1,243 +0,0 @@
|
||||
package com.yutou.bilibili.BiliBili.Tools;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.yutou.bilibili.BiliBili.Datas.AppData;
|
||||
import com.yutou.bilibili.BiliBili.Live;
|
||||
import com.yutou.bilibili.BiliBili.LiveUtils;
|
||||
import com.yutou.bilibili.QQBot.QQBotManager;
|
||||
import com.yutou.bilibili.Tools.*;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.*;
|
||||
|
||||
public class SaveLive {
|
||||
private static SaveLive live;
|
||||
private final List<String> saveList = new ArrayList<>();
|
||||
private final Map<Integer, DownloadThread> downloads = new HashMap<>();
|
||||
private final Map<Integer, Timer> heartbeats = new HashMap<>();
|
||||
|
||||
public static void main(String[] args) {
|
||||
}
|
||||
|
||||
private SaveLive() {
|
||||
}
|
||||
|
||||
public static SaveLive getInstance() {
|
||||
if (live == null) {
|
||||
live = new SaveLive();
|
||||
}
|
||||
return live;
|
||||
}
|
||||
|
||||
public void addLive(Live live) {
|
||||
if (saveList.contains(live.getInfo().getRoomid() + "")) {
|
||||
return;
|
||||
}
|
||||
if (live.geData().checkLiveStatus() == 0) {
|
||||
return;
|
||||
}
|
||||
start(live.getInfo().getRoomid());
|
||||
}
|
||||
|
||||
public void startLive(int roomId) {
|
||||
start(roomId);
|
||||
saveList.add(roomId + "");
|
||||
}
|
||||
|
||||
|
||||
public boolean checkLive(int roomId) {
|
||||
return saveList.contains(roomId + "");
|
||||
}
|
||||
|
||||
public File getLiveFile(int roomId) {
|
||||
return downloads.get(roomId).liveFile;
|
||||
}
|
||||
|
||||
private long timer = 0;
|
||||
|
||||
public void stop(int roomId) {
|
||||
com.yutou.bilibili.Tools.Log.i("t停止录播:" + roomId + " time=" + (System.currentTimeMillis() - timer));
|
||||
saveList.remove(roomId + "");
|
||||
|
||||
if (downloads.containsKey(roomId)) {
|
||||
downloads.get(roomId).isSave = false;
|
||||
downloads.remove(roomId);
|
||||
}
|
||||
|
||||
if (heartbeats.containsKey(roomId)) {
|
||||
heartbeats.get(roomId).cancel();
|
||||
heartbeats.remove(roomId);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void start(int roomId) {
|
||||
if (checkLive(roomId)) {
|
||||
return;
|
||||
}
|
||||
timer = System.currentTimeMillis();
|
||||
DownloadThread thread = new DownloadThread(roomId);
|
||||
downloads.put(roomId, thread);
|
||||
saveList.add(roomId + "");
|
||||
}
|
||||
|
||||
public List<String> getLiveList() {
|
||||
return saveList;
|
||||
}
|
||||
|
||||
private class DownloadThread extends Thread {
|
||||
int roomId = 0;
|
||||
boolean isSave = true;
|
||||
Timer heartbeat;
|
||||
File liveFile;
|
||||
|
||||
public DownloadThread(int roomId) {
|
||||
this.roomId = roomId;
|
||||
isSave = true;
|
||||
|
||||
start();
|
||||
|
||||
}
|
||||
|
||||
private String getLiveUrl(int roomId) {
|
||||
JSONObject json = LiveUtils.http_get(String.format("https://api.live.bilibili.com/xlive/web-room/v1/playUrl/playUrl?cid=%d&platform=web", roomId));
|
||||
if (json != null && json.getInteger("code") == 0) {
|
||||
return json.getJSONObject("data").getJSONArray("durl").getJSONObject(0).getString("url");
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
super.run();
|
||||
String url = getLiveUrl(roomId);
|
||||
try {
|
||||
if (AppData.LIVE_SAVE_FFMPEG) {
|
||||
ffmpegDownload(url);
|
||||
} else {
|
||||
httpDownload(url);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
com.yutou.bilibili.Tools.Log.e(e);
|
||||
if (!(e instanceof FileNotFoundException)) {
|
||||
Log.i("录制发生意外:" + e.getMessage());
|
||||
QQBotManager.getInstance().sendMessage("录制发生意外:" + e.getLocalizedMessage());
|
||||
}
|
||||
}
|
||||
SaveLive.this.stop(roomId);
|
||||
|
||||
}
|
||||
|
||||
private void ffmpegDownload(String url) throws Exception {
|
||||
liveFile = new File(String.format("%slive%s%s%s[%s]%d.mp4",
|
||||
ConfigTools.load(ConfigTools.CONFIG, "liveSavePath"),
|
||||
File.separator,
|
||||
AppTools.getToDayTime(),
|
||||
File.separator,
|
||||
AppTools.getToDayNowTimeToString().replace(":", ""),
|
||||
roomId));
|
||||
if (!liveFile.exists()) {
|
||||
liveFile.mkdirs();
|
||||
liveFile.delete();
|
||||
}
|
||||
String exec = String.format("%s -user_agent \"%s\" -cookies \"%s\" -headers \"%s\" -i \"%s\" -threads 8 -c:v copy -y \"%s\" %s ",
|
||||
AppData.FFMPEG,
|
||||
AppData.BILIBILI_HEADERS,
|
||||
LiveUtils.getCookie(),
|
||||
"Referer:https://live.bilibili.com",
|
||||
url,
|
||||
liveFile.getAbsolutePath(),
|
||||
""
|
||||
);
|
||||
System.out.println(exec);
|
||||
boolean isPrint = false;
|
||||
Process process = AppTools.exec(exec);
|
||||
InputStream inputStream = process.getErrorStream();
|
||||
byte[] bytes = new byte[2048];
|
||||
while (inputStream.read(bytes) > -1 && isSave) {
|
||||
//System.out.println(new String(bytes,StandardCharsets.UTF_8));
|
||||
if (!isPrint) {
|
||||
com.yutou.bilibili.Tools.Log.i("开始录制:" + roomId);
|
||||
QQBotManager.getInstance().sendMessage(roomId + " 已启动录制");
|
||||
isPrint = true;
|
||||
}
|
||||
}
|
||||
process.destroy();
|
||||
System.out.println("----------------stop ffmpeg");
|
||||
inputStream.close();
|
||||
com.yutou.bilibili.Tools.Log.i("录制完成:" + roomId);
|
||||
QQBotManager.getInstance().sendMessage("录制完成:" + roomId);
|
||||
}
|
||||
|
||||
private void httpDownload(String url) throws Exception {
|
||||
HttpURLConnection connection = LiveUtils.getBiliHttpGet(url, LiveUtils.getCookie());
|
||||
connection.setReadTimeout(5000);
|
||||
connection.setConnectTimeout(5000);
|
||||
heartbeat = new Timer();
|
||||
//Heartbeat beat = new Heartbeat();
|
||||
heartbeat.schedule(new Heartbeat(), 0, 30000);
|
||||
heartbeats.put(roomId, heartbeat);
|
||||
//heartbeats.add(beat);
|
||||
InputStream inputStream = connection.getInputStream();
|
||||
liveFile = new File(String.format("%slive%s%s%s[%s]%d.mp4",
|
||||
ConfigTools.load(ConfigTools.CONFIG, "liveSavePath"),
|
||||
File.separator,
|
||||
AppTools.getToDayTime(),
|
||||
File.separator,
|
||||
AppTools.getToDayNowTimeToString().replace(":", ""),
|
||||
roomId));
|
||||
if (!liveFile.exists()) {
|
||||
liveFile.mkdirs();
|
||||
liveFile.delete();
|
||||
}
|
||||
boolean isPrint = false;
|
||||
FileOutputStream outputStream = new FileOutputStream(liveFile);
|
||||
int len;
|
||||
byte[] bytes = new byte[1024];
|
||||
while ((len = inputStream.read(bytes)) != -1 && isSave) {
|
||||
outputStream.write(bytes, 0, len);
|
||||
outputStream.flush();
|
||||
if (!isPrint) {
|
||||
com.yutou.bilibili.Tools.Log.i("开始录制:" + roomId);
|
||||
QQBotManager.getInstance().sendMessage(roomId + " 已启动录制");
|
||||
isPrint = true;
|
||||
}
|
||||
}
|
||||
outputStream.close();
|
||||
inputStream.close();
|
||||
com.yutou.bilibili.Tools.Log.i("录制完成:" + roomId + " save = " + isSave + " len = " + len);
|
||||
QQBotManager.getInstance().sendMessage("录制完成:" + roomId + " save = " + isSave + " len = " + len);
|
||||
}
|
||||
|
||||
private class Heartbeat extends TimerTask {
|
||||
int nextInterval = 1;
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
JSONObject hearBeat;
|
||||
JSONObject userHear = new JSONObject();
|
||||
JSONObject cookie = JSONObject.parseObject(LiveUtils.getFile("cookies.json"));
|
||||
hearBeat = LiveUtils.http_get("https://api.live.bilibili.com/relation/v1/Feed/heartBeat");
|
||||
com.yutou.bilibili.Tools.Log.i(hearBeat + " \t");
|
||||
hearBeat = LiveUtils.http_get(String.format("https://live-trace.bilibili.com/xlive/rdata-interface/v1/heartbeat/webHeartBeat?hb=%s&pf=web", URLEncoder.encode(new String(Base64.getEncoder().encode(String.format("%d|%d|1|0", nextInterval, roomId).getBytes(StandardCharsets.UTF_8))), "UTF-8")));
|
||||
com.yutou.bilibili.Tools.Log.i(hearBeat + "\t");
|
||||
nextInterval = hearBeat.getJSONObject("data").getInteger("next_interval");
|
||||
com.yutou.bilibili.Tools.Log.i("next = " + nextInterval);
|
||||
userHear.put("csrf_token", cookie.getString("bili_jct"));
|
||||
userHear.put("csrf", cookie.getString("bili_jct"));
|
||||
userHear.put("visit_id", "");
|
||||
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) {
|
||||
com.yutou.bilibili.Tools.Log.e(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,5 @@
|
||||
package com.yutou.bilibili;
|
||||
|
||||
import com.yutou.bilibili.BiliBili.Live;
|
||||
import com.yutou.bilibili.BiliBili.Tools.LiveT;
|
||||
import com.yutou.bilibili.QQBot.QQBotManager;
|
||||
import com.yutou.bilibili.Tools.ExcelUtils;
|
||||
import com.yutou.bilibili.Tools.ServiceTools;
|
||||
@@ -9,14 +7,13 @@ import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
@Import({Live.class,ServiceTools.class, ExcelUtils.class, QQBotManager.class})
|
||||
@Import({ServiceTools.class, ExcelUtils.class, QQBotManager.class})
|
||||
@SpringBootApplication
|
||||
public class BilibiliApplication {
|
||||
|
||||
public static String version="0.8.4";
|
||||
|
||||
public static void main(String[] args) {
|
||||
QQBotManager.getInstance().init();
|
||||
SpringApplication.run(BilibiliApplication.class, args);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
package com.yutou.bilibili.Controllers;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.yutou.bilibili.BiliBili.Datas.AppData;
|
||||
import com.yutou.bilibili.QQBot.QQBotManager;
|
||||
import com.yutou.bilibili.Services.ISystemConfigService;
|
||||
import com.yutou.bilibili.Tools.Config;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLDecoder;
|
||||
|
||||
@@ -125,8 +125,6 @@ public class SystemConfigController {
|
||||
@ResponseBody
|
||||
@RequestMapping("/system/qq/login.do")
|
||||
public JSONObject loginQQ() {
|
||||
QQBotManager.getInstance().stop();
|
||||
QQBotManager.getInstance().init();
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("code", 0);
|
||||
json.put("msg", "ok");
|
||||
|
||||
@@ -1,29 +1,24 @@
|
||||
package com.yutou.bilibili.Controllers;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.yutou.bilibili.BiliBili.Services.IBiliBiliLiveService;
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.alibaba.fastjson2.JSONArray;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.yutou.bilibili.Services.ISystemConfigService;
|
||||
import com.yutou.bilibili.BiliBili.Tools.BiliTools;
|
||||
import com.yutou.bilibili.Services.IUserService;
|
||||
import com.yutou.bilibili.Tools.AppTools;
|
||||
import com.yutou.bilibili.Tools.Config;
|
||||
import com.yutou.bilibili.Tools.RedisTools;
|
||||
import com.yutou.bilibili.mybatis.Bili.mybatis.model.BilibiliUpInfo;
|
||||
import com.yutou.bilibili.mybatis.model.UBiliUp;
|
||||
import com.yutou.bilibili.mybatis.model.UUser;
|
||||
import org.apache.tomcat.util.security.MD5Encoder;
|
||||
import com.yutou.utils.AppTools;
|
||||
import com.yutou.utils.RedisTools;
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
|
||||
@@ -31,8 +26,7 @@ import java.util.UUID;
|
||||
public class UserController {
|
||||
@Resource
|
||||
IUserService service;
|
||||
@Resource
|
||||
IBiliBiliLiveService biliService;
|
||||
|
||||
@Resource
|
||||
ISystemConfigService configService;
|
||||
|
||||
@@ -51,7 +45,7 @@ public class UserController {
|
||||
user.setPower("[1,2,3,5,6,7]");
|
||||
user.setSubtime(new Date());
|
||||
user.setLogintoken(UUID.randomUUID().toString());
|
||||
RedisTools.set(user.getLogintoken(),user.getId()+"",30*24*60);
|
||||
RedisTools.set(user.getLogintoken(), user.getId() + "", 30 * 24 * 60);
|
||||
AppTools.setCookie(response, "login", user.getLogintoken(), -1);
|
||||
boolean flag = service.reg(user);
|
||||
if (flag) {
|
||||
@@ -72,7 +66,7 @@ public class UserController {
|
||||
if (service.login(user.getUser(), user.getPassword())) {
|
||||
user = service.getUser(user.getUser());
|
||||
//user.setLogintoken(UUID.randomUUID().toString());
|
||||
RedisTools.set(user.getLogintoken(),user.getId()+"",30*24*60);
|
||||
RedisTools.set(user.getLogintoken(), user.getId() + "", 30 * 24 * 60);
|
||||
AppTools.setCookie(response, "login", user.getLogintoken(), -1);
|
||||
json.put("msg", "登陆成功");
|
||||
json.put("power", user.getPower());
|
||||
@@ -124,7 +118,7 @@ public class UserController {
|
||||
user.setPower(loginUser.getPower());
|
||||
user.setBiliCookie(loginUser.getBiliCookie());
|
||||
user.setSubtime(loginUser.getSubtime());
|
||||
user.setPassword(AppTools.getMD5 (user.getPassword()));
|
||||
user.setPassword(AppTools.getMD5(user.getPassword()));
|
||||
service.update(user);
|
||||
json.put("code", 0);
|
||||
json.put("msg", "修改成功");
|
||||
@@ -156,37 +150,6 @@ public class UserController {
|
||||
@RequestMapping("/user/up/set/add.do")
|
||||
public JSONObject addUp(HttpServletRequest request, BilibiliUpInfo info) {
|
||||
JSONObject json = new JSONObject();
|
||||
String token = AppTools.getLoginToken(request);
|
||||
if (StringUtils.isEmpty(token)) {
|
||||
json.put("code", "-1");
|
||||
json.put("msg", "未登录");
|
||||
} else {
|
||||
int roomid = 0;
|
||||
if (!info.getUrl().startsWith("http")) {
|
||||
info.setUrl("https://live.bilibili.com/" + info.getUrl());
|
||||
}
|
||||
BilibiliUpInfo tmp = biliService.queryUpToUrl(info.getUrl());
|
||||
if (tmp == null) {
|
||||
info = BiliTools.checkout(info);
|
||||
if (info != null) {
|
||||
roomid = info.getRoomid();
|
||||
info.setEnable(0);
|
||||
biliService.addUpInfo(info);
|
||||
}
|
||||
} else {
|
||||
roomid = tmp.getRoomid();
|
||||
}
|
||||
if (roomid == 0) {
|
||||
json.put("code", -1);
|
||||
json.put("msg", "添加失败");
|
||||
return json;
|
||||
}
|
||||
UBiliUp up = new UBiliUp();
|
||||
up.setUid(Integer.parseInt(RedisTools.get(token)));
|
||||
up.setRoomid(roomid);
|
||||
json.put("code", 0);
|
||||
json.put("msg", service.addUp(up) ? "添加成功" : "添加失败");
|
||||
}
|
||||
return json;
|
||||
}
|
||||
|
||||
@@ -199,14 +162,14 @@ public class UserController {
|
||||
json.put("code", -1);
|
||||
json.put("msg", "未登录");
|
||||
} else {
|
||||
UUser user = service.getUserToUid(Integer.parseInt(RedisTools.get(token)));;
|
||||
UUser user = service.getUserToUid(Integer.parseInt(RedisTools.get(token)));
|
||||
if (user == null) {
|
||||
json.put("code", -2);
|
||||
json.put("msg", "未登录");
|
||||
return json;
|
||||
}
|
||||
JSONArray powers = JSONArray.parseArray(user.getPower());
|
||||
JSONObject ujson = (JSONObject) JSON.toJSON(user);
|
||||
JSONObject ujson = JSONObject.parseObject(JSONObject.toJSONString(user));
|
||||
ujson.remove("logintoken");
|
||||
ujson.remove("biliCookie");
|
||||
ujson.remove("password");
|
||||
|
||||
@@ -1,449 +1,212 @@
|
||||
package com.yutou.bilibili.QQBot;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.yutou.bilibili.BiliBili.Controllers.RealTimeDataController;
|
||||
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.*;
|
||||
import com.yutou.bilibili.interfaces.DownloadInterface;
|
||||
import com.yutou.bilibili.mybatis.Bili.mybatis.model.BilibiliUpInfo;
|
||||
import kotlin.coroutines.CoroutineContext;
|
||||
import net.mamoe.mirai.Bot;
|
||||
import net.mamoe.mirai.BotFactory;
|
||||
import net.mamoe.mirai.event.EventHandler;
|
||||
import net.mamoe.mirai.event.GlobalEventChannel;
|
||||
import net.mamoe.mirai.event.ListeningStatus;
|
||||
import net.mamoe.mirai.event.SimpleListenerHost;
|
||||
import net.mamoe.mirai.event.events.GroupMessageEvent;
|
||||
import net.mamoe.mirai.event.events.MessageEvent;
|
||||
import net.mamoe.mirai.message.data.Image;
|
||||
import net.mamoe.mirai.message.data.MessageChainBuilder;
|
||||
import net.mamoe.mirai.utils.BotConfiguration;
|
||||
import net.mamoe.mirai.utils.ExternalResource;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.yutou.napcat.QQDatabase;
|
||||
import com.yutou.napcat.QQNumberManager;
|
||||
import com.yutou.napcat.handle.BaseHandle;
|
||||
import com.yutou.napcat.handle.MessageHandleBuild;
|
||||
import com.yutou.napcat.handle.Reply;
|
||||
import com.yutou.napcat.handle.Text;
|
||||
import com.yutou.napcat.http.NapCatApi;
|
||||
import com.yutou.napcat.model.FriendBean;
|
||||
import com.yutou.napcat.model.GroupBean;
|
||||
import com.yutou.napcat.model.SendMessageResponse;
|
||||
import com.yutou.okhttp.HttpBody;
|
||||
import com.yutou.okhttp.HttpCallback;
|
||||
import com.yutou.utils.ConfigTools;
|
||||
import okhttp3.Headers;
|
||||
import retrofit2.Response;
|
||||
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class QQBotManager implements ApplicationContextAware {
|
||||
private static ApplicationContext applicationContext;
|
||||
public class QQBotManager {
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||
if (QQBotManager.applicationContext == null) {
|
||||
QQBotManager.applicationContext = applicationContext;
|
||||
}
|
||||
}
|
||||
public static Long defGroup = 891655174L;
|
||||
public static Long defQQ = 583819556L;
|
||||
|
||||
private <T> T getBean(Class<T> tClass) {
|
||||
return applicationContext.getBean(tClass);
|
||||
}
|
||||
|
||||
private static class QQCommands {
|
||||
private final static String QQ_HELP = "!help";
|
||||
private final static String QQ_SYSTEM_LOG = "!log";
|
||||
private final static String QQ_SYSTEM_RESTART = "!restart";
|
||||
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_SAVE = "!录播列表";
|
||||
private final static String QQ_LIVE_TO_DAY_DATE = "!今日数据";
|
||||
private final static String QQ_LIVE_DATE = "!数据";
|
||||
private final static String QQ_LIVE_USE_SAVE = "!启动录播";
|
||||
private final static String QQ_LIVE_RELINK="!relink";
|
||||
}
|
||||
|
||||
private static QQBotManager botManager = null;
|
||||
private Bot bot;
|
||||
private static long qqGroup;
|
||||
private static final long qqGroup = 891655174L;
|
||||
private boolean isLogin = false;
|
||||
private static boolean isInit = false;
|
||||
|
||||
@Resource
|
||||
RealTimeDataController realTimeDataController;
|
||||
|
||||
|
||||
private QQBotManager() {
|
||||
qqGroup = ConfigTools.load(ConfigTools.CONFIG, "qq_group",long.class,950075833L);
|
||||
}
|
||||
public void stop(){
|
||||
if(bot!=null){
|
||||
bot.close();
|
||||
bot=null;
|
||||
}
|
||||
}
|
||||
public void init() {
|
||||
if (!ConfigTools.load(ConfigTools.CONFIG, "qq_bot",boolean.class)) {
|
||||
return;
|
||||
}
|
||||
new Thread(() -> {
|
||||
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 (!((boolean) ConfigTools.load(ConfigTools.CONFIG, "qq_debug"))) {
|
||||
noBotLog();
|
||||
noNetworkLog();
|
||||
}
|
||||
}
|
||||
});
|
||||
//Events.registerEvents(bot, new MessageListener());
|
||||
GlobalEventChannel.INSTANCE.registerListenerHost(new MessageListener());
|
||||
Log.i("准备登陆");
|
||||
bot.login();
|
||||
Log.i("登陆成功");
|
||||
Boolean isRun = ConfigTools.load(ConfigTools.CONFIG, "qq_bot", Boolean.class);
|
||||
if (isRun != null && isRun) {
|
||||
isLogin = true;
|
||||
isInit = true;
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
Log.e(e);
|
||||
}
|
||||
String str = sendMessage("姬妻酱上线拉~");
|
||||
Log.i(str);
|
||||
|
||||
}
|
||||
}).start();
|
||||
bot.join();
|
||||
|
||||
}).start();
|
||||
|
||||
init();
|
||||
}
|
||||
}
|
||||
|
||||
public static QQBotManager getInstance() {
|
||||
private void init() {
|
||||
NapCatApi.getGroupApi().getGroupList().enqueue(new HttpCallback<List<GroupBean>>() {
|
||||
@Override
|
||||
public void onResponse(Headers headers,int code, String status, List<GroupBean> response, String rawResponse) {
|
||||
for (GroupBean groupBean : response) {
|
||||
QQDatabase.addGroup(groupBean.getGroupId(), groupBean);
|
||||
QQNumberManager.getManager().addNumber(groupBean.getGroupId(), true);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Throwable throwable) {
|
||||
|
||||
}
|
||||
});
|
||||
NapCatApi.getFriendApi().getFriendList().enqueue(new HttpCallback<List<FriendBean>>() {
|
||||
@Override
|
||||
public void onResponse(Headers headers,int code, String status, List<FriendBean> response, String rawResponse) {
|
||||
for (FriendBean friendBean : response) {
|
||||
QQDatabase.addUser(friendBean.getUserId(), friendBean);
|
||||
QQNumberManager.getManager().addNumber(friendBean.getUserId(), false);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Throwable throwable) {
|
||||
|
||||
}
|
||||
});
|
||||
NapCatApi.getUtilsApi().getLoginInfo().enqueue(new HttpCallback<FriendBean>() {
|
||||
@Override
|
||||
public void onResponse(Headers headers,int code, String status, FriendBean response, String rawResponse) {
|
||||
QQDatabase.setMe(response);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Throwable throwable) {
|
||||
|
||||
}
|
||||
});
|
||||
isInit = true;
|
||||
}
|
||||
|
||||
public synchronized static QQBotManager getInstance() {
|
||||
if (botManager == null && !isInit) {
|
||||
botManager = new QQBotManager();
|
||||
}
|
||||
return botManager;
|
||||
}
|
||||
|
||||
public boolean isLogin() {
|
||||
return isLogin;
|
||||
}
|
||||
|
||||
private Image getImage(File file) {
|
||||
if (bot != null) {
|
||||
ExternalResource resource=ExternalResource.create(file);
|
||||
Image image;
|
||||
image= Objects.requireNonNull(bot.getGroup(qqGroup)).uploadImage(resource);
|
||||
try {
|
||||
resource.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return image;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private String getNotLoginQQ() {
|
||||
return "没有登录QQ";
|
||||
}
|
||||
|
||||
|
||||
public String sendMessage(String text) {
|
||||
if (bot != null) {
|
||||
try {
|
||||
return Objects.requireNonNull(bot.getGroup(qqGroup)).sendMessage(text).toString();
|
||||
} catch (Exception e) {
|
||||
com.yutou.bilibili.Tools.Log.e(e);
|
||||
}
|
||||
}
|
||||
return getNotLoginQQ();
|
||||
public SendMessageResponse sendPrivateMessage(Long qq, BaseHandle<?>... items) {
|
||||
return sendMessage(true, qq, items);
|
||||
}
|
||||
|
||||
public String sendMessage(Long group, String text) {
|
||||
if (bot != null) {
|
||||
try {
|
||||
return Objects.requireNonNull(bot.getGroup(group)).sendMessage(text).toString();
|
||||
} catch (Exception e) {
|
||||
com.yutou.bilibili.Tools.Log.e(e);
|
||||
}
|
||||
}
|
||||
return getNotLoginQQ();
|
||||
private SendMessageResponse sendGroupMessage(Long group, BaseHandle<?>... items) {
|
||||
return sendMessage(false, group, items);
|
||||
}
|
||||
|
||||
public void sendMessage(Long group, MessageChainBuilder builder) {
|
||||
if (bot != null) {
|
||||
Objects.requireNonNull(bot.getGroup(group)).sendMessage(builder.asMessageChain());
|
||||
}
|
||||
public SendMessageResponse sendMessage(boolean user, Long qq, String msg) {
|
||||
return sendMessage(user, qq, new Text(msg));
|
||||
}
|
||||
|
||||
public SendMessageResponse sendMessage(Long qq, BaseHandle<?>... items) {
|
||||
return sendMessage(QQDatabase.checkFriend(qq), qq, Arrays.asList(items));
|
||||
}
|
||||
|
||||
public String sendMessage(File imageFile, String text) {
|
||||
public SendMessageResponse sendMessage(boolean isUser, Long qq, BaseHandle<?>... items) {
|
||||
return sendMessage(isUser, qq, Arrays.asList(items));
|
||||
}
|
||||
|
||||
public SendMessageResponse sendMessage(boolean isUser, Long qq, List<BaseHandle<?>> items) {
|
||||
try {
|
||||
if (bot != null) {
|
||||
Image image = getImage(imageFile);
|
||||
MessageChainBuilder builder = new MessageChainBuilder();
|
||||
if (image != null) {
|
||||
builder.append(image);
|
||||
if(!ConfigTools.load(ConfigTools.CONFIG,ConfigTools.QQ, Boolean.class)){
|
||||
return null;
|
||||
}
|
||||
MessageHandleBuild handleBuild = MessageHandleBuild
|
||||
.create()
|
||||
.setGroup(!isUser)
|
||||
.setQQNumber(qq);
|
||||
for (BaseHandle<?> item : items) {
|
||||
if (item.getData() == null) {
|
||||
continue;
|
||||
}
|
||||
builder.append(text);
|
||||
return Objects.requireNonNull(bot.getGroup(qqGroup)).sendMessage(builder.asMessageChain()).toString();
|
||||
if (item instanceof Reply) {
|
||||
if (((Reply) item).getData().getId() == -1) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
handleBuild.add(item);
|
||||
}
|
||||
Response<HttpBody<SendMessageResponse>> response;
|
||||
if (isUser) {
|
||||
response = NapCatApi.getMessageApi().sendPrivateMsg(
|
||||
handleBuild.build()
|
||||
).execute();
|
||||
} else {
|
||||
response = NapCatApi.getMessageApi().sendGroupMsg(
|
||||
handleBuild.build()
|
||||
).execute();
|
||||
}
|
||||
if (response.body() != null) {
|
||||
return response.body().getData();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
com.yutou.bilibili.Tools.Log.e(e);
|
||||
e.printStackTrace();
|
||||
SendMessageResponse response = new SendMessageResponse();
|
||||
response.setId(-1);
|
||||
response.setE(e.getMessage());
|
||||
return response;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public String sendMessage(String text) {
|
||||
|
||||
return getNotLoginQQ();
|
||||
}
|
||||
|
||||
public String sendMessage(List<File> imgs, String text) {
|
||||
if (bot != null) {
|
||||
MessageChainBuilder builder = new MessageChainBuilder();
|
||||
for (File img : imgs) {
|
||||
builder.append(Objects.requireNonNull(getImage(img)));
|
||||
}
|
||||
builder.append(text);
|
||||
return Objects.requireNonNull(bot.getGroup(qqGroup)).sendMessage(builder.asMessageChain()).toString();
|
||||
}
|
||||
return getNotLoginQQ();
|
||||
public SendMessageResponse sendMessage(Long group, String text) {
|
||||
return sendMessage(QQDatabase.checkFriend(group), group, new Text(text));
|
||||
}
|
||||
|
||||
private static List<String> getImages(String str) {
|
||||
List<String> list = new ArrayList<>();
|
||||
String regex = "<img(.*?)/img>";
|
||||
Pattern pattern = Pattern.compile(regex);
|
||||
Matcher matcher = pattern.matcher(str);
|
||||
while (matcher.find()) {
|
||||
list.add(matcher.group().replace("<img", "")
|
||||
.replace("/img>", "")
|
||||
.trim());
|
||||
public void sendMessage(Long group, StringBuilder builder) {
|
||||
if (QQNumberManager.getManager().isGroup(group)) {
|
||||
System.out.println("发群");
|
||||
} else {
|
||||
System.out.println("发个人");
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public void sendMessage(File imageFile, Long qq, String text) {
|
||||
sendMessage(imageFile, qq, null, text);
|
||||
}
|
||||
|
||||
public void sendMessage(File imageFile, Long qq, String replyMessageId, String text) {
|
||||
|
||||
}
|
||||
|
||||
public void sendMessage(File imageFile, String text) {
|
||||
}
|
||||
|
||||
public void sendMessage(List<File> imgs, Long qq, String text) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
getInstance();
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("t1", 3234567890L);
|
||||
System.out.println("json = " + json);
|
||||
|
||||
String tmp = json.toString();
|
||||
JSONObject json2 = JSONObject.parseObject(tmp);
|
||||
System.out.println("json2 = " + json2);
|
||||
}
|
||||
|
||||
private static class MessageListener extends SimpleListenerHost {
|
||||
@Override
|
||||
public void handleException(@NotNull CoroutineContext context, @NotNull Throwable exception) {
|
||||
// super.handleException(context, exception);
|
||||
exception.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
@EventHandler
|
||||
public ListeningStatus onMessage(MessageEvent event) {
|
||||
String message=event.getMessage().contentToString();
|
||||
String msg = message.replace("!", "!").toLowerCase();
|
||||
msg=msg.trim();
|
||||
StringBuilder builder = new StringBuilder();
|
||||
JSONObject json;
|
||||
String[] cmd = new String[0];
|
||||
int _roomId = 0;
|
||||
switch (msg) {
|
||||
case QQCommands.QQ_GET_IP:
|
||||
json = JSONObject.parseObject(HttpTools.get("https://api.asilu.com/ip/"));
|
||||
String ip = json.getString("ip");
|
||||
getInstance().sendMessage("服务器IP:" + ip);
|
||||
break;
|
||||
case QQCommands.QQ_GET_VERSION:
|
||||
sendVersion();
|
||||
break;
|
||||
case QQCommands.QQ_SYSTEM_RESTART:
|
||||
getInstance().sendMessage("正在重启服务");
|
||||
System.out.println("结束进程");
|
||||
try {
|
||||
AppTools.exec("cd /home/yutou/public/servier/biliob && ./start.sh");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
break;
|
||||
case QQCommands.QQ_LIVE_LIST:
|
||||
builder.append("当前正在记录数据的直播间:");
|
||||
builder.append("\n");
|
||||
for (Live live : Live.lives.values()) {
|
||||
Live.LiveInfo info=live.getLiveInfo();
|
||||
if (live.geData().getLive()==1) {
|
||||
builder.append("【直播中】");
|
||||
}
|
||||
builder
|
||||
.append("名字:").append(live.geData().getName()).append(" ")
|
||||
.append(" roomId:").append(live.geData().getRoomid()).append(" ");
|
||||
if (info != null) {
|
||||
builder.append("标题:").append(info.getTitle()).append("\n");
|
||||
} else {
|
||||
builder.append("\n");
|
||||
}
|
||||
}
|
||||
getInstance().sendMessage(builder.toString());
|
||||
break;
|
||||
case QQCommands.QQ_LIVE_SAVE:
|
||||
builder.append("当前正在录制的直播间:");
|
||||
builder.append("\n");
|
||||
for (String roomId : SaveLive.getInstance().getLiveList()) {
|
||||
BilibiliUpInfo data = new BilibiliUpInfo();
|
||||
data.setRoomid(Integer.parseInt(roomId));
|
||||
Live live = LiveUtils.liveContains(data);
|
||||
if (live != null) {
|
||||
File file = SaveLive.getInstance().getLiveFile(Integer.parseInt(roomId));
|
||||
builder
|
||||
.append("名字:").append(live.geData().getName()).append(" ")
|
||||
.append("文件大小(字节):").append(file.length()).append(" ")
|
||||
.append("roomId:").append(live.geData().getRoomid()).append("\n");
|
||||
}
|
||||
|
||||
}
|
||||
getInstance().sendMessage(builder.toString());
|
||||
break;
|
||||
case QQCommands.QQ_HELP:
|
||||
for (Field field : QQCommands.class.getDeclaredFields()) {
|
||||
try {
|
||||
field.setAccessible(true);
|
||||
builder.append(field.get(null)).append("\n");
|
||||
} catch (IllegalAccessException e) {
|
||||
com.yutou.bilibili.Tools.Log.e(e);
|
||||
}
|
||||
}
|
||||
getInstance().sendMessage(builder.toString());
|
||||
break;
|
||||
case QQCommands.QQ_LIVE_RELINK:
|
||||
for (Live live : Live.lives.values()) {
|
||||
live.stop();
|
||||
}
|
||||
break;
|
||||
default:
|
||||
if (msg.startsWith(QQCommands.QQ_LIVE_TO_DAY_DATE)) {
|
||||
try {
|
||||
cmd = msg.split(" ");
|
||||
_roomId = Integer.parseInt(cmd[1]);
|
||||
QQBotManager.getInstance().sendMessage("请稍等,正在查询...");
|
||||
sendGiftData(_roomId, null, null);
|
||||
} catch (Exception e) {
|
||||
getInstance().sendMessage("参数错误。\n使用方式: " + QQCommands.QQ_LIVE_TO_DAY_DATE + "+空格+roomId");
|
||||
}
|
||||
} else if (msg.startsWith(QQCommands.QQ_LIVE_DATE)) {
|
||||
try {
|
||||
cmd = msg.split(" ");
|
||||
_roomId = Integer.parseInt(cmd[1]);
|
||||
Date startTime = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss").parse(cmd[2]);
|
||||
Date endTime = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss").parse(cmd[3]);
|
||||
QQBotManager.getInstance().sendMessage("请稍等,正在查询...");
|
||||
sendGiftData(_roomId, startTime, endTime);
|
||||
} catch (Exception e) {
|
||||
getInstance().sendMessage("参数错误。" +
|
||||
"\n使用方式: " + QQCommands.QQ_LIVE_DATE + "+空格+roomId+起始时间+空格+结束时间" +
|
||||
"\n时间格式:年-月-日_时:分:秒 例:2021-4-1_12:00:00" +
|
||||
"\n时分秒不可省略");
|
||||
}
|
||||
} else if (msg.startsWith(QQCommands.QQ_LIVE_USE_SAVE)) {
|
||||
try {
|
||||
cmd = msg.split(" ");
|
||||
_roomId = Integer.parseInt(cmd[1]);
|
||||
if (SaveLive.getInstance().checkLive(_roomId)) {
|
||||
SaveLive.getInstance().stop(_roomId);
|
||||
}
|
||||
SaveLive.getInstance().startLive(_roomId);
|
||||
getInstance().sendMessage("已启动" + _roomId + "的录播");
|
||||
} catch (Exception e) {
|
||||
getInstance().sendMessage("参数错误。\n使用方式: " + QQCommands.QQ_LIVE_USE_SAVE + "+空格+roomId");
|
||||
}
|
||||
} else if (msg.startsWith(QQCommands.QQ_SYSTEM_LOG)) {
|
||||
try {
|
||||
cmd = message.replace("!","!").replace(QQCommands.QQ_SYSTEM_LOG,"").split(" ");
|
||||
RedisTools.set(1,"live-log",cmd[1].trim());
|
||||
getInstance().sendMessage("日志设为:"+cmd[1].trim());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}else {
|
||||
Log.i("未知指令:"+msg);
|
||||
}
|
||||
}
|
||||
return ListeningStatus.LISTENING; // 表示继续监听事件
|
||||
}
|
||||
|
||||
public void sendGiftData(int roomId, Date startTime, Date endTime) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
if (startTime == null) {
|
||||
startTime = AppTools.getToDayStartTime();
|
||||
}
|
||||
if (endTime == null) {
|
||||
endTime = AppTools.getToDayNowTime();
|
||||
}
|
||||
if (getInstance().realTimeDataController == null) {
|
||||
getInstance().realTimeDataController = getInstance().getBean(RealTimeDataController.class);
|
||||
}
|
||||
JSONObject json = getInstance().realTimeDataController.queryToDayLiveData(roomId, startTime, endTime);
|
||||
System.out.println(json);
|
||||
builder.append("当前人气:").append(json.getJSONObject("data").getInteger("popular")).append("\n");
|
||||
builder.append("普通观众入场:").append(json.getJSONObject("data").getInteger("userLength")).append("\n");
|
||||
builder.append("舰长入场:").append(json.getJSONObject("data").getInteger("vipLength")).append("\n");
|
||||
int price = 0;
|
||||
for (Object o : json.getJSONObject("data").getJSONArray("price")) {
|
||||
price += ((JSONObject) o).getInteger("price");
|
||||
}
|
||||
builder.append("金瓜子:").append(price).append(" 抽成后:").append((price / 2) / 1000).append("¥").append("\n");
|
||||
builder.append("礼物收益情况:").append("\n");
|
||||
for (Object o : json.getJSONObject("data").getJSONArray("gift")) {
|
||||
builder.append(((JSONObject) o).getString("giftName")).append(":").append(((JSONObject) o).getInteger("size")).append("\n");
|
||||
}
|
||||
getInstance().sendMessage(builder.toString());
|
||||
}
|
||||
|
||||
private List<File> files;
|
||||
private int index = 0;
|
||||
|
||||
private void sendImagesMsg(List<String> imgs, String text) {
|
||||
files = new ArrayList<>();
|
||||
index = 0;
|
||||
if (imgs.size() == 0) {
|
||||
getInstance().sendMessage(text);
|
||||
return;
|
||||
}
|
||||
for (String img : imgs) {
|
||||
Tools.download(img, new DownloadInterface() {
|
||||
@Override
|
||||
public void onDownload(File file) {
|
||||
super.onDownload(file);
|
||||
files.add(file);
|
||||
send(imgs.size(), text);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Exception e) {
|
||||
super.onError(e);
|
||||
index++;
|
||||
send(imgs.size(), text);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void send(int size, String text) {
|
||||
if ((files.size() + index) == size) {
|
||||
String str = getInstance().sendMessage(files, text);
|
||||
Log.i("str = " + str);
|
||||
}
|
||||
}
|
||||
|
||||
private void sendVersion() {
|
||||
String msg = "软件版本:" + BilibiliApplication.version;
|
||||
QQBotManager.getInstance().sendMessage(msg);
|
||||
}
|
||||
|
||||
public boolean isLogin() {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -4,9 +4,9 @@ import com.yutou.bilibili.Services.ISystemConfigService;
|
||||
import com.yutou.bilibili.mybatis.dao.SConfigDao;
|
||||
import com.yutou.bilibili.mybatis.model.SConfig;
|
||||
import com.yutou.bilibili.mybatis.model.SConfigExample;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
@Service("SystemConfigService")
|
||||
|
||||
@@ -1,20 +1,17 @@
|
||||
package com.yutou.bilibili.Services.impl;
|
||||
|
||||
import com.yutou.bilibili.Services.IUserService;
|
||||
import com.yutou.bilibili.Tools.AppTools;
|
||||
import com.yutou.bilibili.mybatis.dao.UBiliUpDao;
|
||||
import com.yutou.bilibili.mybatis.dao.UUserDao;
|
||||
import com.yutou.bilibili.mybatis.model.UBiliUp;
|
||||
import com.yutou.bilibili.mybatis.model.UBiliUpExample;
|
||||
import com.yutou.bilibili.mybatis.model.UUser;
|
||||
import com.yutou.bilibili.mybatis.model.UUserExample;
|
||||
import org.apache.tomcat.util.security.MD5Encoder;
|
||||
import com.yutou.utils.AppTools;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
|
||||
@Service("UserService")
|
||||
@@ -26,7 +23,7 @@ public class UserServiceImpl implements IUserService {
|
||||
@Override
|
||||
public boolean login(String uname, String password) {
|
||||
UUserExample example=new UUserExample();
|
||||
String _password=AppTools.getMD5 (password);
|
||||
String _password= AppTools.getMD5 (password);
|
||||
example.createCriteria().andUserEqualTo(uname).andPasswordEqualTo(_password);
|
||||
return !userDao.selectByExample(example).isEmpty();
|
||||
}
|
||||
|
||||
@@ -1,40 +1,8 @@
|
||||
package com.yutou.bilibili.Test;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.yutou.bilibili.BiliBili.Live;
|
||||
import com.yutou.bilibili.BiliBili.LiveUtils;
|
||||
import com.yutou.bilibili.BiliBili.Tools.LiveT;
|
||||
import com.yutou.bilibili.BiliBili.Tools.SaveLive;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
@Controller
|
||||
public class TestController {
|
||||
|
||||
@ResponseBody
|
||||
@RequestMapping("/add.do")
|
||||
public String test(int a) {
|
||||
|
||||
return "ok";
|
||||
}
|
||||
@ResponseBody
|
||||
@RequestMapping("/get.do")
|
||||
public JSONObject get(){
|
||||
JSONObject json=new JSONObject();
|
||||
JSONArray array=new JSONArray();
|
||||
json.put("size", Live.lives.size());
|
||||
for (Live live :Live.lives.values()) {
|
||||
JSONObject item=new JSONObject();
|
||||
item.put("info", JSON.toJSON(live.getInfo()));
|
||||
item.put("data",JSON.toJSON(live.geData()));
|
||||
item.put("isLive", live.geData().getLive()==1);
|
||||
array.add(item);
|
||||
}
|
||||
json.put("array",array);
|
||||
json.put("saveVideo",JSONArray.toJSON(SaveLive.getInstance().getLiveList()));
|
||||
return json;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,20 +1,23 @@
|
||||
package com.yutou.bilibili.Tools;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson2.JSONArray;
|
||||
import com.yutou.bilibili.Services.IUserService;
|
||||
import com.yutou.bilibili.mybatis.dao.PermissionDao;
|
||||
import com.yutou.bilibili.mybatis.model.Permission;
|
||||
import com.yutou.bilibili.mybatis.model.PermissionExample;
|
||||
import com.yutou.bilibili.mybatis.model.UUser;
|
||||
import com.yutou.utils.AppTools;
|
||||
import com.yutou.utils.Log;
|
||||
import com.yutou.utils.RedisTools;
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.servlet.*;
|
||||
import jakarta.servlet.annotation.WebFilter;
|
||||
import jakarta.servlet.http.Cookie;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.*;
|
||||
import javax.servlet.annotation.WebFilter;
|
||||
import javax.servlet.http.Cookie;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
@@ -59,8 +62,8 @@ public class APIFilter implements Filter {
|
||||
try {
|
||||
url = tmp.split(tmp.split("/")[tmp.split("/").length - 1])[0];
|
||||
} catch (Exception e) {
|
||||
com.yutou.bilibili.Tools.Log.e(e);
|
||||
com.yutou.bilibili.Tools.Log.i("无权限请求:" + tmp);
|
||||
Log.e(e);
|
||||
Log.i("无权限请求:" + tmp);
|
||||
return;
|
||||
}
|
||||
JSONArray powers = JSONArray.parseArray(user.getPower());
|
||||
|
||||
@@ -1,334 +0,0 @@
|
||||
package com.yutou.bilibili.Tools;
|
||||
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import javax.servlet.http.Cookie;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.xml.bind.DatatypeConverter;
|
||||
import java.io.*;
|
||||
import java.net.URLEncoder;
|
||||
import java.security.MessageDigest;
|
||||
import java.text.ParsePosition;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
|
||||
public class AppTools {
|
||||
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 ignored) {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
/**
|
||||
* 获取项目路径
|
||||
*
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
public static String getPath(HttpServletRequest request) {
|
||||
return request.getServletContext().getRealPath("/") + "/";
|
||||
}
|
||||
public static String getToDayTime() {
|
||||
return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
|
||||
} public static String getToDayTime(Date date) {
|
||||
return new SimpleDateFormat("yyyy-MM-dd").format(date);
|
||||
}
|
||||
public static Date getToDayStartTime(){
|
||||
return new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(AppTools.getToDayTime() + " " + "00:00",new ParsePosition(0));
|
||||
}
|
||||
public static Date getToDayNowTime(){
|
||||
return new Date();
|
||||
}
|
||||
public static String getToDayNowTimeToString(){
|
||||
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
|
||||
}
|
||||
public static String getToDayTimeToString(Date date){
|
||||
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
|
||||
}
|
||||
|
||||
|
||||
public static Map<String,String> getUrlParams(String url){
|
||||
Map<String,String> map=new HashMap<>();
|
||||
if(url.contains("?")){
|
||||
String param=url.split("\\?")[1];
|
||||
String[] params=param.split("&");
|
||||
for (String par : params) {
|
||||
map.put(par.split("=")[0],par.split("=")[1]);
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
/**
|
||||
* N以内的不重复随机数
|
||||
*
|
||||
* @param min 最小值
|
||||
* @param max 最大值
|
||||
* @param n
|
||||
* @return
|
||||
*/
|
||||
public static int[] randomCommon(int min, int max, int n) {
|
||||
int len = max - min + 1;
|
||||
if (max < min || n > len) {
|
||||
return new int[0];
|
||||
}
|
||||
// 初始化给定范围的待选数组
|
||||
int[] source = new int[len];
|
||||
for (int i = min; i < min + len; i++) {
|
||||
source[i - min] = i;
|
||||
}
|
||||
int[] result = new int[n];
|
||||
Random rd = new Random();
|
||||
int index = 0;
|
||||
for (int i = 0; i < result.length; i++) {
|
||||
// 待选数组0到(len-2)随机一个下标
|
||||
index = Math.abs(rd.nextInt() % len--);
|
||||
// 将随机到的数放入结果集
|
||||
result[i] = source[index];
|
||||
// 将待选数组中被随机到的数,用待选数组(len-1)下标对应的数替换
|
||||
source[index] = source[len];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
/**
|
||||
* 设置Cookie
|
||||
*
|
||||
* @param response
|
||||
* @param key
|
||||
* @param value
|
||||
* @param time
|
||||
*/
|
||||
public static void setCookie(HttpServletResponse response, String key, String value, int time) {
|
||||
Cookie cookie = new Cookie(key, value);
|
||||
if (time == -1) {
|
||||
time= 31536000;
|
||||
}
|
||||
cookie.setMaxAge(time);
|
||||
cookie.setPath("/");
|
||||
response.addCookie(cookie);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Cookie
|
||||
*
|
||||
* @param request
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
public static Cookie getCookie(HttpServletRequest request, String key) {
|
||||
Cookie[] cookies = request.getCookies();
|
||||
try {
|
||||
for (Cookie cookie : cookies) {
|
||||
if (key != null && cookie.getName().equals(key)) {
|
||||
return cookie;
|
||||
}
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除Cookie
|
||||
*
|
||||
* @param request
|
||||
* @param response
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
public static String deleteCookie(HttpServletRequest request, HttpServletResponse response, String key) {
|
||||
for (Cookie cookie : request.getCookies()) {
|
||||
if (cookie.getName().equals(key)) {
|
||||
com.yutou.bilibili.Tools.Log.i("删除key=" + key);
|
||||
cookie.setMaxAge(0);
|
||||
cookie.setPath("/");
|
||||
cookie.setValue(null);
|
||||
response.addCookie(cookie);
|
||||
}
|
||||
}
|
||||
return "ok";
|
||||
}
|
||||
public static String alphabetsInUpperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
public static String alphabetsInLowerCase = "abcdefghijklmnopqrstuvwxyz";
|
||||
public static String numbers = "0123456789";
|
||||
public static String randomString(int length) {
|
||||
return randomString(length,new String[]{alphabetsInLowerCase,alphabetsInUpperCase,numbers});
|
||||
}
|
||||
public static String randomString(int length,String[] strs){
|
||||
String allCharacters="";
|
||||
for (String str : strs) {
|
||||
allCharacters+=str;
|
||||
}
|
||||
// create a super set of all characters
|
||||
// initialize a string to hold result
|
||||
StringBuffer randomString = new StringBuffer();
|
||||
// loop for 10 times
|
||||
for (int i = 0; i < length; i++) {
|
||||
// generate a random number between 0 and length of all characters
|
||||
int randomIndex = (int)(Math.random() * allCharacters.length());
|
||||
// retrieve character at index and add it to result
|
||||
randomString.append(allCharacters.charAt(randomIndex));
|
||||
}
|
||||
return randomString.toString();
|
||||
}
|
||||
public static String getLoginToken(HttpServletRequest request){
|
||||
String token = request.getParameter("token");
|
||||
if(StringUtils.isEmpty(token)){
|
||||
Cookie cookie=AppTools.getCookie(request,"login");
|
||||
if(cookie!=null){
|
||||
token=cookie.getValue();
|
||||
}
|
||||
}
|
||||
return token;
|
||||
}
|
||||
public static List<File> scanFilePath(String path){
|
||||
Log.i("scan > "+path);
|
||||
List<File> files=new ArrayList<>();
|
||||
File file=new File(path);
|
||||
for (File listFile : Objects.requireNonNull(file.listFiles())) {
|
||||
if(listFile.isDirectory()){
|
||||
files.addAll(scanFilePath(listFile.getAbsolutePath()));
|
||||
}else{
|
||||
files.add(listFile);
|
||||
Log.i("add file is > "+listFile.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
return files;
|
||||
}
|
||||
/**
|
||||
* 构造给前端的文件
|
||||
*
|
||||
* @param file 文件路径
|
||||
* @return 前端获取的文件
|
||||
*/
|
||||
public static ResponseEntity<FileSystemResource> getFile(File file) {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
|
||||
try {
|
||||
headers.add("Content-Disposition", "attachment; filename=" + URLEncoder.encode(file.getName(), "UTF-8"));
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
headers.add("Content-Disposition", "attachment; filename=" + file.getName());
|
||||
}
|
||||
headers.add("Pragma", "no-cache");
|
||||
headers.add("Expires", "0");
|
||||
headers.add("Last-Modified", new Date().toString());
|
||||
headers.add("ETag", String.valueOf(System.currentTimeMillis()));
|
||||
return ResponseEntity.ok().headers(headers).contentLength(file.length()).contentType(MediaType.parseMediaType("application/octet-stream")).body(new FileSystemResource(file));
|
||||
}
|
||||
|
||||
public static String getMD5(String data) {
|
||||
try {
|
||||
MessageDigest digest = MessageDigest.getInstance("MD5");
|
||||
digest.update(data.getBytes());
|
||||
return DatatypeConverter.printHexBinary(digest.digest());
|
||||
} catch (Exception e) {
|
||||
com.yutou.bilibili.Tools.Log.e(e);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
public static boolean copyFileToName(String srcFileName,String destFileName,String fileName,boolean overlay) {
|
||||
File srcFile = new File(srcFileName);
|
||||
// 判断源文件是否存在
|
||||
if (!srcFile.exists()) {
|
||||
System.err.println("源文件不存在:"+srcFile.getAbsolutePath()+" > "+destFileName);
|
||||
return false;
|
||||
} else if (!srcFile.isFile()) {
|
||||
System.err.println("源文件是目录:"+srcFile.getAbsolutePath());
|
||||
return false;
|
||||
}
|
||||
|
||||
// 判断目标文件是否存在
|
||||
File destFile = new File(destFileName);
|
||||
// 如果目标文件所在目录不存在,则创建目录
|
||||
if (!destFile.exists()) {
|
||||
// 目标文件所在目录不存在
|
||||
if (!destFile.mkdirs()) {
|
||||
// 复制文件失败:创建目标文件所在目录失败
|
||||
System.err.println("创建文件夹失败:"+destFile.getAbsolutePath());
|
||||
return false;
|
||||
}
|
||||
|
||||
}else{
|
||||
if(srcFileName.equals("Activity.smali")){
|
||||
com.yutou.bilibili.Tools.Log.i("文件夹已存在:"+destFileName);
|
||||
}
|
||||
}
|
||||
|
||||
// 复制文件
|
||||
int byteread = 0; // 读取的字节数
|
||||
InputStream in = null;
|
||||
OutputStream out = null;
|
||||
|
||||
try {
|
||||
if(fileName==null) {
|
||||
fileName=srcFile.getName();
|
||||
}
|
||||
in = new FileInputStream(srcFile);
|
||||
out = new FileOutputStream(destFile + File.separator +fileName );
|
||||
byte[] buffer = new byte[1024];
|
||||
|
||||
while ((byteread = in.read(buffer)) != -1) {
|
||||
out.write(buffer, 0, byteread);
|
||||
}
|
||||
out.close();
|
||||
in.close();
|
||||
return true;
|
||||
} catch (FileNotFoundException e) {
|
||||
com.yutou.bilibili.Tools.Log.e(e);
|
||||
return false;
|
||||
} catch (IOException e) {
|
||||
com.yutou.bilibili.Tools.Log.e(e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isRuntimeSystemOfWindow(){
|
||||
return System.getProperty ("os.name").contains("Windows");
|
||||
}
|
||||
|
||||
public static Process exec(String exec)throws Exception{
|
||||
if(AppTools.isRuntimeSystemOfWindow()) {
|
||||
return Runtime.getRuntime().exec(new String[]{
|
||||
"cmd",
|
||||
"/c",
|
||||
exec
|
||||
}
|
||||
);
|
||||
}else{
|
||||
return Runtime.getRuntime().exec(new String[]{
|
||||
"sh",
|
||||
"-c",
|
||||
exec
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.yutou.bilibili.Tools;
|
||||
|
||||
import com.yutou.utils.Log;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.context.event.ContextClosedEvent;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -2,18 +2,14 @@ package com.yutou.bilibili.Tools;
|
||||
|
||||
import com.yutou.bilibili.BiliBili.Datas.AppData;
|
||||
import com.yutou.bilibili.BiliBili.Datas.LiveData;
|
||||
import com.yutou.bilibili.BiliBili.Live;
|
||||
import com.yutou.bilibili.BiliBili.LiveUtils;
|
||||
import com.yutou.bilibili.BiliBili.Services.IBiliBiliLiveService;
|
||||
import com.yutou.bilibili.Services.ISystemConfigService;
|
||||
import com.yutou.bilibili.mybatis.Bili.mybatis.model.BilibiliLiveInfo;
|
||||
import com.yutou.bilibili.mybatis.Bili.mybatis.model.BilibiliUpInfo;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.File;
|
||||
import java.text.ParsePosition;
|
||||
import java.text.SimpleDateFormat;
|
||||
@@ -27,8 +23,6 @@ import java.util.TimerTask;
|
||||
*/
|
||||
@Component
|
||||
public class ApplicationInit implements ApplicationRunner {
|
||||
@Resource
|
||||
IBiliBiliLiveService service;
|
||||
@Resource
|
||||
ISystemConfigService configService;
|
||||
private Timer timer;
|
||||
@@ -36,7 +30,6 @@ public class ApplicationInit implements ApplicationRunner {
|
||||
|
||||
@Override
|
||||
public void run(ApplicationArguments args) throws Exception {
|
||||
LiveUtils.LiveGiftConfig.getInstance();
|
||||
AppData.FFMPEG = configService.getConfig(Config.SYSTEM_VIDEO_FFMPEG);
|
||||
AppData.LIVE_SAVE_FFMPEG = configService.getConfig(Config.SYSTEM_VIDEO_SAVE_MODEL) != null && configService.getConfig(Config.SYSTEM_VIDEO_SAVE_MODEL).equals("ffmpeg");
|
||||
startTimer();
|
||||
@@ -59,9 +52,7 @@ public class ApplicationInit implements ApplicationRunner {
|
||||
oldTime = time;
|
||||
switch (time) {
|
||||
case "00:00":
|
||||
for (Live live : Live.lives.values()) {
|
||||
live.clearInfo();
|
||||
}
|
||||
|
||||
break;
|
||||
case "01:00":
|
||||
case "02:00":
|
||||
@@ -93,52 +84,13 @@ public class ApplicationInit implements ApplicationRunner {
|
||||
}
|
||||
|
||||
private void checkLive() {
|
||||
List<BilibiliUpInfo> list = service.getUpInfo();
|
||||
LiveUtils.reloadLiveState(list);
|
||||
for (BilibiliUpInfo info : list) {
|
||||
if (info.getOfflinelistening() == 1) {
|
||||
if (info.getEnable() == 1 ) {
|
||||
Live live = LiveUtils.liveContains(info);
|
||||
if (live == null) {
|
||||
live = new Live();
|
||||
live.add(info.getRoomid(), !StringUtils.isEmpty(AppTools.readFile(new File("cookies.json"))));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}, 0, 2 * 60 * 1000);
|
||||
}
|
||||
}
|
||||
|
||||
public void saveData(String time) {
|
||||
Date date = new Date();
|
||||
String toDay = AppTools.getToDayTime() + " 00:00:00";
|
||||
String nowTime = AppTools.getToDayTime() + " " + time + ":00";
|
||||
for (BilibiliUpInfo info : service.getUpInfo()) {
|
||||
Date startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(toDay, new ParsePosition(0));
|
||||
Date endTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(nowTime, new ParsePosition(0));
|
||||
int userCount = service.queryLiveData(info.getRoomid(), startTime, endTime, new String[]{LiveData.INTERACT_WORD}).size();
|
||||
int vipCount = service.queryLiveData(info.getRoomid(), startTime, endTime, new String[]{LiveData.ENTRY_EFFECT}).size();
|
||||
int giftCount = service.queryLiveData(info.getRoomid(), startTime, endTime, new String[]{
|
||||
LiveData.SEND_GIFT,
|
||||
LiveData.COMBO_SEND,
|
||||
LiveData.GUARD_BUY,
|
||||
LiveData.SUPER_CHAT_MESSAGE}).size();
|
||||
Live live = LiveUtils.liveContains(info);
|
||||
int popularCount = 0;
|
||||
if (live != null) {
|
||||
popularCount = live.getInfo().getPopular();
|
||||
}
|
||||
BilibiliLiveInfo liveInfo = new BilibiliLiveInfo();
|
||||
liveInfo.setRoomid(info.getRoomid());
|
||||
liveInfo.setGiftuser(giftCount);
|
||||
liveInfo.setUserindex(userCount);
|
||||
liveInfo.setVipuserindex(vipCount);
|
||||
liveInfo.setPopular(popularCount);
|
||||
liveInfo.setSubtime(date);
|
||||
service.addLiveInfo(liveInfo);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,91 +0,0 @@
|
||||
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) {
|
||||
return load(type, key, Object.class, null);
|
||||
}
|
||||
|
||||
public static <T> T load(String type, String key, Class<T> t) {
|
||||
return load(type, key, t, null);
|
||||
}
|
||||
|
||||
public static <T> T load(String type, String key, Class<T> t, T def) {
|
||||
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);
|
||||
return json.getObject(key, t);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
|
||||
return def;
|
||||
}
|
||||
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) {
|
||||
Log.i("error = "+file.getAbsolutePath());
|
||||
com.yutou.bilibili.Tools.Log.e(e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -4,13 +4,15 @@ import com.yutou.bilibili.mybatis.Bili.mybatis.dao.BilibiliLiveDataDao;
|
||||
import com.yutou.bilibili.mybatis.Bili.mybatis.dao.BilibiliLiveInfoDao;
|
||||
import com.yutou.bilibili.mybatis.Bili.mybatis.model.BilibiliLiveData;
|
||||
import com.yutou.bilibili.mybatis.Bili.mybatis.model.BilibiliLiveInfo;
|
||||
import com.yutou.utils.AppTools;
|
||||
import com.yutou.utils.Log;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.apache.poi.ss.usermodel.*;
|
||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.util.Date;
|
||||
@@ -28,10 +30,10 @@ public class ExcelUtils implements ApplicationContextAware {
|
||||
public static File getInstance(int roomId, Date startTime, Date endTime,String fileName) {
|
||||
ExcelUtils utils = new ExcelUtils();
|
||||
long timer=System.currentTimeMillis();
|
||||
com.yutou.bilibili.Tools.Log.i("开始注入bean");
|
||||
Log.i("开始注入bean");
|
||||
utils.dataDao = getBean(BilibiliLiveDataDao.class);
|
||||
utils.infoDao = getBean(BilibiliLiveInfoDao.class);
|
||||
com.yutou.bilibili.Tools.Log.i("注入完毕:"+(System.currentTimeMillis()-timer));
|
||||
Log.i("注入完毕:"+(System.currentTimeMillis()-timer));
|
||||
return utils.initTable(roomId, startTime, endTime,fileName);
|
||||
}
|
||||
|
||||
@@ -41,7 +43,7 @@ public class ExcelUtils implements ApplicationContextAware {
|
||||
|
||||
private File initTable(int roomId, Date startTime, Date endTime,String fileName) {
|
||||
long timer=System.currentTimeMillis();
|
||||
com.yutou.bilibili.Tools.Log.i("进入统计:"+timer);
|
||||
Log.i("进入统计:"+timer);
|
||||
Workbook workbook = new XSSFWorkbook();
|
||||
Sheet liveData = workbook.createSheet("直播数据");
|
||||
Sheet liveInfo = workbook.createSheet("小时统计");
|
||||
@@ -66,7 +68,7 @@ public class ExcelUtils implements ApplicationContextAware {
|
||||
createCell(infoRow.createCell(5), "送礼人数");
|
||||
createCell(infoRow.createCell(6), "记录时间");
|
||||
|
||||
com.yutou.bilibili.Tools.Log.i("表头插入完毕:"+(System.currentTimeMillis()-timer));
|
||||
Log.i("表头插入完毕:"+(System.currentTimeMillis()-timer));
|
||||
List<BilibiliLiveData> dataList = dataDao.queryLiveData(roomId, startTime, endTime, new String[]{
|
||||
INTERACT_WORD,
|
||||
ENTRY_EFFECT,
|
||||
@@ -104,7 +106,7 @@ public class ExcelUtils implements ApplicationContextAware {
|
||||
createCell(infoRow.createCell(5), info.getGiftuser());
|
||||
createCell(infoRow.createCell(6), AppTools.getToDayTimeToString(info.getSubtime()));
|
||||
}
|
||||
com.yutou.bilibili.Tools.Log.i("数据填充完毕:"+(System.currentTimeMillis()-timer));
|
||||
Log.i("数据填充完毕:"+(System.currentTimeMillis()-timer));
|
||||
try {
|
||||
if(!new File("excel").exists()){
|
||||
new File("excel").mkdirs();
|
||||
@@ -113,11 +115,11 @@ public class ExcelUtils implements ApplicationContextAware {
|
||||
FileOutputStream fileOutputStream = new FileOutputStream(file);
|
||||
workbook.write(fileOutputStream);
|
||||
workbook.close();
|
||||
com.yutou.bilibili.Tools.Log.i("文件写入完毕:"+(System.currentTimeMillis()-timer));
|
||||
Log.i("文件写入完毕:"+(System.currentTimeMillis()-timer));
|
||||
file.renameTo(new File("excel"+File.separator+fileName));
|
||||
return file;
|
||||
} catch (Exception e) {
|
||||
com.yutou.bilibili.Tools.Log.e(e);
|
||||
Log.e(e);
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
package com.yutou.bilibili.Tools;
|
||||
|
||||
import com.yutou.utils.AppTools;
|
||||
import com.yutou.utils.Log;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
@@ -38,8 +41,8 @@ public class FFmpegUtils {
|
||||
ffmpeg,
|
||||
file.getAbsolutePath(),
|
||||
out.getAbsolutePath() + File.separator + file.getName());
|
||||
com.yutou.bilibili.Tools.Log.i(exec);
|
||||
Process process=AppTools.exec(exec);
|
||||
Log.i(exec);
|
||||
Process process= AppTools.exec(exec);
|
||||
InputStream inputStream = process.getErrorStream();
|
||||
byte[] bytes = new byte[1024];
|
||||
while (inputStream.read(bytes) > -1) {
|
||||
@@ -49,7 +52,7 @@ public class FFmpegUtils {
|
||||
file.delete();
|
||||
new File(out.getAbsolutePath()+File.separator+file.getName()).delete();
|
||||
} catch (Exception e) {
|
||||
com.yutou.bilibili.Tools.Log.e(e);
|
||||
Log.e(e);
|
||||
}
|
||||
nowFFmpegList.remove(file);
|
||||
}
|
||||
@@ -61,13 +64,13 @@ public class FFmpegUtils {
|
||||
int i = -1;
|
||||
while (i != 1) {
|
||||
i = add("D:\\ffmpeg-4.3.1-2020-11-19-full_build\\bin\\ffmpeg.exe", file.getAbsoluteFile(), new File("ffmpeg_out"));
|
||||
com.yutou.bilibili.Tools.Log.i(i);
|
||||
Log.i(i);
|
||||
try {
|
||||
Thread.sleep(300);
|
||||
} catch (InterruptedException e) {
|
||||
com.yutou.bilibili.Tools.Log.e(e);
|
||||
Log.e(e);
|
||||
}
|
||||
}
|
||||
com.yutou.bilibili.Tools.Log.i("转码完成");
|
||||
Log.i("转码完成");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
package com.yutou.bilibili.Tools;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.yutou.bilibili.interfaces.DownloadInterface;
|
||||
import com.yutou.utils.Log;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
@@ -1,63 +0,0 @@
|
||||
package com.yutou.bilibili.Tools;
|
||||
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.logging.FileHandler;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class Log {
|
||||
private static Logger logger;
|
||||
|
||||
public static void i(String tag, Object log) {
|
||||
i('[' + tag + ']' + log);
|
||||
}
|
||||
public static void i(Object log) {
|
||||
if (!((boolean) ConfigTools.load(ConfigTools.CONFIG, "logout"))) {
|
||||
return;
|
||||
}
|
||||
System.out.printf("[%s]%s%n",
|
||||
AppTools.getToDayNowTimeToString(),
|
||||
log
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
public static void e(Exception e) {
|
||||
if (!((boolean) ConfigTools.load(ConfigTools.CONFIG, "logout"))) {
|
||||
return;
|
||||
}
|
||||
i(e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
public static void i(String timer, int roomId, Object log) {
|
||||
String logFlag = RedisTools.get("live-log", 1);
|
||||
if (logFlag!=null&&logFlag.startsWith("true")) {
|
||||
if (logFlag.contains("|")) {
|
||||
if (log.toString().contains(logFlag.split("\\|")[1])) {
|
||||
getLogger("[" + timer.replace(":", "_") + "]" + roomId).log(Level.INFO, log.toString());
|
||||
}
|
||||
} else {
|
||||
getLogger("[" + timer.replace(":", "_") + "]" + roomId).log(Level.INFO, log.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static Logger getLogger(String fileName) {
|
||||
if (logger == null) {
|
||||
try {
|
||||
if (!new File("logs").exists()) {
|
||||
new File("logs").mkdirs();
|
||||
}
|
||||
logger = Logger.getLogger("Live-Log");
|
||||
FileHandler handler = new FileHandler("logs" + File.separator + fileName + ".log");
|
||||
logger.addHandler(handler);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return logger;
|
||||
}
|
||||
}
|
||||
@@ -1,236 +0,0 @@
|
||||
package com.yutou.bilibili.Tools;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.yutou.bilibili.QQBot.QQBotManager;
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisPool;
|
||||
import redis.clients.jedis.JedisPoolConfig;
|
||||
import redis.clients.jedis.JedisPubSub;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
|
||||
|
||||
public class RedisTools {
|
||||
private static boolean isNotInstallRedis = false;
|
||||
private static String host;
|
||||
private static int port;
|
||||
public static int TOKEN_TIMEOUT_DEFAULT = 360;
|
||||
|
||||
private RedisTools() {
|
||||
|
||||
}
|
||||
|
||||
// 写成静态代码块形式,只加载一次,节省资源
|
||||
static {
|
||||
//Properties properties = PropertyUtil.loadProperties("jedis.properties");
|
||||
//host = properties.getProperty("redis.host");
|
||||
//port = Integer.valueOf(properties.getProperty("redis.port"));
|
||||
host = "127.0.0.1";
|
||||
port = 6379;
|
||||
}
|
||||
|
||||
public static boolean set(int dbIndex, String key, String value) {
|
||||
try {
|
||||
if (isNotInstallRedis) {
|
||||
return false;
|
||||
}
|
||||
Jedis jedis = getRedis();
|
||||
jedis.select(dbIndex);
|
||||
String ret = jedis.set(key, value);
|
||||
jedis.close();
|
||||
} catch (Exception e) {
|
||||
// TODO: handle exception
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean set(String key, String value) {
|
||||
return set(0, key, value);
|
||||
}
|
||||
|
||||
public static boolean set(String key, String value, int timeout) {
|
||||
try {
|
||||
if (isNotInstallRedis) {
|
||||
return false;
|
||||
}
|
||||
Jedis jedis = getRedis();
|
||||
if (timeout == -1) {
|
||||
jedis.set(key, value);
|
||||
} else {
|
||||
jedis.setex(key, timeout, value);
|
||||
}
|
||||
jedis.close();
|
||||
} catch (Exception e) {
|
||||
// TODO: handle exception
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static String get(String key, int dbIndex) {
|
||||
String value = "-999";
|
||||
if (isNotInstallRedis) {
|
||||
return value;
|
||||
}
|
||||
try (Jedis jedis = getRedis()) {
|
||||
jedis.select(dbIndex);
|
||||
value = jedis.get(key);
|
||||
jedis.close();
|
||||
} catch (Exception e) {
|
||||
// TODO: handle exception
|
||||
// e.printStackTrace();
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
public static String get(String key) {
|
||||
return get(key, 0);
|
||||
}
|
||||
|
||||
public static boolean remove(String key) {
|
||||
return remove(key,0);
|
||||
}
|
||||
|
||||
public static void removeLoginState(String uid) {
|
||||
Jedis jedis = getRedis();
|
||||
Set<String> keys = jedis.keys("*");
|
||||
for (String string : keys) {
|
||||
if (string.equals(uid)) {
|
||||
jedis.del(string);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static String ping() {
|
||||
Jedis jedis = getRedis();
|
||||
String tmp = jedis.ping();
|
||||
jedis.close();
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public static boolean exists(String key, String value) {
|
||||
if (isNotInstallRedis) {
|
||||
return false;
|
||||
}
|
||||
Jedis jedis = getRedis();
|
||||
boolean flag = value.equals(jedis.get(key));
|
||||
jedis.close();
|
||||
return flag;
|
||||
}
|
||||
|
||||
public static Jedis getRedis() {
|
||||
return new Jedis(host, port);
|
||||
}
|
||||
|
||||
public static boolean remove(String key, int index) {
|
||||
if (isNotInstallRedis) {
|
||||
return false;
|
||||
}
|
||||
Jedis jedis = getRedis();
|
||||
jedis.select(index);
|
||||
Long i = jedis.del(key);
|
||||
jedis.close();
|
||||
if (i > 0) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static class PropertyUtil {
|
||||
|
||||
// 加载property文件到io流里面
|
||||
public static Properties loadProperties(String propertyFile) {
|
||||
Properties properties = new Properties();
|
||||
try {
|
||||
InputStream is = PropertyUtil.class.getClassLoader().getResourceAsStream(propertyFile);
|
||||
if (is == null) {
|
||||
is = PropertyUtil.class.getClassLoader().getResourceAsStream(propertyFile);
|
||||
}
|
||||
properties.load(is);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return properties;
|
||||
}
|
||||
}
|
||||
|
||||
private static Jedis getPoolRedis() {
|
||||
if (isNotInstallRedis) {
|
||||
return null;
|
||||
}
|
||||
JedisPoolConfig poolConfig = new JedisPoolConfig();
|
||||
poolConfig.setMaxIdle(0);
|
||||
poolConfig.setMaxWaitMillis(1000);
|
||||
JedisPool pool = new JedisPool(poolConfig, host);
|
||||
return pool.getResource();
|
||||
}
|
||||
|
||||
public static void pullMsg(String channel, String msg) {
|
||||
Jedis jedis = getPoolRedis();
|
||||
jedis.publish(channel, msg);
|
||||
jedis.close();
|
||||
}
|
||||
|
||||
private static boolean init = false;
|
||||
|
||||
public static void initRedisPoolSub() {
|
||||
if (init)
|
||||
return;
|
||||
init = true;
|
||||
Log.i("初始化订阅");
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Jedis jedis = getPoolRedis();
|
||||
if (jedis != null)
|
||||
jedis.psubscribe(new Consumer(), "*");
|
||||
}
|
||||
}).start();
|
||||
|
||||
}
|
||||
|
||||
protected static class Consumer extends JedisPubSub {
|
||||
@Override
|
||||
public void onPMessage(String pattern, String channel, String message) {
|
||||
super.onPMessage(pattern, channel, message);
|
||||
Log.i("onPMessage: channel=" + channel + " msg=" + message + " pattern=" + pattern);
|
||||
switch (channel) {
|
||||
case "bot":
|
||||
bot(message);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessage(String channel, String message) {
|
||||
super.onMessage(channel, message);
|
||||
Log.i("onMessage: channel=" + channel + " msg=" + message);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public static void bot(String value) {
|
||||
switch (value) {
|
||||
case "getip":
|
||||
JSONObject json = JSONObject.parseObject(HttpTools.get("https://api.asilu.com/ip/"));
|
||||
String ip = json.getString("ip");
|
||||
QQBotManager.getInstance().sendMessage("服务器IP:\n" + ip);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
RedisTools.pullMsg("msg", "abc");
|
||||
}
|
||||
}
|
||||
@@ -1,17 +1,18 @@
|
||||
package com.yutou.bilibili.Tools;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson2.JSONArray;
|
||||
import com.yutou.bilibili.Services.IUserService;
|
||||
import com.yutou.bilibili.mybatis.dao.PermissionDao;
|
||||
import com.yutou.bilibili.mybatis.model.Permission;
|
||||
import com.yutou.bilibili.mybatis.model.PermissionExample;
|
||||
import com.yutou.bilibili.mybatis.model.UUser;
|
||||
import com.yutou.utils.Log;
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.List;
|
||||
|
||||
public class ServiceTools implements ApplicationContextAware {
|
||||
@@ -36,8 +37,8 @@ public class ServiceTools implements ApplicationContextAware {
|
||||
try {
|
||||
url = tmp.split(tmp.split("/")[tmp.split("/").length - 1])[0];
|
||||
} catch (Exception e) {
|
||||
com.yutou.bilibili.Tools.Log.e(e);
|
||||
com.yutou.bilibili.Tools.Log.i("无权限请求:" + tmp);
|
||||
Log.e(e);
|
||||
Log.i("无权限请求:" + tmp);
|
||||
return false;
|
||||
}
|
||||
JSONArray powers = JSONArray.parseArray(user.getPower());
|
||||
|
||||
@@ -1,19 +1,10 @@
|
||||
package com.yutou.bilibili.Tools;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.yutou.bilibili.BiliBili.LiveUtils;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
|
||||
import javax.xml.bind.DatatypeConverter;
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.net.CookieHandler;
|
||||
import java.net.CookieManager;
|
||||
import java.net.HttpCookie;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
|
||||
public class TestMain {
|
||||
public static void main(String[] args) {
|
||||
@@ -21,43 +12,7 @@ public class TestMain {
|
||||
}
|
||||
|
||||
public TestMain() {
|
||||
try {
|
||||
JSONObject cookie = JSONObject.parseObject(LiveUtils.getFile("cookies.json"));
|
||||
JSONObject userHear = new JSONObject();
|
||||
userHear.put("csrf_token",cookie.getString("bili_jct"));
|
||||
userHear.put("csrf",cookie.getString("bili_jct"));
|
||||
userHear.put("visit_id","");
|
||||
HttpURLConnection connection=LiveUtils.getBiliHttpPost("https://api.live.bilibili.com/User/userOnlineHeart",LiveUtils.getCookie());
|
||||
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
||||
|
||||
OutputStream connectionOutputStream = null;
|
||||
connectionOutputStream = connection.getOutputStream();
|
||||
connectionOutputStream.write(HttpTools.toUrlParams(userHear).getBytes(StandardCharsets.UTF_8));
|
||||
connectionOutputStream.flush();
|
||||
JSONObject hearBeat;
|
||||
hearBeat=LiveUtils.http_post("https://api.live.bilibili.com/User/userOnlineHeart", HttpTools.toUrlParams(userHear));
|
||||
/* BufferedInputStream stream = new BufferedInputStream(connection.getInputStream());
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
byte[] bytes = new byte[1024];
|
||||
int len = 0, size;
|
||||
while ((len = stream.read(bytes)) != -1) {
|
||||
outputStream.write(bytes, 0, len);
|
||||
outputStream.flush();
|
||||
}
|
||||
String str = new String(outputStream.toByteArray(), StandardCharsets.UTF_8);*/
|
||||
|
||||
com.yutou.bilibili.Tools.Log.i("["+AppTools.getToDayNowTimeToString()+"]"+hearBeat);
|
||||
} catch (Exception e) {
|
||||
com.yutou.bilibili.Tools.Log.e(e);
|
||||
}
|
||||
}
|
||||
public static int byteArrayToInt(byte[] bytes) {
|
||||
int value=0;
|
||||
for(int i = 0; i < 4; i++) {
|
||||
int shift= (3-i) * 8;
|
||||
value +=(bytes[i] & 0xFF) << shift;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,23 +1,18 @@
|
||||
package com.yutou.bilibili.Tools;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.yutou.bilibili.interfaces.DownloadInterface;
|
||||
import com.yutou.utils.Log;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.http.Cookie;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.*;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.net.URLDecoder;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Base64;
|
||||
@@ -111,7 +106,7 @@ public class Tools {
|
||||
inputStream.close();
|
||||
downloadInterface.onDownload(file);
|
||||
} catch (IOException e) {
|
||||
com.yutou.bilibili.Tools.Log.e(e);
|
||||
Log.e(e);
|
||||
downloadInterface.onError(e);
|
||||
}
|
||||
}
|
||||
@@ -153,7 +148,7 @@ public class Tools {
|
||||
}
|
||||
in.close();
|
||||
} catch (Exception e) {
|
||||
com.yutou.bilibili.Tools.Log.e(e);
|
||||
Log.e(e);
|
||||
return null;
|
||||
}
|
||||
return bytesToHexString(digest.digest());
|
||||
|
||||
@@ -4,7 +4,6 @@ import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.yutou.bilibili.BiliBili.LiveUtils;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
@@ -50,7 +49,6 @@ public class BilibiliUpInfo implements Serializable {
|
||||
public Integer checkLiveStatus(){
|
||||
List<BilibiliUpInfo> list=new ArrayList<>();
|
||||
list.add(this);
|
||||
live= LiveUtils.getStatusInfoByUid(list).getJSONObject("data").getJSONObject(mid+"").getInteger("live_status");
|
||||
return live;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,20 +1,14 @@
|
||||
package com.yutou.bilibili.sqlite;
|
||||
|
||||
import com.yutou.bilibili.BiliBili.Datas.BiliBiliUpData;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
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 com.yutou.databases.AbsDatabasesBean;
|
||||
import com.yutou.databases.SQLiteManager;
|
||||
import com.yutou.utils.Log;
|
||||
|
||||
import java.io.File;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
|
||||
public class BiliBiliLiveDatabasesManager extends SQLiteManager {
|
||||
@@ -24,6 +18,12 @@ public class BiliBiliLiveDatabasesManager extends SQLiteManager {
|
||||
super();
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected AbsDatabasesBean getDataBean() {
|
||||
return null;
|
||||
}
|
||||
|
||||
private JSONObject getInitJSON(){
|
||||
return 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}]}]}");
|
||||
}
|
||||
@@ -31,13 +31,13 @@ public class BiliBiliLiveDatabasesManager extends SQLiteManager {
|
||||
JSONObject json = getInitJSON();
|
||||
json.put("model","create");
|
||||
json.put("file",fileName+".db");
|
||||
build(json);
|
||||
//build(json);
|
||||
}
|
||||
public void init(File file){
|
||||
JSONObject json = getInitJSON();
|
||||
json.put("model","load");
|
||||
json.put("file",file.getAbsolutePath());
|
||||
build(json);
|
||||
// build(json);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -61,7 +61,7 @@ public class BiliBiliLiveDatabasesManager extends SQLiteManager {
|
||||
statement.execute(sql);
|
||||
statement.closeOnCompletion();
|
||||
}catch (Exception e){
|
||||
com.yutou.bilibili.Tools.Log.e(e);
|
||||
Log.e(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,7 +93,7 @@ public class BiliBiliLiveDatabasesManager extends SQLiteManager {
|
||||
set.close();
|
||||
statement.closeOnCompletion();;
|
||||
}catch (Exception e){
|
||||
com.yutou.bilibili.Tools.Log.e(e);
|
||||
Log.e(e);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
@@ -1,150 +0,0 @@
|
||||
package com.yutou.bilibili.sqlite;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
|
||||
import java.io.File;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
public class SQLiteManager {
|
||||
protected Connection conn;
|
||||
private String url = "jdbc:sqlite:";
|
||||
private File sql;
|
||||
|
||||
|
||||
public void startBatch() {
|
||||
try {
|
||||
conn.setAutoCommit(false);
|
||||
} catch (SQLException e) {
|
||||
com.yutou.bilibili.Tools.Log.e(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void closeBatch() {
|
||||
try {
|
||||
conn.setAutoCommit(true);
|
||||
} catch (SQLException e) {
|
||||
com.yutou.bilibili.Tools.Log.e(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void close() {
|
||||
try {
|
||||
conn.close();
|
||||
} catch (SQLException throwables) {
|
||||
throwables.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void commit() {
|
||||
try {
|
||||
conn.commit();
|
||||
} catch (SQLException e) {
|
||||
com.yutou.bilibili.Tools.Log.e(e);
|
||||
}
|
||||
}
|
||||
|
||||
protected SQLiteManager() {
|
||||
|
||||
}
|
||||
|
||||
private void createSql(JSONObject json) {
|
||||
try {
|
||||
sql.mkdirs();
|
||||
sql.delete();
|
||||
conn = DriverManager.getConnection(url + sql.getAbsolutePath());
|
||||
} catch (Exception e) {
|
||||
com.yutou.bilibili.Tools.Log.e(e);
|
||||
}
|
||||
startBatch();
|
||||
JSONArray array = json.getJSONArray("table");
|
||||
for (Object o : array) {
|
||||
com.yutou.bilibili.Tools.Log.i("创建表:" + ((JSONObject) o).getString("name"));
|
||||
createSqlOfTable((JSONObject) o);
|
||||
}
|
||||
closeBatch();
|
||||
}
|
||||
|
||||
private void createSqlOfTable(JSONObject table) {
|
||||
String tableName = table.getString("name");
|
||||
try {
|
||||
Statement statement = conn.createStatement();
|
||||
JSONArray items = table.getJSONArray("item");
|
||||
StringBuilder sql = new StringBuilder();
|
||||
sql.append("CREATE TABLE `")
|
||||
.append(tableName)
|
||||
.append("` (");
|
||||
for (Object item : items) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
JSONObject it = (JSONObject) item;
|
||||
String type;
|
||||
switch (it.getString("type")) {
|
||||
case "int":
|
||||
type = " INTEGER ";
|
||||
break;
|
||||
case "TIME":
|
||||
type = " NUMERIC ";
|
||||
break;
|
||||
default:
|
||||
type = " TEXT ";
|
||||
break;
|
||||
}
|
||||
builder.append("`")
|
||||
.append(it.getString("name"))
|
||||
.append("`")
|
||||
.append(type)
|
||||
.append(it.getBoolean("isNull") ? "" : " NOT NULL ")
|
||||
.append(it.getBoolean("isKey") ? " PRIMARY KEY AUTOINCREMENT " : "")
|
||||
.append(",");
|
||||
sql.append(builder.toString());
|
||||
}
|
||||
sql.append(");");
|
||||
statement.execute(sql.toString().replace(",);", ");"));
|
||||
statement.closeOnCompletion();
|
||||
} catch (Exception e) {
|
||||
com.yutou.bilibili.Tools.Log.e(e);
|
||||
}
|
||||
}
|
||||
|
||||
protected void build(JSONObject json) {
|
||||
try {
|
||||
Class.forName("org.sqlite.JDBC");
|
||||
if ("load".equals(json.getString("model"))) {
|
||||
sql = new File(json.getString("file"));
|
||||
} else {
|
||||
sql = new File("databases" + File.separator + json.getString("file"));
|
||||
}
|
||||
if (!sql.exists()) {
|
||||
createSql(json);
|
||||
} else {
|
||||
conn = DriverManager.getConnection(url + sql.getAbsolutePath());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
com.yutou.bilibili.Tools.Log.e(e);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean setDB(String fileName) {
|
||||
try {
|
||||
Class.forName("org.sqlite.JDBC");
|
||||
sql = new File("db" + File.separator + fileName);
|
||||
if (sql.exists()) {
|
||||
if (conn != null && !conn.isClosed()) {
|
||||
conn.close();
|
||||
}
|
||||
conn = DriverManager.getConnection(url + sql.getAbsolutePath());
|
||||
return true;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
com.yutou.bilibili.Tools.Log.e(e);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package com.yutou.bilibili.sqlite;
|
||||
|
||||
public class YouTubeLiveDatabasesManager extends SQLiteManager{
|
||||
private static YouTubeLiveDatabasesManager manager;
|
||||
public static YouTubeLiveDatabasesManager getInstance(){
|
||||
if(manager==null){
|
||||
manager=new YouTubeLiveDatabasesManager();
|
||||
}
|
||||
return manager;
|
||||
}
|
||||
public YouTubeLiveDatabasesManager() {
|
||||
super();
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user