瀏覽代碼

定时处理文章品类

wangyunpeng 8 月之前
父節點
當前提交
601e578520

+ 2 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/repository/longArticle/ArticleCrawlerPlanRepository.java

@@ -8,5 +8,7 @@ import java.util.List;
 
 @Repository
 public interface ArticleCrawlerPlanRepository extends JpaRepository<ArticleCrawlerPlan, String> {
+
     List<ArticleCrawlerPlan> getByStatus(Integer status);
+
 }

+ 2 - 2
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/XxlJobService.java

@@ -392,8 +392,8 @@ public class XxlJobService {
         return ReturnT.SUCCESS;
     }
 
-    @XxlJob("articlePromotionTraceability")
-    public ReturnT<String> articleCategoryJob() {
+    @XxlJob("articleCategoryJob")
+    public ReturnT<String> articleCategoryJob(String param) {
         articleService.articleCategory();
         return ReturnT.SUCCESS;
     }

+ 65 - 10
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/recommend/ArticleService.java

@@ -1,5 +1,7 @@
 package com.tzld.longarticle.recommend.server.service.recommend;
 
+import com.alibaba.fastjson.JSONObject;
+import com.google.common.collect.Lists;
 import com.google.common.util.concurrent.ThreadFactoryBuilder;
 import com.tzld.longarticle.recommend.server.common.CommonThreadPoolExecutor;
 import com.tzld.longarticle.recommend.server.common.enums.StatusEnum;
@@ -346,21 +348,74 @@ public class ArticleService {
                 saveList.add(item);
             }
             longArticleBaseMapper.batchInsertArticleCategory(saveList);
+            // 抓取计划超过5天设置为已处理
+            String dateStr = crawlerPlan.getCrawlerPlanId().substring(0, 6);
+            if (DateUtils.dateStrToTimestamp(dateStr, "yyyyMMdd") < now - 86400000 * 5) {
+                crawlerPlan.setStatus(StatusEnum.ONE.getCode());
+                crawlerPlan.setUpdateTimestamp(now);
+                articleCrawlerPlanRepository.save(crawlerPlan);
+            }
         }
         List<ArticleCategory> dealList = articleCategoryRepository.getByStatus(ArticleCategoryStatusEnum.WAITING.getCode());
-        for (ArticleCategory articleCategory : dealList) {
-            KimiResult kimiResult = kimiApiService.requestOfficialApi(articleCategory.getTitle(), null, null);
-            articleCategory.setKimiResult(kimiResult.getResponseStr());
-            articleCategory.setUpdateTimestamp(System.currentTimeMillis());
+        for (List<ArticleCategory> partition : Lists.partition(dealList, 20)) {
+            List<String> partitionTitles = partition.stream().map(ArticleCategory::getTitle).collect(Collectors.toList());
+            String prompt = buildKimiPrompt(partitionTitles);
+            KimiResult kimiResult = kimiApiService.requestOfficialApi(prompt, null, null);
+            long now = System.currentTimeMillis();
+            JSONObject obj = null;
             if (kimiResult.isSuccess()) {
-                articleCategory.setCategory(kimiResult.getResponse().getChoices().get(0).getMessage().getContent());
-                articleCategory.setStatus(ArticleCategoryStatusEnum.SUCCESS.getCode());
-            } else {
-                articleCategory.setStatus(ArticleCategoryStatusEnum.FAIL.getCode());
-                articleCategory.setFailReason(kimiResult.getFailReason());
+                obj = JSONObject.parseObject(kimiResult.getResponse().getChoices().get(0).getMessage().getContent());
+            }
+            for (ArticleCategory articleCategory : partition) {
+                articleCategory.setKimiResult(kimiResult.getResponseStr());
+                articleCategory.setUpdateTimestamp(now);
+                if (kimiResult.isSuccess() && Objects.nonNull(obj)) {
+                    articleCategory.setCategory(obj.getString(articleCategory.getTitle()));
+                    articleCategory.setStatus(ArticleCategoryStatusEnum.SUCCESS.getCode());
+                } else {
+                    articleCategory.setStatus(ArticleCategoryStatusEnum.FAIL.getCode());
+                    articleCategory.setFailReason(kimiResult.getFailReason());
+                }
+                articleCategoryRepository.save(articleCategory);
             }
-            articleCategoryRepository.save(articleCategory);
         }
 
     }
+
+    private String buildKimiPrompt(List<String> titleList) {
+        StringBuilder prompt = new StringBuilder("    请帮我完成以下任务:输入为文章的标题,根据标题判断其内容所属的类目,输出为文章标题及其对应的类目。\n" +
+                "    类目需从以下范围内选择:\n" +
+                "    奇闻趣事\n" +
+                "    历史人物\n" +
+                "    家长里短\n" +
+                "    温情故事\n" +
+                "    健康养生\n" +
+                "    生活知识\n" +
+                "    名人八卦\n" +
+                "    政治新闻\n" +
+                "    军事新闻\n" +
+                "    为了更好地完成任务,可参考下列对文章标题的分类:\n" +
+                "    大舅病了,我取了三万元送过去,病房门口听到舅妈的话我改了主意\\t家长里短\n" +
+                "    能活到90岁的老人,基本上在70岁的时候,就不再做这些事了!\\t健康养生\n" +
+                "    去医院看望病人时,切忌带这4样东西,再亲近也不行,这是做人的根本\\t生活知识\n" +
+                "    上海一女子去饭店吃生煎包,戳了个洞想凉一凉,往里一看,瞬间惊呆了\\t奇闻趣事\n" +
+                "    卫生间放一把食盐,一年能省下好几百,涨新知识\\t生活知识\n" +
+                "    中国有一古寺,庙不大,却有武警24小时站岗,到底有何“过人”之处\\t奇闻趣事\n" +
+                "    1974年,苏联外长故意拿邓小平身高“取笑”,邓小平一句话轻松反击\\t历史人物\n" +
+                "    中国最美的女将军:上世纪曾家喻户晓,如今仍然健在\\t历史人物\n" +
+                "    北大才女蒙曼48岁仍未婚,被问最想嫁给谁,一个名字让全场笑喷\\t名人八卦\n" +
+                "    广东一老人去世,家人把老人的旧床垫扔了,环卫工人看到后,竟发现里面藏了15万元现金!家人傻眼了\\t奇闻趣事\n" +
+                "    你返回的结果为每行一个文章标题和它对应的类目,以制表符分隔,具体格式如下:\n" +
+                "    文章标题\\t类目\n" +
+                "    以下是需要分析的文章标题列表,每一行是一个标题:\n");
+        for (String title : titleList) {
+            prompt.append(title).append("\n");
+        }
+        prompt.append("最后输出结果请用JSON格式输出,key为title,value为类目,仅输出JSON,不要任何其他内容 格式如下:\n");
+        prompt.append("{" +
+                "\"大舅病了,我取了三万元送过去,病房门口听到舅妈的话我改了主意\": \"家长里短\",\n" +
+                "\"能活到90岁的老人,基本上在70岁的时候,就不再做这些事了!\": \"健康养生\"\n" +
+                "}");
+        return prompt.toString();
+    }
 }

+ 1 - 1
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/web/XxlJobController.java

@@ -51,7 +51,7 @@ public class XxlJobController {
 
     @GetMapping("/articleCategoryJob")
     public void articleCategoryJob() {
-        service.articleCategoryJob();
+        service.articleCategoryJob(null);
     }
 
 }