|
|
@@ -0,0 +1,158 @@
|
|
|
+package cn.com.ctop.job.bytedance.data.service.impl;
|
|
|
+
|
|
|
+import cn.com.ctop.job.bytedance.data.entity.CsjAlbumResourceInfo;
|
|
|
+import cn.com.ctop.job.bytedance.data.entity.CsjAlbumSeqResourceInfo;
|
|
|
+import cn.com.ctop.job.bytedance.data.mapper.CsjResourceMapper;
|
|
|
+import cn.com.ctop.job.bytedance.data.service.ICsjResourceService;
|
|
|
+import cn.com.ctop.job.bytedance.data.utils.Check;
|
|
|
+import cn.com.ctop.job.bytedance.data.utils.HttpUtils;
|
|
|
+import cn.com.ctop.job.bytedance.utils.OssProperties;
|
|
|
+import cn.com.ctop.job.bytedance.utils.OssUtil;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class CsjResourceServiceImpl implements ICsjResourceService {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ CsjResourceMapper csjResourceMapper;
|
|
|
+ @Autowired
|
|
|
+ OssUtil ossUtil;
|
|
|
+ @Autowired
|
|
|
+ OssProperties ossProperties;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<CsjAlbumResourceInfo> albumList() {
|
|
|
+ return csjResourceMapper.getResources();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String run(CsjAlbumResourceInfo album) {
|
|
|
+ String resourceId = album.getResourceId();
|
|
|
+ String albumId = album.getAlbumId();
|
|
|
+ String token = csjResourceMapper.getToken();
|
|
|
+ if (Check.isNull(token)){
|
|
|
+ return "获取token失败";
|
|
|
+ }
|
|
|
+
|
|
|
+ String csjDownloadUrl = "https://open.douyin.com/api/resource_library/v1/resource/download/";
|
|
|
+ Map<String, String> headers = new HashMap<>();
|
|
|
+ headers.put("access-token", token);
|
|
|
+ headers.put("Content-Type", "application/json");
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
+ params.put("resource_id", resourceId);
|
|
|
+ String res = HttpUtils.httpPostRequest(csjDownloadUrl, params, headers);
|
|
|
+ if (Check.isNull(res)) {
|
|
|
+ return "请求失败,返回结果为空";
|
|
|
+ }
|
|
|
+ JSONObject resObj = JSONObject.parseObject(res);
|
|
|
+ int err_no = resObj.getInteger("err_no");
|
|
|
+ if (err_no != 0) {
|
|
|
+ return "请求失败,错误码:" + err_no + ", 错误信息:" + resObj.getString("err_msg");
|
|
|
+ }
|
|
|
+ JSONObject data = resObj.getJSONObject("data");
|
|
|
+ JSONArray videoList = data.getJSONArray("video_info_list");
|
|
|
+ JSONArray coverList = data.getJSONArray("cover_info_list");
|
|
|
+
|
|
|
+
|
|
|
+ List<CsjAlbumSeqResourceInfo> seqList = album.getSeqList();
|
|
|
+ if (seqList == null){
|
|
|
+ seqList = new ArrayList<>();
|
|
|
+ }else {
|
|
|
+ seqList.removeIf(seq -> seq.getSeq() == null);
|
|
|
+ }
|
|
|
+ Map<Integer, CsjAlbumSeqResourceInfo> seqMap = seqList.stream().collect(Collectors.toMap(CsjAlbumSeqResourceInfo::getSeq, seq -> seq));
|
|
|
+ if (videoList != null && !videoList.isEmpty()){
|
|
|
+ for (int i = 0; i < videoList.size(); i++) {
|
|
|
+ JSONObject videoInfo = videoList.getJSONObject(i);
|
|
|
+ Integer seqNum = videoInfo.getInteger("seq");
|
|
|
+ String videoUrl = videoInfo.getString("url");
|
|
|
+ if (Check.isNull(videoUrl)) {
|
|
|
+ log.error("第" + seqNum + "集视频链接为空");
|
|
|
+ }
|
|
|
+ CsjAlbumSeqResourceInfo seq = seqMap.get(seqNum);
|
|
|
+ if (seq == null){
|
|
|
+ seq = new CsjAlbumSeqResourceInfo(albumId, seqNum);
|
|
|
+ seqList.add(seq);
|
|
|
+ seqMap.put(seqNum, seq);
|
|
|
+ }
|
|
|
+ if (StringUtils.isEmpty(seq.getOssSeqVideo())){
|
|
|
+ String fileName = albumId + "_" + seqNum + "_video.mp4";
|
|
|
+ String ossVideoUrl = uploadToOss(videoUrl, fileName);
|
|
|
+ if (StringUtils.isNotEmpty(ossVideoUrl)){
|
|
|
+ seq.setOssSeqVideo(ossVideoUrl);
|
|
|
+ csjResourceMapper.updateSeq(seq);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (coverList != null && !coverList.isEmpty()){
|
|
|
+ for (int i = 0; i < coverList.size(); i++) {
|
|
|
+ JSONObject coverInfo = coverList.getJSONObject(i);
|
|
|
+ Integer seqNum = coverInfo.getInteger("seq");
|
|
|
+ String coverUrl = coverInfo.getString("url");
|
|
|
+
|
|
|
+ if (seqNum == null || seqNum == 0){
|
|
|
+ if (StringUtils.isEmpty(album.getOssCoverImg())){
|
|
|
+ String fileName = albumId + "_cover.jpg";
|
|
|
+ String ossCoverImg = uploadToOss(coverUrl, fileName);
|
|
|
+ if (StringUtils.isNotEmpty(ossCoverImg)){
|
|
|
+ album.setOssCoverImg(ossCoverImg);
|
|
|
+ csjResourceMapper.updateAlbum(album);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ CsjAlbumSeqResourceInfo seq = seqMap.get(seqNum);
|
|
|
+ if (seq == null){
|
|
|
+ seq = new CsjAlbumSeqResourceInfo(albumId, seqNum);
|
|
|
+ seqList.add(seq);
|
|
|
+ seqMap.put(seqNum, seq);
|
|
|
+ }
|
|
|
+ if (StringUtils.isEmpty(seq.getOssSeqImg())){
|
|
|
+ String fileName = albumId + "_" + seqNum + "_image.jpg";
|
|
|
+ String ossSeqImg = uploadToOss(coverUrl, fileName);
|
|
|
+ if (StringUtils.isNotEmpty(ossSeqImg)){
|
|
|
+ seq.setOssSeqImg(ossSeqImg);
|
|
|
+ csjResourceMapper.updateSeq(seq);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+// //update album, update seq
|
|
|
+// csjResourceMapper.updateAlbum(album);
|
|
|
+// if (album.getSeqList() != null && !album.getSeqList().isEmpty()){
|
|
|
+// csjResourceMapper.batchUpdateAlbumSeq(album.getSeqList());
|
|
|
+// }
|
|
|
+ log.info("处理资源成功: " + album.getAlbumId() + ", 共处理分集: " + seqList.size());
|
|
|
+
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+
|
|
|
+ private String uploadToOss(String url, String fileName){
|
|
|
+ String objectKey = ossProperties.getCsjFileDir() + fileName;
|
|
|
+ String ossUrl = ossProperties.getCsjDomain() + objectKey;
|
|
|
+ try {
|
|
|
+ System.out.println("文件: "+fileName + ", from: " + url);
|
|
|
+// return null;
|
|
|
+ ossUtil.uploadFromUrl(objectKey, url);
|
|
|
+ log.info("上传成功: " + ossUrl);
|
|
|
+ return ossUrl;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("上传失败: " + e.getMessage(), e);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|