提取B站API统一管理

This commit is contained in:
Yutou 2024-01-17 17:22:21 +08:00
parent 68fd6efdf4
commit 4dce74eaf8
12 changed files with 97 additions and 30 deletions

View File

@ -0,0 +1,8 @@
package com.yutou.bilibili;
public class BaseAPI {
public static final String BASE_URL = "https://api.bilibili.com/";
public static final String MCBBS_PNG = "https://www.mcbbs.net/template/mcbbs/image/special_photo_bg.png";
public static final String ACCESS_TOKEN = "https://passport.bilibili.com/login/app/third";
}

View File

@ -0,0 +1,9 @@
package com.yutou.bilibili.api;
public class LiveAPI {
public static final String LIVE_SEND_DANMU="https://api.live.bilibili.com/msg/send";
public static final String LIVE_ROOM_INFO = "https://api.live.bilibili.com/room/v1/Room/get_info";
public static final String LIVE_TASK_PROGRESS = "https://api.live.bilibili.com/xlive/app-ucenter/v1/userTask/GetUserTaskProgress";
public static final String LIVE_SET_TASK_PROGRESS = "https://api.live.bilibili.com/xlive/app-ucenter/v1/userTask/UserTaskReceiveRewards";
}

View File

@ -0,0 +1,7 @@
package com.yutou.bilibili.api;
public class LoginAPI {
public static final String LOGIN_QRCODE = "https://passport.bilibili.com/x/passport-login/web/qrcode/generate";
public static final String LOGIN_QRCODE_POLL="https://passport.bilibili.com/x/passport-login/web/qrcode/poll";
}

View File

@ -0,0 +1,9 @@
package com.yutou.bilibili.api;
public class MangaApi {
public static final String SIGN = "https://manga.bilibili.com/twirp/activity.v1.Activity/ClockIn";
public static final String LIST_PRODUCT_DATE = "https://manga.bilibili.com/twirp/pointshop.v1.Pointshop/ListProduct";
public static final String USER_POINT = "https://manga.bilibili.com/twirp/pointshop.v1.Pointshop/GetUserPoint";
public static final String PAY_MISSION = "https://manga.bilibili.com/twirp/pointshop.v1.Pointshop/Exchange";
}

View File

@ -0,0 +1,7 @@
package com.yutou.bilibili.api;
public class SignApi {
public static final String LIVE_SIGN_COIN = "https://api.live.bilibili.com/xlive/revenue/v1/wallet/silver2coin";
public static final String LIVE_SIGN = "https://api.live.bilibili.com/xlive/web-ucenter/v1/sign/DoSign";
public static final String VIP_SIGN = "https://api.bilibili.com/pgc/activity/score/task/sign";
}

View File

@ -0,0 +1,10 @@
package com.yutou.bilibili.api;
public class UserApi {
@Deprecated
public static final String USER_INFO = "https://api.bilibili.com/x/space/acc/info";
public static final String USER_INFO_V2="https://api.bilibili.com/x/space/wbi/acc/info";
public static final String NAV = "https://api.bilibili.com/x/web-interface/nav";
}

View File

@ -0,0 +1,6 @@
package com.yutou.bilibili.api;
public class VideoApi {
public static final String VIDEO_AI = "https://api.bilibili.com/x/web-interface/view/conclusion/get";
}

View File

