diff --git a/src/main/java/com/kwan/springbootkwan/controller/CsdnController.java b/src/main/java/com/kwan/springbootkwan/controller/CsdnController.java index 59eadb68fc2c2d3f86f7954889aadca1e3dcaa35..83bc5a858f251232c857eabd1038e206e1edf7e0 100644 --- a/src/main/java/com/kwan/springbootkwan/controller/CsdnController.java +++ b/src/main/java/com/kwan/springbootkwan/controller/CsdnController.java @@ -1,8 +1,11 @@ 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.service.CsdnLikeService; import com.kwan.springbootkwan.service.CsdnService; +import com.kwan.springbootkwan.service.CsdnUserInfoService; import lombok.extern.slf4j.Slf4j; import org.apache.ibatis.annotations.Param; import org.springframework.beans.factory.annotation.Autowired; @@ -10,6 +13,8 @@ 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; + @Slf4j @RestController @RequestMapping("/csdn") @@ -18,21 +23,18 @@ public class CsdnController { @Autowired private CsdnService csdnService; @Autowired - private CsdnLikeService csdnLikeService; - - @GetMapping("/isLike") - public Result isLike(@Param("articleId") String articleId, @Param("userName") String userName) { - return Result.ok(csdnLikeService.isLike(articleId, userName)); - } - - @GetMapping("/singleLike") - public Result singleLike(@Param("articleId") String articleId) { - return Result.ok(csdnLikeService.like(articleId)); - } + private CsdnUserInfoService csdnUserInfoService; @GetMapping("/singleTriplet") public Result singleTriplet(@Param("username") String username) { - csdnService.singleArticle(username); + QueryWrapper wrapper = new QueryWrapper<>(); + wrapper.eq("is_delete", 0); + wrapper.eq("user_name", username); + final List list = csdnUserInfoService.list(wrapper); + if (CollectionUtil.isNotEmpty(list)) { + final CsdnUserInfo csdnUserInfo = list.get(0); + csdnService.singleArticle(csdnUserInfo); + } return Result.ok("单人三连完成"); } diff --git a/src/main/java/com/kwan/springbootkwan/entity/CsdnUserInfo.java b/src/main/java/com/kwan/springbootkwan/entity/CsdnUserInfo.java index 98ffb1fcff5db9d557cff020f855651f400c01b9..c4a64638b995c49e0510cb6532ffc7f09787faa4 100644 --- a/src/main/java/com/kwan/springbootkwan/entity/CsdnUserInfo.java +++ b/src/main/java/com/kwan/springbootkwan/entity/CsdnUserInfo.java @@ -1,58 +1,52 @@ package com.kwan.springbootkwan.entity; +import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.extension.activerecord.Model; +import lombok.Data; import java.util.Date; -/** - * csdn用户信息(CsdnUserInfo)表实体类 - * - * @author makejava - * @since 2023-10-23 16:03:14 - */ -@SuppressWarnings("serial") +@Data +@TableName("csdn_user_info") public class CsdnUserInfo extends Model { - //主键id + /** + * 主键id + */ private Integer id; - + /** + * 用户code + */ private String userName; - //创建时间 + /** + * CSDN用户名称 + */ + private String nickName; + /** + * 点赞状态 + */ + private Integer likeStatus; + /** + * 收藏状态 + */ + private Integer collectStatus; + /** + * 评论状态 + */ + private Integer commentStatus; + /** + * 用户权重 + */ + private Integer userWeight; + /** + * 文章类型 + */ + private String articleType; + /** + * 创建时间 + */ private Date createTime; - //逻辑删除,0未删除,1已删除 + /** + * 逻辑删除,0未删除,1已删除 + */ private Integer isDelete; - - - public Integer getId() { - return id; - } - - public void setId(Integer id) { - this.id = id; - } - - public String getUserName() { - return userName; - } - - public void setUserName(String userName) { - this.userName = userName; - } - - public Date getCreateTime() { - return createTime; - } - - public void setCreateTime(Date createTime) { - this.createTime = createTime; - } - - public Integer getIsDelete() { - return isDelete; - } - - public void setIsDelete(Integer isDelete) { - this.isDelete = isDelete; - } - -} - +} \ No newline at end of file diff --git a/src/main/java/com/kwan/springbootkwan/enums/CollectStatus.java b/src/main/java/com/kwan/springbootkwan/enums/CollectStatus.java new file mode 100644 index 0000000000000000000000000000000000000000..9df023bea088f3eca854eb3bede7a1c9f96523c0 --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/enums/CollectStatus.java @@ -0,0 +1,37 @@ +package com.kwan.springbootkwan.enums; + +import lombok.Getter; +import lombok.ToString; + +@Getter +@ToString +public enum CollectStatus { + /** + * 未处理 + */ + UN_PROCESSED(0, "未处理"), + /** + * 已经收藏过 + */ + HAVE_ALREADY_COLLECT(1, "已经收藏过"), + /** + * 收藏已满 + */ + COLLECT_IS_FULL(2, "收藏已满"), + /** + * 参数缺失 + */ + MISSING_PARAMETER(3, "参数缺失"), + /** + * 收藏成功 + */ + COLLECT_SUCCESSFUL(9, "收藏成功"); + + private Integer code; + private String name; + + CollectStatus(Integer code, String name) { + this.code = code; + this.name = name; + } +} \ 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 new file mode 100644 index 0000000000000000000000000000000000000000..47e525dbed49b2c682f26a93bc6992f985236f24 --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/enums/CommentStatus.java @@ -0,0 +1,41 @@ +package com.kwan.springbootkwan.enums; + +import lombok.Getter; +import lombok.ToString; + +@Getter +@ToString +public enum CommentStatus { + /** + * 未处理 + */ + UN_PROCESSED(0, "未处理"), + /** + * 已经评论过 + */ + HAVE_ALREADY_COMMENT(1, "已经评论过"), + /** + * 评论已满 + */ + COMMENT_IS_FULL(2, "评论已满"), + /** + * 禁言 + */ + RESTRICTED_COMMENTS(3, "禁言"), + /** + * 评论太快 + */ + COMMENT_TOO_FAST(4, "评论太快"), + /** + * 评论成功 + */ + COMMENT_SUCCESSFUL(9, "评论成功"); + + private Integer code; + private String name; + + CommentStatus(Integer code, String name) { + this.code = code; + this.name = name; + } +} \ No newline at end of file diff --git a/src/main/java/com/kwan/springbootkwan/enums/LikeStatus.java b/src/main/java/com/kwan/springbootkwan/enums/LikeStatus.java new file mode 100644 index 0000000000000000000000000000000000000000..c033ce5b144cfca0973d0ebe5ad7495ed241dbd0 --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/enums/LikeStatus.java @@ -0,0 +1,37 @@ +package com.kwan.springbootkwan.enums; + +import lombok.Getter; +import lombok.ToString; + +@Getter +@ToString +public enum LikeStatus { + /** + * 未处理 + */ + UN_PROCESSED(0, "未处理"), + /** + * 已经点过赞 + */ + HAVE_ALREADY_LIKED(1, "已经点过赞"), + /** + * 点赞已满 + */ + LIKE_IS_FULL(2, "点赞已满"), + /** + * 取消点赞 + */ + CANCEL_LIKES(3, "取消点赞"), + /** + * 点赞成功 + */ + LIKE_SUCCESSFUL(9, "点赞成功"); + + private Integer code; + private String name; + + LikeStatus(Integer code, String name) { + this.code = code; + this.name = name; + } +} \ No newline at end of file diff --git a/src/main/java/com/kwan/springbootkwan/schedule/CsdnSchedule.java b/src/main/java/com/kwan/springbootkwan/schedule/CsdnSchedule.java index cc7b28f1df17a89bf2999a7e56239b749d12f675..61d8a43a9014f4dfffcbe28ad17e22c540eb9930 100644 --- a/src/main/java/com/kwan/springbootkwan/schedule/CsdnSchedule.java +++ b/src/main/java/com/kwan/springbootkwan/schedule/CsdnSchedule.java @@ -1,12 +1,20 @@ 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.LikeStatus; import com.kwan.springbootkwan.service.CsdnService; +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 @@ -14,12 +22,32 @@ public class CsdnSchedule { @Autowired private CsdnService csdnService; + @Autowired + private CsdnUserInfoService csdnUserInfoService; @Scheduled(cron = "0 0 8,10,12,14,16,18,20 * * ?") public void execute() { - log.info("Scheduled task is running ... ..."); + log.info("execute task is running ... ..."); csdnService.multiTriplet(); - log.info("Scheduled task is finish ... ..."); + log.info("execute task is finish ... ..."); + } + + @Scheduled(cron = "0 0 3 * * ?") + public void executeInit() { + log.info("executeInit task is running ... ..."); + QueryWrapper wrapper = new QueryWrapper<>(); + wrapper.eq("is_delete", 0); + wrapper.orderByAsc("rand()"); + 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.UN_PROCESSED.getCode()); + csdnUserInfoService.updateById(csdnUserInfo); + } + } + log.info("executeInit task is finish ... ..."); } } diff --git a/src/main/java/com/kwan/springbootkwan/service/CsdnCollectService.java b/src/main/java/com/kwan/springbootkwan/service/CsdnCollectService.java index 605582b449812bd1867cab0e7b0f58ae8ca544d6..7355ae88854dd77520dad6f28728b363623eab46 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.CsdnUserInfo; import com.kwan.springbootkwan.entity.resp.BusinessInfoResponse; /** @@ -17,12 +18,12 @@ public interface CsdnCollectService { * * @return */ - Boolean isCollect(String articleId); + Boolean isCollect(String articleId, CsdnUserInfo csdnUserInfo); /** * 点赞和取消点赞接口,true,点过,false,没有点过 * * @return */ - Boolean collect(BusinessInfoResponse.ArticleData.Article article, String username); + Boolean collect(BusinessInfoResponse.ArticleData.Article article, CsdnUserInfo csdnUserInfo); } \ 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 468623e4ba8280ee5284dba16143b9c15e2a7915..440f3d446d20af411c1a6f83e6ec40ab6b1d712c 100644 --- a/src/main/java/com/kwan/springbootkwan/service/CsdnCommentService.java +++ b/src/main/java/com/kwan/springbootkwan/service/CsdnCommentService.java @@ -1,5 +1,6 @@ package com.kwan.springbootkwan.service; +import com.kwan.springbootkwan.entity.CsdnUserInfo; import com.kwan.springbootkwan.entity.resp.BusinessInfoResponse; /** @@ -16,12 +17,12 @@ public interface CsdnCommentService { * * @return */ - Boolean isComment(BusinessInfoResponse.ArticleData.Article article); + Boolean isComment(BusinessInfoResponse.ArticleData.Article article, CsdnUserInfo csdnUserInfo); /** * 点赞和取消点赞接口,true,点过,false,没有点过 * * @return */ - Boolean comment(String articleId); + Boolean comment(String articleId, CsdnUserInfo csdnUserInfo); } \ 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 907ff5306beb2a8765091c0927cde494867ea539..ba3c1cedd3eaa1f8ba676f8f36bfa2f16ae4a2d3 100644 --- a/src/main/java/com/kwan/springbootkwan/service/CsdnLikeService.java +++ b/src/main/java/com/kwan/springbootkwan/service/CsdnLikeService.java @@ -1,6 +1,8 @@ package com.kwan.springbootkwan.service; +import com.kwan.springbootkwan.entity.CsdnUserInfo; + /** * 点赞 * @@ -16,12 +18,12 @@ public interface CsdnLikeService { * * @return */ - Boolean isLike(String articleId, String userName); + Boolean isLike(String articleId, CsdnUserInfo csdnUserInfo); /** * 点赞和取消点赞接口,true,点过,false,没有点过 * * @return */ - Boolean like(String articleId); + Boolean like(String articleId,CsdnUserInfo csdnUserInfo); } \ No newline at end of file diff --git a/src/main/java/com/kwan/springbootkwan/service/CsdnService.java b/src/main/java/com/kwan/springbootkwan/service/CsdnService.java index 88930e4dd8e2f14fa2c87d742bccfa1c1fb469df..675f4271e726fc079d04cd6ef454f598baa8c291 100644 --- a/src/main/java/com/kwan/springbootkwan/service/CsdnService.java +++ b/src/main/java/com/kwan/springbootkwan/service/CsdnService.java @@ -1,5 +1,7 @@ package com.kwan.springbootkwan.service; +import com.kwan.springbootkwan.entity.CsdnUserInfo; + /** * csdn博客自动化 * @@ -14,7 +16,7 @@ public interface CsdnService { * * @return */ - void singleArticle(String username); + void singleArticle(CsdnUserInfo csdnUserInfo); /** * 多人三连 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 1835626e74c7bc79a7842d8fb20d1e6b97c0394b..0314a28716aa76a65dc1551318e43524b7af18b8 100644 --- a/src/main/java/com/kwan/springbootkwan/service/impl/CsdnCollectServiceImpl.java +++ b/src/main/java/com/kwan/springbootkwan/service/impl/CsdnCollectServiceImpl.java @@ -4,16 +4,19 @@ 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.CsdnUserInfo; import com.kwan.springbootkwan.entity.resp.BusinessInfoResponse; import com.kwan.springbootkwan.entity.resp.CollectInfoQuery; import com.kwan.springbootkwan.entity.resp.CollectResponse; import com.kwan.springbootkwan.entity.resp.IsCollectResponse; +import com.kwan.springbootkwan.enums.CollectStatus; import com.kwan.springbootkwan.service.CsdnCollectService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import java.util.ArrayList; +import java.util.List; @Slf4j @Service @@ -30,15 +33,10 @@ public class CsdnCollectServiceImpl implements CsdnCollectService { @Value("${csdn.url.add_collect_url}") private String addCollectUrl; - /** - * 收藏满了 - */ - private static boolean COLLECT_IS_FULL = false; - - @Override - public Boolean isCollect(String articleId) { - if (COLLECT_IS_FULL) { + public Boolean isCollect(String articleId, CsdnUserInfo csdnUserInfo) { + final Integer collectStatus = csdnUserInfo.getCollectStatus(); + if (CollectStatus.HAVE_ALREADY_COLLECT.getCode().equals(collectStatus) || CollectStatus.COLLECT_IS_FULL.getCode().equals(collectStatus)) { return true; } HttpResponse response = HttpUtil.createGet(isCollectUrl) @@ -54,8 +52,10 @@ public class CsdnCollectServiceImpl implements CsdnCollectService { final boolean status = data.status; if (status) { log.info("文章{}已经收藏过", articleId); + csdnUserInfo.setCommentStatus(CollectStatus.HAVE_ALREADY_COLLECT.getCode()); } else { log.info("文章{}未收藏", articleId); + csdnUserInfo.setCommentStatus(CollectStatus.UN_PROCESSED.getCode()); } return status; } @@ -66,8 +66,10 @@ public class CsdnCollectServiceImpl implements CsdnCollectService { } @Override - public Boolean collect(BusinessInfoResponse.ArticleData.Article article, String username) { - if (COLLECT_IS_FULL) { + public Boolean collect(BusinessInfoResponse.ArticleData.Article article, CsdnUserInfo csdnUserInfo) { + 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)) { return true; } final String urlInfo = article.getUrl(); @@ -77,10 +79,10 @@ public class CsdnCollectServiceImpl implements CsdnCollectService { CollectInfoQuery collectInfoQuery = new CollectInfoQuery(); collectInfoQuery.setSourceId(Integer.valueOf(articleId)); collectInfoQuery.setFromType("PC"); - collectInfoQuery.setAuthor(username); + collectInfoQuery.setAuthor(userName); collectInfoQuery.setDescription(article.getDescription()); collectInfoQuery.setSource("blog"); - ArrayList list = new ArrayList<>(); + List list = new ArrayList<>(); list.add(selfFolderId); collectInfoQuery.setFolderIdList(list); collectInfoQuery.setTitle(article.getTitle()); @@ -101,11 +103,13 @@ public class CsdnCollectServiceImpl implements CsdnCollectService { final Long code = collectResponse.code; if (code.equals(200)) { log.info("文章{}收藏成功", articleId); + csdnUserInfo.setCollectStatus(CollectStatus.COLLECT_SUCCESSFUL.getCode()); } else if (code.equals(400000101)) { log.info("收藏文章{}参数缺失", articleId); + csdnUserInfo.setCollectStatus(CollectStatus.MISSING_PARAMETER.getCode()); } else if (code.equals(400)) { - COLLECT_IS_FULL = true; log.info("今日收藏次数已达上限!"); + csdnUserInfo.setCollectStatus(CollectStatus.COLLECT_IS_FULL.getCode()); } return true; } 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 e795f4061ca26c480d1e7d095eecc91e553c3efd..6e8de2bb8a4cb98bbf8dd00188fb6edaeef21a0e 100644 --- a/src/main/java/com/kwan/springbootkwan/service/impl/CsdnCommentServiceImpl.java +++ b/src/main/java/com/kwan/springbootkwan/service/impl/CsdnCommentServiceImpl.java @@ -5,9 +5,11 @@ 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.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.CsdnCommentService; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; @@ -36,21 +38,22 @@ public class CsdnCommentServiceImpl implements CsdnCommentService { private static boolean COMMENT_IS_FULL = false; @Override - public Boolean isComment(BusinessInfoResponse.ArticleData.Article article) { - if (COMMENT_IS_FULL) { + public Boolean isComment(BusinessInfoResponse.ArticleData.Article article, CsdnUserInfo csdnUserInfo) { + final Integer commentStatus = csdnUserInfo.getCommentStatus(); + if (CommentStatus.HAVE_ALREADY_COMMENT.getCode().equals(commentStatus) + || CommentStatus.COMMENT_IS_FULL.getCode().equals(commentStatus) + || CommentStatus.RESTRICTED_COMMENTS.getCode().equals(commentStatus)) { return true; } final String urlInfo = article.getUrl(); String articleId = urlInfo.substring(urlInfo.lastIndexOf("/") + 1); String url = commentListUrl + articleId; - // 使用Hutool发送GET请求 HttpResponse response = HttpUtil.createPost(url) .header("Cookie", csdnCookie) .form("page", 1) .form("size", 50) .form("fold", "unfold") .execute(); - // 打印响应结果 final String body = response.body(); ObjectMapper objectMapper = new ObjectMapper(); CommentListResponse articleInfo; @@ -63,8 +66,8 @@ public class CsdnCommentServiceImpl implements CsdnCommentService { final CommentListResponse.Info info = comment.getInfo(); final String userName = info.getUserName(); if (StringUtils.equals(userName, selfUserName)) { - //评论过 log.info("文章{}已经评论过", articleId); + csdnUserInfo.setCommentStatus(CommentStatus.HAVE_ALREADY_COMMENT.getCode()); return true; } } @@ -72,12 +75,16 @@ public class CsdnCommentServiceImpl implements CsdnCommentService { } catch (JsonProcessingException e) { e.printStackTrace(); } + csdnUserInfo.setCommentStatus(CommentStatus.UN_PROCESSED.getCode()); return false; } @Override - public Boolean comment(String articleId) { - if (COMMENT_IS_FULL) { + public Boolean comment(String articleId, CsdnUserInfo csdnUserInfo) { + final Integer commentStatus = csdnUserInfo.getCommentStatus(); + if (CommentStatus.HAVE_ALREADY_COMMENT.getCode().equals(commentStatus) + || CommentStatus.COMMENT_IS_FULL.getCode().equals(commentStatus) + || CommentStatus.RESTRICTED_COMMENTS.getCode().equals(commentStatus)) { return true; } //评论 @@ -86,11 +93,16 @@ public class CsdnCommentServiceImpl implements CsdnCommentService { final String message = comment.getMessage(); if (code == 200) { log.info("文章{}评论成功", articleId); + csdnUserInfo.setCommentStatus(CommentStatus.COMMENT_SUCCESSFUL.getCode()); } else if (code == 400 && StringUtils.equals(message, "您已达到当日发送上限,请明天尝试!")) { log.info(message); - COMMENT_IS_FULL = true; + csdnUserInfo.setCommentStatus(CommentStatus.COMMENT_IS_FULL.getCode()); + } else if (code == 400 && message.contains("因存在恶意评论嫌疑,您的账号已被禁言")) { + log.info("因存在恶意评论嫌疑,您的账号已被禁言"); + csdnUserInfo.setCommentStatus(CommentStatus.RESTRICTED_COMMENTS.getCode()); } else { log.info("您评论太快了,请休息一下!"); + csdnUserInfo.setCommentStatus(CommentStatus.COMMENT_TOO_FAST.getCode()); } return true; } 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 0f8b006577a45bfb78cc7d862ef12e37a901d881..0f05d0d0e956698cb1210e9b0b1214361f574883 100644 --- a/src/main/java/com/kwan/springbootkwan/service/impl/CsdnLikeServiceImpl.java +++ b/src/main/java/com/kwan/springbootkwan/service/impl/CsdnLikeServiceImpl.java @@ -4,7 +4,9 @@ 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.CsdnUserInfo; import com.kwan.springbootkwan.entity.resp.LikeResponse; +import com.kwan.springbootkwan.enums.LikeStatus; import com.kwan.springbootkwan.service.CsdnLikeService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; @@ -18,14 +20,12 @@ public class CsdnLikeServiceImpl implements CsdnLikeService { private String csdnCookie; @Value("${csdn.url.like_url}") private String url; - /** - * 点赞满了 - */ - private static boolean LIKE_IS_FULL = false; @Override - public Boolean isLike(String articleId, String userName) { - if (LIKE_IS_FULL) { + public Boolean isLike(String articleId, CsdnUserInfo csdnUserInfo) { + final String userName = csdnUserInfo.getUserName(); + final Integer likeStatus = csdnUserInfo.getLikeStatus(); + if (LikeStatus.HAVE_ALREADY_LIKED.getCode().equals(likeStatus) || LikeStatus.LIKE_IS_FULL.getCode().equals(likeStatus)) { return true; } String url = "https://blog.csdn.net/" + userName + "/article/details/" + articleId; @@ -36,16 +36,19 @@ public class CsdnLikeServiceImpl implements CsdnLikeService { final String body = response.body(); if (body.contains("isLikeStatus = true")) { log.info("文章{}已经点过赞", articleId); + csdnUserInfo.setLikeStatus(LikeStatus.HAVE_ALREADY_LIKED.getCode()); return true; } else { log.info("文章{}未点过赞", articleId); + csdnUserInfo.setLikeStatus(LikeStatus.UN_PROCESSED.getCode()); return false; } } @Override - public Boolean like(String articleId) { - if (LIKE_IS_FULL) { + public Boolean like(String articleId, CsdnUserInfo csdnUserInfo) { + final Integer likeStatus = csdnUserInfo.getLikeStatus(); + if (LikeStatus.HAVE_ALREADY_LIKED.getCode().equals(likeStatus) || LikeStatus.LIKE_IS_FULL.getCode().equals(likeStatus)) { return true; } HttpResponse response = HttpUtil.createPost(url) @@ -61,13 +64,15 @@ public class CsdnLikeServiceImpl implements CsdnLikeService { final boolean status = data.status; if (status) { log.info("文章{}点赞成功", articleId); + csdnUserInfo.setLikeStatus(LikeStatus.LIKE_SUCCESSFUL.getCode()); } else { log.info("文章{}点赞取消", articleId); + csdnUserInfo.setLikeStatus(LikeStatus.CANCEL_LIKES.getCode()); } return status; } else if (code == 400) { - LIKE_IS_FULL = true; log.info("今日点赞次数已达上限!"); + csdnUserInfo.setLikeStatus(LikeStatus.LIKE_IS_FULL.getCode()); } } catch (JsonProcessingException e) { e.printStackTrace(); 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 58d31fdad82df51d7900f47f23b1b0ccf256f23b..fadd67175371026000708dc6c0cb5cd10a31b504 100644 --- a/src/main/java/com/kwan/springbootkwan/service/impl/CsdnServiceImpl.java +++ b/src/main/java/com/kwan/springbootkwan/service/impl/CsdnServiceImpl.java @@ -42,7 +42,8 @@ public class CsdnServiceImpl implements CsdnService { private CsdnLikeService csdnLikeService; @Override - public void singleArticle(String username) { + public void singleArticle(CsdnUserInfo csdnUserInfo) { + final String username = csdnUserInfo.getUserName(); List list = this.getArticles(username); if (list == null) { return; @@ -51,26 +52,28 @@ public class CsdnServiceImpl implements CsdnService { 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, username); + final Boolean isLike = csdnLikeService.isLike(articleId, csdnUserInfo); if (!isLike) { - csdnLikeService.like(articleId); + csdnLikeService.like(articleId, csdnUserInfo); } //评论 - final Boolean comment = csdnCommentService.isComment(article); + final Boolean comment = csdnCommentService.isComment(article, csdnUserInfo); if (!comment) { - csdnCommentService.comment(articleId); + csdnCommentService.comment(articleId, csdnUserInfo); } //收藏 - final Boolean collect = csdnCollectService.isCollect(articleId); + final Boolean collect = csdnCollectService.isCollect(articleId, csdnUserInfo); if (!collect) { - csdnCollectService.collect(article, username); + csdnCollectService.collect(article, csdnUserInfo); } } + csdnUserInfoService.updateById(csdnUserInfo); } private List getArticles(String username) { @@ -111,23 +114,9 @@ public class CsdnServiceImpl implements CsdnService { final List list = csdnUserInfoService.list(wrapper); if (CollectionUtil.isNotEmpty(list)) { for (CsdnUserInfo csdnUserInfo : list) { - final String userName = csdnUserInfo.getUserName(); - singleArticle(userName); + singleArticle(csdnUserInfo); } } log.info("全部三连完成"); } - - -// /** -// * 多人三连非数据库 -// */ -// @Override -// public void multiTriplet() { -// String[] userNames = new String[]{"user1", "user2", "user3", "xxxx"}; -// for (String name : userNames) { -// singleArticle(name); -// } -// log.info("全部三连完成"); -// } } \ No newline at end of file