新增B站直播间领电池功能

新增B站APP操作相关API
优化B站登陆根据QQ号分账号存储
修复Redis获取列表时为空报错问题
This commit is contained in:
2023-01-05 14:36:42 +08:00
parent 083218b1cb
commit fec901970a
15 changed files with 611 additions and 157 deletions

View File

@@ -2,8 +2,10 @@ package com.yutou.qqbot.bilibili;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.yutou.qqbot.QQBotManager;
import com.yutou.qqbot.interfaces.ObjectInterface;
import com.yutou.qqbot.utlis.*;
import org.brotli.dec.BrotliInputStream;
import javax.net.ssl.HttpsURLConnection;
import java.io.*;
@@ -12,12 +14,13 @@ import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.GZIPInputStream;
public class BiliBiliUtils {
private static long oldBiliBiliHttpTime = 0;
private long qq;
private long oldBiliBiliHttpTime = 0;
public enum HTTP {
POST, GET
@@ -27,14 +30,22 @@ public class BiliBiliUtils {
BYTE, JSON
}
public synchronized static JSONObject http_get(String url) {
public BiliBiliUtils(long qq) {
this.qq = qq;
}
public static BiliBiliUtils getInstance(long qq) {
return new BiliBiliUtils(qq);
}
public synchronized JSONObject http_get(String url) {
try {
// Log.i("调用url = "+url);
HttpsURLConnection connection = getBiliHttpGet(url, getCookie());
BufferedInputStream stream = new BufferedInputStream(connection.getInputStream());
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] bytes = new byte[1024];
int len = 0, size;
int len;
while ((len = stream.read(bytes)) != -1) {
outputStream.write(bytes, 0, len);
outputStream.flush();
@@ -42,8 +53,7 @@ public class BiliBiliUtils {
String str = outputStream.toString(StandardCharsets.UTF_8);
outputStream.close();
try {
JSONObject json = JSON.parseObject(str);
return json;
return JSON.parseObject(str);
} catch (Exception e) {
JSONObject json = new JSONObject();
json.put("html", str);
@@ -61,15 +71,15 @@ public class BiliBiliUtils {
return null;
}
public static JSONObject http_post(String url, String body) {
public JSONObject http_post(String url, String body) {
return http(url, HTTP.POST, body, RET_MODEL.JSON);
}
public static <T> T http(String url, HTTP model, String body, RET_MODEL ret_model) {
public <T> T http(String url, HTTP model, String body, RET_MODEL ret_model) {
return http(url, model, body, null, ret_model);
}
public static <T> T http(String url, HTTP model, String body, Map<String, String> headers, RET_MODEL ret_model) {
public <T> T http(String url, HTTP model, String body, Map<String, String> headers, RET_MODEL ret_model) {
JSONObject json = null;
BufferedInputStream stream = null;
ByteArrayOutputStream outputStream = null;
@@ -101,9 +111,9 @@ public class BiliBiliUtils {
for (String key : headers.keySet()) {
connection.setRequestProperty(key, headers.get(key));
}
} else {
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
}
System.out.println("url = " + url);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
if (!StringUtils.isEmpty(body)) {
connectionOutputStream = connection.getOutputStream();
@@ -114,7 +124,13 @@ public class BiliBiliUtils {
if (connection.getResponseCode() == 400) {
return null;
}
stream = new BufferedInputStream(connection.getInputStream());
if (connection.getContentEncoding() != null && connection.getContentEncoding().contains("gzip")) {
stream = new BufferedInputStream(new GZIPInputStream(connection.getInputStream()));
} else if (connection.getContentEncoding() != null && connection.getContentEncoding().contains("br")) {
stream = new BufferedInputStream(new BrotliInputStream(connection.getInputStream()));
} else {
stream = new BufferedInputStream(connection.getInputStream());
}
outputStream = new ByteArrayOutputStream();
byte[] bytes = new byte[1024];
int len = 0, size;
@@ -162,12 +178,11 @@ public class BiliBiliUtils {
return null;
}
public static String getCookie() {
if (StringUtils.isEmpty(ConfigTools.readFile(new File("bilibili.cookie")))) {
public String getCookie() {
if (StringUtils.isEmpty(ConfigTools.readFile(new File(qq + "_bilibili.cookie")))) {
return "";
}
JSONObject json = JSON.parseObject(ConfigTools.readFile(new File("bilibili.cookie")));
JSONObject json = JSON.parseObject(ConfigTools.readFile(new File(qq + "_bilibili.cookie")));
StringBuilder builder = new StringBuilder();
for (String s : json.keySet()) {
builder.append(s).append("=").append(json.getString(s)).append(";");
@@ -176,7 +191,7 @@ public class BiliBiliUtils {
return builder.toString();
}
public static HttpURLConnection getBiliHttpPost(String url, String cookie) throws Exception {
public HttpURLConnection getBiliHttpPost(String url, String cookie) throws Exception {
if (System.currentTimeMillis() - oldBiliBiliHttpTime < 1000) {
try {
Thread.sleep(500);
@@ -193,7 +208,7 @@ public class BiliBiliUtils {
return connection;
}
public static HttpsURLConnection getBiliHttpGet(String url, String cookie) throws IOException {
public HttpsURLConnection getBiliHttpGet(String url, String cookie) throws IOException {
if (System.currentTimeMillis() - oldBiliBiliHttpTime < 1000) {
try {
Thread.sleep(500);
@@ -210,7 +225,7 @@ public class BiliBiliUtils {
return connection;
}
public static File download(final String url, final String saveName, boolean isProxy) {
public File download(final String url, final String saveName, boolean isProxy) {
File jar = null;
try {
File savePath = new File(HttpTools.downloadPath + saveName);
@@ -262,7 +277,7 @@ public class BiliBiliUtils {
return null;
}
public static void download_ffmpeg(final List<String> url, final String saveName) {
public void download_ffmpeg(final List<String> url, final String saveName) {
new Thread(() -> {
StringBuilder builder = new StringBuilder();
builder.append(ConfigTools.load(ConfigTools.CONFIG, "ffmpeg", String.class)).append(" ");
@@ -296,7 +311,7 @@ public class BiliBiliUtils {
}
private static void setConnection(String cookie, HttpURLConnection connection) {
private void setConnection(String cookie, HttpURLConnection connection) {
connection.addRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
connection.addRequestProperty("Accept-Language", "zh-CN,zh;q=0.8");
connection.addRequestProperty("Cache-Control", "max-age=0");
@@ -307,8 +322,8 @@ public class BiliBiliUtils {
connection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36");
}
public static JSONObject getLoginInfo() {
JSONObject jsonObject = BiliBiliUtils.http_get("https://api.bilibili.com/x/web-interface/nav");
public JSONObject getLoginInfo() {
JSONObject jsonObject = http_get("https://api.bilibili.com/x/web-interface/nav");
if (jsonObject == null) {
jsonObject = new JSONObject();
jsonObject.put("code", "-1");
@@ -317,9 +332,9 @@ public class BiliBiliUtils {
return jsonObject;
}
public static void main(String[] args) {
public void main(String[] args) {
/* String url="https://xy218x85x123x8xy.mcdn.bilivideo.cn:4483/upgcxcode/12/12/17281212/17281212-16-80.flv?e=ig8euxZM2rNcNbNBhbdVhwdlhbUghwdVhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1660538573&gen=playurlv2&os=mcdn&oi=2936701972&trid=00006f9623cac1514d8ea18fba3a15a756cau&mid=96300&platform=pc&upsig=25ddd1da610960e8e1d2e80dc97c2361&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,mid,platform&mcdnid=11000101&bvc=vod&nettype=0&orderid=0,2&agrr=1&bw=253116&logo=A0000400&requestFrom=BILIBILI_HELPER_2.5.8";
File file=BiliBiliUtils.download(url,"16.mp4",false);
File file=download(url,"16.mp4",false);
System.out.println("file.getAbsolutePath() = " + file.getAbsolutePath());*/
/* System.out.println(getLiveRoom(42062));
System.out.println("--------------------------------------------");
@@ -328,44 +343,44 @@ public class BiliBiliUtils {
System.out.println("sign = " + sign);
}
public static boolean sendLiveDanmu(long roomId, String msg) {
public boolean sendLiveDanmu(long roomId, String msg) {
JSONObject body = new JSONObject();
body.put("msg", msg);
body.put("roomid", roomId);
body.put("color", 16777215);
body.put("fontsize", 25);
body.put("rnd", System.currentTimeMillis() / 1000);
body.put("csrf", BiliLogin.getCookieToken());
body.put("csrf_token", BiliLogin.getCookieToken());
JSONObject post = BiliBiliUtils.http_post("https://api.live.bilibili.com/msg/send", HttpTools.toUrlParams(body));
body.put("csrf", BiliLogin.getCookieToken(qq));
body.put("csrf_token", BiliLogin.getCookieToken(qq));
JSONObject post = http_post("https://api.live.bilibili.com/msg/send", HttpTools.toUrlParams(body));
return post.getInteger("code") == 0;
}
public static String liveSignIn() {
public String liveSignIn() {
//{"code":0,"data":{"coin":1,"gold":19500,"silver":106394,"tid":"Silver2Coin22101413201169763005873"},"message":"兑换成功"}
JSONObject body = new JSONObject();
body.put("csrf", BiliLogin.getCookieToken());
body.put("csrf_token", BiliLogin.getCookieToken());
JSONObject post = BiliBiliUtils.http_post("https://api.live.bilibili.com/xlive/revenue/v1/wallet/silver2coin", HttpTools.toUrlParams(body));
JSONObject post_ = BiliBiliUtils.http_get("https://api.live.bilibili.com/xlive/web-ucenter/v1/sign/DoSign");
body.put("csrf", BiliLogin.getCookieToken(qq));
body.put("csrf_token", BiliLogin.getCookieToken(qq));
JSONObject post = http_post("https://api.live.bilibili.com/xlive/revenue/v1/wallet/silver2coin", HttpTools.toUrlParams(body));
JSONObject post_ = http_get("https://api.live.bilibili.com/xlive/web-ucenter/v1/sign/DoSign");
return post.getString("message") + "|" + post_.getString("message");
}
public static JSONObject getLiveRoom(int roomId) {
public JSONObject getLiveRoom(int roomId) {
JSONObject body = new JSONObject();
body.put("room_id", roomId);
body.put("csrf", BiliLogin.getCookieToken());
body.put("csrf_token", BiliLogin.getCookieToken());
return BiliBiliUtils.http_post("https://api.live.bilibili.com/room/v1/Room/get_info", HttpTools.toUrlParams(body));
body.put("csrf", BiliLogin.getCookieToken(qq));
body.put("csrf_token", BiliLogin.getCookieToken(qq));
return http_post("https://api.live.bilibili.com/room/v1/Room/get_info", HttpTools.toUrlParams(body));
}
public static JSONObject getUserInfo(int mid) {
public JSONObject getUserInfo(int mid) {
JSONObject body = new JSONObject();
body.put("mid", mid);
return BiliBiliUtils.http_get("https://api.bilibili.com/x/space/acc/info?" + HttpTools.toUrlParams(body));
return http_get("https://api.bilibili.com/x/space/acc/info?" + HttpTools.toUrlParams(body));
}
public static boolean checkLiveRoom(int roomId) {
public boolean checkLiveRoom(int roomId) {
JSONObject post = getLiveRoom(roomId);
return post.getInteger("code") == 0;
}