Explorar el Código

抖音下载优化

zhangyong hace 1 año
padre
commit
e1649d46c6

+ 7 - 0
etl-core/src/main/java/com/tzld/crawler/etl/service/impl/EtlServiceImpl.java

@@ -444,6 +444,13 @@ public class EtlServiceImpl implements EtlService {
                                         , !CollectionUtils.isEmpty(proxyPlatform) && proxyPlatform.contains(platfrm),
                                         proxyInfo);
                             }
+                            // 抖音
+                            else if (platform.equals("douyin")) {
+                                FileUtils.downloadDY(fileUrl, fpath,
+                                        !CollectionUtils.isEmpty(randomUaPlatform) && randomUaPlatform.contains(platfrm)
+                                        , !CollectionUtils.isEmpty(proxyPlatform) && proxyPlatform.contains(platfrm),
+                                        proxyInfo);
+                            }
                             else {
                                 String tempUrl = fileUrl;
                                 if (platform.equals("zhufuzhonglaonianrenruyijixiang")) {

+ 49 - 1
etl-core/src/main/java/com/tzld/crawler/etl/util/FileUtils.java

@@ -156,6 +156,54 @@ public class FileUtils {
         log.info("downloaded successfully [{}] to [{}]", fileUrl, filePath);
     }
 
+    public static void downloadDY(String fileUrl, String filePath, boolean useUa, boolean useProxy, Map<String, String> proxyInfo) throws Exception {
+        log.info("begin download [{}] to [{}] useUa [{}] useProxy [{}] proxyInfo[{}]", fileUrl, filePath, useUa, useProxy, proxyInfo);
+        URL url = new URL(fileUrl);
+        HttpURLConnection conn;
+        if (useProxy) {
+            String proxyUrl = proxyInfo.getOrDefault("url", "");
+            int port = Integer.parseInt(proxyInfo.getOrDefault("port", "0"));
+            String username = proxyInfo.getOrDefault("username", "");
+            String password = proxyInfo.getOrDefault("password", "");
+            // 创建代理服务的地址和端口
+            Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyUrl, port));
+            Authenticator authenticator = new Authenticator() {
+                @Override
+                public PasswordAuthentication getPasswordAuthentication() {
+                    return (new PasswordAuthentication(username, password.toCharArray()));
+                }
+            };
+            Authenticator.setDefault(authenticator);
+            conn = (HttpURLConnection) url.openConnection(proxy);
+        } else {
+            conn = (HttpURLConnection) url.openConnection();
+        }
+        // 设置请求头, 添加了防盗链 referer
+        conn.setRequestProperty("Accept", "*/*");
+        conn.setRequestProperty("Accept-Encoding", "identity;q=1, *;q=0");
+        conn.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.9");
+        if (useUa) {
+            conn.setRequestProperty("User-Agent", FakeUserAgent.getRandomUserAgent());
+        }
+        if (conn.getResponseCode() == HttpURLConnection.HTTP_FORBIDDEN) {
+            throw new CommonException(ExceptionEnum.URL_FORBIDDEN);
+        }
+        conn.setConnectTimeout(5000);
+        conn.setReadTimeout(5000);
+        log.info("download file size is {} of url [{}]", formatFileSize(conn.getContentLength()), fileUrl);
+
+        InputStream inputStream = conn.getInputStream();
+        FileOutputStream outputStream = new FileOutputStream(filePath);
+        byte[] buffer = new byte[4096];
+        int len;
+        while ((len = inputStream.read(buffer)) != -1) {
+            outputStream.write(buffer, 0, len);
+        }
+        inputStream.close();
+        outputStream.close();
+        log.info("downloaded successfully [{}] to [{}]", fileUrl, filePath);
+    }
+
     public static void downloadForXGV9(String fileUrl, String filePath, boolean useUa, boolean useProxy, Map<String, String> proxyInfo) throws Exception {
         log.info("begin download [{}] to [{}] useUa [{}] useProxy [{}] proxyInfo[{}]", fileUrl, filePath, useUa, useProxy, proxyInfo);
         SSLUtils.ignoreSsl();
@@ -395,7 +443,7 @@ public class FileUtils {
     public static void main(String[] args) throws Exception {
         // try {
 //        System.out.println("https://api-hl.huoshan.com/hotsoon/item/video/_source/?video_id=v0300fg10000ckmbrbbc77uc3nq19840&line=0&app_id=0&vquality=normal&watermark=0&long_video=0&sf=4&ts=1697528496&item_id=7290410334844718376".replace("https://api-hl.huoshan.com", "http://api-hl.huoshan.com"));
-        downloadForGZH(
+        download(
                 "http://apd-vlive.apdcdn.tc.qq.com/vhot2.qqvideo.tc.qq.com/AnS2yvseXsk6EzDFjAukGDVret6HO8htGN6D3R--6Z_0/B_JxNyiJmktHRgresXhfyMehK2KqMhB6_cZg_nN9wLevBm9wYhzoUZmFJm48dD8xAW/svp_50069/q33463ixnac.mp4?vkey=CF0A6609E8410CF2E8FD4629FA349F4B92CD682DD396496480644842EFE9754BB13B747CB3B39A45E80CAB3819EB88D6FE13A4674940BFBD0F81B53C6BC0FA1AFF8B922BE1A63AB8B3B6608239131B45452F92CB928C5248D0BA834FB38FC3DA63DABF6C3DF3E6AC9EA8146341424DC19EE2D2E4D40458D6",
                 "/Users/luojunhui/Downloads/" + System.currentTimeMillis() + ".mp4",
                 true,