Skip to content

Commit

Permalink
⚡ 无下载章节时不生成epub
Browse files Browse the repository at this point in the history
  • Loading branch information
freeok committed Aug 10, 2024
1 parent 1a11d9e commit a5ec9b0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
15 changes: 11 additions & 4 deletions src/main/java/com/pcdd/sonovel/core/CrawlerPostHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.util.Arrays;
import java.util.List;

import static org.fusesource.jansi.AnsiRenderer.render;

/**
* @author pcdd
*/
Expand Down Expand Up @@ -56,6 +58,11 @@ public void handle(String extName, Book book, File saveDir) {

@SneakyThrows
private void convertToEpub(File dir, Book b) {
if (FileUtil.isDirEmpty(dir)) {
Console.error(render("==> @|red 《{}》({})下载章节数为 0,取消生成 epub|@"), b.getBookName(), b.getAuthor());
return;
}

io.documentnode.epub4j.domain.Book book = new io.documentnode.epub4j.domain.Book();
book.getMetadata().addTitle(b.getBookName());
book.getMetadata().addAuthor(new Author(b.getAuthor()));
Expand All @@ -70,7 +77,7 @@ private void convertToEpub(File dir, Book b) {

int i = 1;
// 遍历下载后的目录,添加章节
for (File file : files(dir)) {
for (File file : sortFilesByName(dir)) {
// 截取第一个 _ 后的字符串,即章节名
String title = StrUtil.subAfter(FileUtil.mainName(file), "_", false);

Expand All @@ -94,7 +101,7 @@ private void mergeTxt(File dir, String... args) {
FileAppender appender = new FileAppender(file, 16, true);
appender.append("文件名\t章节名");

for (File item : files(dir)) {
for (File item : sortFilesByName(dir)) {
String s = FileUtil.readString(item, StandardCharsets.UTF_8);
appender.append(s);
}
Expand All @@ -103,7 +110,7 @@ private void mergeTxt(File dir, String... args) {

private static void generateCatalog(File saveDir) {
List<String> strings = new ArrayList<>();
List<File> files = files(saveDir);
List<File> files = sortFilesByName(saveDir);
String regex = "<title>(.*?)</title>";

strings.add("文件名\t\t章节名");
Expand All @@ -121,7 +128,7 @@ private static void generateCatalog(File saveDir) {
}

// 文件排序,按文件名升序
private List<File> files(File dir) {
private List<File> sortFilesByName(File dir) {
return Arrays.stream(dir.listFiles())
.sorted((o1, o2) -> {
String s1 = o1.getName();
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/pcdd/sonovel/parse/ChapterParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ public Chapter parse(Chapter chapter, SearchResult sr, CountDownLatch latch) {

} catch (Exception e) {
latch.countDown();
Console.error(render("==> @|red 章节下载失败:【{}】({}),原因:{}|@"), chapter.getTitle(), chapter.getUrl(), e.getMessage());
saveErrorLog(chapter, sr, e.getMessage());
// TODO retry
}
Expand Down

0 comments on commit a5ec9b0

Please sign in to comment.