fix:添加状态

上级 60b4ecf5
package com.kwan.springbootkwan.controller; 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.entity.Result;
import com.kwan.springbootkwan.service.CsdnLikeService;
import com.kwan.springbootkwan.service.CsdnService; import com.kwan.springbootkwan.service.CsdnService;
import com.kwan.springbootkwan.service.CsdnUserInfoService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -10,6 +13,8 @@ import org.springframework.web.bind.annotation.GetMapping; ...@@ -10,6 +13,8 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@Slf4j @Slf4j
@RestController @RestController
@RequestMapping("/csdn") @RequestMapping("/csdn")
...@@ -18,21 +23,18 @@ public class CsdnController { ...@@ -18,21 +23,18 @@ public class CsdnController {
@Autowired @Autowired
private CsdnService csdnService; private CsdnService csdnService;
@Autowired @Autowired
private CsdnLikeService csdnLikeService; private CsdnUserInfoService csdnUserInfoService;
@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));
}
@GetMapping("/singleTriplet") @GetMapping("/singleTriplet")
public Result singleTriplet(@Param("username") String username) { public Result singleTriplet(@Param("username") String username) {
csdnService.singleArticle(username); QueryWrapper<CsdnUserInfo> wrapper = new QueryWrapper<>();
wrapper.eq("is_delete", 0);
wrapper.eq("user_name", username);
final List<CsdnUserInfo> list = csdnUserInfoService.list(wrapper);
if (CollectionUtil.isNotEmpty(list)) {
final CsdnUserInfo csdnUserInfo = list.get(0);
csdnService.singleArticle(csdnUserInfo);
}
return Result.ok("单人三连完成"); return Result.ok("单人三连完成");
} }
......
package com.kwan.springbootkwan.entity; package com.kwan.springbootkwan.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.activerecord.Model; import com.baomidou.mybatisplus.extension.activerecord.Model;
import lombok.Data;
import java.util.Date; import java.util.Date;
/** @Data
* csdn用户信息(CsdnUserInfo)表实体类 @TableName("csdn_user_info")
*
* @author makejava
* @since 2023-10-23 16:03:14
*/
@SuppressWarnings("serial")
public class CsdnUserInfo extends Model<CsdnUserInfo> { public class CsdnUserInfo extends Model<CsdnUserInfo> {
//主键id /**
* 主键id
*/
private Integer id; private Integer id;
/**
* 用户code
*/
private String userName; 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; private Date createTime;
//逻辑删除,0未删除,1已删除 /**
* 逻辑删除,0未删除,1已删除
*/
private Integer isDelete; 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
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
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
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
package com.kwan.springbootkwan.schedule; 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.CsdnService;
import com.kwan.springbootkwan.service.CsdnUserInfoService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.List;
@Slf4j @Slf4j
@Component @Component
...@@ -14,12 +22,32 @@ public class CsdnSchedule { ...@@ -14,12 +22,32 @@ public class CsdnSchedule {
@Autowired @Autowired
private CsdnService csdnService; private CsdnService csdnService;
@Autowired
private CsdnUserInfoService csdnUserInfoService;
@Scheduled(cron = "0 0 8,10,12,14,16,18,20 * * ?") @Scheduled(cron = "0 0 8,10,12,14,16,18,20 * * ?")
public void execute() { public void execute() {
log.info("Scheduled task is running ... ..."); log.info("execute task is running ... ...");
csdnService.multiTriplet(); 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<CsdnUserInfo> wrapper = new QueryWrapper<>();
wrapper.eq("is_delete", 0);
wrapper.orderByAsc("rand()");
final List<CsdnUserInfo> 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 ... ...");
} }
} }
package com.kwan.springbootkwan.service; package com.kwan.springbootkwan.service;
import com.kwan.springbootkwan.entity.CsdnUserInfo;
import com.kwan.springbootkwan.entity.resp.BusinessInfoResponse; import com.kwan.springbootkwan.entity.resp.BusinessInfoResponse;
/** /**
...@@ -17,12 +18,12 @@ public interface CsdnCollectService { ...@@ -17,12 +18,12 @@ public interface CsdnCollectService {
* *
* @return * @return
*/ */
Boolean isCollect(String articleId); Boolean isCollect(String articleId, CsdnUserInfo csdnUserInfo);
/** /**
* 点赞和取消点赞接口,true,点过,false,没有点过 * 点赞和取消点赞接口,true,点过,false,没有点过
* *
* @return * @return
*/ */
Boolean collect(BusinessInfoResponse.ArticleData.Article article, String username); Boolean collect(BusinessInfoResponse.ArticleData.Article article, CsdnUserInfo csdnUserInfo);
} }
\ No newline at end of file
package com.kwan.springbootkwan.service; package com.kwan.springbootkwan.service;
import com.kwan.springbootkwan.entity.CsdnUserInfo;
import com.kwan.springbootkwan.entity.resp.BusinessInfoResponse; import com.kwan.springbootkwan.entity.resp.BusinessInfoResponse;
/** /**
...@@ -16,12 +17,12 @@ public interface CsdnCommentService { ...@@ -16,12 +17,12 @@ public interface CsdnCommentService {
* *
* @return * @return
*/ */
Boolean isComment(BusinessInfoResponse.ArticleData.Article article); Boolean isComment(BusinessInfoResponse.ArticleData.Article article, CsdnUserInfo csdnUserInfo);
/** /**
* 点赞和取消点赞接口,true,点过,false,没有点过 * 点赞和取消点赞接口,true,点过,false,没有点过
* *
* @return * @return
*/ */
Boolean comment(String articleId); Boolean comment(String articleId, CsdnUserInfo csdnUserInfo);
} }
\ No newline at end of file
package com.kwan.springbootkwan.service; package com.kwan.springbootkwan.service;
import com.kwan.springbootkwan.entity.CsdnUserInfo;
/** /**
* 点赞 * 点赞
* *
...@@ -16,12 +18,12 @@ public interface CsdnLikeService { ...@@ -16,12 +18,12 @@ public interface CsdnLikeService {
* *
* @return * @return
*/ */
Boolean isLike(String articleId, String userName); Boolean isLike(String articleId, CsdnUserInfo csdnUserInfo);
/** /**
* 点赞和取消点赞接口,true,点过,false,没有点过 * 点赞和取消点赞接口,true,点过,false,没有点过
* *
* @return * @return
*/ */
Boolean like(String articleId); Boolean like(String articleId,CsdnUserInfo csdnUserInfo);
} }
\ No newline at end of file
package com.kwan.springbootkwan.service; package com.kwan.springbootkwan.service;
import com.kwan.springbootkwan.entity.CsdnUserInfo;
/** /**
* csdn博客自动化 * csdn博客自动化
* *
...@@ -14,7 +16,7 @@ public interface CsdnService { ...@@ -14,7 +16,7 @@ public interface CsdnService {
* *
* @return * @return
*/ */
void singleArticle(String username); void singleArticle(CsdnUserInfo csdnUserInfo);
/** /**
* 多人三连 * 多人三连
......
...@@ -4,16 +4,19 @@ import cn.hutool.http.HttpResponse; ...@@ -4,16 +4,19 @@ import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil; import cn.hutool.http.HttpUtil;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper; 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.BusinessInfoResponse;
import com.kwan.springbootkwan.entity.resp.CollectInfoQuery; import com.kwan.springbootkwan.entity.resp.CollectInfoQuery;
import com.kwan.springbootkwan.entity.resp.CollectResponse; import com.kwan.springbootkwan.entity.resp.CollectResponse;
import com.kwan.springbootkwan.entity.resp.IsCollectResponse; import com.kwan.springbootkwan.entity.resp.IsCollectResponse;
import com.kwan.springbootkwan.enums.CollectStatus;
import com.kwan.springbootkwan.service.CsdnCollectService; import com.kwan.springbootkwan.service.CsdnCollectService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
@Slf4j @Slf4j
@Service @Service
...@@ -30,15 +33,10 @@ public class CsdnCollectServiceImpl implements CsdnCollectService { ...@@ -30,15 +33,10 @@ public class CsdnCollectServiceImpl implements CsdnCollectService {
@Value("${csdn.url.add_collect_url}") @Value("${csdn.url.add_collect_url}")
private String addCollectUrl; private String addCollectUrl;
/**
* 收藏满了
*/
private static boolean COLLECT_IS_FULL = false;
@Override @Override
public Boolean isCollect(String articleId) { public Boolean isCollect(String articleId, CsdnUserInfo csdnUserInfo) {
if (COLLECT_IS_FULL) { final Integer collectStatus = csdnUserInfo.getCollectStatus();
if (CollectStatus.HAVE_ALREADY_COLLECT.getCode().equals(collectStatus) || CollectStatus.COLLECT_IS_FULL.getCode().equals(collectStatus)) {
return true; return true;
} }
HttpResponse response = HttpUtil.createGet(isCollectUrl) HttpResponse response = HttpUtil.createGet(isCollectUrl)
...@@ -54,8 +52,10 @@ public class CsdnCollectServiceImpl implements CsdnCollectService { ...@@ -54,8 +52,10 @@ public class CsdnCollectServiceImpl implements CsdnCollectService {
final boolean status = data.status; final boolean status = data.status;
if (status) { if (status) {
log.info("文章{}已经收藏过", articleId); log.info("文章{}已经收藏过", articleId);
csdnUserInfo.setCommentStatus(CollectStatus.HAVE_ALREADY_COLLECT.getCode());
} else { } else {
log.info("文章{}未收藏", articleId); log.info("文章{}未收藏", articleId);
csdnUserInfo.setCommentStatus(CollectStatus.UN_PROCESSED.getCode());
} }
return status; return status;
} }
...@@ -66,8 +66,10 @@ public class CsdnCollectServiceImpl implements CsdnCollectService { ...@@ -66,8 +66,10 @@ public class CsdnCollectServiceImpl implements CsdnCollectService {
} }
@Override @Override
public Boolean collect(BusinessInfoResponse.ArticleData.Article article, String username) { public Boolean collect(BusinessInfoResponse.ArticleData.Article article, CsdnUserInfo csdnUserInfo) {
if (COLLECT_IS_FULL) { 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; return true;
} }
final String urlInfo = article.getUrl(); final String urlInfo = article.getUrl();
...@@ -77,10 +79,10 @@ public class CsdnCollectServiceImpl implements CsdnCollectService { ...@@ -77,10 +79,10 @@ public class CsdnCollectServiceImpl implements CsdnCollectService {
CollectInfoQuery collectInfoQuery = new CollectInfoQuery(); CollectInfoQuery collectInfoQuery = new CollectInfoQuery();
collectInfoQuery.setSourceId(Integer.valueOf(articleId)); collectInfoQuery.setSourceId(Integer.valueOf(articleId));
collectInfoQuery.setFromType("PC"); collectInfoQuery.setFromType("PC");
collectInfoQuery.setAuthor(username); collectInfoQuery.setAuthor(userName);
collectInfoQuery.setDescription(article.getDescription()); collectInfoQuery.setDescription(article.getDescription());
collectInfoQuery.setSource("blog"); collectInfoQuery.setSource("blog");
ArrayList<Integer> list = new ArrayList<>(); List<Integer> list = new ArrayList<>();
list.add(selfFolderId); list.add(selfFolderId);
collectInfoQuery.setFolderIdList(list); collectInfoQuery.setFolderIdList(list);
collectInfoQuery.setTitle(article.getTitle()); collectInfoQuery.setTitle(article.getTitle());
...@@ -101,11 +103,13 @@ public class CsdnCollectServiceImpl implements CsdnCollectService { ...@@ -101,11 +103,13 @@ public class CsdnCollectServiceImpl implements CsdnCollectService {
final Long code = collectResponse.code; final Long code = collectResponse.code;
if (code.equals(200)) { if (code.equals(200)) {
log.info("文章{}收藏成功", articleId); log.info("文章{}收藏成功", articleId);
csdnUserInfo.setCollectStatus(CollectStatus.COLLECT_SUCCESSFUL.getCode());
} else if (code.equals(400000101)) { } else if (code.equals(400000101)) {
log.info("收藏文章{}参数缺失", articleId); log.info("收藏文章{}参数缺失", articleId);
csdnUserInfo.setCollectStatus(CollectStatus.MISSING_PARAMETER.getCode());
} else if (code.equals(400)) { } else if (code.equals(400)) {
COLLECT_IS_FULL = true;
log.info("今日收藏次数已达上限!"); log.info("今日收藏次数已达上限!");
csdnUserInfo.setCollectStatus(CollectStatus.COLLECT_IS_FULL.getCode());
} }
return true; return true;
} }
......
...@@ -5,9 +5,11 @@ import cn.hutool.http.HttpResponse; ...@@ -5,9 +5,11 @@ import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil; import cn.hutool.http.HttpUtil;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper; 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.BusinessInfoResponse;
import com.kwan.springbootkwan.entity.resp.CommentListResponse; import com.kwan.springbootkwan.entity.resp.CommentListResponse;
import com.kwan.springbootkwan.entity.resp.CommentResponse; import com.kwan.springbootkwan.entity.resp.CommentResponse;
import com.kwan.springbootkwan.enums.CommentStatus;
import com.kwan.springbootkwan.service.CsdnCommentService; import com.kwan.springbootkwan.service.CsdnCommentService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
...@@ -36,21 +38,22 @@ public class CsdnCommentServiceImpl implements CsdnCommentService { ...@@ -36,21 +38,22 @@ public class CsdnCommentServiceImpl implements CsdnCommentService {
private static boolean COMMENT_IS_FULL = false; private static boolean COMMENT_IS_FULL = false;
@Override @Override
public Boolean isComment(BusinessInfoResponse.ArticleData.Article article) { public Boolean isComment(BusinessInfoResponse.ArticleData.Article article, CsdnUserInfo csdnUserInfo) {
if (COMMENT_IS_FULL) { 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; return true;
} }
final String urlInfo = article.getUrl(); final String urlInfo = article.getUrl();
String articleId = urlInfo.substring(urlInfo.lastIndexOf("/") + 1); String articleId = urlInfo.substring(urlInfo.lastIndexOf("/") + 1);
String url = commentListUrl + articleId; String url = commentListUrl + articleId;
// 使用Hutool发送GET请求
HttpResponse response = HttpUtil.createPost(url) HttpResponse response = HttpUtil.createPost(url)
.header("Cookie", csdnCookie) .header("Cookie", csdnCookie)
.form("page", 1) .form("page", 1)
.form("size", 50) .form("size", 50)
.form("fold", "unfold") .form("fold", "unfold")
.execute(); .execute();
// 打印响应结果
final String body = response.body(); final String body = response.body();
ObjectMapper objectMapper = new ObjectMapper(); ObjectMapper objectMapper = new ObjectMapper();
CommentListResponse articleInfo; CommentListResponse articleInfo;
...@@ -63,8 +66,8 @@ public class CsdnCommentServiceImpl implements CsdnCommentService { ...@@ -63,8 +66,8 @@ public class CsdnCommentServiceImpl implements CsdnCommentService {
final CommentListResponse.Info info = comment.getInfo(); final CommentListResponse.Info info = comment.getInfo();
final String userName = info.getUserName(); final String userName = info.getUserName();
if (StringUtils.equals(userName, selfUserName)) { if (StringUtils.equals(userName, selfUserName)) {
//评论过
log.info("文章{}已经评论过", articleId); log.info("文章{}已经评论过", articleId);
csdnUserInfo.setCommentStatus(CommentStatus.HAVE_ALREADY_COMMENT.getCode());
return true; return true;
} }
} }
...@@ -72,12 +75,16 @@ public class CsdnCommentServiceImpl implements CsdnCommentService { ...@@ -72,12 +75,16 @@ public class CsdnCommentServiceImpl implements CsdnCommentService {
} catch (JsonProcessingException e) { } catch (JsonProcessingException e) {
e.printStackTrace(); e.printStackTrace();
} }
csdnUserInfo.setCommentStatus(CommentStatus.UN_PROCESSED.getCode());
return false; return false;
} }
@Override @Override
public Boolean comment(String articleId) { public Boolean comment(String articleId, CsdnUserInfo csdnUserInfo) {
if (COMMENT_IS_FULL) { 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; return true;
} }
//评论 //评论
...@@ -86,11 +93,16 @@ public class CsdnCommentServiceImpl implements CsdnCommentService { ...@@ -86,11 +93,16 @@ public class CsdnCommentServiceImpl implements CsdnCommentService {
final String message = comment.getMessage(); final String message = comment.getMessage();
if (code == 200) { if (code == 200) {
log.info("文章{}评论成功", articleId); log.info("文章{}评论成功", articleId);
csdnUserInfo.setCommentStatus(CommentStatus.COMMENT_SUCCESSFUL.getCode());
} else if (code == 400 && StringUtils.equals(message, "您已达到当日发送上限,请明天尝试!")) { } else if (code == 400 && StringUtils.equals(message, "您已达到当日发送上限,请明天尝试!")) {
log.info(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 { } else {
log.info("您评论太快了,请休息一下!"); log.info("您评论太快了,请休息一下!");
csdnUserInfo.setCommentStatus(CommentStatus.COMMENT_TOO_FAST.getCode());
} }
return true; return true;
} }
......
...@@ -4,7 +4,9 @@ import cn.hutool.http.HttpResponse; ...@@ -4,7 +4,9 @@ import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil; import cn.hutool.http.HttpUtil;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.kwan.springbootkwan.entity.CsdnUserInfo;
import com.kwan.springbootkwan.entity.resp.LikeResponse; import com.kwan.springbootkwan.entity.resp.LikeResponse;
import com.kwan.springbootkwan.enums.LikeStatus;
import com.kwan.springbootkwan.service.CsdnLikeService; import com.kwan.springbootkwan.service.CsdnLikeService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
...@@ -18,14 +20,12 @@ public class CsdnLikeServiceImpl implements CsdnLikeService { ...@@ -18,14 +20,12 @@ public class CsdnLikeServiceImpl implements CsdnLikeService {
private String csdnCookie; private String csdnCookie;
@Value("${csdn.url.like_url}") @Value("${csdn.url.like_url}")
private String url; private String url;
/**
* 点赞满了
*/
private static boolean LIKE_IS_FULL = false;
@Override @Override
public Boolean isLike(String articleId, String userName) { public Boolean isLike(String articleId, CsdnUserInfo csdnUserInfo) {
if (LIKE_IS_FULL) { 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; return true;
} }
String url = "https://blog.csdn.net/" + userName + "/article/details/" + articleId; String url = "https://blog.csdn.net/" + userName + "/article/details/" + articleId;
...@@ -36,16 +36,19 @@ public class CsdnLikeServiceImpl implements CsdnLikeService { ...@@ -36,16 +36,19 @@ public class CsdnLikeServiceImpl implements CsdnLikeService {
final String body = response.body(); final String body = response.body();
if (body.contains("isLikeStatus = true")) { if (body.contains("isLikeStatus = true")) {
log.info("文章{}已经点过赞", articleId); log.info("文章{}已经点过赞", articleId);
csdnUserInfo.setLikeStatus(LikeStatus.HAVE_ALREADY_LIKED.getCode());
return true; return true;
} else { } else {
log.info("文章{}未点过赞", articleId); log.info("文章{}未点过赞", articleId);
csdnUserInfo.setLikeStatus(LikeStatus.UN_PROCESSED.getCode());
return false; return false;
} }
} }
@Override @Override
public Boolean like(String articleId) { public Boolean like(String articleId, CsdnUserInfo csdnUserInfo) {
if (LIKE_IS_FULL) { final Integer likeStatus = csdnUserInfo.getLikeStatus();
if (LikeStatus.HAVE_ALREADY_LIKED.getCode().equals(likeStatus) || LikeStatus.LIKE_IS_FULL.getCode().equals(likeStatus)) {
return true; return true;
} }
HttpResponse response = HttpUtil.createPost(url) HttpResponse response = HttpUtil.createPost(url)
...@@ -61,13 +64,15 @@ public class CsdnLikeServiceImpl implements CsdnLikeService { ...@@ -61,13 +64,15 @@ public class CsdnLikeServiceImpl implements CsdnLikeService {
final boolean status = data.status; final boolean status = data.status;
if (status) { if (status) {
log.info("文章{}点赞成功", articleId); log.info("文章{}点赞成功", articleId);
csdnUserInfo.setLikeStatus(LikeStatus.LIKE_SUCCESSFUL.getCode());
} else { } else {
log.info("文章{}点赞取消", articleId); log.info("文章{}点赞取消", articleId);
csdnUserInfo.setLikeStatus(LikeStatus.CANCEL_LIKES.getCode());
} }
return status; return status;
} else if (code == 400) { } else if (code == 400) {
LIKE_IS_FULL = true;
log.info("今日点赞次数已达上限!"); log.info("今日点赞次数已达上限!");
csdnUserInfo.setLikeStatus(LikeStatus.LIKE_IS_FULL.getCode());
} }
} catch (JsonProcessingException e) { } catch (JsonProcessingException e) {
e.printStackTrace(); e.printStackTrace();
......
...@@ -42,7 +42,8 @@ public class CsdnServiceImpl implements CsdnService { ...@@ -42,7 +42,8 @@ public class CsdnServiceImpl implements CsdnService {
private CsdnLikeService csdnLikeService; private CsdnLikeService csdnLikeService;
@Override @Override
public void singleArticle(String username) { public void singleArticle(CsdnUserInfo csdnUserInfo) {
final String username = csdnUserInfo.getUserName();
List<BusinessInfoResponse.ArticleData.Article> list = this.getArticles(username); List<BusinessInfoResponse.ArticleData.Article> list = this.getArticles(username);
if (list == null) { if (list == null) {
return; return;
...@@ -51,26 +52,28 @@ public class CsdnServiceImpl implements CsdnService { ...@@ -51,26 +52,28 @@ public class CsdnServiceImpl implements CsdnService {
final BusinessInfoResponse.ArticleData.Article article = list.get(i); final BusinessInfoResponse.ArticleData.Article article = list.get(i);
final String type = article.getType(); final String type = article.getType();
if (!StringUtils.equals("blog", type)) { if (!StringUtils.equals("blog", type)) {
csdnUserInfo.setArticleType(type);
continue; continue;
} }
final String urlInfo = article.getUrl(); final String urlInfo = article.getUrl();
String articleId = urlInfo.substring(urlInfo.lastIndexOf("/") + 1); String articleId = urlInfo.substring(urlInfo.lastIndexOf("/") + 1);
//点赞 //点赞
final Boolean isLike = csdnLikeService.isLike(articleId, username); final Boolean isLike = csdnLikeService.isLike(articleId, csdnUserInfo);
if (!isLike) { if (!isLike) {
csdnLikeService.like(articleId); csdnLikeService.like(articleId, csdnUserInfo);
} }
//评论 //评论
final Boolean comment = csdnCommentService.isComment(article); final Boolean comment = csdnCommentService.isComment(article, csdnUserInfo);
if (!comment) { if (!comment) {
csdnCommentService.comment(articleId); csdnCommentService.comment(articleId, csdnUserInfo);
} }
//收藏 //收藏
final Boolean collect = csdnCollectService.isCollect(articleId); final Boolean collect = csdnCollectService.isCollect(articleId, csdnUserInfo);
if (!collect) { if (!collect) {
csdnCollectService.collect(article, username); csdnCollectService.collect(article, csdnUserInfo);
} }
} }
csdnUserInfoService.updateById(csdnUserInfo);
} }
private List<BusinessInfoResponse.ArticleData.Article> getArticles(String username) { private List<BusinessInfoResponse.ArticleData.Article> getArticles(String username) {
...@@ -111,23 +114,9 @@ public class CsdnServiceImpl implements CsdnService { ...@@ -111,23 +114,9 @@ public class CsdnServiceImpl implements CsdnService {
final List<CsdnUserInfo> list = csdnUserInfoService.list(wrapper); final List<CsdnUserInfo> list = csdnUserInfoService.list(wrapper);
if (CollectionUtil.isNotEmpty(list)) { if (CollectionUtil.isNotEmpty(list)) {
for (CsdnUserInfo csdnUserInfo : list) { for (CsdnUserInfo csdnUserInfo : list) {
final String userName = csdnUserInfo.getUserName(); singleArticle(csdnUserInfo);
singleArticle(userName);
} }
} }
log.info("全部三连完成"); 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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册