|
@@ -2,6 +2,7 @@ package com.tzld.piaoquan.longarticle.service.local.impl;
|
|
|
|
|
|
import cn.hutool.json.JSONObject;
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.tzld.piaoquan.longarticle.component.RedisLock;
|
|
|
import com.tzld.piaoquan.longarticle.dao.mapper.CrawlerVideoMapper;
|
|
|
import com.tzld.piaoquan.longarticle.dao.mapper.LongArticlesTextMapper;
|
|
|
import com.tzld.piaoquan.longarticle.dao.mapper.MatchVideoMapper;
|
|
@@ -11,18 +12,25 @@ import com.tzld.piaoquan.longarticle.model.vo.MatchVideoVo;
|
|
|
import com.tzld.piaoquan.longarticle.service.local.KimiService;
|
|
|
import com.tzld.piaoquan.longarticle.utils.*;
|
|
|
import com.tzld.piaoquan.longarticle.utils.other.*;
|
|
|
+import com.tzld.piaoquan.longarticle.utils.page.Page;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
import java.util.*;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
@Service
|
|
|
public class MatchVideoServiceImpl {
|
|
|
|
|
|
- private static final int MAX_NUM = 3;
|
|
|
+
|
|
|
+ private static final String KIMI_LOCK_KEY = "kimi_lock_key_%s";
|
|
|
+
|
|
|
+ private static final String CRAWLER_LOCK_KEY = "crawler_lock_key_%s";
|
|
|
+
|
|
|
|
|
|
@Autowired
|
|
|
KimiService kimiService;
|
|
@@ -30,15 +38,18 @@ public class MatchVideoServiceImpl {
|
|
|
@Autowired
|
|
|
private MatchVideoMapper matchVideoMapper;
|
|
|
|
|
|
- @Autowired
|
|
|
- private CrawlerVideoMapper crawlerVideoMapper;
|
|
|
-
|
|
|
@Autowired
|
|
|
private LongArticlesTextMapper longArticlesTextMapper;
|
|
|
|
|
|
@Autowired
|
|
|
private CrawlerVideoServiceImpl crawlerVideoService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private RedisLock redisLock;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RedisTemplate<String, Object> redisTemplate;
|
|
|
+
|
|
|
@Transactional
|
|
|
public void addMatchVideo(MatchContent matchContent) {
|
|
|
MatchVideoExample example = new MatchVideoExample();
|
|
@@ -53,9 +64,9 @@ public class MatchVideoServiceImpl {
|
|
|
matchVideo.setContentId(matchContent.getSourceId());
|
|
|
matchVideo.setAccountName(matchContent.getAccountName());
|
|
|
matchVideo.setGhId(matchContent.getGhId());
|
|
|
- matchVideo.setFlowPoolLevel(matchVideo.getFlowPoolLevel());
|
|
|
- matchVideo.setProcessTimes(1);
|
|
|
+ matchVideo.setFlowPoolLevel(matchContent.getFlowPoolLevelTag());
|
|
|
matchVideo.setContentStatus(0);
|
|
|
+ matchVideo.setPublishFlag(2);
|
|
|
long timestamp = System.currentTimeMillis() / 1000;
|
|
|
matchVideo.setContentStatusUpdateTime(Long.valueOf(timestamp).intValue());
|
|
|
matchVideo.setRequestTimestamp(Long.valueOf(timestamp).intValue());
|
|
@@ -73,47 +84,116 @@ public class MatchVideoServiceImpl {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public void matchContent() {
|
|
|
- CrawlerVideoExample example = new CrawlerVideoExample();
|
|
|
- example.createCriteria().andDownloadStatusEqualTo(0);
|
|
|
- crawlerVideoMapper.selectByExample(example);
|
|
|
+ public void getMatchVideo() {
|
|
|
+ List<Integer> targetStatus = new ArrayList<Integer>() {{
|
|
|
+ add(0);
|
|
|
+ add(1);
|
|
|
+ add(2);
|
|
|
+ }};
|
|
|
+ List<MatchVideo> matchVideos = null;
|
|
|
+ //循环增加游标位置,通过主键索引过滤已经处理的数据
|
|
|
+ do {
|
|
|
+ Integer id = (Integer) redisTemplate.opsForValue().get("last_match_video_id");
|
|
|
+ if (id == null) {
|
|
|
+ id = 0;
|
|
|
+ }
|
|
|
+ MatchVideoExample example = new MatchVideoExample();
|
|
|
+ example.createCriteria().andIdGreaterThan(id);
|
|
|
+ example.setOrderByClause("id asc");
|
|
|
+ Page page = new Page<>();
|
|
|
+ page.setCurrentPage(1);
|
|
|
+ page.setPageSize(5000);
|
|
|
+ example.setPage(page);
|
|
|
+ matchVideos = matchVideoMapper.selectByExample(example);
|
|
|
+ boolean flag = true;
|
|
|
+ for (MatchVideo matchVideo : matchVideos) {
|
|
|
+ if (targetStatus.contains(matchVideo.getContentStatus())) {
|
|
|
+ flag = false;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (flag) {
|
|
|
+ Integer lastId = matchVideos.get(matchVideos.size() - 1).getId();
|
|
|
+ redisTemplate.opsForValue().set("last_match_video_id", lastId);
|
|
|
+ }
|
|
|
+ } while (CollectionUtils.isEmpty(matchVideos));
|
|
|
+
|
|
|
+ int pageNum = 1;
|
|
|
+ do {
|
|
|
+ Integer id = (Integer) redisTemplate.opsForValue().get("last_match_video_id");
|
|
|
+ if (id == null) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ int pageSize = 5000;
|
|
|
+ MatchVideoExample example = new MatchVideoExample();
|
|
|
+ example.createCriteria().andIdGreaterThan(id).andContentStatusIn(targetStatus);
|
|
|
+ example.setOrderByClause("id asc");
|
|
|
+ Page page = new Page<>();
|
|
|
+ page.setCurrentPage(pageNum);
|
|
|
+ page.setPageSize(pageSize);
|
|
|
+ example.setPage(page);
|
|
|
+ matchVideos = matchVideoMapper.selectByExample(example);
|
|
|
+ for (MatchVideo matchVideo : matchVideos) {
|
|
|
+ //TODO 加入等待队列 多线程处理
|
|
|
+ }
|
|
|
+ pageNum++;
|
|
|
+ } while (CollectionUtils.isEmpty(matchVideos));
|
|
|
+
|
|
|
}
|
|
|
|
|
|
|
|
|
public void processMatchContent(MatchVideo matchVideo) {
|
|
|
- //1.执行kimi任务
|
|
|
- LongArticlesText kimiText = kimiService.getKimiText(matchVideo.getContentId());
|
|
|
- if (kimiText == null) {
|
|
|
- //TODO 查询信息重新生成kimi信息
|
|
|
- return;
|
|
|
- }
|
|
|
- if (kimiText.getKimiStatus() == 0) {
|
|
|
- //TODO 加锁
|
|
|
- kimiText = kimiService.getAndUpdateContent(matchVideo.getContentId());
|
|
|
+ if (matchVideo.getContentStatus() == 0) {
|
|
|
+ //1.执行kimi任务
|
|
|
+ LongArticlesText kimiText = kimiService.getKimiText(matchVideo.getContentId());
|
|
|
if (kimiText == null) {
|
|
|
- //TODO kimi结果获取失败
|
|
|
+ //TODO 查询信息重新生成kimi信息
|
|
|
return;
|
|
|
}
|
|
|
+ //执行kimi任务
|
|
|
+ if (kimiText.getKimiStatus() == 0) {
|
|
|
+ String lockKey = String.format(KIMI_LOCK_KEY, matchVideo.getContentId());
|
|
|
+ String lockValue = UUID.randomUUID().toString();
|
|
|
+ boolean lock = redisLock.tryLock(lockKey, lockValue, 300, TimeUnit.SECONDS);
|
|
|
+ if (lock) {
|
|
|
+ kimiText = kimiService.getAndUpdateContent(matchVideo.getContentId());
|
|
|
+ redisLock.unlock(lockKey, lockValue);
|
|
|
+ if (kimiText == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ //更新状态为kimi执行完成
|
|
|
+ updateStatus(matchVideo.getId(), 1);
|
|
|
+ }
|
|
|
}
|
|
|
- boolean existCrawlerVideo = existCrawlerVideo(matchVideo.getContentId());
|
|
|
- if(!existCrawlerVideo){
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- //2.执行爬虫任务
|
|
|
- int retry = 0;
|
|
|
- long count = crawlerVideoService.countCrawlerVideo(matchVideo.getContentId());
|
|
|
- if (count < 3) {
|
|
|
- crawlerVideoService.addCrawlerVideo(matchVideo.getContentId(), kimiText);
|
|
|
+ boolean existCrawlerVideo = crawlerVideoService.existCrawlerVideo(matchVideo.getContentId());
|
|
|
+ if (!existCrawlerVideo) {
|
|
|
+ String lockKey = String.format(CRAWLER_LOCK_KEY, matchVideo.getContentId());
|
|
|
+ String lockValue = UUID.randomUUID().toString();
|
|
|
+ boolean lock = redisLock.tryLock(lockKey, lockValue, 600, TimeUnit.SECONDS);
|
|
|
+ if (lock) {
|
|
|
+ LongArticlesText kimiText = kimiService.getKimiText(matchVideo.getContentId());
|
|
|
+ crawlerVideoService.addCrawlerVideo(matchVideo.getContentId(), kimiText);
|
|
|
+ redisLock.unlock(lockKey, lockValue);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ //更新状态为etl执行完成
|
|
|
+ updateStatus(matchVideo.getId(), 3);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public boolean existCrawlerVideo(String contentId) {
|
|
|
- CrawlerVideoExample example = new CrawlerVideoExample();
|
|
|
- example.createCriteria().andContentIdEqualTo(contentId).andDownloadStatusEqualTo(2);
|
|
|
- long l = crawlerVideoMapper.countByExample(example);
|
|
|
- return l >= MAX_NUM;
|
|
|
+ private void updateStatus(Integer id, Integer status) {
|
|
|
+ MatchVideo matchVideo = new MatchVideo();
|
|
|
+ matchVideo.setId(id);
|
|
|
+ matchVideo.setContentStatus(status);
|
|
|
+ matchVideoMapper.updateByPrimaryKeySelective(matchVideo);
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
}
|
|
|
|
|
|
|