|  | @@ -0,0 +1,183 @@
 | 
	
		
			
				|  |  | +package com.tzld.piaoquan.longarticle.utils;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +import cn.hutool.http.HttpRequest;
 | 
	
		
			
				|  |  | +import cn.hutool.http.HttpResponse;
 | 
	
		
			
				|  |  | +import cn.hutool.json.JSONObject;
 | 
	
		
			
				|  |  | +import cn.hutool.json.JSONUtil;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +import java.io.IOException;
 | 
	
		
			
				|  |  | +import java.net.Authenticator;
 | 
	
		
			
				|  |  | +import java.net.InetSocketAddress;
 | 
	
		
			
				|  |  | +import java.net.PasswordAuthentication;
 | 
	
		
			
				|  |  | +import java.net.Proxy;
 | 
	
		
			
				|  |  | +import java.security.MessageDigest;
 | 
	
		
			
				|  |  | +import java.security.NoSuchAlgorithmException;
 | 
	
		
			
				|  |  | +import java.util.ArrayList;
 | 
	
		
			
				|  |  | +import java.util.Base64;
 | 
	
		
			
				|  |  | +import java.util.List;
 | 
	
		
			
				|  |  | +import java.util.UUID;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +public class HkspSearch {
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    public static void main(String[] args) throws IOException {
 | 
	
		
			
				|  |  | +        System.out.println(hkspSearch("测试", "", "ccc"));
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    public static List<JSONObject> hkspSearch(String key, String sensitiveWords, String traceId) throws IOException {
 | 
	
		
			
				|  |  | +        long timestampMilliseconds = System.currentTimeMillis();
 | 
	
		
			
				|  |  | +        String url = "https://haokan.baidu.com/haokan/ui-search/pc/search/video";
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        // 生成签名
 | 
	
		
			
				|  |  | +        String strings = String.format("%d_%s_%d_%d_%d", 1, key, 10, timestampMilliseconds, 1);
 | 
	
		
			
				|  |  | +        String sign = md5(strings);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        JSONObject params = new JSONObject();
 | 
	
		
			
				|  |  | +        params.put("pn", 1);
 | 
	
		
			
				|  |  | +        params.put("rn", 10);
 | 
	
		
			
				|  |  | +        params.put("type", "video");
 | 
	
		
			
				|  |  | +        params.put("query", key);
 | 
	
		
			
				|  |  | +        params.put("sign", sign);
 | 
	
		
			
				|  |  | +        params.put("version", 1);
 | 
	
		
			
				|  |  | +        params.put("timestamp", timestampMilliseconds);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        String base64String = Base64.getEncoder().encodeToString(UUID.randomUUID().toString().getBytes());
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        // 这行代码是身份验证的关键配置,不然身份验证不起作用
 | 
	
		
			
				|  |  | +        System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");
 | 
	
		
			
				|  |  | +        // 身份验证
 | 
	
		
			
				|  |  | +        Authenticator.setDefault(
 | 
	
		
			
				|  |  | +                new Authenticator() {
 | 
	
		
			
				|  |  | +                    public PasswordAuthentication getPasswordAuthentication() {
 | 
	
		
			
				|  |  | +                        return new PasswordAuthentication(
 | 
	
		
			
				|  |  | +                                "t11983523373311", "mtuhdr2z".toCharArray());
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +        );
 | 
	
		
			
				|  |  | +        // 设置代理服务器
 | 
	
		
			
				|  |  | +        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("l901.kdltps.com", 15818));
 | 
	
		
			
				|  |  | +        HttpResponse response = HttpRequest.get(url)
 | 
	
		
			
				|  |  | +                .header("authority", "haokan.baidu.com")
 | 
	
		
			
				|  |  | +                .header("accept", "*/*")
 | 
	
		
			
				|  |  | +                .header("accept-language", "zh,en;q=0.9,zh-CN;q=0.8")
 | 
	
		
			
				|  |  | +                .header("cookie", "BIDUPSID=" + base64String)
 | 
	
		
			
				|  |  | +                .header("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Edg/122.0.0.0") // 假用户代理
 | 
	
		
			
				|  |  | +                .header("x-requested-with", "xmlhttprequest")
 | 
	
		
			
				|  |  | +                .timeout(120000) // 设置超时时间
 | 
	
		
			
				|  |  | +//                .setProxy(proxy)
 | 
	
		
			
				|  |  | +                .form(params)
 | 
	
		
			
				|  |  | +                .execute();
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        List<JSONObject> resultList = new ArrayList<>();
 | 
	
		
			
				|  |  | +        try {
 | 
	
		
			
				|  |  | +            System.out.println(response.body());
 | 
	
		
			
				|  |  | +            JSONObject jsonResponse = JSONUtil.parseObj(response.body());
 | 
	
		
			
				|  |  | +            List<JSONObject> dataList = jsonResponse.getByPath("data.list", List.class);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            for (JSONObject data : dataList) {
 | 
	
		
			
				|  |  | +                try {
 | 
	
		
			
				|  |  | +                    String videoId = data.getStr("vid");
 | 
	
		
			
				|  |  | +                    String title = data.getStr("title");
 | 
	
		
			
				|  |  | +                    int duration = parseDuration(data.getStr("duration"));
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                    if (sensitiveFlag(sensitiveWords, title) && duration <= 300) {
 | 
	
		
			
				|  |  | +                        JSONObject res = getVideoDetail(videoId);
 | 
	
		
			
				|  |  | +                        if (res != null) {
 | 
	
		
			
				|  |  | +                            resultList.add(res);
 | 
	
		
			
				|  |  | +                        }
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                } catch (Exception e) {
 | 
	
		
			
				|  |  | +                    // 处理异常
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            logging(traceId, key, dataList.size(), resultList.size());
 | 
	
		
			
				|  |  | +            return resultList;
 | 
	
		
			
				|  |  | +        } catch (Exception e) {
 | 
	
		
			
				|  |  | +            logging(traceId, key, e.getMessage());
 | 
	
		
			
				|  |  | +            return new ArrayList<>();
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    private static int parseDuration(String duration) {
 | 
	
		
			
				|  |  | +        String[] parts = duration.split(":");
 | 
	
		
			
				|  |  | +        return Integer.parseInt(parts[0]) * 60 + Integer.parseInt(parts[1]);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    private static String md5(String input) {
 | 
	
		
			
				|  |  | +        try {
 | 
	
		
			
				|  |  | +            MessageDigest md = MessageDigest.getInstance("MD5");
 | 
	
		
			
				|  |  | +            byte[] messageDigest = md.digest(input.getBytes());
 | 
	
		
			
				|  |  | +            StringBuilder sb = new StringBuilder();
 | 
	
		
			
				|  |  | +            for (byte b : messageDigest) {
 | 
	
		
			
				|  |  | +                sb.append(String.format("%02x", b));
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            return sb.toString();
 | 
	
		
			
				|  |  | +        } catch (NoSuchAlgorithmException e) {
 | 
	
		
			
				|  |  | +            throw new RuntimeException(e);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    private static boolean sensitiveFlag(String sensitiveWords, String title) {
 | 
	
		
			
				|  |  | +        // 实现敏感词检查逻辑
 | 
	
		
			
				|  |  | +        return true; // 示例
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    private static void logging(String traceId, String key, int searchCount, int usefulCount) {
 | 
	
		
			
				|  |  | +        // 实现日志记录逻辑
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    private static void logging(String traceId, String key, String message) {
 | 
	
		
			
				|  |  | +        // 实现日志记录逻辑
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    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());
 | 
	
		
			
				|  |  | +        HttpRequest proxyHttpRequest = getProxyHttpRequest(url);
 | 
	
		
			
				|  |  | +        HttpResponse response = proxyHttpRequest
 | 
	
		
			
				|  |  | +                .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")
 | 
	
		
			
				|  |  | +                .form(params)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                .setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("l901.kdltps.com", 15818)))
 | 
	
		
			
				|  |  | +                .basicProxyAuth("t11983523373311", "mtuhdr2z")
 | 
	
		
			
				|  |  | +                .execute();
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        return JSONUtil.parseObj(response.body()).getByPath("data.apiData.curVideoMeta", JSONObject.class);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    public static HttpRequest getProxyHttpRequest(String url) {
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        // 代理的地址和端口
 | 
	
		
			
				|  |  | +        String proxyHost = "l901.kdltps.com";
 | 
	
		
			
				|  |  | +        int proxyPort = 15818;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        // 用户名和密码
 | 
	
		
			
				|  |  | +        String username = "t11983523373311"; // 替换为你的用户名
 | 
	
		
			
				|  |  | +        String password = "mtuhdr2z"; // 替换为你的密码
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        // 创建代理请求
 | 
	
		
			
				|  |  | +        String proxyUrl = String.format("https://%s:%s@%s:%d", username, password, proxyHost, proxyPort);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        // 发送请求
 | 
	
		
			
				|  |  | +        HttpRequest httpRequest = HttpRequest.get(url) // 替换为你要请求的 URL
 | 
	
		
			
				|  |  | +                .proxyAuth(proxyUrl);
 | 
	
		
			
				|  |  | +        return httpRequest;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +}
 |