|
|
@@ -12,10 +12,14 @@ import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.time.Duration;
|
|
|
import java.util.HashMap;
|
|
|
+import java.util.HashSet;
|
|
|
import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
|
|
|
@Service
|
|
|
@Slf4j
|
|
|
@@ -24,6 +28,8 @@ public class BytedanceMaterialReportServiceImpl implements IBytedanceMaterialRep
|
|
|
private BytedancePromoterReportMapper promoterReportMapper;
|
|
|
@Autowired
|
|
|
private DorisMultiTableStreamLoadManager dorisLoadManager;
|
|
|
+ @Autowired
|
|
|
+ private RedisTemplate redisTemplate_db2;
|
|
|
|
|
|
@Override
|
|
|
public void getBytedanceMaterialDailyReport(Long advertiserId, String accessToken, String startDate, String endDate, Integer page, Long errorId, Integer isLog, Integer isSend) {
|
|
|
@@ -45,6 +51,7 @@ public class BytedanceMaterialReportServiceImpl implements IBytedanceMaterialRep
|
|
|
headers.put("Content-Type", "application/json");
|
|
|
String s = HttpUtils.httpGet(url, paramsMap, headers);
|
|
|
JSONObject result = JSONObject.parseObject(s);
|
|
|
+ Set<Long> materialIds = new HashSet<>();
|
|
|
if (Check.isNull(result)) {
|
|
|
log.info("账户id:{},素材日报请求结果返回为空", advertiserId);
|
|
|
if (isLog == 1) {
|
|
|
@@ -76,6 +83,12 @@ public class BytedanceMaterialReportServiceImpl implements IBytedanceMaterialRep
|
|
|
dimensionsJson.put("stat_time_day", dimensionsJson.getString("stat_time_day").replace("-", ""));
|
|
|
dimensionsJson.put("advertiser_id", advertiserId);
|
|
|
dorisLoadManager.add("media_api", "bytedance_material_report_daily", dimensionsJson);
|
|
|
+
|
|
|
+ Long materialId = dimensionsJson.getLong("material_id");
|
|
|
+ String imageMode = dimensionsJson.getString("image_mode");
|
|
|
+ if (!materialIds.contains(materialId) && !redisTemplate_db2.hasKey("mvid:1:" + materialId) && imageMode.contains("视频")) {
|
|
|
+ materialIds.add(materialId);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -93,6 +106,11 @@ public class BytedanceMaterialReportServiceImpl implements IBytedanceMaterialRep
|
|
|
}
|
|
|
return;
|
|
|
}
|
|
|
+
|
|
|
+ if (!Check.isNull(materialIds)) {
|
|
|
+ getBytedanceMaterialVideo(advertiserId, materialIds, accessToken, null, null, 1, null, 1, null, 1);
|
|
|
+ }
|
|
|
+
|
|
|
if (page < totalPage) {
|
|
|
getBytedanceMaterialDailyReport(advertiserId, accessToken, startDate, endDate, page + 1, errorId, isLog, isSend);
|
|
|
} else {
|
|
|
@@ -109,4 +127,94 @@ public class BytedanceMaterialReportServiceImpl implements IBytedanceMaterialRep
|
|
|
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ public void getBytedanceMaterialVideo(Long advertiserId, Set<Long> materialIds, String accessToken, String startTime, String endTime, Integer page, Long errorId, Integer isLog, Integer isSend, int retry) {
|
|
|
+ try {
|
|
|
+ String url = "https://api.oceanengine.com/open_api/2/file/video/get/";
|
|
|
+ Map<String, Object> paramsMap = new HashMap<>();
|
|
|
+ paramsMap.put("advertiser_id", advertiserId);
|
|
|
+ JSONObject filtering = new JSONObject();
|
|
|
+ if (!Check.isNull(startTime) && !Check.isNull(endTime)) {
|
|
|
+ filtering.put("start_time", startTime);
|
|
|
+ filtering.put("end_time", endTime);
|
|
|
+ }
|
|
|
+ if (!Check.isNull(materialIds)) {
|
|
|
+ filtering.put("material_ids", materialIds);
|
|
|
+ }
|
|
|
+ paramsMap.put("filtering", filtering);
|
|
|
+ paramsMap.put("page", page);
|
|
|
+ paramsMap.put("page_size", 100);
|
|
|
+ Map<String, String> headers = new HashMap<>();
|
|
|
+ headers.put("Access-Token", accessToken);
|
|
|
+ headers.put("Content-Type", "application/json");
|
|
|
+ String s = HttpUtils.httpGet(url, paramsMap, headers);
|
|
|
+ JSONObject result = JSONObject.parseObject(s);
|
|
|
+ if (Check.isNull(result)) {
|
|
|
+ log.info("账户id:{},获取视频素材结果返回为空", advertiserId);
|
|
|
+ if (isLog == 1) {
|
|
|
+ BytedanceErrorLog errorInfo = new BytedanceErrorLog();
|
|
|
+ errorInfo.setAdvertiserId(advertiserId);
|
|
|
+ errorInfo.setStartDate(Check.isNull(startTime) ? "" : startTime);
|
|
|
+ errorInfo.setEndDate(Check.isNull(endTime) ? "" : endTime);
|
|
|
+ errorInfo.setDataDimension("material_video_get");
|
|
|
+ errorInfo.setDataType("daily");
|
|
|
+ errorInfo.setMsg(new JSONObject().fluentPut("msg", "结果为空").fluentPut("material_ids", materialIds).toJSONString());
|
|
|
+ promoterReportMapper.replaceErrorInfo(errorInfo);
|
|
|
+ }
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Integer code = result.getInteger("code");
|
|
|
+ int totalPage = 1;
|
|
|
+ if (code == 0) {
|
|
|
+ JSONObject data = result.getJSONObject("data");
|
|
|
+ if (!Check.isNull(data)) {
|
|
|
+ totalPage = data.getJSONObject("page_info").getIntValue("total_page");
|
|
|
+ JSONArray rows = data.getJSONArray("list");
|
|
|
+ if (!Check.isNull(rows)) {
|
|
|
+ for (int i = 0; i < rows.size(); i++) {
|
|
|
+ JSONObject rowJson = rows.getJSONObject(i);
|
|
|
+ String materialId = rowJson.getString("material_id");
|
|
|
+ redisTemplate_db2.opsForValue().set("mvid:1:" + materialId, materialId, Duration.ofSeconds(30 * 24 * 3600 + (int) (Math.random() * 7200)));
|
|
|
+ rowJson.put("advertiser_id", advertiserId);
|
|
|
+ dorisLoadManager.add("media_api", "bytedance_video_list", rowJson);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (retry <= 5) {
|
|
|
+ try {
|
|
|
+ Thread.sleep(800);
|
|
|
+ } catch (Exception ignored) {
|
|
|
+ }
|
|
|
+ getBytedanceMaterialVideo(advertiserId, materialIds, accessToken, startTime, endTime, page, errorId, isLog, isSend, retry + 1);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ log.info("账户id:{},日期:{}获取视频素材失败,返回数据:{}", advertiserId, startTime, result);
|
|
|
+ if (isLog == 1) {
|
|
|
+ BytedanceErrorLog errorInfo = new BytedanceErrorLog();
|
|
|
+ errorInfo.setAdvertiserId(advertiserId);
|
|
|
+ errorInfo.setStartDate(Check.isNull(startTime) ? "" : startTime);
|
|
|
+ errorInfo.setEndDate(Check.isNull(endTime) ? "" : endTime);
|
|
|
+ errorInfo.setDataDimension("material_video_get");
|
|
|
+ errorInfo.setDataType("daily");
|
|
|
+ errorInfo.setMsg(result.fluentPut("material_ids", materialIds).toJSONString());
|
|
|
+ promoterReportMapper.replaceErrorInfo(errorInfo);
|
|
|
+ }
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (page < totalPage) {
|
|
|
+ getBytedanceMaterialVideo(advertiserId, materialIds, accessToken, startTime, endTime, page + 1, errorId, isLog, isSend, 1);
|
|
|
+ } else {
|
|
|
+ log.info("账户ID:{}, 开始日期:{}, 结束日期:{}, totalPage:{}, 获取视频素材成功", advertiserId, startTime, endTime, totalPage);
|
|
|
+ if (errorId != null) {
|
|
|
+ promoterReportMapper.deleteInfoByErrorId(errorId);
|
|
|
+ }
|
|
|
+ if (isSend != null && 1 == isSend) {
|
|
|
+ AlertUtils.sendMessage(advertiserId, startTime, endTime, 1, "material", 1, null);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|