|
|
@@ -0,0 +1,221 @@
|
|
|
+package cn.com.ctop.job.adq.data.service.impl;
|
|
|
+
|
|
|
+import cn.com.ctop.job.adq.data.entity.AdqImageInfo;
|
|
|
+import cn.com.ctop.job.adq.data.entity.AdqVideoInfo;
|
|
|
+import cn.com.ctop.job.adq.data.mapper.AdqMediaInfoMapper;
|
|
|
+import cn.com.ctop.job.adq.data.service.IAdqMediaInfoService;
|
|
|
+import cn.com.ctop.job.adq.data.utils.Check;
|
|
|
+import cn.com.ctop.job.adq.data.utils.HttpUtils;
|
|
|
+import cn.com.ctop.job.adq.utils.RandomStringGenerator;
|
|
|
+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.stereotype.Service;
|
|
|
+
|
|
|
+import java.time.Instant;
|
|
|
+import java.time.LocalDate;
|
|
|
+import java.time.ZoneId;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class AdqMediaInfoServiceImpl implements IAdqMediaInfoService {
|
|
|
+ @Autowired
|
|
|
+ private AdqMediaInfoMapper adqMediaInfoMapper;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void getVideoListAll(Long advertiserId, String accessToken) {
|
|
|
+ log.info("全量拉取视频列表开始,account_id:{}", advertiserId);
|
|
|
+ getVideoList(advertiserId, accessToken, 1, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void getVideoListIncrement(Long advertiserId, String accessToken) {
|
|
|
+ long twoDaysAgo = LocalDate.now(ZoneId.of("Asia/Shanghai")).minusDays(2)
|
|
|
+ .atStartOfDay(ZoneId.of("Asia/Shanghai")).toEpochSecond();
|
|
|
+ System.err.println(twoDaysAgo);
|
|
|
+ log.info("增量拉取视频列表开始,account_id:{},起始时间戳:{}", advertiserId, twoDaysAgo);
|
|
|
+ String filtering = "[{\"field\":\"last_modified_time\",\"operator\":\"GREATER_EQUALS\",\"values\":[\"" + twoDaysAgo + "\"]}]";
|
|
|
+ getVideoList(advertiserId, accessToken, 1, filtering);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void getImageListAll(Long advertiserId, String accessToken) {
|
|
|
+ log.info("全量拉取图片列表开始,account_id:{}", advertiserId);
|
|
|
+ getImageList(advertiserId, accessToken, 1, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void getImageListIncrement(Long advertiserId, String accessToken) {
|
|
|
+ long twoDaysAgo = LocalDate.now(ZoneId.of("Asia/Shanghai")).minusDays(2)
|
|
|
+ .atStartOfDay(ZoneId.of("Asia/Shanghai")).toEpochSecond();
|
|
|
+ log.info("增量拉取图片列表开始,account_id:{},起始时间戳:{}", advertiserId, twoDaysAgo);
|
|
|
+ String filtering = "[{\"field\":\"last_modified_time\",\"operator\":\"GREATER_EQUALS\",\"values\":[\"" + twoDaysAgo + "\"]}]";
|
|
|
+ getImageList(advertiserId, accessToken, 1, filtering);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取视频列表(内部递归方法)
|
|
|
+ *
|
|
|
+ * @param filtering 过滤条件JSON字符串,null表示不过滤(全量)
|
|
|
+ */
|
|
|
+ private void getVideoList(Long advertiserId, String accessToken, Integer page, String filtering) {
|
|
|
+ try {
|
|
|
+ Thread.sleep(500);
|
|
|
+ Map<String, Object> paramsMap = new HashMap<>();
|
|
|
+ paramsMap.put("access_token", accessToken);
|
|
|
+ long timestamp = Instant.now().getEpochSecond();
|
|
|
+ paramsMap.put("timestamp", String.valueOf(timestamp));
|
|
|
+ paramsMap.put("nonce", RandomStringGenerator.generateRandomString(32));
|
|
|
+ paramsMap.put("account_id", advertiserId);
|
|
|
+ paramsMap.put("page", page);
|
|
|
+ paramsMap.put("page_size", 100);
|
|
|
+ if (filtering != null) {
|
|
|
+ paramsMap.put("filtering", filtering);
|
|
|
+ }
|
|
|
+
|
|
|
+ String s = HttpUtils.httpGet("https://api.e.qq.com/v3.0/videos/get", paramsMap, null);
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(s);
|
|
|
+ if (Check.isNull(jsonObject)) {
|
|
|
+ log.error("获取视频列表失败,account_id:{},返回为空", advertiserId);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ Integer code = jsonObject.getInteger("code");
|
|
|
+ if (code != 0) {
|
|
|
+ log.error("获取视频列表失败,account_id:{},返回信息:{}", advertiserId, jsonObject);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ JSONObject data = jsonObject.getJSONObject("data");
|
|
|
+ if (Check.isNull(data)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ int totalPage = data.getJSONObject("page_info").getIntValue("total_page");
|
|
|
+ JSONArray list = data.getJSONArray("list");
|
|
|
+ List<AdqVideoInfo> videoList = new ArrayList<>();
|
|
|
+
|
|
|
+ if (!Check.isNull(list)) {
|
|
|
+ for (int i = 0; i < list.size(); i++) {
|
|
|
+ JSONObject item = list.getJSONObject(i);
|
|
|
+ AdqVideoInfo video = new AdqVideoInfo();
|
|
|
+ video.setAdvertiserId(advertiserId);
|
|
|
+ video.setVideoId(item.getLong("video_id"));
|
|
|
+ video.setWidth(item.getInteger("width"));
|
|
|
+ video.setHeight(item.getInteger("height"));
|
|
|
+ video.setVideoFrames(item.getInteger("video_frames"));
|
|
|
+ video.setVideoFps(item.getFloat("video_fps"));
|
|
|
+ video.setVideoCodec(item.getString("video_codec"));
|
|
|
+ video.setVideoBitRate(item.getInteger("video_bit_rate"));
|
|
|
+ video.setAudioCodec(item.getString("audio_codec"));
|
|
|
+ video.setAudioBitRate(item.getInteger("audio_bit_rate"));
|
|
|
+ video.setFileSize(item.getLong("file_size"));
|
|
|
+ video.setType(item.getString("type"));
|
|
|
+ video.setSignature(item.getString("signature"));
|
|
|
+ video.setSystemStatus(item.getString("system_status"));
|
|
|
+ video.setDescription(item.getString("description"));
|
|
|
+ video.setPreviewUrl(item.getString("preview_url"));
|
|
|
+ video.setKeyFrameImageUrl(item.getString("key_frame_image_url"));
|
|
|
+ video.setCreatedTime(item.getLong("created_time"));
|
|
|
+ video.setLastModifiedTime(item.getLong("last_modified_time"));
|
|
|
+ videoList.add(video);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!Check.isNull(videoList)) {
|
|
|
+ adqMediaInfoMapper.batchInsertVideoInfo(videoList);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (page < totalPage) {
|
|
|
+ getVideoList(advertiserId, accessToken, page + 1, filtering);
|
|
|
+ } else {
|
|
|
+ log.info("获取视频列表完成,account_id:{},总页数:{}", advertiserId, totalPage);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("获取视频列表异常,account_id:{}", advertiserId, e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取图片列表(内部递归方法)
|
|
|
+ *
|
|
|
+ * @param filtering 过滤条件JSON字符串,null表示不过滤(全量)
|
|
|
+ */
|
|
|
+ private void getImageList(Long advertiserId, String accessToken, Integer page, String filtering) {
|
|
|
+ try {
|
|
|
+ Thread.sleep(500);
|
|
|
+ Map<String, Object> paramsMap = new HashMap<>();
|
|
|
+ paramsMap.put("access_token", accessToken);
|
|
|
+ long timestamp = Instant.now().getEpochSecond();
|
|
|
+ paramsMap.put("timestamp", String.valueOf(timestamp));
|
|
|
+ paramsMap.put("nonce", RandomStringGenerator.generateRandomString(32));
|
|
|
+ paramsMap.put("account_id", advertiserId);
|
|
|
+ paramsMap.put("page", page);
|
|
|
+ paramsMap.put("page_size", 100);
|
|
|
+ if (filtering != null) {
|
|
|
+ paramsMap.put("filtering", filtering);
|
|
|
+ }
|
|
|
+
|
|
|
+ String s = HttpUtils.httpGet("https://api.e.qq.com/v3.0/images/get", paramsMap, null);
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(s);
|
|
|
+ if (Check.isNull(jsonObject)) {
|
|
|
+ log.error("获取图片列表失败,account_id:{},返回为空", advertiserId);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ Integer code = jsonObject.getInteger("code");
|
|
|
+ if (code != 0) {
|
|
|
+ log.error("获取图片列表失败,account_id:{},返回信息:{}", advertiserId, jsonObject);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ JSONObject data = jsonObject.getJSONObject("data");
|
|
|
+ if (Check.isNull(data)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ int totalPage = data.getJSONObject("page_info").getIntValue("total_page");
|
|
|
+ JSONArray list = data.getJSONArray("list");
|
|
|
+ List<AdqImageInfo> imageList = new ArrayList<>();
|
|
|
+
|
|
|
+ if (!Check.isNull(list)) {
|
|
|
+ for (int i = 0; i < list.size(); i++) {
|
|
|
+ JSONObject item = list.getJSONObject(i);
|
|
|
+ AdqImageInfo image = new AdqImageInfo();
|
|
|
+ image.setAdvertiserId(advertiserId);
|
|
|
+ image.setImageId(item.getString("image_id"));
|
|
|
+ image.setWidth(item.getInteger("width"));
|
|
|
+ image.setHeight(item.getInteger("height"));
|
|
|
+ image.setFileSize(item.getLong("file_size"));
|
|
|
+ image.setType(item.getString("type"));
|
|
|
+ image.setSignature(item.getString("signature"));
|
|
|
+ image.setDescription(item.getString("description"));
|
|
|
+ image.setSourceSignature(item.getString("source_signature"));
|
|
|
+ image.setPreviewUrl(item.getString("preview_url"));
|
|
|
+ image.setSourceType(item.getString("source_type"));
|
|
|
+ image.setImageUsage(item.getString("image_usage"));
|
|
|
+ image.setCreatedTime(item.getLong("created_time"));
|
|
|
+ image.setLastModifiedTime(item.getLong("last_modified_time"));
|
|
|
+ imageList.add(image);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!Check.isNull(imageList)) {
|
|
|
+ adqMediaInfoMapper.batchInsertImageInfo(imageList);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (page < totalPage) {
|
|
|
+ getImageList(advertiserId, accessToken, page + 1, filtering);
|
|
|
+ } else {
|
|
|
+ log.info("获取图片列表完成,account_id:{},总页数:{}", advertiserId, totalPage);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("获取图片列表异常,account_id:{}", advertiserId, e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|