fix:点赞评论

上级 2fc342ca
package com.kwan.springbootkwan.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CsdnStatusResp {
public Integer code;
public String value;
}
......@@ -40,6 +40,8 @@ public class BusinessInfo {
public Integer viewCount;
public Object expandTitle;
public Object cover;
public Object fileType;
public Object content;
}
}
......
package com.kwan.springbootkwan.entity.resp;
import lombok.Data;
@Data
public class LikeData {
public int code;
public String message;
public String traceId;
public Object data;
}
\ No newline at end of file
......@@ -8,14 +8,14 @@ import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
@Slf4j
@Component
public class CsdnSchedule {
@Autowired
private CsdnService csdnService;
@Scheduled(cron = "0 0 10,15,21 * * ?")
@Scheduled(cron = "0 0 6,8,10,12,14,16,18,20,22 * * ?")
public void execute() {
log.info("Scheduled task is running ... ...");
csdnService.multiTriplet();
......
package com.kwan.springbootkwan.service;
import com.kwan.springbootkwan.entity.CsdnStatusResp;
/**
* csdn博客自动化
*
......@@ -9,11 +11,11 @@ package com.kwan.springbootkwan.service;
*/
public interface CsdnService {
/**
* 查询单篇文章信息
* 单人三连
*
* @return
*/
String singleArticle(String username);
CsdnStatusResp singleArticle(String username);
/**
* 查询是否评论过
......@@ -35,7 +37,7 @@ public interface CsdnService {
*
* @param articleId
*/
void comment(String articleId);
int comment(String articleId);
/**
* 三连
......
......@@ -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.CsdnStatusResp;
import com.kwan.springbootkwan.entity.CsdnUserInfo;
import com.kwan.springbootkwan.entity.resp.BusinessInfo;
import com.kwan.springbootkwan.entity.resp.CommentData;
import com.kwan.springbootkwan.entity.resp.LikeData;
import com.kwan.springbootkwan.service.CsdnService;
import com.kwan.springbootkwan.service.CsdnUserInfoService;
import lombok.extern.slf4j.Slf4j;
......@@ -25,6 +27,10 @@ public class CsdnServiceImpl implements CsdnService {
* 构建header参数
*/
private static final String CUSTOM_HEADER_NAME = "Cookie";
/**
* 点赞满了
*/
private static boolean LIKE_IS_FULL = false;
/**
* 参数值
*/
......@@ -34,13 +40,13 @@ public class CsdnServiceImpl implements CsdnService {
private CsdnUserInfoService csdnUserInfoService;
@Override
public String singleArticle(String username) {
public CsdnStatusResp singleArticle(String username) {
String url = "https://blog.csdn.net/community/home-api/v1/get-business-list";
// 使用Hutool发送GET请求
HttpResponse response = HttpUtil.createGet(url)
.header(CUSTOM_HEADER_NAME, CUSTOM_HEADER_VALUE)
.form("page", 1)
.form("size", 2)
.form("size", 5)
.form("businessType", "lately")
.form("noMore", false)
.form("username", username)
......@@ -56,23 +62,41 @@ public class CsdnServiceImpl implements CsdnService {
}
final BusinessInfo.ArticleData data = businessInfo.getData();
final List<BusinessInfo.ArticleData.Article> list = data.getList();
final BusinessInfo.ArticleData.Article article = list.get(0);
final String type = article.getType();
if (!StringUtils.equals("blog", type)) {
return "非博客不能评论";
//处理评论过
int commentNum = 0;
int successNum = 0;
int notBlogNum = 0;
int count = 3;//评论3篇
for (int i = 0; i < count; i++) {
final BusinessInfo.ArticleData.Article article = list.get(i);
final String type = article.getType();
if (!StringUtils.equals("blog", type)) {
notBlogNum++;
continue;
}
final String urlInfo = article.getUrl();
String articleId = urlInfo.substring(urlInfo.lastIndexOf("/") + 1);
final Boolean articleInfo = this.getArticleInfo(articleId);
if (!articleInfo) {
//进行点赞和评论
final int comment = this.comment(articleId);
// 打印响应结果
if (comment == 200) {
successNum++;
if (!LIKE_IS_FULL) {
this.like(articleId);
}
} else if (comment == 400) {
return new CsdnStatusResp(400, "您评论太快了,请休息一下!");
}
} else {
commentNum++;
}
}
final String urlInfo = article.getUrl();
String articleId = urlInfo.substring(urlInfo.lastIndexOf("/") + 1);
final Boolean articleInfo = this.getArticleInfo(articleId);
if (!articleInfo) {
//进行点赞和评论
this.like(articleId);
this.comment(articleId);
return "三连完成";
} else {
return "文章已经评论过,不能再次评论";
if (count != successNum) {
return new CsdnStatusResp(300, "评论过的数量=" + commentNum + "非博客的数量:" + notBlogNum);
}
return new CsdnStatusResp(200, "三连完成");
}
@Override
......@@ -118,13 +142,25 @@ public class CsdnServiceImpl implements CsdnService {
.form("articleId", articleId)
.execute();
// 打印响应结果
if (response.getStatus() == 200) {
final String body = response.body();
ObjectMapper objectMapper = new ObjectMapper();
LikeData likeData = null;
try {
likeData = objectMapper.readValue(body, LikeData.class);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
final int code = likeData.code;
if (code == 200) {
log.info("点赞成功");
} else if (code == 400) {
LIKE_IS_FULL = true;
log.info("今日点赞次数已达上限!");
}
}
@Override
public void comment(String articleId) {
public int comment(String articleId) {
String url = "https://blog.csdn.net/phoenix/web/v1/comment/submit";
// 使用Hutool发送GET请求
HttpResponse response = HttpUtil.createPost(url)
......@@ -132,22 +168,25 @@ public class CsdnServiceImpl implements CsdnService {
.form("articleId", articleId)
.form("content", "支持博主优质文章,讲解得非常详细,干货满满,通俗易懂,期待博主下次更新")
.execute();
// 打印响应结果
if (response.getStatus() == 200) {
log.info("评论成功");
}
return response.getStatus();
}
@Override
public void multiTriplet() {
final List<CsdnUserInfo> list = csdnUserInfoService.list();
if (CollectionUtil.isNotEmpty(list)) {
list.forEach(csdnUserInfo -> {
for (CsdnUserInfo csdnUserInfo : list) {
// 构建参数
String username = csdnUserInfo.getUserName();
final String res = singleArticle(username);
log.info("用户{}:{}", username, res);
});
final CsdnStatusResp csdnStatusResp = singleArticle(username);
final Integer code = csdnStatusResp.getCode();
final String value = csdnStatusResp.getValue();
if (code.equals(400)) {
log.info("用户{}:code={},{}", username, code, value);
break;
}
log.info("用户{}:{}", username, value);
}
}
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册