ManagerApiService.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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.util.Objects;
  12. @Slf4j
  13. @Component
  14. public class ManagerApiService {
  15. @Autowired
  16. private HttpPoolClient httpPoolClient;
  17. @Value("${manager.api.host:https://testadmin.piaoquantv.com/manager}")
  18. private String managerApiHost;
  19. public JSONObject videoMultiTitleSave(Long videoId, String title) {
  20. String url = managerApiHost + "/video/multiTitleV2/saveNoAuth";
  21. JSONObject res = null;
  22. try {
  23. JSONObject param = new JSONObject();
  24. param.put("videoId", videoId);
  25. param.put("title", title);
  26. param.put("source", 1);
  27. String post = httpPoolClient.post(url, param.toJSONString());
  28. res = JSONObject.parseObject(post);
  29. } catch (Exception e) {
  30. log.error("ManagerApiService videoMultiTitleSave error, videoId={} title={}",
  31. videoId, title, e);
  32. }
  33. if (Objects.isNull(res)) {
  34. throw new CommonException(ExceptionEnum.VIDEO_MULTI_TITLE_SAVE_FAILED);
  35. }
  36. if (res.getInteger("code") != 0) {
  37. log.error("ManagerApiService videoMultiTitleSave error, videoId={} title={} res={}",
  38. videoId, title, res);
  39. throw new CommonException(ExceptionEnum.VIDEO_MULTI_TITLE_SAVE_FAILED.getCode(),
  40. ExceptionEnum.VIDEO_MULTI_TITLE_SAVE_FAILED.getMsg() + "," + res.getString("msg"));
  41. }
  42. return res.getJSONObject("content");
  43. }
  44. public JSONObject videoMultiCoverSave(Long videoId, String coverUrl) {
  45. String url = managerApiHost + "/video/multiCover/saveNoAuth";
  46. JSONObject res = null;
  47. try {
  48. JSONObject param = new JSONObject();
  49. param.put("videoId", videoId);
  50. param.put("coverUrl", coverUrl);
  51. param.put("source", 1);
  52. String post = httpPoolClient.post(url, param.toJSONString());
  53. res = JSONObject.parseObject(post);
  54. } catch (Exception e) {
  55. log.error("ManagerApiService videoMultiCoverSave error, videoId={} coverUrl={}",
  56. videoId, coverUrl, e);
  57. }
  58. if (Objects.isNull(res)) {
  59. throw new CommonException(ExceptionEnum.VIDEO_MULTI_COVER_SAVE_FAILED);
  60. }
  61. if (res.getInteger("code") != 0) {
  62. log.error("ManagerApiService videoMultiCoverSave error, videoId={} coverUrl={} res={}",
  63. videoId, coverUrl, res);
  64. throw new CommonException(ExceptionEnum.VIDEO_MULTI_COVER_SAVE_FAILED.getCode(),
  65. ExceptionEnum.VIDEO_MULTI_COVER_SAVE_FAILED.getMsg() + "," + res.getString("msg"));
  66. }
  67. return res.getJSONObject("content");
  68. }
  69. public JSONArray videoMultiCoverListV2(Long videoId) {
  70. String url = managerApiHost + "/video/multiCover/listV2";
  71. JSONObject res = null;
  72. int retry = 0;
  73. while (retry < 3) {
  74. try {
  75. JSONObject param = new JSONObject();
  76. param.put("videoId", videoId);
  77. param.put("range", "2h");
  78. String post = httpPoolClient.post(url, param.toJSONString());
  79. res = JSONObject.parseObject(post);
  80. break;
  81. } catch (Exception e) {
  82. log.error("ManagerApiService videoMultiCoverListV2 error, videoId={} range={}",
  83. videoId, "2h", e);
  84. retry++;
  85. }
  86. }
  87. if (Objects.isNull(res)) {
  88. throw new CommonException(ExceptionEnum.VIDEO_MULTI_COVER_LIST_FAILED);
  89. }
  90. if (res.getInteger("code") != 0) {
  91. log.error("ManagerApiService videoMultiCoverListV2 error, videoId={} range={} res={}",
  92. videoId, "2h", res);
  93. throw new CommonException(ExceptionEnum.VIDEO_MULTI_COVER_LIST_FAILED.getCode(),
  94. ExceptionEnum.VIDEO_MULTI_COVER_LIST_FAILED.getMsg() + "," + res.getString("msg"));
  95. }
  96. return res.getJSONArray("content");
  97. }
  98. public JSONArray videoMultiTitleListV2(Long videoId) {
  99. String url = managerApiHost + "/video/multiTitleV2/listV2";
  100. JSONObject res = null;
  101. int retry = 0;
  102. while (retry < 3) {
  103. try {
  104. JSONObject param = new JSONObject();
  105. param.put("videoId", videoId);
  106. param.put("range", "4h");
  107. String post = httpPoolClient.post(url, param.toJSONString());
  108. res = JSONObject.parseObject(post);
  109. break;
  110. } catch (Exception e) {
  111. log.error("ManagerApiService videoMultiCoverListV2 error, videoId={} range={}",
  112. videoId, "4h", e);
  113. retry++;
  114. }
  115. }
  116. if (Objects.isNull(res)) {
  117. throw new CommonException(ExceptionEnum.VIDEO_MULTI_TITLE_LIST_FAILED);
  118. }
  119. if (res.getInteger("code") != 0) {
  120. log.error("ManagerApiService videoMultiTitleListV2 error, videoId={} range={} res={}",
  121. videoId, "4h", res);
  122. throw new CommonException(ExceptionEnum.VIDEO_MULTI_TITLE_LIST_FAILED.getCode(),
  123. ExceptionEnum.VIDEO_MULTI_TITLE_LIST_FAILED.getMsg() + "," + res.getString("msg"));
  124. }
  125. return res.getJSONArray("content");
  126. }
  127. }