fix:多人评论

上级 c0611570
...@@ -33,12 +33,18 @@ public class CsdnController { ...@@ -33,12 +33,18 @@ public class CsdnController {
@GetMapping("/singleTriplet") @GetMapping("/singleTriplet")
public Result singleTriplet(@Param("username") String username) { public Result singleTriplet(@Param("username") String username) {
csdnService.singleArticle(username); csdnService.singleArticle(username);
return Result.ok("三连完成"); return Result.ok("单人三连完成");
} }
@GetMapping("/multiTriplet") @GetMapping("/multiTriplet")
public Result multiTriplet() { public Result multiTriplet() {
csdnService.multiTriplet(); csdnService.multiTriplet();
return Result.ok("三连完成"); return Result.ok("多人三连完成");
}
@GetMapping("/multiComment")
public Result multiComment() {
csdnService.multiComment();
return Result.ok("多人评论完成");
} }
} }
...@@ -21,5 +21,12 @@ public class CsdnSchedule { ...@@ -21,5 +21,12 @@ public class CsdnSchedule {
csdnService.multiTriplet(); csdnService.multiTriplet();
log.info("Scheduled task is finish ... ..."); log.info("Scheduled task is finish ... ...");
} }
@Scheduled(cron = "0 0/30 * * * ?")
public void executeMultiComment() {
log.info("Scheduled task is running ... ...");
csdnService.multiComment();
log.info("Scheduled task is finish ... ...");
}
} }
...@@ -20,4 +20,9 @@ public interface CsdnService { ...@@ -20,4 +20,9 @@ public interface CsdnService {
* 多人三连 * 多人三连
*/ */
void multiTriplet(); void multiTriplet();
/**
* 多人只评论
*/
void multiComment();
} }
\ No newline at end of file
...@@ -42,6 +42,37 @@ public class CsdnServiceImpl implements CsdnService { ...@@ -42,6 +42,37 @@ public class CsdnServiceImpl implements CsdnService {
@Override @Override
public void singleArticle(String username) { public void singleArticle(String username) {
List<BusinessInfoResponse.ArticleData.Article> list = this.getArticles(username);
if (list == null) {
return;
}
for (int i = 0; i < numOfArticlesPerPerson; i++) {
final BusinessInfoResponse.ArticleData.Article article = list.get(i);
final String type = article.getType();
if (!StringUtils.equals("blog", type)) {
continue;
}
final String urlInfo = article.getUrl();
String articleId = urlInfo.substring(urlInfo.lastIndexOf("/") + 1);
//点赞
final Boolean isLike = csdnLikeService.isLike(articleId, username);
if (!isLike) {
csdnLikeService.like(articleId);
}
//评论
final Boolean comment = csdnCommentService.isComment(article);
if (!comment) {
csdnCommentService.comment(articleId);
}
//收藏
final Boolean collect = csdnCollectService.isCollect(articleId);
if (!collect) {
csdnCollectService.collect(article, username);
}
}
}
private List<BusinessInfoResponse.ArticleData.Article> getArticles(String username) {
HttpResponse response = HttpUtil.createGet(url) HttpResponse response = HttpUtil.createGet(url)
.header("Cookie", csdnCookie) .header("Cookie", csdnCookie)
.form("page", 1) .form("page", 1)
...@@ -53,14 +84,63 @@ public class CsdnServiceImpl implements CsdnService { ...@@ -53,14 +84,63 @@ public class CsdnServiceImpl implements CsdnService {
final String body = response.body(); final String body = response.body();
ObjectMapper objectMapper = new ObjectMapper(); ObjectMapper objectMapper = new ObjectMapper();
BusinessInfoResponse businessInfoResponse; BusinessInfoResponse businessInfoResponse;
List<BusinessInfoResponse.ArticleData.Article> list = null;
try { try {
businessInfoResponse = objectMapper.readValue(body, BusinessInfoResponse.class); businessInfoResponse = objectMapper.readValue(body, BusinessInfoResponse.class);
final BusinessInfoResponse.ArticleData data = businessInfoResponse.getData(); final BusinessInfoResponse.ArticleData data = businessInfoResponse.getData();
final List<BusinessInfoResponse.ArticleData.Article> list = data.getList(); list = data.getList();
if (CollectionUtil.isNotEmpty(list)) { } catch (JsonProcessingException e) {
final int size = list.size(); e.printStackTrace();
if (size < numOfArticlesPerPerson) { }
numOfArticlesPerPerson = size; if (CollectionUtil.isEmpty(list)) {
return null;
}
final int size = list.size();
if (size < numOfArticlesPerPerson) {
numOfArticlesPerPerson = size;
}
return list;
}
@Override
public void multiTriplet() {
final List<CsdnUserInfo> list = csdnUserInfoService.list();
if (CollectionUtil.isNotEmpty(list)) {
for (CsdnUserInfo csdnUserInfo : list) {
singleArticle(csdnUserInfo.getUserName());
}
}
log.info("全部三连完成");
}
// /**
// * 多人三连非数据库
// */
// @Override
// public void multiTriplet() {
// String[] userNames = new String[]{"user1", "user2", "user3", "xxxx"};
// for (String name : userNames) {
// singleArticle(name);
// }
// log.info("全部三连完成");
// }
@Override
public void multiComment() {
final List<CsdnUserInfo> userNames = csdnUserInfoService.list();
if (CollectionUtil.isNotEmpty(userNames)) {
for (CsdnUserInfo csdnUserInfo : userNames) {
final String userName = csdnUserInfo.getUserName();
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
List<BusinessInfoResponse.ArticleData.Article> list = this.getArticles(userName);
if (list == null) {
continue;
} }
for (int i = 0; i < numOfArticlesPerPerson; i++) { for (int i = 0; i < numOfArticlesPerPerson; i++) {
final BusinessInfoResponse.ArticleData.Article article = list.get(i); final BusinessInfoResponse.ArticleData.Article article = list.get(i);
...@@ -70,37 +150,14 @@ public class CsdnServiceImpl implements CsdnService { ...@@ -70,37 +150,14 @@ public class CsdnServiceImpl implements CsdnService {
} }
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);
if (!isLike) {
csdnLikeService.like(articleId);
}
//评论 //评论
final Boolean comment = csdnCommentService.isComment(article); final Boolean comment = csdnCommentService.isComment(article);
if (!comment) { if (!comment) {
csdnCommentService.comment(articleId); csdnCommentService.comment(articleId);
} }
//收藏
final Boolean collect = csdnCollectService.isCollect(articleId);
if (!collect) {
csdnCollectService.collect(article, username);
}
} }
} }
} catch (JsonProcessingException e) {
e.printStackTrace();
} }
} log.info("多人只评论完成");
@Override
public void multiTriplet() {
final List<CsdnUserInfo> list = csdnUserInfoService.list();
if (CollectionUtil.isNotEmpty(list)) {
for (CsdnUserInfo csdnUserInfo : list) {
singleArticle(csdnUserInfo.getUserName());
}
}
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.
先完成此消息的编辑!
想要评论请 注册