123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- 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.*;
- 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 List<JSONObject> hkspSearch(String key, List<String> sensitiveWords, String traceId) {
- try {
- long timestampMilliseconds = System.currentTimeMillis();
- String url = "https://haokan.baidu.com/haokan/ui-search/pc/search/video";
- String encodedKey = URLEncoder.encode(key, "UTF-8");
- String strings = String.format("%d_%s_%d_%d_%d", 1, encodedKey, 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());
- }
- }
- );
- 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(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("l901.kdltps.com", 15818)))
- .form(params)
- .execute();
- List<JSONObject> resultList = new ArrayList<>();
- 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) {
- // 处理异常
- }
- }
- return resultList;
- } catch (Exception e) {
- 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(List<String> sensitiveWords, String title) {
- // 实现敏感词检查逻辑
- return true; // 示例
- }
- 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());
- // 这行代码是身份验证的关键配置,不然身份验证不起作用
- System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");
- // 身份验证
- Authenticator.setDefault(
- new Authenticator() {
- public PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(
- "t11983523373311", "mtuhdr2z".toCharArray());
- }
- }
- );
- 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")
- .form(params)
- .setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("l901.kdltps.com", 15818)))
- .execute();
- return JSONUtil.parseObj(response.body()).getByPath("data.apiData.curVideoMeta", JSONObject.class);
- }
- }
|