SampleTest.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. package org.jeecg;
  2. import cn.com.ctop.bytedance.entity.BytedancePlanDailyReport;
  3. import cn.com.ctop.bytedance.service.IBytedancePlanDailyReportService;
  4. import cn.com.ctop.bytedance.service.IReportService;
  5. import cn.com.ctop.common.module.entity.CtopOauthToken;
  6. import cn.com.ctop.common.module.entity.UserAllocation;
  7. import cn.com.ctop.common.module.service.ICtopOauthTokenService;
  8. import cn.com.ctop.common.module.service.IUserAllocationService;
  9. import cn.com.ctop.common.module.utils.CtopAdConstant;
  10. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  11. import lombok.extern.slf4j.Slf4j;
  12. import org.jeecg.common.util.DateUtils;
  13. import org.junit.Test;
  14. import org.junit.runner.RunWith;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.beans.factory.annotation.Value;
  17. import org.springframework.boot.test.context.SpringBootTest;
  18. import org.springframework.test.context.junit4.SpringRunner;
  19. import java.io.*;
  20. import java.net.HttpURLConnection;
  21. import java.net.URL;
  22. import java.text.SimpleDateFormat;
  23. import java.util.Date;
  24. import java.util.List;
  25. @RunWith(SpringRunner.class)
  26. @SpringBootTest
  27. @Slf4j
  28. public class SampleTest {
  29. @Value("${jeecg.path.chrome-driver}")
  30. private String chromeDriver;
  31. @Test
  32. public void kuaisShouReport() {
  33. try {
  34. /* String url = "https://ad.e.kuaishou.com/rest/openapi/v1/async_task/create";
  35. Map<String, String> headers = new HashMap<>();
  36. headers.put("Content-Type", " application/json");
  37. headers.put("Access-Token", "eb9a0d18072eabc339ee26ad010ecd6b");
  38. JSONObject param = new JSONObject();
  39. param.put("advertiser_id", 142402);
  40. param.put("task_name", "142402-test");
  41. JSONObject taskParams = new JSONObject();
  42. taskParams.put("start_date", "2020-01-01");
  43. taskParams.put("end_date", "2020-03-05");
  44. taskParams.put("view_type", 1);
  45. param.put("task_params",taskParams);
  46. String result = HttpUtils.kuaiShouhttpPostRequest(url, param.toJSONString(), headers);
  47. JSONObject resultJson = JSONObject.parseObject(result);
  48. System.err.println(resultJson);*/
  49. // 1229917 142402
  50. /* String url = "https://ad.e.kuaishou.com/rest/openapi/v1/async_task/list";
  51. Map<String, String> headers = new HashMap<>();
  52. headers.put("Content-Type", " application/json");
  53. headers.put("Access-Token", "eb9a0d18072eabc339ee26ad010ecd6b");
  54. JSONObject param = new JSONObject();
  55. param.put("advertiser_id", 142402);
  56. JSONArray taskIds = new JSONArray();
  57. taskIds.add(1229917);
  58. param.put("task_ids", taskIds);
  59. String result = HttpUtils.kuaiShouhttpPostRequest(url, param.toJSONString(), headers);
  60. JSONObject resultJson = JSONObject.parseObject(result);
  61. System.err.println(resultJson);*/
  62. /* String url = "https://ad.e.kuaishou.com/rest/openapi/v1/async_task/download";
  63. Map<String, String> headers = new HashMap<>();
  64. headers.put("Access-Token", "eb9a0d18072eabc339ee26ad010ecd6b");
  65. Map<String,Object> param = new HashMap<>();
  66. param.put("advertiser_id", 142402);
  67. param.put("task_id", "1229917");
  68. String result = HttpUtils.KuaiShouttpGetRequest(url, param,headers);*/
  69. URL url = new URL("https://ad.e.kuaishou.com/rest/openapi/v1/async_task/download?task_id=1229917&advertiser_id=142402");
  70. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  71. //设置超时间为3秒
  72. conn.setConnectTimeout(10 * 1000);
  73. //防止屏蔽程序抓取而返回403错误
  74. conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
  75. conn.setRequestProperty("Access-Token", "eb9a0d18072eabc339ee26ad010ecd6b");
  76. //得到输入流
  77. InputStream inputStream = conn.getInputStream();
  78. //获取自己数组
  79. byte[] getData = readInputStream(inputStream);
  80. //文件保存位置
  81. File saveDir = new File("D:\\file");
  82. if (!saveDir.exists()) {
  83. saveDir.mkdirs();
  84. }
  85. File file = new File(saveDir + File.separator + "qqww.csv");
  86. FileOutputStream fos = new FileOutputStream(file);
  87. fos.write(getData);
  88. if (fos != null) {
  89. fos.close();
  90. }
  91. if (inputStream != null) {
  92. inputStream.close();
  93. }
  94. } catch (Exception e) {
  95. e.printStackTrace();
  96. }
  97. }
  98. @Autowired
  99. private ICtopOauthTokenService tokenService;
  100. @Test
  101. public void testBakToken() {
  102. Date getDate = DateUtils.addDay(new Date(), -1);
  103. QueryWrapper<UserAllocation> queryWrapper = new QueryWrapper<>();
  104. queryWrapper.eq("project_id", 235);
  105. List<UserAllocation> allocations = userAllocationService.list(queryWrapper);
  106. if (null != allocations && allocations.size() > 0) {
  107. for (UserAllocation allocation : allocations) {
  108. CtopOauthToken token = tokenService.getTokenByAccountId(allocation.getAccountId());
  109. if (null != token) {
  110. reportService.getAdvertiserPlanReport(token, getDate, getDate, CtopAdConstant.BYTEDANCE_REPORT_TYPE_DAILY);
  111. }
  112. }
  113. }
  114. planDailyReportService.cleanChannelCodeData(235, getDate);
  115. }
  116. @Autowired
  117. private IUserAllocationService userAllocationService;
  118. private void formatZybData(Integer projectId, Date getDate) {
  119. //清洗关于作业帮数据
  120. //1:查询作业帮相关的用户数据
  121. QueryWrapper<UserAllocation> wrapper = new QueryWrapper<>();
  122. wrapper.eq("project_id", projectId);
  123. List<UserAllocation> allocations = userAllocationService.list(wrapper);
  124. if (null != allocations && allocations.size() > 0) {
  125. allocations.forEach(allocation -> {
  126. //根据accountId和时间,查询相关的日报表信息
  127. QueryWrapper<BytedancePlanDailyReport> queryWrapper = new QueryWrapper<>();
  128. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  129. String dateString = dateFormat.format(getDate) + " 00:00:00";
  130. queryWrapper.eq("advertiser_id", allocation.getAccountId())
  131. .eq("stat_datetime", dateString)
  132. ;
  133. List<BytedancePlanDailyReport> bytedancePlanDailyReports = planDailyReportService.list(queryWrapper);
  134. if (null != bytedancePlanDailyReports && bytedancePlanDailyReports.size() > 0) {
  135. bytedancePlanDailyReports.forEach(report -> {
  136. String adName = report.getAdName();
  137. String[] tags = adName.split("-");
  138. report.setAdsense(tags[1]);
  139. report.setPlanCode(tags[2]);
  140. String materialName = tags[3];
  141. report.setMaterialName(materialName);
  142. if (materialName.contains("其他")) {
  143. report.setMaterialType("其他");
  144. } else if (materialName.contains("图片轮播")) {
  145. report.setMaterialType("图片轮播");
  146. } else if (materialName.contains("大字报")) {
  147. report.setMaterialType("大字报");
  148. } else if (materialName.contains("文字动画")) {
  149. report.setMaterialType("文字动画");
  150. } else if (materialName.contains("采访形式")) {
  151. report.setMaterialType("采访形式");
  152. } else if (materialName.contains("剧情")) {
  153. report.setMaterialType("剧情");
  154. } else if (materialName.contains("口播")) {
  155. report.setMaterialType("口播");
  156. } else {
  157. report.setMaterialType("其他");
  158. }
  159. planDailyReportService.updateById(report);
  160. });
  161. }
  162. });
  163. }
  164. }
  165. @Autowired
  166. private IBytedancePlanDailyReportService planDailyReportService;
  167. @Autowired
  168. private IReportService reportService;
  169. /**
  170. * CSV文件生成方法
  171. *
  172. * @param head 文件头
  173. * @param dataList 数据列表
  174. * @param outPutPath 文件输出路径
  175. * @param filename 文件名
  176. * @return
  177. */
  178. public static File createCSVFile(String head, List<CtopOauthToken> dataList, String outPutPath, String filename) {
  179. File csvFile = null;
  180. BufferedWriter csvWtriter = null;
  181. try {
  182. csvFile = new File(outPutPath + File.separator + filename + ".csv");
  183. File parent = csvFile.getParentFile();
  184. if (parent != null && !parent.exists()) {
  185. parent.mkdirs();
  186. }
  187. csvFile.createNewFile();
  188. // GB2312使正确读取分隔符","
  189. csvWtriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
  190. csvFile), "GB2312"), 1024);
  191. // 写入文件头部
  192. writeRow(head, csvWtriter);
  193. // 写入文件内容
  194. for (CtopOauthToken row : dataList) {
  195. writeRow(row.toString(), csvWtriter);
  196. }
  197. csvWtriter.flush();
  198. } catch (Exception e) {
  199. e.printStackTrace();
  200. } finally {
  201. try {
  202. csvWtriter.close();
  203. } catch (IOException e) {
  204. e.printStackTrace();
  205. }
  206. }
  207. return csvFile;
  208. }
  209. /**
  210. * 写一行数据方法
  211. *
  212. * @param csvWriter
  213. * @throws IOException
  214. */
  215. private static void writeRow(String data, BufferedWriter csvWriter) throws IOException {
  216. // 写入文件头部
  217. csvWriter.write(data);
  218. csvWriter.newLine();
  219. }
  220. public static byte[] readInputStream(InputStream inputStream) throws IOException {
  221. byte[] buffer = new byte[1024];
  222. int len = 0;
  223. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  224. while ((len = inputStream.read(buffer)) != -1) {
  225. bos.write(buffer, 0, len);
  226. }
  227. bos.close();
  228. return bos.toByteArray();
  229. }
  230. }