ソースを参照

Merge branch 'dev-xym-add-exp' of Server/long-article-manage into master

xueyiming 3 ヶ月 前
コミット
8c74d37b75

+ 26 - 20
long-article-server/src/main/java/com/tzld/piaoquan/longarticle/utils/other/HkspSearch.java

@@ -147,26 +147,32 @@ public class HkspSearch {
     }
 
     public static JSONObject getVideoDetail(String videoId) {
-        String url = "https://haokan.baidu.com/v";
-        JSONObject params = new JSONObject();
-        params.put("vid", videoId);
-        params.put("_format", "json");
-
-        String base64String = Base64.getEncoder().encodeToString(UUID.randomUUID().toString().getBytes());
-        HttpResponse response = HttpRequest.get(url)
-                .header("Accept", "*/*")
-                .header("cookie", "BIDUPSID=" + base64String)
-                .header("Accept-Language", "en,zh;q=0.9,zh-CN;q=0.8")
-                .header("Cache-Control", "no-cache")
-                .header("Connection", "keep-alive")
-                .header("Content-Type", "application/x-www-form-urlencoded")
-                .header("Referer", "https://haokan.baidu.com")
-                .header("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")
-                .timeout(120000) // 设置超时时间
-                .form(params)
-                .setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(PROXY_HOST, PROXY_PORT)))
-                .execute();
-        return JSONObject.parseObject(response.body()).getJSONObject("data").getJSONObject("apiData").getJSONObject("curVideoMeta");
+        try {
+            String url = "https://haokan.baidu.com/v";
+            JSONObject params = new JSONObject();
+            params.put("vid", videoId);
+            params.put("_format", "json");
+
+            String base64String = Base64.getEncoder().encodeToString(UUID.randomUUID().toString().getBytes());
+            HttpResponse response = HttpRequest.get(url)
+                    .header("Accept", "*/*")
+                    .header("cookie", "BIDUPSID=" + base64String)
+                    .header("Accept-Language", "en,zh;q=0.9,zh-CN;q=0.8")
+                    .header("Cache-Control", "no-cache")
+                    .header("Connection", "keep-alive")
+                    .header("Content-Type", "application/x-www-form-urlencoded")
+                    .header("Referer", "https://haokan.baidu.com")
+                    .header("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")
+                    .timeout(120000) // 设置超时时间
+                    .form(params)
+                    .setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(PROXY_HOST, PROXY_PORT)))
+                    .execute();
+            return JSONObject.parseObject(response.body()).getJSONObject("data").getJSONObject("apiData").getJSONObject("curVideoMeta");
+        } catch (Exception e) {
+            log.error("getVideoDetail error", e);
+        }
+        return null;
+
     }
 
 }

+ 6 - 2
long-article-server/src/main/java/com/tzld/piaoquan/longarticle/utils/other/VideoDownloader.java

@@ -5,6 +5,7 @@ import com.alibaba.fastjson.JSONObject;
 import com.tzld.piaoquan.longarticle.utils.ConfigUtil;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.codec.digest.DigestUtils;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.util.CollectionUtils;
 
 import java.io.File;
@@ -40,7 +41,7 @@ public class VideoDownloader {
             }
 
             //百度重新获取链接
-            if (download == 403 && Objects.equals(platform, "baidu_search")) {
+            if ((download == 403 || StringUtils.isEmpty(videoUrl)) && Objects.equals(platform, "baidu_search")) {
                 JSONObject videoDetail = HkspSearch.getVideoDetail(outVideoId);
                 if (videoDetail != null) {
                     String newVideoUrl = videoDetail.getString("playurl");
@@ -51,7 +52,7 @@ public class VideoDownloader {
             }
 
             //抖音重新获取链接
-            if (download == 403 && Objects.equals(platform, "dy_search")) {
+            if ((download == 403 || StringUtils.isEmpty(videoUrl)) && Objects.equals(platform, "dy_search")) {
                 JSONObject videoDetail = DouyinSearch.douyinDetail(outVideoId);
                 if (videoDetail != null) {
                     JSONArray videoUrlList = videoDetail.getJSONArray("video_url_list");
@@ -70,6 +71,9 @@ public class VideoDownloader {
 
     public static int download(String path, String videoUrl, String platform) {
         try {
+            if (StringUtils.isEmpty(videoUrl)) {
+                return -1;
+            }
             File file = new File(path);
             if (file.exists()) {
                 file.delete();