Skip to content

Commit

Permalink
确保配置文件中配置为空时程序能够正常识别
Browse files Browse the repository at this point in the history
  • Loading branch information
jiayaoO3O committed Feb 18, 2021
1 parent c220531 commit a6d3427
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
19 changes: 15 additions & 4 deletions src/main/java/vip/comic18/finder/service/AsyncTaskService.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public Future<BufferedImage> getImage(String url) {
while(httpResponse == null || bufferedImage == null) {
try {
ThreadUtil.sleep(RandomUtil.randomInt(2) * 1000L);
httpResponse = HttpUtil.createPost(url).cookie(cookie).setHttpProxy(proxyHost, proxyPort).execute();
httpResponse = this.createPost(url).execute();
bufferedImage = ImgUtil.read(httpResponse.bodyStream());
} catch(Exception e) {
log.error("getImage->下载图片失败:[{}]", e.getLocalizedMessage(), e);
Expand Down Expand Up @@ -151,18 +151,29 @@ public void saveImage(String path, BufferedImage bufferedImage) {
}

@Async
public void saveImage(HttpRequest httpRequest, File photoFile) {
public void saveImage(String url, File photoFile) {
HttpResponse httpResponse = null;
while(httpResponse == null) {
try {
ThreadUtil.sleep(RandomUtil.randomInt(2) * 1000L);
httpResponse = httpRequest.cookie(cookie).setHttpProxy(proxyHost, proxyPort).execute();
log.info("getImage->获取图片:[{}]成功", httpRequest.getUrl());
httpResponse = this.createPost(url).execute();
log.info("getImage->获取图片:[{}]成功", url);
} catch(Exception e) {
log.error("saveImage->下载图片失败:[{}]", e.getLocalizedMessage(), e);
}
}
httpResponse.writeBody(photoFile);
log.info("saveImage->保存图片:[{}]成功", photoFile.getPath());
}

public HttpRequest createPost(String url) {
HttpRequest post = HttpUtil.createPost(url);
if((proxyHost != null && proxyPort == 0)) {
post.setHttpProxy(proxyHost, proxyPort);
}
if(cookie != null) {
post.cookie(cookie);
}
return post;
}
}
18 changes: 6 additions & 12 deletions src/main/java/vip/comic18/finder/service/ComicService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -30,15 +29,6 @@ public class ComicService {
@Value("${comic.download.path}")
private String downloadPath;

@Value("${comic.proxy.host}")
private String proxyHost;

@Value("${comic.proxy.port}")
private int proxyPort;

@Value("${comic.download.cookie}")
private String cookie;


/**
* 下载漫画到本地
Expand All @@ -48,6 +38,10 @@ public class ComicService {
*/
public void downloadComic(ComicEntity comicEntity) throws ExecutionException, InterruptedException {
String title = comicEntity.getTitle();
if(downloadPath == null) {
log.error("downloadComic->下载路径downloadPath错误,无法下载文件,请确保配置文件中下载路径正确");
return;
}
File comicDir = FileUtil.mkdir(downloadPath + File.separatorChar + title);
for(ChapterEntity chapter : comicEntity.getChapters()) {
File chapterDir = FileUtil.file(comicDir.getPath() + File.separatorChar + chapter.getName());
Expand All @@ -67,7 +61,7 @@ public void downloadComic(ComicEntity comicEntity) throws ExecutionException, In
image = taskService.reverseImage(image).get();
taskService.saveImage(chapterDir.getPath() + File.separatorChar + photo.getName(), image);
}
taskService.saveImage(HttpUtil.createPost(photo.getUrl()), photoFile);
taskService.saveImage(photo.getUrl(), photoFile);
}
}
}
Expand All @@ -80,7 +74,7 @@ public void downloadComic(ComicEntity comicEntity) throws ExecutionException, In
*/
public ComicEntity getComicInfo(String comicHomePage) throws ExecutionException, InterruptedException {
ComicEntity comicEntity = new ComicEntity();
HttpResponse httpResponse = HttpUtil.createPost(comicHomePage).cookie(cookie).setHttpProxy(proxyHost, proxyPort).execute();
HttpResponse httpResponse = taskService.createPost(comicHomePage).execute();
String body = httpResponse.body();
String title = StrUtil.subBetween(body, "<div itemprop=\"name\" class=\"pull-left\">\n", "\n</div>");
title = StrUtil.replaceChars(title, new char[]{'/', '\\'}, StrUtil.DASHED);
Expand Down

0 comments on commit a6d3427

Please sign in to comment.