|
@@ -1,6 +1,8 @@
|
|
|
package com.tzld.piaoquan.longarticle.utils.other;
|
|
|
|
|
|
import cn.hutool.http.HttpUtil;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import com.tzld.piaoquan.longarticle.utils.ConfigUtil;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.codec.digest.DigestUtils;
|
|
@@ -9,10 +11,14 @@ import org.apache.http.client.methods.HttpGet;
|
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
import org.apache.http.impl.client.HttpClients;
|
|
|
import org.apache.http.message.BasicHeader;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
import java.io.*;
|
|
|
+import java.net.Authenticator;
|
|
|
import java.net.HttpURLConnection;
|
|
|
+import java.net.PasswordAuthentication;
|
|
|
import java.net.URL;
|
|
|
+import java.nio.file.Files;
|
|
|
import java.util.Objects;
|
|
|
import java.util.UUID;
|
|
|
|
|
@@ -27,82 +33,95 @@ public class VideoDownloader {
|
|
|
|
|
|
public static String downloadCover(String outVideoId, String platform, String coverUrl) {
|
|
|
String path = generateCoverPath(platform, outVideoId);
|
|
|
- if (download(path, coverUrl, platform)) {
|
|
|
+ if (download(path, coverUrl, platform) == 0) {
|
|
|
return path;
|
|
|
}
|
|
|
return "";
|
|
|
}
|
|
|
|
|
|
public static String downloadVideo(String outVideoId, String platform, String videoUrl) {
|
|
|
- String path = generateVideoPath(platform, outVideoId);
|
|
|
- if (download(path, videoUrl, platform)) {
|
|
|
- return path;
|
|
|
- }
|
|
|
- return "";
|
|
|
- }
|
|
|
-
|
|
|
- public static boolean download(String path, String videoUrl, String platform) {
|
|
|
- try {
|
|
|
- int retries = 0;
|
|
|
- long fileSize = 0;
|
|
|
+ int retries = 0;
|
|
|
+ while (retries < MAX_RETRIES) {
|
|
|
+ String path = generateVideoPath(platform, outVideoId);
|
|
|
+ int download = download(path, videoUrl, platform);
|
|
|
+ if (download == 0) {
|
|
|
+ return path;
|
|
|
+ }
|
|
|
|
|
|
- while (retries < MAX_RETRIES) {
|
|
|
- File file = new File(path);
|
|
|
- if (file.exists()) {
|
|
|
- file.delete();
|
|
|
- }
|
|
|
- HttpURLConnection connection = (HttpURLConnection) new URL(videoUrl).openConnection();
|
|
|
- connection.setRequestMethod("GET");
|
|
|
- connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0");
|
|
|
- connection.setRequestProperty("Accept", "*/*");
|
|
|
- connection.setRequestProperty("Range", "bytes=" + fileSize + "-");
|
|
|
- connection.setRequestProperty("accept-language", "en,zh;q=0.9,zh-CN;q=0.8");
|
|
|
- if (Objects.equals(platform, "dy_search")) {
|
|
|
- connection.setRequestProperty("referer", "https://v11-coldf.douyinvod.com/");
|
|
|
+ //百度重新获取链接
|
|
|
+ if (download == 403 && Objects.equals(platform, "baidu_search")) {
|
|
|
+ JSONObject videoDetail = HkspSearch.getVideoDetail(outVideoId);
|
|
|
+ if (videoDetail != null) {
|
|
|
+ String newVideoUrl = videoDetail.getString("playurl");
|
|
|
+ if (newVideoUrl != null) {
|
|
|
+ videoUrl = newVideoUrl;
|
|
|
+ }
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
-
|
|
|
- // 设置代理
|
|
|
- System.setProperty("http.proxyHost", PROXY_HOST);
|
|
|
- System.setProperty("http.proxyPort", String.valueOf(PROXY_PORT));
|
|
|
- System.setProperty("http.proxyUser", USERNAME);
|
|
|
- System.setProperty("http.proxyPassword", PASSWORD);
|
|
|
-
|
|
|
- // 连接并获取响应
|
|
|
- connection.connect();
|
|
|
-
|
|
|
- int responseCode = connection.getResponseCode();
|
|
|
- if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_PARTIAL) {
|
|
|
- // 下载文件
|
|
|
- try (InputStream inputStream = connection.getInputStream();
|
|
|
- OutputStream outputStream = new FileOutputStream(file, true)) { // 追加模式
|
|
|
- byte[] buffer = new byte[1024 * 1024]; // 1MB buffer
|
|
|
- int bytesRead;
|
|
|
- while ((bytesRead = inputStream.read(buffer)) != -1) {
|
|
|
- outputStream.write(buffer, 0, bytesRead);
|
|
|
+ //抖音重新获取链接
|
|
|
+ if (download == 403 && Objects.equals(platform, "dy_search")) {
|
|
|
+ JSONObject videoDetail = DouyinSearch.douyinDetail(outVideoId);
|
|
|
+ if (videoDetail != null) {
|
|
|
+ JSONArray videoUrlList = videoDetail.getJSONArray("video_url_list");
|
|
|
+ if (!CollectionUtils.isEmpty(videoUrlList)) {
|
|
|
+ String newVideoUrl = videoUrlList.getJSONObject(0).getString("video_url");
|
|
|
+ if (newVideoUrl != null) {
|
|
|
+ videoUrl = newVideoUrl;
|
|
|
}
|
|
|
}
|
|
|
+ }
|
|
|
+ }
|
|
|
+ retries++;
|
|
|
+ }
|
|
|
+ return "";
|
|
|
+ }
|
|
|
|
|
|
- // 检查文件是否为空
|
|
|
- if (file.length() == 0) {
|
|
|
- System.out.println("下载的文件为空,重试...");
|
|
|
- retries++;
|
|
|
- continue;
|
|
|
- } else {
|
|
|
- return true;
|
|
|
+ public static int download(String path, String videoUrl, String platform) {
|
|
|
+ try {
|
|
|
+ File file = new File(path);
|
|
|
+ if (file.exists()) {
|
|
|
+ file.delete();
|
|
|
+ }
|
|
|
+ // 设置代理
|
|
|
+ System.setProperty("http.proxyHost", PROXY_HOST);
|
|
|
+ System.setProperty("http.proxyPort", String.valueOf(PROXY_PORT));
|
|
|
+ System.setProperty("http.proxyUser", USERNAME);
|
|
|
+ System.setProperty("http.proxyPassword", PASSWORD);
|
|
|
+
|
|
|
+ HttpURLConnection connection = (HttpURLConnection) new URL(videoUrl).openConnection();
|
|
|
+ connection.setRequestMethod("GET");
|
|
|
+ connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0");
|
|
|
+ connection.setRequestProperty("Accept", "*/*");
|
|
|
+ connection.setRequestProperty("accept-language", "en,zh;q=0.9,zh-CN;q=0.8");
|
|
|
+ if (Objects.equals(platform, "dy_search")) {
|
|
|
+ connection.setRequestProperty("referer", "https://v11-coldf.douyinvod.com/");
|
|
|
+ }
|
|
|
+ // 连接并获取响应
|
|
|
+ connection.connect();
|
|
|
+
|
|
|
+ int responseCode = connection.getResponseCode();
|
|
|
+ if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_PARTIAL) {
|
|
|
+ // 下载文件
|
|
|
+ try (InputStream inputStream = connection.getInputStream();
|
|
|
+ OutputStream outputStream = Files.newOutputStream(file.toPath())) { // 追加模式
|
|
|
+ byte[] buffer = new byte[1024 * 1024]; // 1MB buffer
|
|
|
+ int bytesRead;
|
|
|
+ while ((bytesRead = inputStream.read(buffer)) != -1) {
|
|
|
+ outputStream.write(buffer, 0, bytesRead);
|
|
|
}
|
|
|
- } else {
|
|
|
- System.out.println("下载失败,HTTP 状态码:" + responseCode);
|
|
|
- retries++;
|
|
|
+ }
|
|
|
+ // 检查文件是否为空
|
|
|
+ if (file.length() != 0) {
|
|
|
+ return 0;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- log.error("下载失败,已达到最大重试次数:" + MAX_RETRIES);
|
|
|
- return false;
|
|
|
+ return responseCode;
|
|
|
} catch (Exception e) {
|
|
|
log.error("download error", e);
|
|
|
}
|
|
|
- return false;
|
|
|
+ return -1;
|
|
|
}
|
|
|
|
|
|
private static String generateVideoPath(String platform, String videoId) {
|
|
@@ -119,4 +138,17 @@ public class VideoDownloader {
|
|
|
return String.join(File.separator, ConfigUtil.getConfig("download.path"), coverName); // 返回文件路径和封面路径
|
|
|
}
|
|
|
|
|
|
+ public static void main(String[] args) {
|
|
|
+ System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");
|
|
|
+ Authenticator.setDefault(
|
|
|
+ new Authenticator() {
|
|
|
+ public PasswordAuthentication getPasswordAuthentication() {
|
|
|
+ return new PasswordAuthentication(
|
|
|
+ "t11983523373311", "mtuhdr2z".toCharArray());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ );
|
|
|
+ downloadVideo("2562802497357206175", "baidu_search", "https://vdept3.bdstatic.com/mda-qkt120240g2y8uka/cae_h264/1732792181901415854/mda-qkt120240g2y8uka.mp4?v_from_s=hkapp-haokan-suzhou&auth_key=1738759058-0-0-115429d241bdab4208d998b5f3f95507&bcevod_channel=searchbox_feed&cr=0&cd=0&pd=1&pt=3&logid=2258462520&vid=2562802497357206175&abtest=132219_1");
|
|
|
+ }
|
|
|
+
|
|
|
}
|