How to query your own CSDN blog quality score in batches


Git address: csdn quality score

One: Story Background

I'm currently applying for Blogging Expert. I didn't pay attention to the blog quality score before. When I applied, I was rejected because of the low blog quality score. So I made a decision to improve the previous blog while producing a high-quality blog. Improve your blog quality score.

Two: Concrete implementation

2.1 csdn interface analysis

If you want to query the quality score of your own csdn in batches, it mainly involves 2 interfaces. The first must be the interface of our query quality score. The other is that we need to get all the url addresses of our blogs to perform corresponding information queries. Let's find these two interfaces.

  • CSDN query quality score interface
    insert image description here
    We directly open the address of CSDN query blog quality score, enter the address of one of your own blogs, click OK, trigger the query blog quality score interface, open the console to find it. I found that the first interface is what I need, because I just want to enter the url address of a csdn blog to get the corresponding blog quality score.
  • Get all your own information interface
    After analyzing the above, we have found the result we need, so the next step is to find the url address of your blog. Before looking, we can think about the direction, where may have the url addresses of all our blogs. Is it possible to return this information on all my blog addresses and my homepage. Let's find the corresponding interface
    insert image description here
    Through the return value of the interface on the homepage, we found this interface. The address of the interface is:
    https://blog.csdn.net/community/home-api/v1/get-business-list?page=1&size=20&businessType=blog&orderby=&noMore=false&year=&month=&username=hlzdbk
    This interface is a paging query. I tested it and it can only return a maximum of one hundred items each time . We can modify the size of the page and size according to the number of our blogs. Paging query own blog address. Of course, don't forget to change the username to your own.

2.2 apiPost call interface

If we want to call the csdn interface in batches through code, we must first ensure that the APIPost call is successful. Of course postMan is also available. Calling the general description interface on these tools is no problem, and it supports our calling. Only with such a basis can we write code.
insert image description here
Here, look at the request header of the corresponding interface by yourself, what is put in the request header, and try which ones are necessary and which ones are not. Keep trying until the corresponding interface is tuned.
For example, take getting all blogs as an example
insert image description here
. At this point, we have analyzed the whole process, and the rest is how to implement this function through code.

2.2 RestTemplate makes interface calls

What I use here is the interface call made by spring's RestTemplate, because I did a research in this area before, and I happened to have a small project for my own testing, so I don't need to create a new project, just develop it directly. Here is the code for everyone.

2.2.1 Maven dependencies that need to be introduced

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.3.12.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>RestTemplate</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>RestTemplate</name>
	<description>RestTemplate</description>
	<properties>
		<java.version>1.8</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.73</version>
		</dependency>

		<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>
	</dependencies>
</project>

This mainly introduces the dependencies of springboot, the dependencies of analyzing json, and the dependencies needed to export Excel.

I will give the code of the main core implementation here. As for the code from the controller all the way to the service, I will not post it here.

2.2.2 Call

public String csdn(UserModel2 userModel2){
    
    

        Integer blogNum = userModel2.getBlogNum();
        Integer pageNum = 100;

        Integer page = blogNum/pageNum + 1 ;
        List<ScoreModel> modelList = new ArrayList<>();
        for (int i = 1; i <= page; i++) {
    
    
            List<ScoreModel> scoreModels = csdnCheck(String.valueOf(i), userModel2.getUserId());
            modelList.addAll(scoreModels);
        }
        save(modelList, userModel2.getUserId());
        return "success";
    }

The userModel2 here puts two attributes, one is the csdn user id, and the other is the total number of blogs
insert image description here

