mirror of
https://github.com/ClovertaTheTrilobita/SanYeCao-WebAPi.git
synced 2026-04-01 23:14:51 +00:00
fix: 完善错误URL异常抛出,添加了更详细的异常提示
This commit is contained in:
parent
764e9ecb1e
commit
f1ea269155
3 changed files with 18 additions and 5 deletions
|
|
@ -15,7 +15,7 @@ public class BiliApiController {
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/bili")
|
@GetMapping("/bili")
|
||||||
public BiliApi BiliApi(@RequestParam(value = "bvid", required = true) String bvid) {
|
public BiliApi BiliApi(@RequestParam(value = "bvid", defaultValue = "empty") String bvid) {
|
||||||
return biliApiService.getPagesInfo(bvid);
|
return biliApiService.getPagesInfo(bvid);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,13 @@
|
||||||
package com.cloverta.webapi.restservice;
|
package com.cloverta.webapi.restservice;
|
||||||
|
|
||||||
import com.cloverta.webapi.model.BiliVid;
|
import com.cloverta.webapi.model.BiliVid;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||||
public record BiliApi(String status,
|
public record BiliApi(String status,
|
||||||
|
String message,
|
||||||
String bvId,
|
String bvId,
|
||||||
String name,
|
String name,
|
||||||
String author,
|
String author,
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,9 @@ import java.util.*;
|
||||||
@Service
|
@Service
|
||||||
public class BiliApiService {
|
public class BiliApiService {
|
||||||
public BiliApi getPagesInfo(String bvid) {
|
public BiliApi getPagesInfo(String bvid) {
|
||||||
|
if (bvid.equals("empty")) {
|
||||||
|
return generateBiliApi("ERROR", "Missing param 'bvid'", null, null, null, null, null, null);
|
||||||
|
}
|
||||||
List<BiliVid> biliVids = new ArrayList<>();
|
List<BiliVid> biliVids = new ArrayList<>();
|
||||||
HttpURLConnection connection = null;
|
HttpURLConnection connection = null;
|
||||||
InputStream is = null;
|
InputStream is = null;
|
||||||
|
|
@ -46,7 +49,10 @@ public class BiliApiService {
|
||||||
|
|
||||||
String jsonResponse = result.toString();
|
String jsonResponse = result.toString();
|
||||||
JSONObject root = new JSONObject(jsonResponse);
|
JSONObject root = new JSONObject(jsonResponse);
|
||||||
JSONObject data = root.getJSONObject("data");
|
JSONObject 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");
|
||||||
String author = data.getJSONObject("owner").getString("name");
|
String author = data.getJSONObject("owner").getString("name");
|
||||||
|
|
@ -73,9 +79,13 @@ public class BiliApiService {
|
||||||
// System.out.println(biliVid.getCid());
|
// System.out.println(biliVid.getCid());
|
||||||
}
|
}
|
||||||
|
|
||||||
return generateBiliApi("OK", bvId, name, author, imageUrl, description, appendVidUrl(biliVids));
|
return generateBiliApi("OK", "success", bvId, name, author, imageUrl, description, appendVidUrl(biliVids));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
return generateBiliApi("ERROR", "Can't access data: api.bilibili.com unreachable. Please contact cloverta@petalmail.com for further support.", null, null, null, null, null, null);
|
||||||
|
}catch (org.json.JSONException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return generateBiliApi("ERROR", "Invalid BVId: Please check whether there was a typo or whether the video exists.", null, null, null, null, null, null);
|
||||||
} finally {
|
} finally {
|
||||||
if (null != br) {
|
if (null != br) {
|
||||||
try {
|
try {
|
||||||
|
|
@ -97,7 +107,6 @@ public class BiliApiService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<BiliVid> appendVidUrl(List<BiliVid> biliVids) {
|
public List<BiliVid> appendVidUrl(List<BiliVid> biliVids) {
|
||||||
|
|
@ -173,13 +182,14 @@ public class BiliApiService {
|
||||||
}
|
}
|
||||||
|
|
||||||
public BiliApi generateBiliApi(String status,
|
public BiliApi generateBiliApi(String status,
|
||||||
|
String message,
|
||||||
String bvId,
|
String bvId,
|
||||||
String name,
|
String name,
|
||||||
String author,
|
String author,
|
||||||
String imageUrl,
|
String imageUrl,
|
||||||
String description,
|
String description,
|
||||||
List<BiliVid> data) {
|
List<BiliVid> data) {
|
||||||
return new BiliApi(status, bvId, name, author, imageUrl, description, data);
|
return new BiliApi(status, message, bvId, name, author, imageUrl, description, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
// public static void main(String[] args) {
|
// public static void main(String[] args) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue