请求方法里使用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 {
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 {

View File

@ -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<Gallery> 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<String, String> extraProperties = new HashMap<>();
extraProperties.put("origin", origin);
extraProperties.put("referer", download_link);
HashMap<String, String> 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<String, String> 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<String, String> header: headers.entrySet()){
for (Map.Entry<String, String> header: headers.entrySet())
httpGet.addHeader(header.getKey(), header.getValue());
}
httpResponse = httpClient.execute(httpGet);
} else {
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());
}
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);
}