文章目录
- 前言
- 代码实现
- pom.xml
- 实体类
- 工具类
- 质量分查询
- 效果
- 开源仓库
前言
在CSDN平台申请“专家博主”、“优质创作者”等称号的时候,往往会对博客的质量分有一定的要求,这时候我们需要审视以往所发表的博客,找出质量分较低的博客,并进行质量优化。单篇博客的质量分查询可以在CSDN博客质量分中查询,查询方式如下:
但如果作者发布的文章数量较多的话,一篇一篇查询将会是一个非常大的工作量。为了简化这个过程,我使用Java实现了一段代码,可以批量查询自己所发布文章的质量分
代码实现
pom.xml
引入代码需要使用的一些工具
<dependencies>
<!-- 进行json与Java类的转化 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.32</version>
</dependency>
<!-- 简化实体类的编写 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
</dependency>
<!-- 用于导出excel表格 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.4</version>
</dependency>
<!-- 用于发送请求 -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-http</artifactId>
<version>5.8.13</version>
</dependency>
</dependencies>
实体类
用于存储文章的属性,如标题、url、质量分……
package org.dam.entity;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @Author dam
* @create 2024/8/5 9:38
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class ArticleEntity {
private String url;
private String title;
private Double score;
private String postTime;
}
工具类
用来读写txt文件
package org.dam.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
/**
* @Author dam
* @create 2024/8/5 10:13
*/
public class TxtUtil {
public static String read(File f, String charset) throws Exception {
FileInputStream fstream = new FileInputStream(f);
try {
int fileSize = (int) f.length();
byte[] buffer = new byte[fileSize];
fstream.read(buffer);
return new String(buffer, charset);
} finally {
try {
fstream.close();
} catch (Exception e) {
}
}
}
public static void write(File f, String text, String charset) throws Exception {
FileOutputStream fstream = new FileOutputStream(f);
try {
fstream.write(text.getBytes(charset));
} finally {
fstream.close();
}
}
}
质量分查询
在批量查询文章的质量分之前,需要先获取文章列表相关信息,使用如下链接可以获取文章列表信息
https://blog.csdn.net/community/home-api/v1/get-business-list?page=[页码]&size=100&businessType=blog&username=[CSDN用户名]
-
[CSDN用户名]
:需要替换成自己的用户名,例如我的是laodanqiu
-
[页码]
:分页查询的页面,从1开始 -
size: 最大就是100,可以设置为比100小的数,设置为比100大的数,也只能一次性查出100篇
访问该链接,即可获得查询文章列表的json数据
有时候访问该接口需要进行安全验证,拖动滑块即可
将上述json数据存储到项目的article-list.json
中供后续读取
在获取到文章列表信息json之后,即可批量获取文章的质量分,其实就是使用Hutool来调用质量分查询接口,循环查询每篇文章的质量分,然后存储起来,输出到excel表格中
package org.dam.util;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.dam.entity.ArticleEntity;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
/**
* @Author dam
* @create 2024/8/5 9:36
*/
public class CsdnScoreUtil {
/**
* 查询质量分的地址
*/
private static final String SCORE_URL = "https://bizapi.csdn.net/trends/api/v1/get-article-score";
private static final String X_CA_KEY = "203930474";
private static final String X_CA_NONCE = "9cf8f777-27fe-4a6b-a968-86c17c6ab787";
private static final String X_CA_SIGNATURE = "ruA7Zl7QDFkIbJYd/uP5pfi9PA7ZAPcbZ9htV4wjbos=";
private static final String X_CA_SIGNATURE_HEADERS = "x-ca-key,x-ca-nonce";
private static final String X_CA_SIGNED_CONTENT_TYPE = "multipart/form-data";
/**
* 获取所有文章的质量分
*
* @param articleListObject
* @return
*/
public List<ArticleEntity> csdnScoreCheck(JSONObject articleListObject) {
JSONObject articleListMes = articleListObject.getJSONObject("data");
JSONArray jsonArray = (JSONArray) articleListMes.get("list");
List<ArticleEntity> articleEntityList = new ArrayList<>();
for (Object o : jsonArray) {
JSONObject one = (JSONObject) o;
articleEntityList.add(ArticleEntity.builder()
.url(String.valueOf(one.get("url")))
.title(String.valueOf(one.get("title")))
.build());
}
// 调用csdn接口查询所有的博客质量分
HttpRequest post = HttpUtil.createPost(SCORE_URL)
.header("accept", "application/json, text/plain, */*")
.header("x-ca-key", X_CA_KEY)
.header("x-ca-nonce", X_CA_NONCE)
.header("x-ca-signature", X_CA_SIGNATURE)
.header("x-ca-signature-headers", X_CA_SIGNATURE_HEADERS)
.header("x-ca-signed-content-type", X_CA_SIGNED_CONTENT_TYPE);
for (ArticleEntity articleEntity : articleEntityList) {
post.form("url", articleEntity.getUrl());
HttpResponse response = post.execute();
if (response.isOk()) {
JSONObject scoreMes = JSON.parseObject(response.body());
ArticleEntity articleEntity2 = JSON.toJavaObject(scoreMes.getJSONObject("data"), ArticleEntity.class);
articleEntity.setScore(articleEntity2.getScore());
articleEntity.setPostTime(articleEntity2.getPostTime());
System.out.println("名称:" + articleEntity.getTitle() + "\t质量分:" + articleEntity.getScore() + "\t发表时间:" + articleEntity.getPostTime());
} else {
System.err.println(articleEntity.getUrl() + "请求失败: " + response.getStatus());
}
}
return articleEntityList;
}
/**
* 读取json文件,获取文章标题和链接
*
* @throws Exception
*/
public void getScoreByJson() throws Exception {
String read = TxtUtil.read(new File("src/main/java/org/dam/input/article-list.json"), "utf-8");
List<ArticleEntity> articleEntityList = csdnScoreCheck(JSON.parseObject(read));
save(articleEntityList);
}
/**
* 将检查到的质量信息存储到excel表格中
*
* @param scoreEntities
* @throws IOException
*/
public void save(List<ArticleEntity> scoreEntities) throws IOException {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet();
int rowIndex = 0;
Row row = sheet.createRow(rowIndex++);
row.createCell(0).setCellValue("文章名称");
row.createCell(1).setCellValue("质量分");
row.createCell(2).setCellValue("发布时间");
// 按照分数升序排序
Collections.sort(scoreEntities, ((o1, o2) -> {
return Double.compare(o1.getScore(), o2.getScore());
}));
for (ArticleEntity article : scoreEntities) {
row = sheet.createRow(rowIndex++);
row.createCell(0).setCellValue(article.getTitle());
row.createCell(1).setCellValue(article.getScore());
row.createCell(2).setCellValue(article.getPostTime());
}
System.out.println("开始输出结果");
FileOutputStream outputStream = null;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
try {
outputStream = new FileOutputStream("src/main/java/org/dam/output/质量列表 " + simpleDateFormat.format(new Date()) + ".xlsx");
workbook.write(outputStream);
System.out.println("文件输出完成");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
请求头中需要携带一些参数,如果上述代码的参数过期了,可以去网站中发送一个请求来复制相关信息
效果
生成的excel文件如下
我在输出excel文件的时候,默认将文章按照质量分递增排序了,方便我快速找出哪些文章需要优化改进
开源仓库
CSDN质量分批量查询