ManagerApiService.java 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package com.tzld.piaoquan.api.component;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.tzld.piaoquan.api.common.enums.ExceptionEnum;
  5. import com.tzld.piaoquan.api.common.exception.CommonException;
  6. import com.tzld.piaoquan.growth.common.component.HttpPoolClient;
  7. import lombok.extern.slf4j.Slf4j;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.beans.factory.annotation.Value;
  10. import org.springframework.stereotype.Component;
  11. import java.net.URLEncoder;
  12. import java.nio.charset.StandardCharsets;
  13. import java.util.List;
  14. import java.util.Objects;
  15. @Slf4j
  16. @Component
  17. public class ManagerApiService {
  18. @Autowired
  19. private HttpPoolClient httpPoolClient;
  20. @Value("${manager.api.host:https://testadmin.piaoquantv.com/manager}")
  21. private String managerApiHost;
  22. @Value("${video.vector.api.host:http://api-internal.piaoquantv.com/videoVector}")
  23. private String videoVectorApiHost;
  24. public JSONObject videoMultiTitleSave(Long videoId, String title) {
  25. String url = managerApiHost + "/video/multiTitleV2/saveNoAuth";
  26. JSONObject res = null;
  27. try {
  28. JSONObject param = new JSONObject();
  29. param.put("videoId", videoId);
  30. param.put("title", title);
  31. param.put("source", 1);
  32. String post = httpPoolClient.post(url, param.toJSONString());
  33. res = JSONObject.parseObject(post);
  34. } catch (Exception e) {
  35. log.error("ManagerApiService videoMultiTitleSave error, videoId={} title={}",
  36. videoId, title, e);
  37. }
  38. if (Objects.isNull(res)) {
  39. throw new CommonException(ExceptionEnum.VIDEO_MULTI_TITLE_SAVE_FAILED);
  40. }
  41. if (res.getInteger("code") != 0) {
  42. log.error("ManagerApiService videoMultiTitleSave error, videoId={} title={} res={}",
  43. videoId, title, res);
  44. throw new CommonException(ExceptionEnum.VIDEO_MULTI_TITLE_SAVE_FAILED.getCode(),
  45. ExceptionEnum.VIDEO_MULTI_TITLE_SAVE_FAILED.getMsg() + "," + res.getString("msg"));
  46. }
  47. return res.getJSONObject("content");
  48. }
  49. public JSONObject videoMultiCoverSave(Long videoId, String coverUrl) {
  50. String url = managerApiHost + "/video/multiCover/saveNoAuth";
  51. JSONObject res = null;
  52. try {
  53. JSONObject param = new JSONObject();
  54. param.put("videoId", videoId);
  55. param.put("coverUrl", coverUrl);
  56. param.put("source", 1);
  57. String post = httpPoolClient.post(url, param.toJSONString());
  58. res = JSONObject.parseObject(post);
  59. } catch (Exception e) {
  60. log.error("ManagerApiService videoMultiCoverSave error, videoId={} coverUrl={}",
  61. videoId, coverUrl, e);
  62. }
  63. if (Objects.isNull(res)) {
  64. throw new CommonException(ExceptionEnum.VIDEO_MULTI_COVER_SAVE_FAILED);
  65. }
  66. if (res.getInteger("code") != 0) {
  67. log.error("ManagerApiService videoMultiCoverSave error, videoId={} coverUrl={} res={}",
  68. videoId, coverUrl, res);
  69. throw new CommonException(ExceptionEnum.VIDEO_MULTI_COVER_SAVE_FAILED.getCode(),
  70. ExceptionEnum.VIDEO_MULTI_COVER_SAVE_FAILED.getMsg() + "," + res.getString("msg"));
  71. }
  72. return res.getJSONObject("content");
  73. }
  74. public JSONArray videoMultiCoverListV2(Long videoId) {
  75. String url = managerApiHost + "/video/multiCover/listV2";
  76. JSONObject res = null;
  77. int retry = 0;
  78. while (retry < 3) {
  79. try {
  80. JSONObject param = new JSONObject();
  81. param.put("videoId", videoId);
  82. param.put("range", "2h");
  83. String post = httpPoolClient.post(url, param.toJSONString());
  84. res = JSONObject.parseObject(post);
  85. break;
  86. } catch (Exception e) {
  87. log.error("ManagerApiService videoMultiCoverListV2 error, videoId={} range={}",
  88. videoId, "2h", e);
  89. retry++;
  90. }
  91. }
  92. if (Objects.isNull(res)) {
  93. throw new CommonException(ExceptionEnum.VIDEO_MULTI_COVER_LIST_FAILED);
  94. }
  95. if (res.getInteger("code") != 0) {
  96. log.error("ManagerApiService videoMultiCoverListV2 error, videoId={} range={} res={}",
  97. videoId, "2h", res);
  98. throw new CommonException(ExceptionEnum.VIDEO_MULTI_COVER_LIST_FAILED.getCode(),
  99. ExceptionEnum.VIDEO_MULTI_COVER_LIST_FAILED.getMsg() + "," + res.getString("msg"));
  100. }
  101. return res.getJSONArray("content");
  102. }
  103. public JSONArray videoMultiTitleListV2(Long videoId) {
  104. String url = managerApiHost + "/video/multiTitleV2/listV2";
  105. JSONObject res = null;
  106. int retry = 0;
  107. while (retry < 3) {
  108. try {
  109. JSONObject param = new JSONObject();
  110. param.put("videoId", videoId);
  111. param.put("range", "4h");
  112. String post = httpPoolClient.post(url, param.toJSONString());
  113. res = JSONObject.parseObject(post);
  114. break;
  115. } catch (Exception e) {
  116. log.error("ManagerApiService videoMultiCoverListV2 error, videoId={} range={}",
  117. videoId, "4h", e);
  118. retry++;
  119. }
  120. }
  121. if (Objects.isNull(res)) {
  122. throw new CommonException(ExceptionEnum.VIDEO_MULTI_TITLE_LIST_FAILED);
  123. }
  124. if (res.getInteger("code") != 0) {
  125. log.error("ManagerApiService videoMultiTitleListV2 error, videoId={} range={} res={}",
  126. videoId, "4h", res);
  127. throw new CommonException(ExceptionEnum.VIDEO_MULTI_TITLE_LIST_FAILED.getCode(),
  128. ExceptionEnum.VIDEO_MULTI_TITLE_LIST_FAILED.getMsg() + "," + res.getString("msg"));
  129. }
  130. return res.getJSONArray("content");
  131. }
  132. /**
  133. * 按标题搜索视频(调用 manager 平台开放分页接口,无需鉴权)
  134. *
  135. * @param title 搜索关键词
  136. * @param pageNum 页码
  137. * @param pageSize 每页数量
  138. * @return 接口返回的 content 对象,包含 totalSize 和 objs 列表
  139. */
  140. public JSONObject searchVideoByTitle(String title, int pageNum, int pageSize) {
  141. try {
  142. String encodedTitle = URLEncoder.encode(title, StandardCharsets.UTF_8.name());
  143. String url = managerApiHost + "/openapi/video/page?pageNum=" + pageNum + "&pageSize=" + pageSize
  144. + "&title=" + encodedTitle
  145. + "&sortField=4&sortType=desc&status=1&recommendStatus=-6&auditStaus=5&categoryId=55&muid=7";
  146. String response = httpPoolClient.get(url);
  147. if (response == null) {
  148. return null;
  149. }
  150. JSONObject res = JSONObject.parseObject(response);
  151. if (res != null && res.getInteger("code") == 0) {
  152. return res.getJSONObject("content");
  153. }
  154. log.error("ManagerApiService searchVideoByTitle failed, title={} pageNum={} pageSize={} res={}",
  155. title, pageNum, pageSize, res);
  156. } catch (Exception e) {
  157. log.error("ManagerApiService searchVideoByTitle error, title={} pageNum={} pageSize={}",
  158. title, pageNum, pageSize, e);
  159. }
  160. return null;
  161. }
  162. /**
  163. * 调用视频向量搜索接口,根据查询文本召回视频并计算综合评分
  164. *
  165. * @param queryText 查询文本
  166. * @param topN 返回数量
  167. * @return 接口返回的 data 对象(RecallVideoScoreVO)
  168. */
  169. public JSONObject recallVideoWithScore(String queryText, int topN) {
  170. String url = videoVectorApiHost + "/videoSearch/recallWithScore";
  171. try {
  172. JSONObject param = new JSONObject();
  173. param.put("queryText", queryText);
  174. param.put("configCode", "ALL");
  175. param.put("topN", topN);
  176. param.put("simMin", 0.8);
  177. String post = httpPoolClient.post(url, param.toJSONString());
  178. JSONObject res = JSONObject.parseObject(post);
  179. if (res != null && "0".equals(res.getString("code"))) {
  180. return res.getJSONObject("data");
  181. }
  182. log.error("ManagerApiService recallVideoWithScore failed, queryText={} topN={} res={}",
  183. queryText, topN, res);
  184. } catch (Exception e) {
  185. log.error("ManagerApiService recallVideoWithScore error, queryText={} topN={}",
  186. queryText, topN, e);
  187. }
  188. return null;
  189. }
  190. }