DouyinSearch.java 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package com.tzld.piaoquan.longarticle.utils;
  2. import cn.hutool.http.HttpRequest;
  3. import cn.hutool.http.HttpResponse;
  4. import cn.hutool.json.JSONUtil;
  5. import cn.hutool.json.JSONObject;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. public class DouyinSearch {
  9. public static List<JSONObject> douyinSearch(String keyword, String sensitiveWords, String traceId) {
  10. String url = "http://8.217.190.241:8888/crawler/dou_yin/top_hub_content";
  11. JSONObject payload = new JSONObject();
  12. payload.put("keyword", keyword);
  13. payload.put("category", "全部");
  14. payload.put("period", "近90天");
  15. payload.put("content_modal", "视频");
  16. payload.put("cursor", "");
  17. HttpResponse response = HttpRequest.post(url)
  18. .header("Content-Type", "application/json")
  19. .body(payload.toString())
  20. .execute();
  21. List<JSONObject> resultList = new ArrayList<>();
  22. try {
  23. JSONObject jsonResponse = JSONUtil.parseObj(response.body());
  24. List<JSONObject> dtList = jsonResponse.getByPath("data.data", List.class);
  25. for (JSONObject obj : dtList) {
  26. try {
  27. String title = obj.getStr("video_desc");
  28. String videoId = obj.getStr("video_id");
  29. int duration = obj.getInt("duration");
  30. if (sensitiveFlag(sensitiveWords, title) && duration < 30000) {
  31. JSONObject res = douyinDetail(videoId);
  32. if (res != null) {
  33. resultList.add(res);
  34. }
  35. }
  36. } catch (Exception e) {
  37. System.out.println(e.getMessage());
  38. // 处理异常
  39. }
  40. }
  41. return resultList;
  42. } catch (Exception e) {
  43. return new ArrayList<>();
  44. }
  45. }
  46. public static JSONObject douyinDetail(String videoId) {
  47. String url = "http://8.217.190.241:8888/crawler/dou_yin/detail";
  48. JSONObject payload = new JSONObject();
  49. payload.put("content_id", videoId);
  50. HttpResponse response = HttpRequest.post(url)
  51. .header("Content-Type", "application/json")
  52. .body(payload.toString())
  53. .execute();
  54. JSONObject videoInfo = JSONUtil.parseObj(response.body()).getByPath("data.data", JSONObject.class);
  55. if ("note".equals(videoInfo.getStr("content_type"))) {
  56. return null;
  57. } else {
  58. return videoInfo;
  59. }
  60. }
  61. private static boolean sensitiveFlag(String sensitiveWords, String title) {
  62. // 实现敏感词检查逻辑
  63. return true; // 示例
  64. }
  65. public static void main(String[] args) {
  66. System.out.println(douyinSearch("美女", "", ""));
  67. }
  68. }