30
30
import javax .xml .transform .stream .StreamResult ;
31
31
import lombok .CustomLog ;
32
32
import net .cofcool .sourcebox .Tool ;
33
+ import net .cofcool .sourcebox .ToolContext ;
33
34
import net .cofcool .sourcebox .ToolName ;
34
35
import org .apache .commons .io .FileUtils ;
35
36
import org .apache .commons .io .FilenameUtils ;
@@ -57,6 +58,7 @@ public class HtmlDownloader implements Tool {
57
58
private Set <OutputType > outputTypes = EnumSet .of (OutputType .html );
58
59
private Proxy proxy ;
59
60
private String filter ;
61
+ private ToolContext context ;
60
62
61
63
private Connection connection ;
62
64
@@ -68,6 +70,7 @@ public ToolName name() {
68
70
@ Override
69
71
public void run (Args args ) throws Exception {
70
72
var urls = new ArrayList <String >();
73
+ context = args .getContext ();
71
74
72
75
args .readArg ("urlFile" ).ifPresent (a -> {
73
76
try {
@@ -171,7 +174,7 @@ private void downloadUrl(String folder, String url, int depth, String expression
171
174
var dir = Paths .get (folder , title ).toFile ();
172
175
folder = dir .toString ();
173
176
FileUtils .forceMkdir (dir );
174
- log . info ( "Create dir {0} " , dir );
177
+ context . write ( String . format ( "Create dir %s " , dir ) );
175
178
}
176
179
177
180
if (clean ) {
@@ -185,7 +188,7 @@ private void downloadUrl(String folder, String url, int depth, String expression
185
188
if (filter == null || title .contains (filter )) {
186
189
for (OutputType type : outputTypes ) {
187
190
type .applyOutput (doc , folder , title );
188
- log . info ( "Save {0} file to {1} from <<{2} >>: {3} " , type , folder , title , url );
191
+ context . write ( String . format ( "Save %s file to %s from <<%s >>: %s " , type , folder , title , url ) );
189
192
}
190
193
}
191
194
@@ -204,7 +207,7 @@ private void downloadUrl(String folder, String url, int depth, String expression
204
207
try {
205
208
downloadUrl (folder , href , depth , "false" );
206
209
} catch (Exception e ) {
207
- log . error ( "Download " + href , e );
210
+ context . write ( String . format ( "Download %s error: %s" , href , e . getMessage ()) );
208
211
}
209
212
}
210
213
}
@@ -233,7 +236,7 @@ private void downloadImages(Elements imgs, String folder, String expression) thr
233
236
var splitIdx = src .indexOf ("," );
234
237
imgData = Base64 .getDecoder ().decode (src .substring (splitIdx + 1 ));
235
238
} else {
236
- log . info ( "Image url {0} is invalid" , src );
239
+ context . write ( String . format ( "Image url %s is invalid" , src ) );
237
240
continue ;
238
241
}
239
242
@@ -242,9 +245,9 @@ private void downloadImages(Elements imgs, String folder, String expression) thr
242
245
File file = Paths .get (folder , name ).toFile ();
243
246
FileUtils .writeByteArrayToFile (file , imgData );
244
247
img .attr ("src" , String .join ("/" , "." , IMGS_FOLDER , name ));
245
- log . info ( "Download image from {0} " , file );
248
+ context . write ( String . format ( "Download image from %s " , file ) );
246
249
} catch (Exception e ) {
247
- log . error ( "Download " + src + " image error" , e );
250
+ context . write ( String . format ( "Download %s image error: %s " , src , e . getMessage ()) );
248
251
}
249
252
}
250
253
}
@@ -372,17 +375,17 @@ private static class ToEpub implements Output {
372
375
private static final String START_HTML = """
373
376
<?xml version='1.0' encoding='utf-8'?>
374
377
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh">
375
-
378
+
376
379
<head>
377
380
<title id="toc_1">%s</title>
378
381
</head>
379
-
382
+
380
383
<body>
381
-
384
+
382
385
<h1>%s</h1>
383
-
386
+
384
387
</body>
385
-
388
+
386
389
</html>
387
390
""" ;
388
391
@@ -473,6 +476,7 @@ public void finished() {
473
476
navMap .appendChild (navPoint );
474
477
}
475
478
479
+
476
480
var transformer = TransformerFactory .newInstance ().newTransformer ();
477
481
478
482
var outputStream = new ByteArrayOutputStream ();
@@ -487,7 +491,7 @@ public void finished() {
487
491
488
492
IOUtils .closeQuietly (zos );
489
493
} catch (Exception e ) {
490
- log . error ("Save epub file error" , e );
494
+ throw new RuntimeException ("Save epub file error" , e );
491
495
}
492
496
zos = null ;
493
497
files = null ;
0 commit comments