请求方法里使用body里的payload指定post请求时数据的提交方式;优化创建任务失败判断逻辑

This commit is contained in:
chuzhongzai 2023-12-23 15:55:36 +08:00
parent f451aaaf9c
commit 1db8cdf264
2 changed files with 26 additions and 33 deletions

View File

@ -108,9 +108,9 @@ public class GalleryManageService {
//尝试下载本子返回结果 //尝试下载本子返回结果
try { try {
gallery = GalleryUtil.parse(link, true, targetResolution); gallery = GalleryUtil.parse(link, true, targetResolution);
if (gallery == null) { if (gallery == null || !gallery.getStatus().equals("已提交")) {
log.error("创建任务: {}解析失败", link); log.error("创建任务失败: {}", link);
response.failure("任务解析失败,未知原因,请检查链接是否正常"); response.failure("提交任务失败,未知原因,请检查链接是否正常");
pushService.taskCreateReport(user.getUsername(), link, response); pushService.taskCreateReport(user.getUsername(), link, response);
return response.toJSONString(); return response.toJSONString();
} else { } else {

View File

@ -26,6 +26,10 @@ public class GalleryUtil {
static String POST = "post"; static String POST = "post";
static String GET = "get"; static String GET = "get";
static String FORM_DATA = "formData";
static String JSON = "json";
public static ArrayList<Gallery> galleriesForDownload; public static ArrayList<Gallery> galleriesForDownload;
static { static {
@ -69,13 +73,12 @@ public class GalleryUtil {
//查找下载页面链接并初始化参数 //查找下载页面链接并初始化参数
String download_link = galleryDoc.select("#gd5 > p:nth-child(2) > a").attr("onclick").split("'")[1]; String download_link = galleryDoc.select("#gd5 > p:nth-child(2) > a").attr("onclick").split("'")[1];
HashMap<String, String> extraProperties = new HashMap<>(); HashMap<String, String> headers = new HashMap<>();
extraProperties.put("origin", origin); headers.put("origin", origin);
extraProperties.put("referer", download_link); 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); Document downloadDoc = Jsoup.parse(downloadPage);
Elements resolutions = downloadDoc.select("#db > div > table > tbody > tr > td"); Elements resolutions = downloadDoc.select("#db > div > table > tbody > tr > td");
download_link = downloadDoc.select("#hathdl_form").attr("action"); download_link = downloadDoc.select("#hathdl_form").attr("action");
@ -101,10 +104,8 @@ public class GalleryUtil {
} }
availableResolution.put("Original", tempResolutions.get("Original")); availableResolution.put("Original", tempResolutions.get("Original"));
gallery.setAvailableResolution(availableResolution); gallery.setAvailableResolution(availableResolution);
//如果不是下载则直接返回 //如果不是下载则直接返回
if(!isDownload){ if(!isDownload){
gallery.setStatus("等待确认下载"); gallery.setStatus("等待确认下载");
@ -123,8 +124,9 @@ public class GalleryUtil {
//提交下载请求 //提交下载请求
HashMap<String, String> body = new HashMap<>(); HashMap<String, String> body = new HashMap<>();
body.put("payload", FORM_DATA);
body.put("hathdl_xres", targetResolution.replace("x", "").replace("Original", "org")); 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); 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 链接 * @param url 链接
@ -188,25 +178,28 @@ public class GalleryUtil {
if(method.equals(GET)){ if(method.equals(GET)){
HttpGet httpGet = new HttpGet(url); HttpGet httpGet = new HttpGet(url);
for (Map.Entry<String, String> header: headers.entrySet()){ for (Map.Entry<String, String> header: headers.entrySet())
httpGet.addHeader(header.getKey(), header.getValue()); httpGet.addHeader(header.getKey(), header.getValue());
}
httpResponse = httpClient.execute(httpGet); httpResponse = httpClient.execute(httpGet);
} else { } else {
HttpPost httpPost = new HttpPost(url); HttpPost httpPost = new HttpPost(url);
for (Map.Entry<String, String> header: headers.entrySet()){ for (Map.Entry<String, String> header: headers.entrySet())
httpPost.addHeader(header.getKey(), header.getValue()); httpPost.addHeader(header.getKey(), header.getValue());
}
if(body != null) { if(body != null) {
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(); MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
body.forEach(multipartEntityBuilder::addTextBody); body.forEach(multipartEntityBuilder::addTextBody);
// EntityBuilder entityBuilder = EntityBuilder.create();
// entityBuilder.setContentType(ContentType.APPLICATION_JSON);
// entityBuilder.setText(CustomUtil.objectMapper.writeValueAsString(body));
httpPost.setEntity(multipartEntityBuilder.build()); httpPost.setEntity(multipartEntityBuilder.build());
} }
}
httpResponse = httpClient.execute(httpPost); httpResponse = httpClient.execute(httpPost);
} }
HttpEntity responseEntity = httpResponse.getEntity(); HttpEntity responseEntity = httpResponse.getEntity();