Procházet zdrojové kódy

修改 ETL 下载,对于西瓜特定域名需要特定防盗链的下载;

罗俊辉 před 1 rokem
rodič
revize
6531e16f33

+ 8 - 1
etl-core/src/main/java/com/tzld/crawler/etl/service/impl/EtlServiceImpl.java

@@ -422,7 +422,14 @@ public class EtlServiceImpl implements EtlService {
                                         !CollectionUtils.isEmpty(randomUaPlatform) && randomUaPlatform.contains(platfrm)
                                         , !CollectionUtils.isEmpty(proxyPlatform) && proxyPlatform.contains(platfrm),
                                         proxyInfo);
-                            } else {
+                            }
+                            else if (fileUrl.contains("v9-xg-web-pc.ixigua.com")) {
+                                FileUtils.downloadForXG(fileUrl, fpath,
+                                        !CollectionUtils.isEmpty(randomUaPlatform) && randomUaPlatform.contains(platfrm)
+                                        , !CollectionUtils.isEmpty(proxyPlatform) && proxyPlatform.contains(platfrm),
+                                        proxyInfo);
+                            }
+                            else {
                                 String tempUrl = fileUrl;
                                 if (platform.equals("zhufuzhonglaonianrenruyijixiang")) {
                                     tempUrl = tempUrl.replace("https://api-hl.huoshan.com", "http://api-hl.huoshan.com");

+ 59 - 2
etl-core/src/main/java/com/tzld/crawler/etl/util/FileUtils.java

@@ -138,6 +138,57 @@ public class FileUtils {
         log.info("downloaded successfully [{}] to [{}]", fileUrl, filePath);
     }
 
+    public static void downloadForXG(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();
+        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();
+        }
+        if (useUa) {
+            conn.setRequestProperty("User-Agent", FakeUserAgent.getRandomUserAgent());
+        }
+        conn.setRequestProperty("Accept", "*/*");
+        conn.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.9");
+        conn.setRequestProperty("Host", "v9-xg-web-pc.ixigua.com");
+        conn.setRequestProperty("Origin", "https://www.ixigua.com/");
+        conn.setRequestProperty("Referer", "https://www.ixigua.com/");
+
+        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 downloadForGZH(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();
@@ -194,8 +245,14 @@ 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"));
-        download("http://api-hl.huoshan.com/hotsoon/item/video/_source/?video_id=v0d00fg10000ckjb1bbc77u340cpgelg&line=0&app_id=0&vquality=normal&watermark=0&long_video=0&sf=4&ts=1697529427&item_id=7288707446217002255", "/Users/dingyunpeng/Downloads/" + System.currentTimeMillis(), true);
+//        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"));
+        downloadForXG(
+                "https://v9-xg-web-pc.ixigua.com/bf37bd4847ea0ef73c1419c3b7def5cd/654a30d9/video/tos/cn/tos-cn-ve-0026/oIBPpenxRaObASeRJ7XGCi5DAM78gBlGARgAUd/?a=1768&ch=0&cr=7&dr=0&er=0&cd=0%7C0%7C0%7C1&cv=1&br=196&bt=196&cs=4&ds=3&eid=1025&ft=_z7ehvvBQ3AUq8yq8Z.wNnOYZlcg6EbK2bLyA0wpuZmka&mime_type=video_mp4&qs=0&rc=ZGU5PDozZzRlOzwzOzg5Z0BpajprZmg6ZmhobzMzNGQzM0AyNmFiM2IxXmExLjQ1Yy5jYSM2MGBzcjRfLS9gLS1kLi9zcw%3D%3D&btag=e00028000&dy_q=1699357188&feature_id=e2624171dedb27d48fb2006f0f0a26ec&l=20231107193948643DF7B13B8779C9525F&__vid=v0201ag10000cl2e0ebc77uevcch379g",
+                "/Users/luojunhui/Downloads/" + System.currentTimeMillis(),
+                true,
+                false,
+                null
+                );
         //
         // } catch (Exception e) {
         //     e.printStackTrace();