feat: 新增github stars查询

This commit is contained in:
ClovertaTheTrilobita 2025-04-20 15:35:25 +08:00
parent f3a1d5a4c5
commit 9ddba89441
9 changed files with 176 additions and 3 deletions

View file

@ -89,6 +89,13 @@
<artifactId>jackson-databind</artifactId> <artifactId>jackson-databind</artifactId>
<version>2.15.3</version> <version>2.15.3</version>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/jakarta.servlet/jakarta.servlet-api -->
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>6.0.0</version>
<scope>provided</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>

View file

@ -2,10 +2,13 @@ package com.cloverta.webapi.controller;
import com.cloverta.webapi.restservice.BiliApi; import com.cloverta.webapi.restservice.BiliApi;
import com.cloverta.webapi.service.BiliApiService; import com.cloverta.webapi.service.BiliApiService;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@ResponseStatus(HttpStatus.OK)
@RestController @RestController
public class BiliApiController { public class BiliApiController {
private final BiliApiService biliApiService; private final BiliApiService biliApiService;

View file

@ -1,14 +1,20 @@
package com.cloverta.webapi.controller; package com.cloverta.webapi.controller;
import com.cloverta.webapi.restservice.Error; import com.cloverta.webapi.restservice.Error;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.boot.web.servlet.error.ErrorController; import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@RestController @RestController
public class CustomErrorController implements ErrorController { public class CustomErrorController implements ErrorController {
@RequestMapping("/error") @RequestMapping("/error")
public Error error() { public Error error(HttpServletRequest request) {
return new Error("ERROR", "Something went wrong... I hope it wasn't my fault."); String message = (String) request.getAttribute("javax.servlet.error.message");
if (message == null) {
message = "Something went wrong...";
}
return new Error("ERROR", message);
} }
} }

View file

@ -0,0 +1,28 @@
package com.cloverta.webapi.controller;
import com.cloverta.webapi.exception.GithubException;
import com.cloverta.webapi.restservice.GithubStars;
import com.cloverta.webapi.service.GithubStarsService;
import com.cloverta.webapi.service.GreetingService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GithubStarsController {
private final GithubStarsService githubStarsService;
public GithubStarsController(GithubStarsService githubStarsService) {
this.githubStarsService = githubStarsService;
}
@GetMapping("/github/stars")
public GithubStars getGithubStars(@RequestParam(value = "user", required = true) String user) {
try {
return githubStarsService.getGithubStars(user);
} catch (Exception e) {
throw new GithubException(e.getMessage());
}
}
}

View file

@ -0,0 +1,7 @@
package com.cloverta.webapi.exception;
public class GithubException extends RuntimeException {
public GithubException(String message) {
super(message);
}
}

View file

@ -0,0 +1,23 @@
package com.cloverta.webapi.handler;
import com.cloverta.webapi.exception.GithubException;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(GithubException.class)
public String handleGithubException(GithubException ex, HttpServletRequest request) {
request.setAttribute("javax.servlet.error.message", ex.getMessage());
return "forward:/error";
}
@ExceptionHandler(Exception.class)
public String handleGeneralException(Exception ex, HttpServletRequest request) {
request.setAttribute("javax.servlet.error.message", ex.getMessage());
return "forward:/error";
}
}

View file

@ -0,0 +1,4 @@
package com.cloverta.webapi.restservice;
public record GithubStars(String status, String message, int stars) {
}

View file

@ -51,7 +51,7 @@ public class BiliApiService {
JSONObject root = new JSONObject(jsonResponse); JSONObject root = new JSONObject(jsonResponse);
JSONObject data; JSONObject data;
data = root.getJSONObject("data"); data = root.getJSONObject("data");
String bvId = data.getString("bvid"); String bvId = data.getString("bvid");
String name = data.getString("title"); String name = data.getString("title");

View file

@ -0,0 +1,95 @@
package com.cloverta.webapi.service;
import com.cloverta.webapi.model.BiliVid;
import com.cloverta.webapi.restservice.GithubStars;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Service;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
@Service
public class GithubStarsService {
public GithubStars generateGithubStars(boolean status, String message, int stars){
if (status) {
return new GithubStars("OK", message, stars);
}else {
return new GithubStars("ERROR", message, stars);
}
}
public GithubStars getGithubStars(String userName) {
HttpURLConnection connection = null;
InputStream is = null;
BufferedReader br = null;
StringBuilder result = new StringBuilder();
try {
//创建连接
URL url = new URL(String.format("https://api.github.com/users/%s/repos", userName));
connection = (HttpURLConnection) url.openConnection();
//设置请求方式
connection.setRequestMethod("GET");
//设置连接超时时间
connection.setReadTimeout(15000);
//开始连接
connection.connect();
//获取响应数据
if (connection.getResponseCode() == 200) {
//获取返回的数据
is = connection.getInputStream();
if (null != is) {
br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
String temp = null;
while (null != (temp = br.readLine())) {
result.append(temp);
}
}
}
String jsonResponse = result.toString();
JSONArray root = new JSONArray(jsonResponse);
int stars = 0;
for (Object info : root){
JSONObject obj = (JSONObject) info;
if (!obj.getBoolean("fork")){
stars += obj.getInt("stargazers_count");
}
}
return generateGithubStars(true, "success", stars);
} catch (IOException e) {
e.printStackTrace();
return generateGithubStars(false, "Cant access data: https://api.github.com unreachable. Please contact cloverta@petalmail.com for further support.", -1);
}catch (org.json.JSONException e) {
e.printStackTrace();
return generateGithubStars(false, "Invalid User Name: Please check whether there was a typo or whether the user exists.", -1);
} finally {
if (null != br) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != is) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//关闭远程连接
if (connection != null) {
connection.disconnect();
}
}
}
}