2022-04-08 02:26:29 +08:00
|
|
|
package com.yutou.qqbot.bilibili;
|
|
|
|
|
2022-04-28 09:50:33 +08:00
|
|
|
import com.alibaba.fastjson2.JSON;
|
|
|
|
import com.alibaba.fastjson2.JSONObject;
|
2022-04-08 02:26:29 +08:00
|
|
|
import com.yutou.qqbot.utlis.ConfigTools;
|
2022-04-28 09:50:33 +08:00
|
|
|
import com.yutou.qqbot.utlis.StringUtils;
|
2022-04-08 02:26:29 +08:00
|
|
|
|
|
|
|
import javax.net.ssl.HttpsURLConnection;
|
|
|
|
import java.io.*;
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
|
|
|
public class BiliBiliUtils {
|
|
|
|
private static long oldBiliBiliHttpTime = 0;
|
|
|
|
|
|
|
|
public synchronized static JSONObject http_get(String url) {
|
|
|
|
try {
|
|
|
|
// Log.i("调用url = "+url);
|
2022-04-08 02:40:33 +08:00
|
|
|
HttpsURLConnection connection = getBiliHttpGet(url, getCookie());
|
2022-04-08 02:26:29 +08:00
|
|
|
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 = outputStream.toString(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);
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static JSONObject http_post(String url, String body) {
|
|
|
|
JSONObject json = null;
|
|
|
|
try {
|
|
|
|
if (System.currentTimeMillis() - oldBiliBiliHttpTime < 1000) {
|
|
|
|
try {
|
|
|
|
Thread.sleep(500);
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
oldBiliBiliHttpTime = System.currentTimeMillis();
|
|
|
|
}
|
|
|
|
HttpURLConnection connection = getBiliHttpPost(url, getCookie());
|
|
|
|
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
|
|
|
|
OutputStream connectionOutputStream = null;
|
|
|
|
if (!StringUtils.isEmpty(body)) {
|
|
|
|
connectionOutputStream = connection.getOutputStream();
|
|
|
|
connectionOutputStream.write(body.getBytes(StandardCharsets.UTF_8));
|
|
|
|
connectionOutputStream.flush();
|
|
|
|
}
|
|
|
|
connection.connect();
|
|
|
|
if(connection.getResponseCode()==400){
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
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 = outputStream.toString(StandardCharsets.UTF_8);
|
|
|
|
outputStream.close();
|
|
|
|
try {
|
|
|
|
json = JSON.parseObject(str);
|
|
|
|
json.put("cookie", connection.getHeaderField("Set-Cookie"));
|
|
|
|
return json;
|
|
|
|
} catch (Exception e) {
|
|
|
|
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) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return json;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String getCookie() {
|
|
|
|
if (StringUtils.isEmpty(ConfigTools.readFile(new File("bilibili.cookie")))) {
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
2022-04-28 09:50:33 +08:00
|
|
|
JSONObject json = JSON.parseObject(ConfigTools.readFile(new File("bilibili.cookie")));
|
2022-04-08 02:26:29 +08:00
|
|
|
StringBuilder builder = new StringBuilder();
|
|
|
|
for (String s : json.keySet()) {
|
|
|
|
builder.append(s).append("=").append(json.getString(s)).append(";");
|
|
|
|
|
|
|
|
}
|
|
|
|
return builder.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static HttpURLConnection getBiliHttpPost(String url, String cookie) throws Exception {
|
|
|
|
if (System.currentTimeMillis() - oldBiliBiliHttpTime < 1000) {
|
|
|
|
try {
|
|
|
|
Thread.sleep(500);
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
oldBiliBiliHttpTime = System.currentTimeMillis();
|
|
|
|
}
|
|
|
|
HttpsURLConnection connection = (HttpsURLConnection) new URL(url).openConnection();
|
|
|
|
connection.setRequestMethod("POST");
|
|
|
|
connection.setDoOutput(true);
|
|
|
|
connection.setDoInput(true);
|
|
|
|
setConnection(cookie, connection);
|
|
|
|
return connection;
|
|
|
|
}
|
|
|
|
|
2022-04-08 02:40:33 +08:00
|
|
|
public static HttpsURLConnection getBiliHttpGet(String url, String cookie) throws IOException {
|
2022-04-08 02:26:29 +08:00
|
|
|
if (System.currentTimeMillis() - oldBiliBiliHttpTime < 1000) {
|
|
|
|
try {
|
|
|
|
Thread.sleep(500);
|
|
|
|
} catch (InterruptedException ignored) {
|
|
|
|
}
|
|
|
|
oldBiliBiliHttpTime = System.currentTimeMillis();
|
|
|
|
}
|
2022-04-08 02:40:33 +08:00
|
|
|
HttpsURLConnection connection = (HttpsURLConnection) new URL(url).openConnection();
|
2022-04-08 02:26:29 +08:00
|
|
|
setConnection(cookie, connection);
|
|
|
|
connection.setReadTimeout(5000);
|
|
|
|
connection.setConnectTimeout(5000);
|
|
|
|
return connection;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void setConnection(String cookie, HttpURLConnection connection) {
|
|
|
|
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");
|
2022-04-08 02:40:33 +08:00
|
|
|
//connection.setRequestProperty("Referer", ".bilibili.com");
|
2022-04-08 02:26:29 +08:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
|
|
|
public static JSONObject getLoginInfo() {
|
|
|
|
JSONObject jsonObject = BiliBiliUtils.http_get("https://api.bilibili.com/x/web-interface/nav");
|
|
|
|
if (jsonObject == null) {
|
|
|
|
jsonObject = new JSONObject();
|
|
|
|
jsonObject.put("code", "-1");
|
|
|
|
jsonObject.put("data", new JSONObject());
|
|
|
|
}
|
|
|
|
return jsonObject;
|
|
|
|
}
|
|
|
|
}
|