SampleTest.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package org.jeecg;
  2. import cn.com.ctop.common.module.entity.CtopOauthToken;
  3. import cn.com.ctop.common.module.mapper.CtopOauthTokenMapper;
  4. import cn.com.ctop.common.module.service.ICtopOauthTokenService;
  5. import cn.com.ctop.common.module.utils.CtopAdConstant;
  6. import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouHistoryReportTaskService;
  7. import cn.com.ctop.kuaishou.modules.batch.service.IKuaishouInterfaceService;
  8. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  9. import lombok.extern.slf4j.Slf4j;
  10. import org.jeecg.common.util.DateUtils;
  11. import org.junit.Test;
  12. import org.junit.runner.RunWith;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.boot.test.context.SpringBootTest;
  15. import org.springframework.test.context.junit4.SpringRunner;
  16. import java.text.SimpleDateFormat;
  17. import java.util.Date;
  18. import java.util.List;
  19. import java.util.concurrent.CountDownLatch;
  20. import java.util.concurrent.ExecutorService;
  21. import java.util.concurrent.Executors;
  22. @RunWith(SpringRunner.class)
  23. @SpringBootTest
  24. @Slf4j
  25. public class SampleTest {
  26. @Autowired
  27. private IKuaishouInterfaceService kuaishouInterfaceService;
  28. @Autowired
  29. private IKuaiShouHistoryReportTaskService reportTaskService;
  30. @Autowired
  31. private CtopOauthTokenMapper tokenMapper;
  32. @Test
  33. public void kuaisShouReport() {
  34. try {
  35. QueryWrapper<CtopOauthToken> queryWrapper = new QueryWrapper<>();
  36. queryWrapper.eq("account_id",3727345L);
  37. CtopOauthToken token = tokenMapper.selectOne(queryWrapper);
  38. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
  39. Date parse = simpleDateFormat.parse("2020-03-12");
  40. kuaishouInterfaceService.getAdvertiserReportDaily(token,parse,parse);
  41. // reportTaskService.createTask(3727345L, "871fc2c248782a94ad2962c941555abc", "");
  42. // reportTaskService.getTaskList();
  43. /*
  44. reportTaskService.createTask(3727345L, "871fc2c248782a94ad2962c941555abc");
  45. reportTaskService.getTaskList();*/
  46. /* String url = "https://ad.e.kuaishou.com/rest/openapi/v1/async_task/list";
  47. Map<String, String> headers = new HashMap<>();
  48. headers.put("Content-Type", " application/json");
  49. headers.put("Access-Token", "eb9a0d18072eabc339ee26ad010ecd6b");
  50. JSONObject param = new JSONObject();
  51. param.put("advertiser_id", 142402);
  52. JSONArray taskIds = new JSONArray();
  53. taskIds.add(1229917);
  54. param.put("task_ids", taskIds);
  55. String result = HttpUtils.kuaiShouhttpPostRequest(url, param.toJSONString(), headers);
  56. JSONObject resultJson = JSONObject.parseObject(result);
  57. System.err.println(resultJson);*/
  58. /* String url = "https://ad.e.kuaishou.com/rest/openapi/v1/async_task/download";
  59. Map<String, String> headers = new HashMap<>();
  60. headers.put("Access-Token", "eb9a0d18072eabc339ee26ad010ecd6b");
  61. Map<String,Object> param = new HashMap<>();
  62. param.put("advertiser_id", 142402);
  63. param.put("task_id", "1229917");
  64. String result = HttpUtils.KuaiShouttpGetRequest(url, param,headers);*/
  65. /*URL url = new URL("https://ad.e.kuaishou.com/rest/openapi/v1/async_task/download?task_id=1229917&advertiser_id=142402");
  66. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  67. //设置超时间为3秒
  68. conn.setConnectTimeout(10 * 1000);
  69. //防止屏蔽程序抓取而返回403错误
  70. conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
  71. conn.setRequestProperty("Access-Token", "6a1fbe20e49fe519ec8120aac5c9e55a");
  72. //得到输入流
  73. InputStream inputStream = conn.getInputStream();
  74. //获取自己数组
  75. byte[] getData = readInputStream(inputStream);
  76. //文件保存位置
  77. File saveDir = new File("D:\\file");
  78. if (!saveDir.exists()) {
  79. saveDir.mkdirs();
  80. }
  81. String fileName = "123.csv";
  82. File file = new File(saveDir + File.separator + fileName);
  83. FileOutputStream fos = new FileOutputStream(file);
  84. fos.write(getData);
  85. if (fos != null) {
  86. fos.close();
  87. }
  88. if (inputStream != null) {
  89. inputStream.close();
  90. }*/
  91. /* String localPath = "D:\\file\\123.csv";
  92. reportDailyAccountMapper.loadAccountDailyReport(142402L,localPath);*/
  93. } catch (Exception e) {
  94. e.printStackTrace();
  95. }
  96. }
  97. @Autowired
  98. private ICtopOauthTokenService tokenService;
  99. private static ExecutorService executorService = Executors.newFixedThreadPool(3);
  100. private static CountDownLatch countDownLatch = null;
  101. @Test
  102. public void testLoadJob() {
  103. log.info("快手获取日报数据任务执行开始");
  104. Long start = System.currentTimeMillis();
  105. Date getDate = DateUtils.addDay(new Date(), -1);
  106. List<CtopOauthToken> tokens = tokenService.getTokenListByType(CtopAdConstant.PLATFORM_TYPE_KUAISHOU);
  107. if (null == tokens || tokens.size() <= 0) {
  108. log.info("快手获取日报数据任务执行失败:未获取到可用的token");
  109. return;
  110. }
  111. String getDateStr = DateUtils.formatDate(getDate);
  112. countDownLatch = new CountDownLatch(tokens.size());
  113. for (CtopOauthToken token : tokens) {
  114. executorService.submit(new Runnable() {
  115. @Override
  116. public void run() {
  117. //创建任务
  118. try {
  119. reportTaskService.createTask(token.getAccountId(), token.getAccessToken(), getDateStr, getDateStr, CtopAdConstant.KUAISHOU_LOAD_JOB_TYPE_DAILY);
  120. } catch (Exception e) {
  121. e.printStackTrace();
  122. } finally {
  123. countDownLatch.countDown();
  124. }
  125. }
  126. });
  127. }
  128. try {
  129. countDownLatch.await();
  130. } catch (InterruptedException e) {
  131. e.printStackTrace();
  132. }
  133. Long end = System.currentTimeMillis();
  134. log.info("快手获取日报数据任务执行结束,执行耗时:{}秒", (end - start) / 1000);
  135. }
  136. }