新增B站视频下载功能
新增弹幕转ass功能 优化B站网络请求头代码
This commit is contained in:
@@ -2,18 +2,29 @@ package com.yutou.qqbot.bilibili;
|
||||
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.yutou.qqbot.utlis.ConfigTools;
|
||||
import com.yutou.qqbot.utlis.StringUtils;
|
||||
import com.yutou.qqbot.interfaces.ObjectInterface;
|
||||
import com.yutou.qqbot.utlis.*;
|
||||
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
import java.io.*;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Proxy;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
|
||||
public class BiliBiliUtils {
|
||||
private static long oldBiliBiliHttpTime = 0;
|
||||
|
||||
public enum HTTP {
|
||||
POST, GET
|
||||
}
|
||||
|
||||
public enum RET_MODEL {
|
||||
BYTE, JSON
|
||||
}
|
||||
|
||||
public synchronized static JSONObject http_get(String url) {
|
||||
try {
|
||||
// Log.i("调用url = "+url);
|
||||
@@ -49,7 +60,15 @@ public class BiliBiliUtils {
|
||||
}
|
||||
|
||||
public static 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) {
|
||||
JSONObject json = null;
|
||||
BufferedInputStream stream = null;
|
||||
ByteArrayOutputStream outputStream = null;
|
||||
OutputStream connectionOutputStream = null;
|
||||
HttpURLConnection connection = null;
|
||||
try {
|
||||
if (System.currentTimeMillis() - oldBiliBiliHttpTime < 1000) {
|
||||
try {
|
||||
@@ -59,49 +78,68 @@ public class BiliBiliUtils {
|
||||
}
|
||||
oldBiliBiliHttpTime = System.currentTimeMillis();
|
||||
}
|
||||
HttpURLConnection connection = getBiliHttpPost(url, getCookie());
|
||||
if (model == HTTP.POST) {
|
||||
connection = getBiliHttpPost(url, getCookie());
|
||||
} else {
|
||||
connection = getBiliHttpGet(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){
|
||||
if (connection.getResponseCode() == 400) {
|
||||
return null;
|
||||
}
|
||||
BufferedInputStream stream = new BufferedInputStream(connection.getInputStream());
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
stream = new BufferedInputStream(connection.getInputStream());
|
||||
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();
|
||||
}
|
||||
if (ret_model == RET_MODEL.BYTE) {
|
||||
return (T) outputStream.toByteArray();
|
||||
}
|
||||
String str = outputStream.toString(StandardCharsets.UTF_8);
|
||||
outputStream.close();
|
||||
|
||||
try {
|
||||
json = JSON.parseObject(str);
|
||||
json.put("cookie", connection.getHeaderField("Set-Cookie"));
|
||||
return json;
|
||||
return (T) 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();
|
||||
return (T) json;
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (stream != null) {
|
||||
stream.close();
|
||||
}
|
||||
if (outputStream != null) {
|
||||
outputStream.close();
|
||||
}
|
||||
if (connectionOutputStream != null) {
|
||||
connectionOutputStream.close();
|
||||
}
|
||||
if (connection != null) {
|
||||
connection.disconnect();
|
||||
}
|
||||
|
||||
} catch (Exception ignored) {
|
||||
|
||||
}
|
||||
}
|
||||
return json;
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String getCookie() {
|
||||
@@ -150,15 +188,101 @@ public class BiliBiliUtils {
|
||||
return connection;
|
||||
}
|
||||
|
||||
public static File download(final String url, final String saveName, boolean isProxy) {
|
||||
File jar = null;
|
||||
try {
|
||||
File savePath = new File(HttpTools.downloadPath+saveName);
|
||||
Proxy proxy = null;
|
||||
if (!savePath.exists()) {
|
||||
savePath.mkdirs();
|
||||
}
|
||||
savePath.delete();
|
||||
Log.i("DOWNLOAD", "下载文件:" + url + " 保存文件:" + saveName);
|
||||
if (isProxy) {
|
||||
proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 7890));
|
||||
}
|
||||
HttpsURLConnection connection;
|
||||
if (isProxy) {
|
||||
connection = (HttpsURLConnection) new URL(url).openConnection(proxy);
|
||||
} else {
|
||||
connection = (HttpsURLConnection) new URL(url).openConnection();
|
||||
}
|
||||
setConnection(getCookie(), connection);
|
||||
|
||||
InputStream inputStream = connection.getInputStream();
|
||||
jar = new File(HttpTools.downloadPath + saveName + "_tmp.tmp");
|
||||
jar.createNewFile();
|
||||
Log.i("DOWNLOAD", "临时保存文件:" + jar.getAbsolutePath());
|
||||
OutputStream outputStream = new FileOutputStream(jar);
|
||||
byte[] bytes = new byte[1024];
|
||||
double size = connection.getContentLength();
|
||||
double downSize = 0;
|
||||
int len;
|
||||
while ((len = inputStream.read(bytes)) > 0) {
|
||||
outputStream.write(bytes, 0, len);
|
||||
downSize += len;
|
||||
}
|
||||
outputStream.close();
|
||||
inputStream.close();
|
||||
File oldJar = new File(HttpTools.downloadPath + saveName);
|
||||
if (oldJar.exists()) {
|
||||
oldJar.delete();
|
||||
}
|
||||
jar.renameTo(oldJar);
|
||||
Log.i("DOWNLOAD", "实际保存:" + oldJar.getAbsolutePath() + " " + oldJar.getName());
|
||||
return oldJar;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
if (jar != null) {
|
||||
jar.delete();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static 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(" ");
|
||||
/* builder.append("-user_agent").append(" ");
|
||||
builder.append("\"").append("User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36 Referer:https://live.bilibili.com").append("\"").append(" ");
|
||||
builder.append("-cookies").append(" ");
|
||||
builder.append("\"").append(getCookie()).append("\"").append(" ");
|
||||
builder.append("-headers").append(" ");
|
||||
builder.append("\"").append("Referer:https://live.bilibili.com").append("\"").append(" ");*/
|
||||
for (String _url : url) {
|
||||
builder.append("-i").append(" ");
|
||||
builder.append("\"").append(_url).append("\"").append(" ");
|
||||
}
|
||||
builder.append("-vcodec").append(" ");
|
||||
builder.append("copy").append(" ");
|
||||
builder.append("-acodec").append(" ");
|
||||
builder.append("copy").append(" ");
|
||||
builder.append("-threads").append(" ");
|
||||
builder.append("8").append(" ");
|
||||
// builder.append("-y").append(" ");
|
||||
builder.append(new File(HttpTools.downloadPath + saveName + ".mp4").getAbsolutePath()).append(" ");
|
||||
System.out.println(builder);
|
||||
AppTools.exec(builder.toString(), new ObjectInterface() {
|
||||
@Override
|
||||
public void out(String data) {
|
||||
super.out(data);
|
||||
System.out.println("data = " + data);
|
||||
}
|
||||
}, false, false);
|
||||
}).start();
|
||||
}
|
||||
|
||||
|
||||
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");
|
||||
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");
|
||||
connection.setRequestProperty("Referer", "https://www.bilibili.com");
|
||||
connection.addRequestProperty("Connection", "keep-alive");
|
||||
connection.addRequestProperty("Upgrade-Insecure-Requests", "1");
|
||||
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 static JSONObject getLoginInfo() {
|
||||
|
||||
Reference in New Issue
Block a user