@ -1,6 +1,9 @@
package com.yutou.qqbot.bilibili;
import com.alibaba.fastjson2.JSONObject;
import com.yutou.bilibili.BaseAPI;
import com.yutou.bilibili.api.LiveAPI;
import com.yutou.bilibili.api.VideoApi;
import com.yutou.qqbot.QQBotManager;
import com.yutou.qqbot.models.BiliBili.BiliVideo;
import com.yutou.qqbot.utlis.AppTools;
@ -27,9 +30,9 @@ public class BiliBiliAppUtils {
public String getAccessToken() {
try {
String tmpUrl = "https://www.mcbbs.net/template/mcbbs/image/special_photo_bg.png";
String tmpUrl = BaseAPI.MCBBS_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);
JSONObject get = biliUtils.http_get(BaseAPI.ACCESS_TOKEN+"?appkey=" + AppKey + "&api=" + tmpUrl + "&sign=" + sign);
assert get != null;
String uri = get.getJSONObject("data").getString("confirm_uri");
HttpsURLConnection connection = biliUtils.getBiliHttpGet(uri, biliUtils.getCookie());
@ -103,7 +106,7 @@ public class BiliBiliAppUtils {
}
public AppUserTask getTaskProgress() {
JSONObject task = biliUtils.http_get("https://api.live.bilibili.com/xlive/app-ucenter/v1/userTask/GetUserTaskProgress");
JSONObject task = biliUtils.http_get(LiveAPI.LIVE_TASK_PROGRESS);
System.out.println("task = " + task);
assert task != null;
return task.getObject("data", AppUserTask.class);
@ -115,7 +118,7 @@ public class BiliBiliAppUtils {
map.put("reward_index", index + "");
JSONObject httpGet = biliUtils.http(
"https://api.live.bilibili.com/xlive/app-ucenter/v1/userTask/UserTaskReceiveRewards",
LiveAPI.LIVE_SET_TASK_PROGRESS,
BiliBiliUtils.HTTP.POST,
HttpTools.toUrlParams(sign(map)),
getHeaderMap(),
@ -151,7 +154,8 @@ public class BiliBiliAppUtils {
body.put("up_mid",videoInfo.getJSONObject("data").getJSONObject("owner").getString("mid"));
body.put("bvid",videoInfo.getJSONObject("data").getString("bvid"));
BiliBiliWbiSign.getWbiSign(body);
JSONObject object = BiliBiliUtils.getInstance(QQBotManager.defQQ).http_get("https://api.bilibili.com/x/web-interface/view/conclusion/get"+"?"+HttpTools.toUrlParams(body));
JSONObject object = BiliBiliUtils.getInstance(QQBotManager.defQQ)
.http_get(VideoApi.VIDEO_AI +"?"+HttpTools.toUrlParams(body));
if(object.getInteger("code")==0){
if(object.getJSONObject("data").getInteger("code")==0) {
return object.getJSONObject("data").getJSONObject("model_result").getString("summary");

View File

@ -2,6 +2,7 @@ package com.yutou.qqbot.bilibili;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.yutou.bilibili.api.MangaApi;
import com.yutou.qqbot.QQBotManager;
import com.yutou.qqbot.interfaces.ObjectInterface;
import com.yutou.qqbot.utlis.HttpTools;
@ -20,11 +21,11 @@ public class BiliBiliManga {
public static JSONObject sign() {
JSONObject body = new JSONObject();
body.put("platform", "android");
return BiliBiliUtils.getInstance(QQBotManager.defQQ).http_post("https://manga.bilibili.com/twirp/activity.v1.Activity/ClockIn", HttpTools.toUrlParams(body));
return BiliBiliUtils.getInstance(QQBotManager.defQQ).http_post(MangaApi.SIGN, HttpTools.toUrlParams(body));
}
private static JSONObject getListProductDate() {
return BiliBiliUtils.getInstance(QQBotManager.defQQ).http_post("https://manga.bilibili.com/twirp/pointshop.v1.Pointshop/ListProduct", "");
return BiliBiliUtils.getInstance(QQBotManager.defQQ).http_post(MangaApi.LIST_PRODUCT_DATE, "");
}
private static boolean isPayMission = false;
@ -65,7 +66,7 @@ public class BiliBiliManga {
}
public static int getMyPoint() {
JSONObject user = biliUtils.http_post("https://manga.bilibili.com/twirp/pointshop.v1.Pointshop/GetUserPoint", "");
JSONObject user = biliUtils.http_post(MangaApi.USER_POINT, "");
if (user != null && user.getInteger("code") == 0) {
return user.getJSONObject("data").getInteger("point");
}
@ -133,7 +134,7 @@ public class BiliBiliManga {
mission.schedule(new TimerTask() {
@Override
public void run() {
JSONObject post = biliUtils.http_post("https://manga.bilibili.com/twirp/pointshop.v1.Pointshop/Exchange", HttpTools.toUrlParams(data));
JSONObject post = biliUtils.http_post(MangaApi.PAY_MISSION, HttpTools.toUrlParams(data));
if (post == null) {
anInterface.out("网络请求失败,请查看日志");
cancel();

View File

@ -2,6 +2,10 @@ package com.yutou.qqbot.bilibili;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.yutou.bilibili.api.LiveAPI;
import com.yutou.bilibili.api.LoginAPI;
import com.yutou.bilibili.api.SignApi;
import com.yutou.bilibili.api.UserApi;
import com.yutou.qqbot.QQBotManager;
import com.yutou.qqbot.interfaces.ObjectInterface;
import com.yutou.qqbot.utlis.*;
@ -16,6 +20,7 @@ import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.zip.GZIPInputStream;
public class BiliBiliUtils {
@ -40,7 +45,7 @@ public class BiliBiliUtils {
public synchronized JSONObject http_get(String url) {
try {
Log.i("调用url = "+url);
Log.i("调用url = " + url);
HttpsURLConnection connection = getBiliHttpGet(url, getCookie());
BufferedInputStream stream = new BufferedInputStream(connection.getInputStream());
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
@ -73,9 +78,9 @@ public class BiliBiliUtils {
public JSONObject http_login_getSid(String url) {
try {
Log.i("调用url = "+url);
Log.i("调用url = " + url);
HttpsURLConnection connection = (HttpsURLConnection) new URL(url).openConnection();
setConnection(null,connection);
setConnection(null, connection);
connection.setRequestMethod("GET");
connection.connect();
BufferedInputStream stream = new BufferedInputStream(connection.getInputStream());
@ -94,12 +99,12 @@ public class BiliBiliUtils {
outputStream.close();
try {
JSONObject json = JSON.parseObject(str);
if(cookie!=null){
StringBuilder ck=new StringBuilder();
if (cookie != null) {
StringBuilder ck = new StringBuilder();
for (String key : cookie) {
ck.append(";").append(key);
}
json.put("cookie",ck.toString());
json.put("cookie", ck.toString());
}
return json;
} catch (Exception e) {
@ -366,14 +371,14 @@ public class BiliBiliUtils {
connection.setRequestProperty("Referer", "https://www.bilibili.com");
connection.addRequestProperty("Connection", "keep-alive");
connection.addRequestProperty("Upgrade-Insecure-Requests", "1");
if(!StringUtils.isEmpty(cookie)) {
if (!StringUtils.isEmpty(cookie)) {
connection.addRequestProperty("Cookie", cookie);
}
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 JSONObject getLoginInfo() {
JSONObject jsonObject = http_get("https://api.bilibili.com/x/web-interface/nav");
JSONObject jsonObject = http_get(UserApi.NAV);
if (jsonObject == null) {
jsonObject = new JSONObject();
jsonObject.put("code", "-1");
@ -389,7 +394,7 @@ public class BiliBiliUtils {
/* System.out.println(getLiveRoom(42062));
System.out.println("--------------------------------------------");
System.out.println(getUserInfo(730732));*/
String sign = new BiliBiliUtils(583819556).liveSignIn();
JSONObject sign = new BiliBiliUtils(583819556).getUserInfo(96300);
System.out.println("sign = " + sign);
}
@ -402,7 +407,7 @@ public class BiliBiliUtils {
body.put("rnd", System.currentTimeMillis() / 1000);
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));
JSONObject post = http_post(LiveAPI.LIVE_SEND_DANMU, HttpTools.toUrlParams(body));
return post.getInteger("code") == 0;
}
@ -411,9 +416,9 @@ public class BiliBiliUtils {
JSONObject body = new JSONObject();
body.put("csrf", BiliLogin.getCookieToken(qq));
body.put("csrf_token", BiliLogin.getCookieToken(qq));
JSONObject toCoin = http_post("https://api.live.bilibili.com/xlive/revenue/v1/wallet/silver2coin", HttpTools.toUrlParams(body));
JSONObject liveSign = http_get("https://api.live.bilibili.com/xlive/web-ucenter/v1/sign/DoSign");
JSONObject vipSign = http_post("https://api.bilibili.com/pgc/activity/score/task/sign", null);
JSONObject toCoin = http_post(SignApi.LIVE_SIGN_COIN, HttpTools.toUrlParams(body));
JSONObject liveSign = http_get(SignApi.LIVE_SIGN);
JSONObject vipSign = http_post(SignApi.VIP_SIGN, null);
return "银瓜子兑换硬币:" + toCoin.getString("message") + "|" + "直播签到:" + liveSign.getString("message") + "|大会员中心签到:" + vipSign.getString("message");
}
@ -422,13 +427,14 @@ public class BiliBiliUtils {
body.put("room_id", roomId);
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));
return http_post(LiveAPI.LIVE_ROOM_INFO, HttpTools.toUrlParams(body));
}
public JSONObject getUserInfo(int mid) {
JSONObject body = new JSONObject();
body.put("mid", mid);
return http_get("https://api.bilibili.com/x/space/acc/info?" + HttpTools.toUrlParams(body));
TreeMap<String, String> body = new TreeMap<>();
body.put("mid", mid + "");
BiliBiliWbiSign.getWbiSign(body);
return http_get(UserApi.USER_INFO_V2 + "?" + HttpTools.toUrlParams(body));
}
public boolean checkLiveRoom(int roomId) {

View File

@ -3,6 +3,7 @@ package com.yutou.qqbot.bilibili;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.yutou.bilibili.api.LoginAPI;
import com.yutou.qqbot.QQBotManager;
import com.yutou.qqbot.utlis.*;
@ -25,7 +26,7 @@ public class BiliLogin {
}
public JSONObject login() {
JSONObject login = JSON.parseObject(HttpTools.get("https://passport.bilibili.com/x/passport-login/web/qrcode/generate"));
JSONObject login = JSON.parseObject(HttpTools.get(LoginAPI.LOGIN_QRCODE));
JSONObject json = new JSONObject();
System.out.println("login = " + login);
json.put("code", login.getInteger("code"));
@ -50,8 +51,8 @@ public class BiliLogin {
cancel();
return;
}
JSONObject json = biliUtils.http_login_getSid("https://passport.bilibili.com/x/passport-login/web/qrcode/poll?qrcode_key=" + oauthKey);
Log.i("B站登陆",json.toJSONString());
JSONObject json = biliUtils.http_login_getSid(LoginAPI.LOGIN_QRCODE_POLL + "?qrcode_key=" + oauthKey);
Log.i("B站登陆", json.toJSONString());
if (json.getInteger("code") == 0 && json.getJSONObject("data").getInteger("code") == 0) {
String[] split = json.getString("cookie").split(";");

View File

@ -26,7 +26,6 @@ public class BiliBiliWbiSign {
return getWbiSign(body);
}
body.put("wts", String.valueOf(System.currentTimeMillis() / 1000));
body.put("wts", "1705394671");
String params = HttpTools.toUrlParams(body);
System.out.println("params = " + params);
body.put("w_rid", AppTools.getMD5(params + genMixinKey(rawWbiKey)));