From 79a62152f695bf409830ed8e9f851f08881711de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=A6=E8=8B=B1=E6=9D=B0?= <327782001@qq.com> Date: Tue, 24 Oct 2023 14:59:33 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E5=A4=9A=E4=BA=BA=E8=AF=84=E8=AE=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/CsdnController.java | 10 +- .../springbootkwan/schedule/CsdnSchedule.java | 7 ++ .../springbootkwan/service/CsdnService.java | 5 + .../service/impl/CsdnServiceImpl.java | 115 +++++++++++++----- 4 files changed, 106 insertions(+), 31 deletions(-) diff --git a/src/main/java/com/kwan/springbootkwan/controller/CsdnController.java b/src/main/java/com/kwan/springbootkwan/controller/CsdnController.java index 0d4feb6..838eb33 100644 --- a/src/main/java/com/kwan/springbootkwan/controller/CsdnController.java +++ b/src/main/java/com/kwan/springbootkwan/controller/CsdnController.java @@ -33,12 +33,18 @@ public class CsdnController { @GetMapping("/singleTriplet") public Result singleTriplet(@Param("username") String username) { csdnService.singleArticle(username); - return Result.ok("三连完成"); + return Result.ok("单人三连完成"); } @GetMapping("/multiTriplet") public Result multiTriplet() { csdnService.multiTriplet(); - return Result.ok("三连完成"); + return Result.ok("多人三连完成"); + } + + @GetMapping("/multiComment") + public Result multiComment() { + csdnService.multiComment(); + return Result.ok("多人评论完成"); } } diff --git a/src/main/java/com/kwan/springbootkwan/schedule/CsdnSchedule.java b/src/main/java/com/kwan/springbootkwan/schedule/CsdnSchedule.java index cc7b28f..233eb54 100644 --- a/src/main/java/com/kwan/springbootkwan/schedule/CsdnSchedule.java +++ b/src/main/java/com/kwan/springbootkwan/schedule/CsdnSchedule.java @@ -21,5 +21,12 @@ public class CsdnSchedule { csdnService.multiTriplet(); log.info("Scheduled task is finish ... ..."); } + + @Scheduled(cron = "0 0/30 * * * ?") + public void executeMultiComment() { + log.info("Scheduled task is running ... ..."); + csdnService.multiComment(); + log.info("Scheduled task is finish ... ..."); + } } diff --git a/src/main/java/com/kwan/springbootkwan/service/CsdnService.java b/src/main/java/com/kwan/springbootkwan/service/CsdnService.java index 88930e4..313f59e 100644 --- a/src/main/java/com/kwan/springbootkwan/service/CsdnService.java +++ b/src/main/java/com/kwan/springbootkwan/service/CsdnService.java @@ -20,4 +20,9 @@ public interface CsdnService { * 多人三连 */ void multiTriplet(); + + /** + * 多人只评论 + */ + void multiComment(); } \ No newline at end of file diff --git a/src/main/java/com/kwan/springbootkwan/service/impl/CsdnServiceImpl.java b/src/main/java/com/kwan/springbootkwan/service/impl/CsdnServiceImpl.java index b550191..6c55434 100644 --- a/src/main/java/com/kwan/springbootkwan/service/impl/CsdnServiceImpl.java +++ b/src/main/java/com/kwan/springbootkwan/service/impl/CsdnServiceImpl.java @@ -42,6 +42,37 @@ public class CsdnServiceImpl implements CsdnService { @Override public void singleArticle(String username) { + List list = this.getArticles(username); + if (list == null) { + return; + } + for (int i = 0; i < numOfArticlesPerPerson; i++) { + final BusinessInfoResponse.ArticleData.Article article = list.get(i); + final String type = article.getType(); + if (!StringUtils.equals("blog", type)) { + continue; + } + final String urlInfo = article.getUrl(); + String articleId = urlInfo.substring(urlInfo.lastIndexOf("/") + 1); + //点赞 + final Boolean isLike = csdnLikeService.isLike(articleId, username); + if (!isLike) { + csdnLikeService.like(articleId); + } + //评论 + final Boolean comment = csdnCommentService.isComment(article); + if (!comment) { + csdnCommentService.comment(articleId); + } + //收藏 + final Boolean collect = csdnCollectService.isCollect(articleId); + if (!collect) { + csdnCollectService.collect(article, username); + } + } + } + + private List getArticles(String username) { HttpResponse response = HttpUtil.createGet(url) .header("Cookie", csdnCookie) .form("page", 1) @@ -53,14 +84,63 @@ public class CsdnServiceImpl implements CsdnService { final String body = response.body(); ObjectMapper objectMapper = new ObjectMapper(); BusinessInfoResponse businessInfoResponse; + List list = null; try { businessInfoResponse = objectMapper.readValue(body, BusinessInfoResponse.class); final BusinessInfoResponse.ArticleData data = businessInfoResponse.getData(); - final List list = data.getList(); - if (CollectionUtil.isNotEmpty(list)) { - final int size = list.size(); - if (size < numOfArticlesPerPerson) { - numOfArticlesPerPerson = size; + list = data.getList(); + } catch (JsonProcessingException e) { + e.printStackTrace(); + } + if (CollectionUtil.isEmpty(list)) { + return null; + } + final int size = list.size(); + if (size < numOfArticlesPerPerson) { + numOfArticlesPerPerson = size; + } + return list; + } + + @Override + public void multiTriplet() { + final List list = csdnUserInfoService.list(); + if (CollectionUtil.isNotEmpty(list)) { + for (CsdnUserInfo csdnUserInfo : list) { + singleArticle(csdnUserInfo.getUserName()); + } + } + log.info("全部三连完成"); + } + + +// /** +// * 多人三连非数据库 +// */ +// @Override +// public void multiTriplet() { +// String[] userNames = new String[]{"user1", "user2", "user3", "xxxx"}; +// for (String name : userNames) { +// singleArticle(name); +// } +// log.info("全部三连完成"); +// } + + + @Override + public void multiComment() { + final List userNames = csdnUserInfoService.list(); + if (CollectionUtil.isNotEmpty(userNames)) { + for (CsdnUserInfo csdnUserInfo : userNames) { + final String userName = csdnUserInfo.getUserName(); + try { + Thread.sleep(10000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + List list = this.getArticles(userName); + if (list == null) { + continue; } for (int i = 0; i < numOfArticlesPerPerson; i++) { final BusinessInfoResponse.ArticleData.Article article = list.get(i); @@ -70,37 +150,14 @@ public class CsdnServiceImpl implements CsdnService { } final String urlInfo = article.getUrl(); String articleId = urlInfo.substring(urlInfo.lastIndexOf("/") + 1); - //点赞 - final Boolean isLike = csdnLikeService.isLike(articleId, username); - if (!isLike) { - csdnLikeService.like(articleId); - } //评论 final Boolean comment = csdnCommentService.isComment(article); if (!comment) { csdnCommentService.comment(articleId); } - //收藏 - final Boolean collect = csdnCollectService.isCollect(articleId); - if (!collect) { - csdnCollectService.collect(article, username); - } } } - - } catch (JsonProcessingException e) { - e.printStackTrace(); } - } - - @Override - public void multiTriplet() { - final List list = csdnUserInfoService.list(); - if (CollectionUtil.isNotEmpty(list)) { - for (CsdnUserInfo csdnUserInfo : list) { - singleArticle(csdnUserInfo.getUserName()); - } - } - log.info("全部三连完成"); + log.info("多人只评论完成"); } } \ No newline at end of file -- GitLab