From 22e74e8cd5059113a4b77788e501cd3f6933be08 Mon Sep 17 00:00:00 2001 From: LamGC Date: Sun, 26 Apr 2020 15:53:39 +0800 Subject: [PATCH] =?UTF-8?q?[Add]=20PixivIllustType=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E4=BA=86=E5=AF=B9IllustInfo=E7=9A=84=E8=A7=A3=E6=9E=90;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../net/lamgc/cgj/pixiv/PixivDownload.java | 19 +++++++++++++++++- .../lamgc/cgj/pixiv/PixivDownloadTest.java | 20 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/main/java/net/lamgc/cgj/pixiv/PixivDownload.java b/src/main/java/net/lamgc/cgj/pixiv/PixivDownload.java index 3589eeb..2f1c74a 100644 --- a/src/main/java/net/lamgc/cgj/pixiv/PixivDownload.java +++ b/src/main/java/net/lamgc/cgj/pixiv/PixivDownload.java @@ -599,12 +599,29 @@ public class PixivDownload { return null; } + /** + * 通过预加载数据获取作品类型 + * @param illustId 作品Id + * @param preLoadDataObject 预加载数据(IllustInfo也可以) + * @return 如果存在illustType属性, 则返回对应项, 如没有, 或数据内不存在指定作品id的数据, 返回null + */ public static PixivIllustType getIllustTypeByPreLoadData(int illustId, JsonObject preLoadDataObject) { - JsonObject illustData; + JsonObject illustData = null; if(preLoadDataObject.has("illust")) { illustData = preLoadDataObject.getAsJsonObject("illust").getAsJsonObject(String.valueOf(illustId)); } else if(preLoadDataObject.has(String.valueOf(illustId))) { illustData = preLoadDataObject.getAsJsonObject(String.valueOf(illustId)); + } else if(preLoadDataObject.has("body")) { // 解析IllustInfo + for (JsonElement jsonElement : preLoadDataObject.getAsJsonObject("body").getAsJsonArray("illusts")) { + JsonObject illustInfo = jsonElement.getAsJsonObject(); + if (illustInfo.get("illustId").getAsInt() == illustId) { + illustData = illustInfo; + break; + } + } + if(illustData == null) { + return null; + } } else { illustData = preLoadDataObject; } diff --git a/src/test/java/net/lamgc/cgj/pixiv/PixivDownloadTest.java b/src/test/java/net/lamgc/cgj/pixiv/PixivDownloadTest.java index 74515c6..9745fed 100644 --- a/src/test/java/net/lamgc/cgj/pixiv/PixivDownloadTest.java +++ b/src/test/java/net/lamgc/cgj/pixiv/PixivDownloadTest.java @@ -1,8 +1,15 @@ package net.lamgc.cgj.pixiv; +import com.google.gson.Gson; +import com.google.gson.JsonObject; import org.apache.commons.io.IOUtils; import org.apache.http.HttpHost; import org.apache.http.client.CookieStore; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.util.EntityUtils; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Ignore; @@ -202,4 +209,17 @@ public class PixivDownloadTest { log.info(new PixivDownload(cookieStore, proxy).getIllustPreLoadDataById(64076261).toString()); } + @Test + public void illustInfoTest() throws IOException { + CloseableHttpClient httpClient = HttpClientBuilder.create().setProxy(new HttpHost("127.0.0.1", 1001)).build(); + HttpGet request = new HttpGet(PixivURL.getPixivIllustInfoAPI(80880547)); + CloseableHttpResponse response = httpClient.execute(request); + String body = EntityUtils.toString(response.getEntity()); + JsonObject illustInfoResult = new Gson().fromJson(body, JsonObject.class); + log.info("IllustInfoJsonResult: {}", illustInfoResult); + PixivDownload.PixivIllustType illustType = PixivDownload.PixivIllustType.getIllustTypeByPreLoadData(80880547, illustInfoResult); + log.info("IllustType: {}", illustType); + Assert.assertEquals(PixivDownload.PixivIllustType.UGOIRA, illustType); + } + }