2.2.2 Two interfaces

    public List<ScoreModel>  csdnCheck(String page,String userId){
    
    
        //首先查询所有的文章
        String url = "https://blog.csdn.net/community/home-api/v1/get-business-list?page="+page+"&size=500&businessType=blog&orderby=&noMore=false&year=&month=&username="+userId;
        System.out.println(url);
        JSONObject forObject = restTemplate.getForObject(url, JSONObject.class);
        LinkedHashMap data = (LinkedHashMap)forObject.get("data");
        ArrayList list = (ArrayList)data.get("list");

        List<String> urlList = new ArrayList<>();
        for (Object o : list) {
    
    
            LinkedHashMap one = (LinkedHashMap) o;
            urlList.add(String.valueOf(one.get("url")));
        }
//
//        //循环调用csdn接口查询所有的博客质量分
        String urlScore = "https://bizapi.csdn.net/trends/api/v1/get-article-score";
//
        //请求头
        HttpHeaders headers = new HttpHeaders();
        headers.set("accept","application/json, text/plain, */*");
        headers.set("x-ca-key","203930474");
        headers.set("x-ca-nonce","22cd11a0-760a-45c1-8089-14e53123a852");
        headers.set("x-ca-signature","RaEczPkQ22Ep/k9/AI737gCtn8qX67CV/uGdhQiPIdQ=");
        headers.set("x-ca-signature-headers","x-ca-key,x-ca-nonce");
        headers.set("x-ca-signed-content-type","multipart/form-data");

        headers.setContentType(MediaType.MULTIPART_FORM_DATA);


        //调用接口获取数据
        List<ScoreModel> scoreModels = new ArrayList<>();
        for (String bkUrl : urlList) {
    
    
            MultiValueMap<String,String> requestBody = new LinkedMultiValueMap<>();
            requestBody.put("url", Collections.singletonList(bkUrl));
            HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(requestBody, headers);

            URI uri = URI.create(urlScore);

            ResponseEntity<String> responseEntity = restTemplate.postForEntity(uri, requestEntity, String.class);
            JSONObject data1 = JSON.parseObject(responseEntity.getBody(),JSONObject.class) ;
            ScoreModel scoreModel = JSONObject.parseObject(data1.get("data").toString(),ScoreModel.class);
            scoreModels.add(scoreModel);
            System.out.println("名称:  "+scoreModel.getTitle() +"分数:   " + scoreModel.getScore() +"时间:  " + scoreModel.getPost_time());
        }
        return scoreModels;
    }

Here I define a model myself. This model is the data item I want to query, which are grades, name, and release time. The rest is to send the corresponding interface request through the RestTemplate, first find all the url addresses, and then query all the scores through the url addresses, put the data into the model I created, assemble it into a list and return it.

2.2.3 Export Excel to your computer

public void save(List<ScoreModel> scoreModels ,String userId){
    
    
        //创建Excel工作簿对象
        Workbook workbook = new XSSFWorkbook();
        String sheetName = "Sheet1";
        workbook.createSheet(sheetName);

        // 将数据写入工作表中
        int rowIndex = 0;
        Row row = workbook.getSheet(sheetName).createRow(rowIndex++);
        Cell cell1 = row.createCell(0);
        cell1.setCellValue("名称");
        Cell cell2 = row.createCell(1);
        cell2.setCellValue("成绩");
        Cell cell3 = row.createCell(2);
        cell3.setCellValue("时间");

        for (ScoreModel person : scoreModels) {
    
    
            row = workbook.getSheet(sheetName).createRow(rowIndex++);
            try {
    
    
                BeanUtils.setProperty(person, "title", person.getTitle());
                BeanUtils.setProperty(person, "score", person.getScore());
                BeanUtils.setProperty(person, "post_time", person.getPost_time());

                row.createCell(0).setCellValue(person.getTitle());
                row.createCell(1).setCellValue(person.getScore());
                row.createCell(2).setCellValue(person.getPost_time());
            } catch (Exception e) {
    
    
                e.printStackTrace();
            }
        }


        // 保存 Excel 文件到本地磁盘
        try (FileOutputStream outputStream = new FileOutputStream("D:\\DeskTop\\"+userId+".xlsx")) {
    
    
            workbook.write(outputStream);
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }

Create an Excel workbook object here, and save the corresponding data in Excel. If you want to use it, don’t forget to modify the local export path. I exported it directly to the desktop.

2.2.4 Effect

insert image description here

Three: Summary and Improvement

Unwilling is the driving force behind discovery. You can also query your own blog quality score by sending your own url address one by one CV to the interface for querying blog quality scores given by CSDN. If I choose to do so, then I will not study how to realize the function of querying the quality score of my blog in batches through code, and I will lose a chance to learn how to use RestTemplate and how to export Excel tables.
Through this article, you can query the quality of your own blogs in batches and help us modify the average quality of our original blogs.

Guess you like

Origin blog.csdn.net/hlzdbk/article/details/130554646