新增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

@@ -0,0 +1,76 @@
package com.yutou.qqbot.bilibili;
import lombok.Data;
import java.util.List;
@Data
public class AppUserTask {
private long isSurplus;
private long status;
private long progress;
private long target;
private Wallet wallet;
private List<WeekTask> weekTask;
private long weekTotal;
private long weekGroup;
private DayTask dayTask;
public String toMessageFormat() {
StringBuilder sb = new StringBuilder();
sb.append("当前电池数量:").append(String.format("%.2f", (double) wallet.gold / 100)).append("\n");
sb.append("当前银瓜子数量:").append(wallet.silver).append("\n");
sb.append("每日领取电池:").append(dayTask.toMessageFormat()).append("\n");
weekTask.forEach(task -> sb.append(task.toMessageFormat(weekTotal)).append("\n"));
return sb.toString();
}
@Data
public static class DayTask {
private int status;
private long progress;
private long target;
public String toMessageFormat() {
return switch (getStatus()) {
case 0 -> "不可领取,需要发送弹幕:" + getTarget() + ",进度:" + getProgress();
case 2 -> "未领取";
case 3 -> "已领取";
default -> "未知状态:" + this;
};
}
}
@Data
public static class Wallet {
private long gold;
private long silver;
}
// WeekTask.java
@Data
public static class WeekTask {
private long rewardNum;
private long minimalDay;
private int status;
private int id;
public String toMessageFormat(long totalNum) {
return switch (getStatus()) {
case 0 ->
"任务id:" + id + ",不可领取, 进度天数:" + totalNum + ",需要天数:" + minimalDay + ",任务奖励电池:" + rewardNum;
case 2 -> "任务id:" + id + ":未领取" + ",任务奖励电池:" + rewardNum;
case 3 -> "任务id:" + id + ":已领取" + ",任务奖励电池:" + rewardNum;
default -> "未知状态:" + this;
};
}
}
}

View File

