diff --git a/src/main/java/com/kwan/springbootkwan/controller/CsdnController.java b/src/main/java/com/kwan/springbootkwan/controller/CsdnController.java index 40c3982e754bd54b8b92ad3c9450942f073e5398..9285ac297ad87763e83628226f294c5c6547ae6e 100644 --- a/src/main/java/com/kwan/springbootkwan/controller/CsdnController.java +++ b/src/main/java/com/kwan/springbootkwan/controller/CsdnController.java @@ -1,13 +1,9 @@ package com.kwan.springbootkwan.controller; -import cn.hutool.core.collection.CollectionUtil; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.kwan.springbootkwan.entity.CsdnUserInfo; import com.kwan.springbootkwan.entity.Result; -import com.kwan.springbootkwan.enums.CollectStatus; -import com.kwan.springbootkwan.enums.CommentStatus; -import com.kwan.springbootkwan.enums.LikeStatus; -import com.kwan.springbootkwan.service.CsdnCommentService; +import com.kwan.springbootkwan.service.CsdnAutoReplyService; import com.kwan.springbootkwan.service.CsdnService; import com.kwan.springbootkwan.service.CsdnUserInfoService; import lombok.extern.slf4j.Slf4j; @@ -17,7 +13,6 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; -import java.util.List; import java.util.Objects; @Slf4j @@ -28,7 +23,7 @@ public class CsdnController { @Autowired private CsdnService csdnService; @Autowired - private CsdnCommentService csdnCommentService; + private CsdnAutoReplyService csdnAutoReplyService; @Autowired private CsdnUserInfoService csdnUserInfoService; @@ -52,23 +47,13 @@ public class CsdnController { @GetMapping("/autoReply") public Result autoReply() { - csdnCommentService.commentSelf(); + csdnAutoReplyService.commentSelf(); return Result.ok("自动回复完成"); } @GetMapping("/resetCurrentStatus") public Result resetCurrentStatus() { - QueryWrapper wrapper = new QueryWrapper<>(); - wrapper.eq("is_delete", 0); - final List list = csdnUserInfoService.list(wrapper); - if (CollectionUtil.isNotEmpty(list)) { - for (CsdnUserInfo csdnUserInfo : list) { - csdnUserInfo.setLikeStatus(LikeStatus.UN_PROCESSED.getCode()); - csdnUserInfo.setCollectStatus(CollectStatus.UN_PROCESSED.getCode()); - csdnUserInfo.setCommentStatus(CommentStatus.RESTRICTED_COMMENTS.getCode()); - csdnUserInfoService.updateById(csdnUserInfo); - } - } + csdnUserInfoService.resetCurrentStatus(); return Result.ok("重置当天状态完成"); } } diff --git a/src/main/java/com/kwan/springbootkwan/controller/CsdnTripletDayInfoController.java b/src/main/java/com/kwan/springbootkwan/controller/CsdnTripletDayInfoController.java new file mode 100644 index 0000000000000000000000000000000000000000..0eed70f04cf7e1fa8e3441d11ea8a8df722cf9fd --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/controller/CsdnTripletDayInfoController.java @@ -0,0 +1,28 @@ +package com.kwan.springbootkwan.controller; + + +import com.kwan.springbootkwan.entity.Result; +import com.kwan.springbootkwan.service.CsdnTripletDayInfoService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * 每日三连监控(CsdnTripletDayInfo)表控制层 + * + * @author makejava + * @since 2023-10-26 20:55:30 + */ +@RestController +@RequestMapping("/dayInfo") +public class CsdnTripletDayInfoController { + @Autowired + private CsdnTripletDayInfoService csdnTripletDayInfoService; + + @GetMapping("/add") + public Result autoReply() { + return Result.ok(csdnTripletDayInfoService.todayInfo()); + } +} + diff --git a/src/main/java/com/kwan/springbootkwan/entity/CsdnTripletDayInfo.java b/src/main/java/com/kwan/springbootkwan/entity/CsdnTripletDayInfo.java new file mode 100644 index 0000000000000000000000000000000000000000..40aa5fa134b7f2167fcf543d0bca3342223387e4 --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/entity/CsdnTripletDayInfo.java @@ -0,0 +1,36 @@ +package com.kwan.springbootkwan.entity; + +import com.baomidou.mybatisplus.annotation.TableName; +import com.baomidou.mybatisplus.extension.activerecord.Model; +import com.fasterxml.jackson.annotation.JsonFormat; +import lombok.Data; + +import java.util.Date; + +/** + * 每日三连监控(CsdnTripletDayInfo)表实体类 + * + * @author makejava + * @since 2023-10-26 20:55:31 + */ +@Data +@TableName("csdn_triplet_day_info") +public class CsdnTripletDayInfo extends Model { + //主键id + private Integer id; + //三连日期 + @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8") + private Date tripletDate; + //点赞数量 + private Integer likeNum; + //收藏数量 + private Integer collectNum; + //评论数量 + private Integer commentNum; + //创建时间 + private Date createTime; + //更新时间 + private Date updateTime; + //逻辑删除,0未删除,1已删除 + private Integer isDelete; +} \ No newline at end of file diff --git a/src/main/java/com/kwan/springbootkwan/enums/CommentStatus.java b/src/main/java/com/kwan/springbootkwan/enums/CommentStatus.java index 9900a48fc3b157dd99fae6e3eade2b22d3690c70..c16210d02acf24fc0afd1dd31558b709f15e25f9 100644 --- a/src/main/java/com/kwan/springbootkwan/enums/CommentStatus.java +++ b/src/main/java/com/kwan/springbootkwan/enums/CommentStatus.java @@ -29,7 +29,7 @@ public enum CommentStatus { /** * 评论已经到了49条 */ - COMMENT_NUM_49(5, "评论已经到了49条"), + COMMENT_NUM_49(5, "当日评论已经到了49条"), /** * 其他错误 */ diff --git a/src/main/java/com/kwan/springbootkwan/mapper/CsdnTripletDayInfoMapper.java b/src/main/java/com/kwan/springbootkwan/mapper/CsdnTripletDayInfoMapper.java new file mode 100644 index 0000000000000000000000000000000000000000..d0e03eed5352c3601846ed8b440cd2357ebccd6a --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/mapper/CsdnTripletDayInfoMapper.java @@ -0,0 +1,15 @@ +package com.kwan.springbootkwan.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.kwan.springbootkwan.entity.CsdnTripletDayInfo; + +/** + * 每日三连监控(CsdnTripletDayInfo)表数据库访问层 + * + * @author makejava + * @since 2023-10-26 20:55:30 + */ +public interface CsdnTripletDayInfoMapper extends BaseMapper { + +} + diff --git a/src/main/java/com/kwan/springbootkwan/schedule/CsdnSchedule.java b/src/main/java/com/kwan/springbootkwan/schedule/CsdnSchedule.java index b50db83ce18f8d2c316bcb681a19500f179cc59a..47d9c7c699eced0ffb4721e11d076c723ca82c06 100644 --- a/src/main/java/com/kwan/springbootkwan/schedule/CsdnSchedule.java +++ b/src/main/java/com/kwan/springbootkwan/schedule/CsdnSchedule.java @@ -1,23 +1,15 @@ package com.kwan.springbootkwan.schedule; - -import cn.hutool.core.collection.CollectionUtil; -import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; -import com.kwan.springbootkwan.entity.CsdnUserInfo; -import com.kwan.springbootkwan.enums.CollectStatus; -import com.kwan.springbootkwan.enums.CommentStatus; -import com.kwan.springbootkwan.enums.LikeStatus; +import com.kwan.springbootkwan.service.CsdnAutoReplyService; import com.kwan.springbootkwan.service.CsdnCommentService; import com.kwan.springbootkwan.service.CsdnService; +import com.kwan.springbootkwan.service.CsdnTripletDayInfoService; import com.kwan.springbootkwan.service.CsdnUserInfoService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; -import java.util.List; - - @Slf4j @Component public class CsdnSchedule { @@ -28,6 +20,10 @@ public class CsdnSchedule { private CsdnCommentService csdnCommentService; @Autowired private CsdnUserInfoService csdnUserInfoService; + @Autowired + private CsdnAutoReplyService csdnAutoReplyService; + @Autowired + private CsdnTripletDayInfoService csdnTripletDayInfoService; @Scheduled(cron = "0 0 8,10,12,14,16,18,20 * * ?") public void execute() { @@ -39,25 +35,22 @@ public class CsdnSchedule { @Scheduled(cron = "0 0/30 0 * * ?") public void executeReply() { log.info("executeReply task is running ... ..."); - csdnCommentService.commentSelf(); + csdnAutoReplyService.commentSelf(); log.info("executeReply task is finish ... ..."); } @Scheduled(cron = "0 0/30 * * * ?") public void executeInit() { log.info("executeInit task is running ... ..."); - QueryWrapper wrapper = new QueryWrapper<>(); - wrapper.eq("is_delete", 0); - final List list = csdnUserInfoService.list(wrapper); - if (CollectionUtil.isNotEmpty(list)) { - for (CsdnUserInfo csdnUserInfo : list) { - csdnUserInfo.setLikeStatus(LikeStatus.UN_PROCESSED.getCode()); - csdnUserInfo.setCollectStatus(CollectStatus.UN_PROCESSED.getCode()); - csdnUserInfo.setCommentStatus(CommentStatus.RESTRICTED_COMMENTS.getCode()); - csdnUserInfoService.updateById(csdnUserInfo); - } - } + csdnUserInfoService.resetCurrentStatus(); log.info("executeInit task is finish ... ..."); } -} + @Scheduled(cron = "0 0 1 * * ?") + public void resetTripletDayInfo() { + log.info("resetTripletDayInfo task is running ... ..."); + //新增当前天的新的一条数据 + csdnTripletDayInfoService.todayInfo(); + log.info("resetTripletDayInfo task is finish ... ..."); + } +} \ No newline at end of file diff --git a/src/main/java/com/kwan/springbootkwan/service/CsdnArticleService.java b/src/main/java/com/kwan/springbootkwan/service/CsdnArticleService.java index b81f295669975352d9c02f5a164b07e585387e5b..24cdbd8a021f111046b356aa0acc53b9785fdd22 100644 --- a/src/main/java/com/kwan/springbootkwan/service/CsdnArticleService.java +++ b/src/main/java/com/kwan/springbootkwan/service/CsdnArticleService.java @@ -15,7 +15,7 @@ import java.util.List; public interface CsdnArticleService { /** - * 获取最新的文章 + * 获取最新的5篇文章 * * @param username * @return diff --git a/src/main/java/com/kwan/springbootkwan/service/CsdnAutoReplyService.java b/src/main/java/com/kwan/springbootkwan/service/CsdnAutoReplyService.java new file mode 100644 index 0000000000000000000000000000000000000000..b4b03a07cdc5bc831bc8dadae13e8b8f81c2beb3 --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/service/CsdnAutoReplyService.java @@ -0,0 +1,18 @@ +package com.kwan.springbootkwan.service; + +/** + * 自动回复 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/10/24 01:25 + */ +public interface CsdnAutoReplyService { + + /** + * 评论自己的文章 + * + * @return + */ + void commentSelf(); +} \ No newline at end of file diff --git a/src/main/java/com/kwan/springbootkwan/service/CsdnCollectService.java b/src/main/java/com/kwan/springbootkwan/service/CsdnCollectService.java index beabbf3d902d3cc5fa0c3704fd408b6e00db38c9..de555fbf0bf3627924e0349bcc6b8bd78e6db2ea 100644 --- a/src/main/java/com/kwan/springbootkwan/service/CsdnCollectService.java +++ b/src/main/java/com/kwan/springbootkwan/service/CsdnCollectService.java @@ -1,6 +1,7 @@ package com.kwan.springbootkwan.service; +import com.kwan.springbootkwan.entity.CsdnTripletDayInfo; import com.kwan.springbootkwan.entity.CsdnUserInfo; import com.kwan.springbootkwan.entity.resp.BusinessInfoResponse; @@ -25,5 +26,5 @@ public interface CsdnCollectService { * * @return */ - Boolean collect(BusinessInfoResponse.ArticleData.Article article, CsdnUserInfo csdnUserInfo); + Boolean collect(BusinessInfoResponse.ArticleData.Article article, CsdnUserInfo csdnUserInfo, CsdnTripletDayInfo csdnTripletDayInfo); } \ No newline at end of file diff --git a/src/main/java/com/kwan/springbootkwan/service/CsdnCommentService.java b/src/main/java/com/kwan/springbootkwan/service/CsdnCommentService.java index f3b823aa551957771c58269c0c9040cb4ecd0908..b2d4fee76f4bd6fff78fc6493ddbc7c91716e5d0 100644 --- a/src/main/java/com/kwan/springbootkwan/service/CsdnCommentService.java +++ b/src/main/java/com/kwan/springbootkwan/service/CsdnCommentService.java @@ -1,7 +1,9 @@ package com.kwan.springbootkwan.service; +import com.kwan.springbootkwan.entity.CsdnTripletDayInfo; import com.kwan.springbootkwan.entity.CsdnUserInfo; import com.kwan.springbootkwan.entity.resp.BusinessInfoResponse; +import com.kwan.springbootkwan.entity.resp.CommentResponse; /** * 评论 @@ -24,13 +26,15 @@ public interface CsdnCommentService { * * @return */ - Boolean comment(String articleId, CsdnUserInfo csdnUserInfo); - + Boolean comment(String articleId, CsdnUserInfo csdnUserInfo, CsdnTripletDayInfo csdnTripletDayInfo); /** - * 评论自己的文章 + * 评论文章 * + * @param articleId + * @param commentInfo + * @param commentId * @return */ - void commentSelf(); + CommentResponse dealComment(String articleId, String commentInfo, Integer commentId); } \ No newline at end of file diff --git a/src/main/java/com/kwan/springbootkwan/service/CsdnLikeService.java b/src/main/java/com/kwan/springbootkwan/service/CsdnLikeService.java index 6c4cd357171ca118aaaba4e94c51d00c25df51e2..df710e527079c064715d4ddbd99d8ddb99e161d8 100644 --- a/src/main/java/com/kwan/springbootkwan/service/CsdnLikeService.java +++ b/src/main/java/com/kwan/springbootkwan/service/CsdnLikeService.java @@ -1,6 +1,7 @@ package com.kwan.springbootkwan.service; +import com.kwan.springbootkwan.entity.CsdnTripletDayInfo; import com.kwan.springbootkwan.entity.CsdnUserInfo; /** @@ -24,5 +25,5 @@ public interface CsdnLikeService { * * @return */ - Boolean like(String articleId, CsdnUserInfo csdnUserInfo); + Boolean like(String articleId, CsdnUserInfo csdnUserInfo, CsdnTripletDayInfo csdnTripletDayInfo); } \ No newline at end of file diff --git a/src/main/java/com/kwan/springbootkwan/service/CsdnTripletDayInfoService.java b/src/main/java/com/kwan/springbootkwan/service/CsdnTripletDayInfoService.java new file mode 100644 index 0000000000000000000000000000000000000000..124eda842f9cb1a910ffb087a1f8cfa1c3e897d8 --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/service/CsdnTripletDayInfoService.java @@ -0,0 +1,21 @@ +package com.kwan.springbootkwan.service; + +import com.baomidou.mybatisplus.extension.service.IService; +import com.kwan.springbootkwan.entity.CsdnTripletDayInfo; + +/** + * 每日三连监控(CsdnTripletDayInfo)表服务接口 + * + * @author makejava + * @since 2023-10-26 20:55:31 + */ +public interface CsdnTripletDayInfoService extends IService { + + + /** + * 重置每日三连监控 + */ + CsdnTripletDayInfo todayInfo(); + +} + diff --git a/src/main/java/com/kwan/springbootkwan/service/CsdnUserInfoService.java b/src/main/java/com/kwan/springbootkwan/service/CsdnUserInfoService.java index 7e733c24be213f7b90859e61c49486ab3a253b24..16ddd8460b96bf91dabdea8bc220dedb83e0a0ed 100644 --- a/src/main/java/com/kwan/springbootkwan/service/CsdnUserInfoService.java +++ b/src/main/java/com/kwan/springbootkwan/service/CsdnUserInfoService.java @@ -11,5 +11,8 @@ import com.kwan.springbootkwan.entity.CsdnUserInfo; */ public interface CsdnUserInfoService extends IService { + + void resetCurrentStatus(); + } diff --git a/src/main/java/com/kwan/springbootkwan/service/impl/CsdnAutoReplyServiceImpl.java b/src/main/java/com/kwan/springbootkwan/service/impl/CsdnAutoReplyServiceImpl.java new file mode 100644 index 0000000000000000000000000000000000000000..ce048fb70106c2090b0ed16ff8f8e90f9096e22b --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/service/impl/CsdnAutoReplyServiceImpl.java @@ -0,0 +1,126 @@ +package com.kwan.springbootkwan.service.impl; + + +import cn.hutool.core.collection.CollectionUtil; +import cn.hutool.http.HttpResponse; +import cn.hutool.http.HttpUtil; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.kwan.springbootkwan.entity.CsdnUserInfo; +import com.kwan.springbootkwan.entity.resp.BusinessInfoResponse; +import com.kwan.springbootkwan.entity.resp.CommentListResponse; +import com.kwan.springbootkwan.entity.resp.CommentResponse; +import com.kwan.springbootkwan.service.CsdnArticleService; +import com.kwan.springbootkwan.service.CsdnAutoReplyService; +import com.kwan.springbootkwan.service.CsdnCommentService; +import com.kwan.springbootkwan.service.CsdnService; +import com.kwan.springbootkwan.service.CsdnUserInfoService; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Objects; + + +@Slf4j +@Service +public class CsdnAutoReplyServiceImpl implements CsdnAutoReplyService { + @Value("${csdn.cookie}") + private String csdnCookie; + @Value("#{'${csdn.self_reply}'.split(';')}") + private String[] selfReply; + @Value("${csdn.self_user_name}") + private String selfUserName; + @Value("${csdn.url.is_comment_list_url}") + private String commentListUrl; + + @Autowired + private CsdnService csdnService; + @Autowired + private CsdnArticleService csdnArticleService; + @Autowired + private CsdnCommentService csdnCommentService; + @Autowired + private CsdnUserInfoService csdnUserInfoService; + + @Override + public void commentSelf() { + List list = csdnArticleService.getArticles(selfUserName); + if (list == null) { + return; + } + for (BusinessInfoResponse.ArticleData.Article article : list) { + final String type = article.getType(); + if (StringUtils.equals("blog", type)) { + final String urlInfo = article.getUrl(); + String articleId = urlInfo.substring(urlInfo.lastIndexOf("/") + 1); + String url = commentListUrl + articleId; + HttpResponse response = HttpUtil.createPost(url) + .header("Cookie", csdnCookie) + .form("page", 1) + .form("size", 200)//获取当前文章的200条评论 + .execute(); + final String body = response.body(); + ObjectMapper objectMapper = new ObjectMapper(); + CommentListResponse articleInfo; + try { + articleInfo = objectMapper.readValue(body, CommentListResponse.class); + final CommentListResponse.DataInfo data = articleInfo.getData(); + final List otherCommentList = data.getList(); + if (CollectionUtil.isNotEmpty(otherCommentList)) { + for (CommentListResponse.Comment oneComment : otherCommentList) { + final CommentListResponse.Info info = oneComment.getInfo(); + final String userName = info.getUserName(); + final String nickName = info.getNickName(); + final Integer commentId = info.getCommentId(); + if (!StringUtils.equals(userName, selfUserName)) { + final List sub = oneComment.getSub(); + boolean flag = false; + if (CollectionUtil.isNotEmpty(sub)) { + for (CommentListResponse.SubComment subComment : sub) { + //如果没有自己的评论,需要评论 + final String subUserName = subComment.getUserName(); + if (StringUtils.equals(subUserName, selfUserName)) { + flag = true; + } + } + } + if (CollectionUtil.isEmpty(sub) || !flag) { + //需要评论 + int start = -1; + int end = selfReply.length; + int temp_count = (int) (Math.floor(Math.random() * (start - end + 1)) + end); + CommentResponse reply = csdnCommentService.dealComment(articleId, selfReply[temp_count], commentId); + log.info(reply.toString()); + } + //三连此评论人 + QueryWrapper wrapper = new QueryWrapper<>(); + wrapper.eq("is_delete", 0); + wrapper.eq("user_name", userName).last("limit 1"); + CsdnUserInfo csdnUserInfo = csdnUserInfoService.getOne(wrapper); + if (Objects.isNull(csdnUserInfo)) { + //新增用户 + csdnUserInfo = new CsdnUserInfo(); + csdnUserInfo.setUserName(userName); + csdnUserInfo.setNickName(nickName); + csdnUserInfo.setLikeStatus(0); + csdnUserInfo.setCollectStatus(0); + csdnUserInfo.setCommentStatus(0); + csdnUserInfo.setUserWeight(7); + csdnUserInfoService.save(csdnUserInfo); + } + csdnService.singleArticle(csdnUserInfo); + } + } + } + } catch (JsonProcessingException e) { + e.printStackTrace(); + } + } + } + } +} diff --git a/src/main/java/com/kwan/springbootkwan/service/impl/CsdnCollectServiceImpl.java b/src/main/java/com/kwan/springbootkwan/service/impl/CsdnCollectServiceImpl.java index 3b9053d8a624f86490486893eee410d5ce5d6050..24bd6c5b29a6d22045ae1ce027b3a4a148fe09f5 100644 --- a/src/main/java/com/kwan/springbootkwan/service/impl/CsdnCollectServiceImpl.java +++ b/src/main/java/com/kwan/springbootkwan/service/impl/CsdnCollectServiceImpl.java @@ -4,6 +4,7 @@ import cn.hutool.http.HttpResponse; import cn.hutool.http.HttpUtil; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; +import com.kwan.springbootkwan.entity.CsdnTripletDayInfo; import com.kwan.springbootkwan.entity.CsdnUserInfo; import com.kwan.springbootkwan.entity.resp.BusinessInfoResponse; import com.kwan.springbootkwan.entity.resp.CollectInfoQuery; @@ -66,7 +67,7 @@ public class CsdnCollectServiceImpl implements CsdnCollectService { } @Override - public Boolean collect(BusinessInfoResponse.ArticleData.Article article, CsdnUserInfo csdnUserInfo) { + public Boolean collect(BusinessInfoResponse.ArticleData.Article article, CsdnUserInfo csdnUserInfo, CsdnTripletDayInfo csdnTripletDayInfo) { final String userName = csdnUserInfo.getUserName(); final Integer collectStatus = csdnUserInfo.getCollectStatus(); if (CollectStatus.HAVE_ALREADY_COLLECT.getCode().equals(collectStatus) || CollectStatus.COLLECT_IS_FULL.getCode().equals(collectStatus)) { @@ -104,6 +105,7 @@ public class CsdnCollectServiceImpl implements CsdnCollectService { if (code == 200) { log.info("文章{}收藏成功", articleId); csdnUserInfo.setCollectStatus(CollectStatus.COLLECT_SUCCESSFUL.getCode()); + csdnTripletDayInfo.setCollectNum(csdnTripletDayInfo.getCollectNum() + 1); } else if (code == 400000101) { log.info("收藏文章{}参数缺失", articleId); csdnUserInfo.setCollectStatus(CollectStatus.MISSING_PARAMETER.getCode()); diff --git a/src/main/java/com/kwan/springbootkwan/service/impl/CsdnCommentServiceImpl.java b/src/main/java/com/kwan/springbootkwan/service/impl/CsdnCommentServiceImpl.java index 7afb4df8811594f431e1de129ceb8c9cafdebe38..a310a05031458ae86dca049d303c0e236a66c858 100644 --- a/src/main/java/com/kwan/springbootkwan/service/impl/CsdnCommentServiceImpl.java +++ b/src/main/java/com/kwan/springbootkwan/service/impl/CsdnCommentServiceImpl.java @@ -5,16 +5,15 @@ import cn.hutool.http.HttpResponse; import cn.hutool.http.HttpUtil; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; +import com.kwan.springbootkwan.entity.CsdnTripletDayInfo; import com.kwan.springbootkwan.entity.CsdnUserInfo; import com.kwan.springbootkwan.entity.resp.BusinessInfoResponse; import com.kwan.springbootkwan.entity.resp.CommentListResponse; import com.kwan.springbootkwan.entity.resp.CommentResponse; import com.kwan.springbootkwan.enums.CommentStatus; -import com.kwan.springbootkwan.service.CsdnArticleService; import com.kwan.springbootkwan.service.CsdnCommentService; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; @@ -29,8 +28,6 @@ public class CsdnCommentServiceImpl implements CsdnCommentService { private String csdnCookie; @Value("#{'${csdn.self_comment}'.split(';')}") private String[] selfComment; - @Value("#{'${csdn.self_reply}'.split(';')}") - private String[] selfReply; @Value("${csdn.self_user_name}") private String selfUserName; @Value("${csdn.url.is_comment_list_url}") @@ -38,8 +35,6 @@ public class CsdnCommentServiceImpl implements CsdnCommentService { @Value("${csdn.url.comment_url}") private String commentUrl; - @Autowired - private CsdnArticleService csdnArticleService; @Override public Boolean isComment(BusinessInfoResponse.ArticleData.Article article, CsdnUserInfo csdnUserInfo) { @@ -56,7 +51,6 @@ public class CsdnCommentServiceImpl implements CsdnCommentService { .header("Cookie", csdnCookie) .form("page", 1) .form("size", 50) - .form("fold", "unfold") .execute(); final String body = response.body(); ObjectMapper objectMapper = new ObjectMapper(); @@ -83,7 +77,7 @@ public class CsdnCommentServiceImpl implements CsdnCommentService { } @Override - public Boolean comment(String articleId, CsdnUserInfo csdnUserInfo) { + public Boolean comment(String articleId, CsdnUserInfo csdnUserInfo, CsdnTripletDayInfo csdnTripletDayInfo) { final Integer commentStatus = csdnUserInfo.getCommentStatus(); if (CommentStatus.HAVE_ALREADY_COMMENT.getCode().equals(commentStatus) || CommentStatus.COMMENT_IS_FULL.getCode().equals(commentStatus) @@ -100,6 +94,7 @@ public class CsdnCommentServiceImpl implements CsdnCommentService { if (code == 200) { log.info("文章{}评论成功", articleId); csdnUserInfo.setCommentStatus(CommentStatus.COMMENT_SUCCESSFUL.getCode()); + csdnTripletDayInfo.setCommentNum(csdnTripletDayInfo.getCommentNum() + 1); } else if (code == 400 && StringUtils.equals(message, "您已达到当日发送上限,请明天尝试!")) { log.info(message); csdnUserInfo.setCommentStatus(CommentStatus.COMMENT_IS_FULL.getCode()); @@ -121,73 +116,14 @@ public class CsdnCommentServiceImpl implements CsdnCommentService { return true; } - @Override - public void commentSelf() { - List list = csdnArticleService.getArticles(selfUserName); - if (list == null) { - return; - } - for (BusinessInfoResponse.ArticleData.Article article : list) { - final String type = article.getType(); - if (!StringUtils.equals("blog", type)) { - continue; - } - final String urlInfo = article.getUrl(); - String articleId = urlInfo.substring(urlInfo.lastIndexOf("/") + 1); - String url = commentListUrl + articleId; - HttpResponse response = HttpUtil.createPost(url) - .header("Cookie", csdnCookie) - .form("page", 1) - .form("size", 100) - .execute(); - final String body = response.body(); - ObjectMapper objectMapper = new ObjectMapper(); - CommentListResponse articleInfo; - try { - articleInfo = objectMapper.readValue(body, CommentListResponse.class); - final CommentListResponse.DataInfo data = articleInfo.getData(); - final List otherCommentList = data.getList(); - if (CollectionUtil.isNotEmpty(otherCommentList)) { - for (CommentListResponse.Comment oneComment : otherCommentList) { - final CommentListResponse.Info info = oneComment.getInfo(); - final String userName = info.getUserName(); - final Integer commentId = info.getCommentId(); - if (!StringUtils.equals(userName, selfUserName)) { - final List sub = oneComment.getSub(); - boolean flag = false; - if (CollectionUtil.isNotEmpty(sub)) { - for (CommentListResponse.SubComment subComment : sub) { - //如果没有自己的评论,需要评论 - final String subUserName = subComment.getUserName(); - if (StringUtils.equals(subUserName, selfUserName)) { - flag = true; - } - } - } - if (CollectionUtil.isEmpty(sub) || !flag) { - //需要评论 - int start = -1; - int end = selfReply.length; - int temp_count = (int) (Math.floor(Math.random() * (start - end + 1)) + end); - CommentResponse reply = this.dealComment(articleId, selfReply[temp_count], commentId); - log.info(reply.toString()); - } - } - } - } - } catch (JsonProcessingException e) { - e.printStackTrace(); - } - } - } - /** * 评论文章 * * @param articleId * @return */ - private CommentResponse dealComment(String articleId, String commentInfo, Integer commentId) { + @Override + public CommentResponse dealComment(String articleId, String commentInfo, Integer commentId) { HttpResponse response; if (Objects.nonNull(commentId)) { response = HttpUtil.createPost(commentUrl) diff --git a/src/main/java/com/kwan/springbootkwan/service/impl/CsdnLikeServiceImpl.java b/src/main/java/com/kwan/springbootkwan/service/impl/CsdnLikeServiceImpl.java index 0f05d0d0e956698cb1210e9b0b1214361f574883..39195203a22ca00d3b95d2c8b0f179f371e4fb11 100644 --- a/src/main/java/com/kwan/springbootkwan/service/impl/CsdnLikeServiceImpl.java +++ b/src/main/java/com/kwan/springbootkwan/service/impl/CsdnLikeServiceImpl.java @@ -4,6 +4,7 @@ import cn.hutool.http.HttpResponse; import cn.hutool.http.HttpUtil; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; +import com.kwan.springbootkwan.entity.CsdnTripletDayInfo; import com.kwan.springbootkwan.entity.CsdnUserInfo; import com.kwan.springbootkwan.entity.resp.LikeResponse; import com.kwan.springbootkwan.enums.LikeStatus; @@ -46,7 +47,7 @@ public class CsdnLikeServiceImpl implements CsdnLikeService { } @Override - public Boolean like(String articleId, CsdnUserInfo csdnUserInfo) { + public Boolean like(String articleId, CsdnUserInfo csdnUserInfo, CsdnTripletDayInfo csdnTripletDayInfo) { final Integer likeStatus = csdnUserInfo.getLikeStatus(); if (LikeStatus.HAVE_ALREADY_LIKED.getCode().equals(likeStatus) || LikeStatus.LIKE_IS_FULL.getCode().equals(likeStatus)) { return true; @@ -65,6 +66,7 @@ public class CsdnLikeServiceImpl implements CsdnLikeService { if (status) { log.info("文章{}点赞成功", articleId); csdnUserInfo.setLikeStatus(LikeStatus.LIKE_SUCCESSFUL.getCode()); + csdnTripletDayInfo.setLikeNum(csdnTripletDayInfo.getLikeNum() + 1); } else { log.info("文章{}点赞取消", articleId); csdnUserInfo.setLikeStatus(LikeStatus.CANCEL_LIKES.getCode()); 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 fb53145b4db25424c1cde6de5b768094e54a8e5e..6faf99f53787e822c6c0ee6885d8c2c727f63224 100644 --- a/src/main/java/com/kwan/springbootkwan/service/impl/CsdnServiceImpl.java +++ b/src/main/java/com/kwan/springbootkwan/service/impl/CsdnServiceImpl.java @@ -2,6 +2,7 @@ package com.kwan.springbootkwan.service.impl; import cn.hutool.core.collection.CollectionUtil; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.kwan.springbootkwan.entity.CsdnTripletDayInfo; import com.kwan.springbootkwan.entity.CsdnUserInfo; import com.kwan.springbootkwan.entity.resp.BusinessInfoResponse; import com.kwan.springbootkwan.enums.CommentStatus; @@ -10,6 +11,7 @@ import com.kwan.springbootkwan.service.CsdnCollectService; import com.kwan.springbootkwan.service.CsdnCommentService; import com.kwan.springbootkwan.service.CsdnLikeService; import com.kwan.springbootkwan.service.CsdnService; +import com.kwan.springbootkwan.service.CsdnTripletDayInfoService; import com.kwan.springbootkwan.service.CsdnUserInfoService; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; @@ -27,7 +29,8 @@ public class CsdnServiceImpl implements CsdnService { @Value("${csdn.num_of_articles_per_person}") private Integer numOfArticlesPerPerson; - + @Autowired + private CsdnLikeService csdnLikeService; @Autowired private CsdnUserInfoService csdnUserInfoService; @Autowired @@ -37,54 +40,54 @@ public class CsdnServiceImpl implements CsdnService { @Autowired private CsdnCommentService csdnCommentService; @Autowired - private CsdnLikeService csdnLikeService; + private CsdnTripletDayInfoService csdnTripletDayInfoService; @Override public void singleArticle(CsdnUserInfo csdnUserInfo) { final String username = csdnUserInfo.getUserName(); List list = csdnArticleService.getArticles(username); - if (list == null) { - return; - } - final int size = list.size(); - numOfArticlesPerPerson = size < numOfArticlesPerPerson ? size : numOfArticlesPerPerson; - 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)) { - csdnUserInfo.setArticleType(type); - continue; - } - final String urlInfo = article.getUrl(); - String articleId = urlInfo.substring(urlInfo.lastIndexOf("/") + 1); - //点赞 - final Boolean isLike = csdnLikeService.isLike(articleId, csdnUserInfo); - if (!isLike) { - csdnLikeService.like(articleId, csdnUserInfo); - } - //查看已经评论总数量 - QueryWrapper wrapper = new QueryWrapper<>(); - wrapper.eq("is_delete", 0); - wrapper.eq("comment_status", 9); - final List comments = csdnUserInfoService.list(wrapper); - if (CollectionUtil.isEmpty(comments) || comments.size() < 49) { - //评论 - final Boolean comment = csdnCommentService.isComment(article, csdnUserInfo); - if (!comment) { - csdnCommentService.comment(articleId, csdnUserInfo); + if (CollectionUtil.isNotEmpty(list)) { + final int size = list.size(); + numOfArticlesPerPerson = size < numOfArticlesPerPerson ? size : numOfArticlesPerPerson; + 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)) { + csdnUserInfo.setArticleType(type); + csdnUserInfo.setUpdateTime(new Date()); + csdnUserInfoService.updateById(csdnUserInfo); + continue; } - } - if (CollectionUtil.isNotEmpty(comments) && comments.size() >= 49) { - csdnUserInfo.setCommentStatus(CommentStatus.COMMENT_NUM_49.getCode()); - } - //收藏 - final Boolean collect = csdnCollectService.isCollect(articleId, csdnUserInfo); - if (!collect) { - csdnCollectService.collect(article, csdnUserInfo); + //获取每日三连总信息 + final CsdnTripletDayInfo csdnTripletDayInfo = csdnTripletDayInfoService.todayInfo(); + final String urlInfo = article.getUrl(); + String articleId = urlInfo.substring(urlInfo.lastIndexOf("/") + 1); + //点赞 + final Boolean isLike = csdnLikeService.isLike(articleId, csdnUserInfo); + if (!isLike) { + csdnLikeService.like(articleId, csdnUserInfo, csdnTripletDayInfo); + } + final Integer commentNum = csdnTripletDayInfo.getCommentNum(); + if (commentNum < 49) { + //评论 + final Boolean comment = csdnCommentService.isComment(article, csdnUserInfo); + if (!comment) { + csdnCommentService.comment(articleId, csdnUserInfo, csdnTripletDayInfo); + } + } else { + csdnUserInfo.setCommentStatus(CommentStatus.COMMENT_NUM_49.getCode()); + } + //收藏 + final Boolean collect = csdnCollectService.isCollect(articleId, csdnUserInfo); + if (!collect) { + csdnCollectService.collect(article, csdnUserInfo, csdnTripletDayInfo); + } + csdnTripletDayInfo.setUpdateTime(new Date()); + csdnTripletDayInfoService.updateById(csdnTripletDayInfo); + csdnUserInfo.setUpdateTime(new Date()); + csdnUserInfoService.updateById(csdnUserInfo); } } - csdnUserInfo.setUpdateTime(new Date()); - csdnUserInfoService.updateById(csdnUserInfo); } diff --git a/src/main/java/com/kwan/springbootkwan/service/impl/CsdnTripletDayInfoServiceImpl.java b/src/main/java/com/kwan/springbootkwan/service/impl/CsdnTripletDayInfoServiceImpl.java new file mode 100644 index 0000000000000000000000000000000000000000..37bf4207218a77d2022c40fff92006d9b17b4dcf --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/service/impl/CsdnTripletDayInfoServiceImpl.java @@ -0,0 +1,43 @@ +package com.kwan.springbootkwan.service.impl; + +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.kwan.springbootkwan.entity.CsdnTripletDayInfo; +import com.kwan.springbootkwan.mapper.CsdnTripletDayInfoMapper; +import com.kwan.springbootkwan.service.CsdnTripletDayInfoService; +import org.springframework.stereotype.Service; + +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Objects; + +/** + * 每日三连监控(CsdnTripletDayInfo)表服务实现类 + * + * @author makejava + * @since 2023-10-26 20:55:31 + */ +@Service("csdnTripletDayInfoService") +public class CsdnTripletDayInfoServiceImpl extends ServiceImpl implements CsdnTripletDayInfoService { + @Override + public CsdnTripletDayInfo todayInfo() { + Date currentDate = new Date(); + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + String formattedDate = sdf.format(currentDate); + QueryWrapper wrapper = new QueryWrapper<>(); + wrapper.eq("is_delete", 0); + wrapper.eq("triplet_date", formattedDate); + final CsdnTripletDayInfo one = this.getOne(wrapper); + if (Objects.isNull(one)) { + CsdnTripletDayInfo csdnTripletDayInfo = new CsdnTripletDayInfo(); + csdnTripletDayInfo.setTripletDate(new Date()); + csdnTripletDayInfo.setLikeNum(0); + csdnTripletDayInfo.setCollectNum(0); + csdnTripletDayInfo.setCommentNum(0); + csdnTripletDayInfo.setUpdateTime(new Date()); + this.save(csdnTripletDayInfo); + } + return one; + } +} + diff --git a/src/main/java/com/kwan/springbootkwan/service/impl/CsdnUserInfoServiceImpl.java b/src/main/java/com/kwan/springbootkwan/service/impl/CsdnUserInfoServiceImpl.java index 77c95da5291b0e41e5f9a62d23a38ebbde28f3d0..2777873d4dc13cb0547019489fd7bb9aafd0d55a 100644 --- a/src/main/java/com/kwan/springbootkwan/service/impl/CsdnUserInfoServiceImpl.java +++ b/src/main/java/com/kwan/springbootkwan/service/impl/CsdnUserInfoServiceImpl.java @@ -1,11 +1,23 @@ package com.kwan.springbootkwan.service.impl; +import cn.hutool.core.collection.CollectionUtil; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.kwan.springbootkwan.entity.CsdnUserInfo; +import com.kwan.springbootkwan.entity.resp.BusinessInfoResponse; +import com.kwan.springbootkwan.enums.CollectStatus; +import com.kwan.springbootkwan.enums.CommentStatus; +import com.kwan.springbootkwan.enums.LikeStatus; import com.kwan.springbootkwan.mapper.CsdnUserInfoMapper; +import com.kwan.springbootkwan.service.CsdnArticleService; +import com.kwan.springbootkwan.service.CsdnCollectService; import com.kwan.springbootkwan.service.CsdnUserInfoService; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.List; + /** * csdn用户信息(CsdnUserInfo)表服务实现类 * @@ -15,5 +27,38 @@ import org.springframework.stereotype.Service; @Service("csdnUserInfoService") public class CsdnUserInfoServiceImpl extends ServiceImpl implements CsdnUserInfoService { + + @Autowired + private CsdnCollectService csdnCollectService; + @Autowired + private CsdnArticleService csdnArticleService; + + @Override + public void resetCurrentStatus() { + QueryWrapper wrapper = new QueryWrapper<>(); + wrapper.eq("is_delete", 0); + final List list = this.list(wrapper); + if (CollectionUtil.isNotEmpty(list)) { + for (CsdnUserInfo csdnUserInfo : list) { + final String userName = csdnUserInfo.getUserName(); + final List articles = csdnArticleService.getArticles(userName); + if (CollectionUtil.isNotEmpty(articles)) { + final BusinessInfoResponse.ArticleData.Article article = articles.get(0); + final String type = article.getType(); + if (StringUtils.equals(type, "blog")) { + final String urlInfo = article.getUrl(); + String articleId = urlInfo.substring(urlInfo.lastIndexOf("/") + 1); + final Boolean collect = csdnCollectService.isCollect(articleId, csdnUserInfo); + if (!collect) { + csdnUserInfo.setLikeStatus(LikeStatus.UN_PROCESSED.getCode()); + csdnUserInfo.setCollectStatus(CollectStatus.UN_PROCESSED.getCode()); + csdnUserInfo.setCommentStatus(CommentStatus.UN_PROCESSED.getCode()); + this.updateById(csdnUserInfo); + } + } + } + } + } + } }