From 1db8cdf2640dd5b5101cfc78d89dd4b2ebeb8809 Mon Sep 17 00:00:00 2001 From: chuzhongzai Date: Sat, 23 Dec 2023 15:55:36 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=B7=E6=B1=82=E6=96=B9=E6=B3=95=E9=87=8C?= =?UTF-8?q?=E4=BD=BF=E7=94=A8body=E9=87=8C=E7=9A=84payload=E6=8C=87?= =?UTF-8?q?=E5=AE=9Apost=E8=AF=B7=E6=B1=82=E6=97=B6=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E7=9A=84=E6=8F=90=E4=BA=A4=E6=96=B9=E5=BC=8F;=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E5=88=9B=E5=BB=BA=E4=BB=BB=E5=8A=A1=E5=A4=B1=E8=B4=A5?= =?UTF-8?q?=E5=88=A4=E6=96=AD=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Service/GalleryManageService.java | 6 +-- .../lion/lionwebsite/Util/GalleryUtil.java | 53 ++++++++----------- 2 files changed, 26 insertions(+), 33 deletions(-) diff --git a/src/main/java/com/lion/lionwebsite/Service/GalleryManageService.java b/src/main/java/com/lion/lionwebsite/Service/GalleryManageService.java index 7fb0ffe..b6aac9d 100644 --- a/src/main/java/com/lion/lionwebsite/Service/GalleryManageService.java +++ b/src/main/java/com/lion/lionwebsite/Service/GalleryManageService.java @@ -108,9 +108,9 @@ public class GalleryManageService { //尝试下载本子,返回结果 try { gallery = GalleryUtil.parse(link, true, targetResolution); - if (gallery == null) { - log.error("创建任务: {},解析失败", link); - response.failure("任务解析失败,未知原因,请检查链接是否正常"); + if (gallery == null || !gallery.getStatus().equals("已提交")) { + log.error("创建任务失败: {},", link); + response.failure("提交任务失败,未知原因,请检查链接是否正常"); pushService.taskCreateReport(user.getUsername(), link, response); return response.toJSONString(); } else { diff --git a/src/main/java/com/lion/lionwebsite/Util/GalleryUtil.java b/src/main/java/com/lion/lionwebsite/Util/GalleryUtil.java index c79c30a..3a9f2d1 100644 --- a/src/main/java/com/lion/lionwebsite/Util/GalleryUtil.java +++ b/src/main/java/com/lion/lionwebsite/Util/GalleryUtil.java @@ -26,6 +26,10 @@ public class GalleryUtil { static String POST = "post"; static String GET = "get"; + static String FORM_DATA = "formData"; + + static String JSON = "json"; + public static ArrayList galleriesForDownload; static { @@ -69,13 +73,12 @@ public class GalleryUtil { //查找下载页面链接并初始化参数 String download_link = galleryDoc.select("#gd5 > p:nth-child(2) > a").attr("onclick").split("'")[1]; - HashMap extraProperties = new HashMap<>(); - extraProperties.put("origin", origin); - extraProperties.put("referer", download_link); - + HashMap headers = new HashMap<>(); + headers.put("origin", origin); + headers.put("referer", download_link); //访问下载页面,获取分辨率 - String downloadPage = requests(download_link, GET, extraProperties, null); + String downloadPage = requests(download_link, GET, headers, null); Document downloadDoc = Jsoup.parse(downloadPage); Elements resolutions = downloadDoc.select("#db > div > table > tbody > tr > td"); download_link = downloadDoc.select("#hathdl_form").attr("action"); @@ -101,10 +104,8 @@ public class GalleryUtil { } availableResolution.put("Original", tempResolutions.get("Original")); - gallery.setAvailableResolution(availableResolution); - //如果不是下载,则直接返回 if(!isDownload){ gallery.setStatus("等待确认下载"); @@ -123,8 +124,9 @@ public class GalleryUtil { //提交下载请求 HashMap body = new HashMap<>(); + body.put("payload", FORM_DATA); body.put("hathdl_xres", targetResolution.replace("x", "").replace("Original", "org")); - downloadPage = requests(download_link, POST, extraProperties, body); + downloadPage = requests(download_link, POST, headers, body); downloadDoc = Jsoup.parse(downloadPage); //判断下载请求是否提交成功 @@ -139,18 +141,6 @@ public class GalleryUtil { } - public static String queryUpdateLink(String link) throws IOException{ - String page = requests(link, GET, null, null); - Document document = Jsoup.parse(page); - - Elements select = document.select("#gnd"); - if(!select.isEmpty()) - return select.select("a").getFirst().attributes().get("href"); - - return null; - } - - /** * 验证链接 * @param url 链接 @@ -188,24 +178,27 @@ public class GalleryUtil { if(method.equals(GET)){ HttpGet httpGet = new HttpGet(url); - for (Map.Entry header: headers.entrySet()){ + for (Map.Entry header: headers.entrySet()) httpGet.addHeader(header.getKey(), header.getValue()); - } httpResponse = httpClient.execute(httpGet); } else { HttpPost httpPost = new HttpPost(url); - for (Map.Entry header: headers.entrySet()){ + for (Map.Entry header: headers.entrySet()) httpPost.addHeader(header.getKey(), header.getValue()); - } if(body != null) { - MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create(); - body.forEach(multipartEntityBuilder::addTextBody); -// EntityBuilder entityBuilder = EntityBuilder.create(); -// entityBuilder.setContentType(ContentType.APPLICATION_JSON); -// entityBuilder.setText(CustomUtil.objectMapper.writeValueAsString(body)); - httpPost.setEntity(multipartEntityBuilder.build()); + String payload; + if((payload = body.remove("payload")).equals(JSON)) { + EntityBuilder entityBuilder = EntityBuilder.create(); + entityBuilder.setContentType(ContentType.APPLICATION_JSON); + entityBuilder.setText(CustomUtil.objectMapper.writeValueAsString(body)); + httpPost.setEntity(entityBuilder.build()); + } else if(payload.equals(FORM_DATA)) { + MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create(); + body.forEach(multipartEntityBuilder::addTextBody); + httpPost.setEntity(multipartEntityBuilder.build()); + } } httpResponse = httpClient.execute(httpPost); }