新增B站漫画签到

This commit is contained in:
2022-04-08 02:26:29 +08:00
parent d31534311f
commit abfe65a534
12 changed files with 423 additions and 15 deletions

View File

@@ -0,0 +1,16 @@
package com.yutou.qqbot.bilibili;
import com.alibaba.fastjson.JSONObject;
import com.yutou.qqbot.utlis.HttpTools;
public class BiliBiliManga {
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));
}
public static void main(String[] args) {
BiliBiliManga.sign();
}
}

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

View File

@@ -0,0 +1,73 @@
package com.yutou.qqbot.bilibili;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yutou.qqbot.utlis.ConfigTools;
import com.yutou.qqbot.utlis.HttpTools;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
public class BiliLogin {
public JSONObject login(){
JSONObject login= JSONObject.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;
new Timer().schedule(new TimerTask() {
@Override
public void run() {
if((System.currentTimeMillis()-time)>5*60*1000){
cancel();
return;
}
JSONObject json=JSONObject.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);
JSONObject cookie=new JSONObject();
JSONArray array=new JSONArray();
for (String key : map.keySet()) {
cookie.put(key,map.get(key));
}
System.out.println(array);
JSONObject tmp=BiliBiliUtils.http_post(_url,"");
System.out.println("tmp = " + tmp);
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());
cancel();
}
}
},0,3000);
}
public boolean testLogin(){
JSONObject jsonObject = BiliBiliUtils.getLoginInfo();
return jsonObject.getInteger("code")==0;
}
public static void main(String[] args) {
BiliLogin login = new BiliLogin();
login.testLogin();
}
}