新增B站漫画签到
This commit is contained in:
173
src/main/java/com/yutou/qqbot/bilibili/BiliBiliUtils.java
Normal file
173
src/main/java/com/yutou/qqbot/bilibili/BiliBiliUtils.java
Normal file
@@ -0,0 +1,173 @@
|
||||
package com.yutou.qqbot.bilibili;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.yutou.qqbot.utlis.ConfigTools;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
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);
|
||||
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 = 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 "";
|
||||
}
|
||||
JSONObject json = JSONObject.parseObject(ConfigTools.readFile(new File("bilibili.cookie")));
|
||||
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;
|
||||
}
|
||||
|
||||
public static HttpURLConnection getBiliHttpGet(String url, String cookie) throws IOException {
|
||||
if (System.currentTimeMillis() - oldBiliBiliHttpTime < 1000) {
|
||||
try {
|
||||
Thread.sleep(500);
|
||||
} catch (InterruptedException ignored) {
|
||||
}
|
||||
oldBiliBiliHttpTime = System.currentTimeMillis();
|
||||
}
|
||||
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
|
||||
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");
|
||||
connection.setRequestProperty("Referer", ".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");
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user