Browse Source

Merge branch 'dataforseo' of wangfan/adweb3-server into master

wangfan 5 months ago
parent
commit
cf1f261479

+ 1 - 0
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/adweb/seo/mapper/xml/SeoKeywordsMapper.xml

@@ -180,5 +180,6 @@
         <if test = "keywordType != null">
             AND keyword_type = #{keywordType}
         </if>
+        AND last_search_time <![CDATA[ <]]> CURDATE()
     </select>
 </mapper>

+ 56 - 14
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/adweb/seo/service/dataforseo/DataForSEOService.java

@@ -1,18 +1,32 @@
 package org.jeecg.modules.adweb.seo.service.dataforseo;
 
 import io.github.dataforseo.client.ApiClient;
+import io.github.dataforseo.client.ApiException;
 import io.github.dataforseo.client.api.SerpApi;
 import io.github.dataforseo.client.auth.HttpBasicAuth;
+import io.github.dataforseo.client.model.*;
 
 import jakarta.annotation.PostConstruct;
 
+import lombok.extern.slf4j.Slf4j;
+
+import org.jeecg.modules.adweb.common.util.DateUtil;
+import org.jeecg.modules.adweb.common.util.ListUtil;
+import org.jeecg.modules.adweb.seo.entity.SeoKeywords;
 import org.jeecg.modules.adweb.seo.entity.SeoKeywordsSerp;
+import org.jeecg.modules.adweb.seo.mapper.SeoKeywordsMapper;
+import org.jeecg.modules.adweb.seo.service.ISeoKeywordsService;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 
+import java.util.Date;
+import java.util.List;
+
 /**
  * @author wfansh
  */
+@Slf4j
 @Service
 public class DataForSEOService {
 
@@ -25,6 +39,10 @@ public class DataForSEOService {
     @Value("${dataforseo.api-path}")
     private String apiPath;
 
+    @Autowired private SeoKeywordsMapper seoKeywordsMapper;
+
+    @Autowired private ISeoKeywordsService seoKeywordsService;
+
     private SerpApi serpApi;
 
     @PostConstruct
@@ -47,22 +65,46 @@ public class DataForSEOService {
      *
      * @param keywordType 1 - 指定词; 2 - 长尾词
      */
-    public void syncKeywordsSerp(int keywordType) {
+    public void syncKeywordsSerp(int keywordType, int limit) throws ApiException {
+        Date now = new Date();
+
         // 1. 查询待更新keywords
+        List<SeoKeywords> seoKeywordsList =
+                seoKeywordsMapper.getKeywordsToSerp(keywordType).subList(0, limit);
+        if (ListUtil.isEmpty(seoKeywordsList)) {
+            log.info("没有待Serp查询的关键词");
+            return;
+        }
 
+        // 2. 发送DataForSEO Serp查询请求
+        //        List<SerpGoogleOrganicLiveAdvancedRequestInfo> serpTasks =
+        //                seoKeywordsList.stream()
+        //                        .map(
+        //                                seoKeyword -> {
+        //                                    SerpGoogleOrganicLiveAdvancedRequestInfo serpTask =
+        //                                            new
+        // SerpGoogleOrganicLiveAdvancedRequestInfo();
+        //                                    serpTask.setKeyword(seoKeyword.getKeywords());
+        //                                    serpTask.setTag(Integer.toString(seoKeyword.getId()));
+        //                                    return serpTask;
+        //                                })
+        //                        .toList();
+        //
+        //        SerpGoogleOrganicLiveAdvancedResponseInfo serpResults =
+        //                serpApi.googleOrganicLiveAdvanced(serpTasks);
 
-//        // TODO: 判断网站状态
-//        List<GoogleGTM> googleGTMS = googleGTMService.list();
-//
-//        for (GoogleGTM googleGTM : googleGTMS) {
-//            // 每个帐号同步更新三张报表
-//            try {
-//                this.syncGACountryReport(googleGTM);
-//                this.syncGASourceMediumReport(googleGTM);
-//                this.syncGAPagePathReport(googleGTM);
-//            } catch (RuntimeException e) {
-//                log.warn("同步GA报表异常, siteId = {], error = {}", googleGTM.getSiteId(), e);
-//            }
-//        }
+        // 3. 更新SeoKeywords表
+        seoKeywordsList.forEach(
+                seoKeyword -> {
+                    // TODO: why?
+                    seoKeyword.setTimerLastSearchTime(DateUtil.getTodayZeroTime(now));
+                    // On search.
+                    seoKeyword.setSearchStatus(1);
+                });
+        seoKeywordsService.updateBatchById(seoKeywordsList);
+        log.info(
+                "{}个关键词serp查询任务创建完成 {}",
+                seoKeywordsList.size(),
+                seoKeywordsList.stream().map(SeoKeywords::getId).toList());
     }
 }

+ 4 - 23
jeecg-module-system/jeecg-system-start/src/test/java/org/jeecg/modules/adweb/seo/service/DataForSEOTest.java

@@ -1,40 +1,21 @@
 package org.jeecg.modules.adweb.seo.service;
 
-import io.github.dataforseo.client.api.SerpApi;
-import io.github.dataforseo.client.model.SerpGoogleOrganicLiveAdvancedRequestInfo;
-import io.github.dataforseo.client.model.SerpGoogleOrganicLiveAdvancedResponseInfo;
 
-import org.junit.jupiter.api.Disabled;
+import org.jeecg.modules.adweb.common.constant.AdwebConstant;
+import org.jeecg.modules.adweb.seo.service.dataforseo.DataForSEOService;
 import org.junit.jupiter.api.Test;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.test.context.ActiveProfiles;
 
-import java.util.ArrayList;
-import java.util.List;
-
 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
 @ActiveProfiles("dev")
 public class DataForSEOTest {
 
-    @Autowired private SerpApi serpApi;
+    @Autowired private DataForSEOService dataForSEOService;
 
     @Test
-    @Disabled("DataForSEO API charges")
     public void testSerpGoogleOrganic() throws Exception {
-        SerpGoogleOrganicLiveAdvancedRequestInfo task =
-                new SerpGoogleOrganicLiveAdvancedRequestInfo();
-
-        task.setLocationCode(2840);
-        task.setLanguageCode("en");
-        task.setKeyword("Aluminum Plastic Composite Panel Extrusion Line");
-
-        List<SerpGoogleOrganicLiveAdvancedRequestInfo> serpTaskRequestInfo =
-                new ArrayList<SerpGoogleOrganicLiveAdvancedRequestInfo>();
-        serpTaskRequestInfo.add(task);
-
-        SerpGoogleOrganicLiveAdvancedResponseInfo result =
-                serpApi.googleOrganicLiveAdvanced(serpTaskRequestInfo);
-        System.out.println(result);
+        dataForSEOService.syncKeywordsSerp(AdwebConstant.KEYWORD_TYPE_APPOINT, 3);
     }
 }