@@ -0,0 +1,141 @@
package com.yutou.qqbot.bilibili;
import com.alibaba.fastjson2.JSONObject;
import com.yutou.qqbot.QQBotManager;
import com.yutou.qqbot.utlis.AppTools;
import com.yutou.qqbot.utlis.HttpTools;
import javax.net.ssl.HttpsURLConnection;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
public class BiliBiliAppUtils {
private static final String AppKey = "1d8b6e7d45233436";
private static final String AppSec = "560c52ccd288fed045859ed18bffd973";
private BiliBiliUtils biliUtils;
public BiliBiliAppUtils(Long qq) {
biliUtils = BiliBiliUtils.getInstance(qq);
}
public String getAccessToken() {
try {
String tmpUrl = "https://www.mcbbs.net/template/mcbbs/image/special_photo_bg.png";
String sign = AppTools.getMD5("api=" + tmpUrl + AppSec);
JSONObject get = biliUtils.http_get(" https://passport.bilibili.com/login/app/third?appkey=" + AppKey + "&api=" + tmpUrl + "&sign=" + sign);
assert get != null;
String uri = get.getJSONObject("data").getString("confirm_uri");
HttpsURLConnection connection = biliUtils.getBiliHttpGet(uri, biliUtils.getCookie());
connection.connect();
if (connection.getResponseCode() == 200) {
Map<String, String> params = HttpTools.getUrlParams(connection.getURL().toString());
return params.get("access_key");
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private Map<String, String> sign(Map<String, String> map) {
map.putAll(getDefaultHeader());
map = sort(map);
StringBuilder builder = new StringBuilder();
for (String key : map.keySet()) {
builder.append(key).append("=").append(map.get(key)).append("&");
}
String param = builder.substring(0, builder.length() - 1);
map.put("sign", AppTools.getMD5(param + AppSec));
return map;
}
private Map<String, String> getDefaultHeader() {
Map<String, String> map = new TreeMap<>();
map.put("access_key", getAccessToken());
map.put("actionKey", "appkey");
map.put("appkey", AppKey);
map.put("build", "7120200");
map.put("c_locale", "zh_CN");
map.put("channel", "xiaomi_cn_tv.danmaku.bili_20210930");
map.put("device", "android");
map.put("disable_rcmd", "0");
map.put("mobi_app", "android");
map.put("platform", "android");
map.put("s_locale", "zh_CN");
map.put("statistics", URLEncoder.encode("{\"appId\":1,\"platform\":3,\"version\":\"7.12.0\",\"abtest\":\"\"}", Charset.defaultCharset()));
map.put("ts", (System.currentTimeMillis() / 1000) + "");
return map;
}
private LinkedHashMap<String, String> sort(Map<String, String> map) {
return new LinkedHashMap<>(new TreeMap<>(map));
}
private Map<String, String> getHeaderMap() {
String md5_1 = AppTools.getMD5(System.currentTimeMillis() + "");
String md5_2 = AppTools.getMD5(System.currentTimeMillis() + "");
String trace_id = md5_1 + ":" + md5_2.substring(0, 16) + ":0:0";
Map<String, String> map = new TreeMap<>();
map.put("x-bili-mid", "96300");
map.put("x-bili-trace-id", trace_id);
map.put("x-bili-aurora-zone", "");
map.put("x-bili-aurora-eid", "WFICRlE=");
map.put("APP-KEY", "android64");
map.put("bili-http-engine", "cronet");
map.put("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
map.put("session_id", md5_1.substring(0, 8));
map.put("Host", "api.live.bilibili.com");
map.put("Connection", "keep-alive");
map.put("fp_local", md5_1 + md5_2);
map.put("fp_remote", md5_1 + md5_2);
map.put("env", "prod");
map.put("buvid", "XUF43FCF17D1747514C79C6D3D43B64C8D1B2");
map.put("Accept-Encoding", "gzip, deflate, br");
map.put("User-Agent", "Mozilla/5.0 BiliDroid/7.4.0 (bbcallen@gmail.com) os/android model/22061218C mobi_app/android build/7040300 channel/xiaomi_cn_tv.danmaku.bili_20210930 innerVer/7040310 osVer/12 network/2");
return map;
}
public AppUserTask getTaskProgress() {
JSONObject task = biliUtils.http_get("https://api.live.bilibili.com/xlive/app-ucenter/v1/userTask/GetUserTaskProgress");
assert task != null;
return task.getObject("data", AppUserTask.class);
}
private void setUserTaskProgress(int index) {
Map<String, String> map = new TreeMap<>();
map.put("target_id", "33989");
map.put("reward_index", index + "");
JSONObject httpGet = biliUtils.http(
"https://api.live.bilibili.com/xlive/app-ucenter/v1/userTask/UserTaskReceiveRewards",
BiliBiliUtils.HTTP.POST,
HttpTools.toUrlParams(sign(map)),
getHeaderMap(),
BiliBiliUtils.RET_MODEL.JSON
);
System.out.println("任务 " + index + " :" + httpGet);
}
public AppUserTask startAppTask() {
AppUserTask task = getTaskProgress();
if (task.getDayTask().getStatus() == 2) {
setUserTaskProgress(0);
}
List<AppUserTask.WeekTask> taskList = task.getWeekTask();
taskList.forEach(weekTask -> {
if (weekTask.getStatus() == 2) {
setUserTaskProgress(weekTask.getId());
}
});
return task;
}
public static void main(String[] args) throws Exception {
System.out.println(new BiliBiliAppUtils(QQBotManager.defQQ).startAppTask().toMessageFormat());
}
}

View File

@@ -2,6 +2,7 @@ package com.yutou.qqbot.bilibili;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.yutou.qqbot.QQBotManager;
import com.yutou.qqbot.interfaces.ObjectInterface;
import com.yutou.qqbot.utlis.HttpTools;
import lombok.Data;
@@ -12,23 +13,28 @@ import java.util.Timer;
import java.util.TimerTask;
public class BiliBiliManga {
public BiliBiliManga() {
biliUtils = BiliBiliUtils.getInstance(QQBotManager.defQQ);
}
public static JSONObject sign() {
JSONObject body = new JSONObject();
body.put("platform", "android");
return BiliBiliUtils.http_post("https://manga.bilibili.com/twirp/activity.v1.Activity/ClockIn", HttpTools.toUrlParams(body));
return BiliBiliUtils.getInstance(QQBotManager.defQQ).http_post("https://manga.bilibili.com/twirp/activity.v1.Activity/ClockIn", HttpTools.toUrlParams(body));
}
private static JSONObject getListProductDate() {
return BiliBiliUtils.http_post("https://manga.bilibili.com/twirp/pointshop.v1.Pointshop/ListProduct", "");
return BiliBiliUtils.getInstance(QQBotManager.defQQ).http_post("https://manga.bilibili.com/twirp/pointshop.v1.Pointshop/ListProduct", "");
}
private static boolean isPayMission = false;
private static Product missionProduct = new Product();
private static Timer mission = null;
private final List<ObjectInterface> anInterface = new ArrayList<>();
private static BiliBiliUtils biliUtils = null;
private ObjectInterface anInterface = null;
public void addInterface(ObjectInterface objectInterface) {
anInterface.add(objectInterface);
anInterface = objectInterface;
}
public static boolean isPayMission() {
@@ -36,7 +42,7 @@ public class BiliBiliManga {
}
public static String getMission() {
return missionProduct+" 兑换数量:"+missionProduct.getPayAmount();
return missionProduct + " 兑换数量:" + missionProduct.getPayAmount();
}
public static List<Product> getListProduct() {
@@ -59,7 +65,7 @@ public class BiliBiliManga {
}
public static int getMyPoint() {
JSONObject user = BiliBiliUtils.http_post("https://manga.bilibili.com/twirp/pointshop.v1.Pointshop/GetUserPoint", "");
JSONObject user = biliUtils.http_post("https://manga.bilibili.com/twirp/pointshop.v1.Pointshop/GetUserPoint", "");
if (user != null && user.getInteger("code") == 0) {
return user.getJSONObject("data").getInteger("point");
}
@@ -70,7 +76,12 @@ public class BiliBiliManga {
JSONObject json = new JSONObject();
if (isPayMission) {
json.put("code", 2);
json.put("msg", "任务正在进行:" +getMission());
json.put("msg", "任务正在进行:" + getMission());
return json;
}
if (biliUtils == null) {
json.put("code", -1);
json.put("msg", "B站未登录");
return json;
}
int userPoint = getMyPoint();
@@ -107,9 +118,9 @@ public class BiliBiliManga {
if (num == 0) {
json.put("code", 3);
json.put("msg", "商品无货,正在抢购");
}else {
} else {
json.put("code", 0);
json.put("msg", "任务创建成功:"+missionProduct+" 兑换数量:"+num);
json.put("msg", "任务创建成功:" + missionProduct + " 兑换数量:" + num);
}
return json;
}
@@ -122,24 +133,18 @@ public class BiliBiliManga {
mission.schedule(new TimerTask() {
@Override
public void run() {
JSONObject post = BiliBiliUtils.http_post("https://manga.bilibili.com/twirp/pointshop.v1.Pointshop/Exchange", HttpTools.toUrlParams(data));
JSONObject post = biliUtils.http_post("https://manga.bilibili.com/twirp/pointshop.v1.Pointshop/Exchange", HttpTools.toUrlParams(data));
if (post == null) {
for (ObjectInterface objectInterface : anInterface) {
objectInterface.out("网络请求失败,请查看日志");
}
anInterface.out("网络请求失败,请查看日志");
cancel();
return;
}
if (post.getInteger("code") == 0) {
for (ObjectInterface mInt : anInterface) {
mInt.out("兑换成功,任务已取消");
}
anInterface.out("兑换成功,任务已取消");
isPayMission = false;
cancel();
} else {
for (ObjectInterface objectInterface : anInterface) {
objectInterface.out("[" + post.getInteger("code") + "]" + post.getString("msg"));
}
anInterface.out("[" + post.getInteger("code") + "]" + post.getString("msg"));
}
}
}, 0, 1000);

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

View File

@@ -15,73 +15,92 @@ import java.util.TimerTask;
public class BiliLogin {
BiliBiliUtils biliUtils;
private long qq;
public JSONObject login(){
JSONObject login= JSON.parseObject(HttpTools.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"));
public BiliLogin(Long qq) {
biliUtils = BiliBiliUtils.getInstance(qq);
this.qq=qq;
}
public JSONObject login() {
JSONObject login = JSON.parseObject(HttpTools.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(() -> 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;
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){
if ((System.currentTimeMillis() - time) > 5 * 60 * 1000) {
cancel();
return;
}
JSONObject json=JSON.parseObject(HttpTools.post("https://passport.bilibili.com/qrcode/getLoginInfo",bd.getBytes(StandardCharsets.UTF_8)));
if(json.containsKey("code")&&json.getInteger("code")==0){
JSONObject json = JSON.parseObject(HttpTools.post("https://passport.bilibili.com/qrcode/getLoginInfo", bd.getBytes(StandardCharsets.UTF_8)));
if (json.containsKey("code") && json.getInteger("code") == 0) {
System.out.println("json = " + json);
String _url=json.getJSONObject("data").getString("url");
Map<String,String> map=HttpTools.getUrlParams(_url);
String _url = json.getJSONObject("data").getString("url");
Map<String, String> map = HttpTools.getUrlParams(_url);
JSONObject cookie=new JSONObject();
JSONArray array=new JSONArray();
JSONObject cookie = new JSONObject();
JSONArray array = new JSONArray();
for (String key : map.keySet()) {
cookie.put(key,map.get(key));
cookie.put(key, map.get(key));
}
System.out.println(array);
if (biliUtils == null) {
biliUtils = BiliBiliUtils.getInstance(qq);
}
assert biliUtils != null;
JSONObject tmp=BiliBiliUtils.http_post(_url,"");
JSONObject tmp = biliUtils.http_post(_url, "");
System.out.println("tmp = " + tmp);
if(tmp==null){
if (tmp == null) {
cancel();
return;
}
String sid=tmp.getString("cookie");
sid=sid.split("sid=")[1];
sid=sid.split(";")[0];
cookie.put("sid",sid);
cookie.put("Domain",".bilibili.com");
ConfigTools.saveFile(new File("bilibili.cookie"),cookie.toJSONString());
String sid = tmp.getString("cookie");
sid = sid.split("sid=")[1];
sid = sid.split(";")[0];
cookie.put("sid", sid);
cookie.put("Domain", ".bilibili.com");
ConfigTools.saveFile(new File(qq + "_bilibili.cookie"), cookie.toJSONString());
cancel();
}
}
},0,3000);
}, 0, 3000);
}
public boolean testLogin(){
JSONObject jsonObject = BiliBiliUtils.getLoginInfo();
return jsonObject.getInteger("code")==0;
public boolean testLogin() {
if (biliUtils == null) {
return false;
}
JSONObject jsonObject = biliUtils.getLoginInfo();
return jsonObject.getInteger("code") == 0;
}
public static String getCookieToken(){
if (StringUtils.isEmpty(ConfigTools.readFile(new File("bilibili.cookie")))) {
public static String getCookieToken(Long qq) {
if (StringUtils.isEmpty(ConfigTools.readFile(new File(qq+"_bilibili.cookie")))) {
return null;
}
JSONObject json = JSON.parseObject(ConfigTools.readFile(new File("bilibili.cookie")));
JSONObject json = JSON.parseObject(ConfigTools.readFile(new File(qq+"_bilibili.cookie")));
return json.getString("bili_jct");
}
public static void main(String[] args) {
BiliLogin login = new BiliLogin();
BiliLogin login = new BiliLogin(583819556L);
boolean testLogin = login.testLogin();
System.out.println("testLogin = " + testLogin);
// JSONObject json = login.login();
// QRCodeUtils.createQRCode("bili_login", json.getString("url"));
}
}