package org.jeecg; import cn.com.ctop.bytedance.entity.BytedancePlanDailyReport; import cn.com.ctop.bytedance.service.IBytedancePlanDailyReportService; import cn.com.ctop.bytedance.service.IReportService; import cn.com.ctop.common.module.entity.CtopOauthToken; import cn.com.ctop.common.module.entity.UserAllocation; import cn.com.ctop.common.module.service.ICtopOauthTokenService; import cn.com.ctop.common.module.service.IUserAllocationService; import cn.com.ctop.common.module.utils.CtopAdConstant; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import lombok.extern.slf4j.Slf4j; import org.jeecg.common.util.DateUtils; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.io.*; import java.net.HttpURLConnection; import java.net.URL; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; @RunWith(SpringRunner.class) @SpringBootTest @Slf4j public class SampleTest { @Value("${jeecg.path.chrome-driver}") private String chromeDriver; @Test public void kuaisShouReport() { try { /* String url = "https://ad.e.kuaishou.com/rest/openapi/v1/async_task/create"; Map headers = new HashMap<>(); headers.put("Content-Type", " application/json"); headers.put("Access-Token", "eb9a0d18072eabc339ee26ad010ecd6b"); JSONObject param = new JSONObject(); param.put("advertiser_id", 142402); param.put("task_name", "142402-test"); JSONObject taskParams = new JSONObject(); taskParams.put("start_date", "2020-01-01"); taskParams.put("end_date", "2020-03-05"); taskParams.put("view_type", 1); param.put("task_params",taskParams); String result = HttpUtils.kuaiShouhttpPostRequest(url, param.toJSONString(), headers); JSONObject resultJson = JSONObject.parseObject(result); System.err.println(resultJson);*/ // 1229917 142402 /* String url = "https://ad.e.kuaishou.com/rest/openapi/v1/async_task/list"; Map headers = new HashMap<>(); headers.put("Content-Type", " application/json"); headers.put("Access-Token", "eb9a0d18072eabc339ee26ad010ecd6b"); JSONObject param = new JSONObject(); param.put("advertiser_id", 142402); JSONArray taskIds = new JSONArray(); taskIds.add(1229917); param.put("task_ids", taskIds); String result = HttpUtils.kuaiShouhttpPostRequest(url, param.toJSONString(), headers); JSONObject resultJson = JSONObject.parseObject(result); System.err.println(resultJson);*/ /* String url = "https://ad.e.kuaishou.com/rest/openapi/v1/async_task/download"; Map headers = new HashMap<>(); headers.put("Access-Token", "eb9a0d18072eabc339ee26ad010ecd6b"); Map param = new HashMap<>(); param.put("advertiser_id", 142402); param.put("task_id", "1229917"); String result = HttpUtils.KuaiShouttpGetRequest(url, param,headers);*/ URL url = new URL("https://ad.e.kuaishou.com/rest/openapi/v1/async_task/download?task_id=1229917&advertiser_id=142402"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); //设置超时间为3秒 conn.setConnectTimeout(10 * 1000); //防止屏蔽程序抓取而返回403错误 conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); conn.setRequestProperty("Access-Token", "eb9a0d18072eabc339ee26ad010ecd6b"); //得到输入流 InputStream inputStream = conn.getInputStream(); //获取自己数组 byte[] getData = readInputStream(inputStream); //文件保存位置 File saveDir = new File("D:\\file"); if (!saveDir.exists()) { saveDir.mkdirs(); } File file = new File(saveDir + File.separator + "qqww.csv"); FileOutputStream fos = new FileOutputStream(file); fos.write(getData); if (fos != null) { fos.close(); } if (inputStream != null) { inputStream.close(); } } catch (Exception e) { e.printStackTrace(); } } @Autowired private ICtopOauthTokenService tokenService; @Test public void testBakToken() { Date getDate = DateUtils.addDay(new Date(), -1); QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("project_id", 235); List allocations = userAllocationService.list(queryWrapper); if (null != allocations && allocations.size() > 0) { for (UserAllocation allocation : allocations) { CtopOauthToken token = tokenService.getTokenByAccountId(allocation.getAccountId()); if (null != token) { reportService.getAdvertiserPlanReport(token, getDate, getDate, CtopAdConstant.BYTEDANCE_REPORT_TYPE_DAILY); } } } planDailyReportService.cleanChannelCodeData(235, getDate); } @Autowired private IUserAllocationService userAllocationService; private void formatZybData(Integer projectId, Date getDate) { //清洗关于作业帮数据 //1:查询作业帮相关的用户数据 QueryWrapper wrapper = new QueryWrapper<>(); wrapper.eq("project_id", projectId); List allocations = userAllocationService.list(wrapper); if (null != allocations && allocations.size() > 0) { allocations.forEach(allocation -> { //根据accountId和时间,查询相关的日报表信息 QueryWrapper queryWrapper = new QueryWrapper<>(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); String dateString = dateFormat.format(getDate) + " 00:00:00"; queryWrapper.eq("advertiser_id", allocation.getAccountId()) .eq("stat_datetime", dateString) ; List bytedancePlanDailyReports = planDailyReportService.list(queryWrapper); if (null != bytedancePlanDailyReports && bytedancePlanDailyReports.size() > 0) { bytedancePlanDailyReports.forEach(report -> { String adName = report.getAdName(); String[] tags = adName.split("-"); report.setAdsense(tags[1]); report.setPlanCode(tags[2]); String materialName = tags[3]; report.setMaterialName(materialName); if (materialName.contains("其他")) { report.setMaterialType("其他"); } else if (materialName.contains("图片轮播")) { report.setMaterialType("图片轮播"); } else if (materialName.contains("大字报")) { report.setMaterialType("大字报"); } else if (materialName.contains("文字动画")) { report.setMaterialType("文字动画"); } else if (materialName.contains("采访形式")) { report.setMaterialType("采访形式"); } else if (materialName.contains("剧情")) { report.setMaterialType("剧情"); } else if (materialName.contains("口播")) { report.setMaterialType("口播"); } else { report.setMaterialType("其他"); } planDailyReportService.updateById(report); }); } }); } } @Autowired private IBytedancePlanDailyReportService planDailyReportService; @Autowired private IReportService reportService; /** * CSV文件生成方法 * * @param head 文件头 * @param dataList 数据列表 * @param outPutPath 文件输出路径 * @param filename 文件名 * @return */ public static File createCSVFile(String head, List dataList, String outPutPath, String filename) { File csvFile = null; BufferedWriter csvWtriter = null; try { csvFile = new File(outPutPath + File.separator + filename + ".csv"); File parent = csvFile.getParentFile(); if (parent != null && !parent.exists()) { parent.mkdirs(); } csvFile.createNewFile(); // GB2312使正确读取分隔符"," csvWtriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream( csvFile), "GB2312"), 1024); // 写入文件头部 writeRow(head, csvWtriter); // 写入文件内容 for (CtopOauthToken row : dataList) { writeRow(row.toString(), csvWtriter); } csvWtriter.flush(); } catch (Exception e) { e.printStackTrace(); } finally { try { csvWtriter.close(); } catch (IOException e) { e.printStackTrace(); } } return csvFile; } /** * 写一行数据方法 * * @param csvWriter * @throws IOException */ private static void writeRow(String data, BufferedWriter csvWriter) throws IOException { // 写入文件头部 csvWriter.write(data); csvWriter.newLine(); } public static byte[] readInputStream(InputStream inputStream) throws IOException { byte[] buffer = new byte[1024]; int len = 0; ByteArrayOutputStream bos = new ByteArrayOutputStream(); while ((len = inputStream.read(buffer)) != -1) { bos.write(buffer, 0, len); } bos.close(); return bos.toByteArray(); } }