1 package be
.nikiroo
.fanfix
;
4 import java
.io
.IOException
;
5 import java
.net
.MalformedURLException
;
7 import java
.util
.ArrayList
;
10 import javax
.net
.ssl
.SSLException
;
12 import be
.nikiroo
.fanfix
.bundles
.Config
;
13 import be
.nikiroo
.fanfix
.bundles
.StringId
;
14 import be
.nikiroo
.fanfix
.data
.Chapter
;
15 import be
.nikiroo
.fanfix
.data
.Story
;
16 import be
.nikiroo
.fanfix
.library
.BasicLibrary
;
17 import be
.nikiroo
.fanfix
.library
.CacheLibrary
;
18 import be
.nikiroo
.fanfix
.library
.LocalLibrary
;
19 import be
.nikiroo
.fanfix
.library
.RemoteLibrary
;
20 import be
.nikiroo
.fanfix
.library
.RemoteLibraryServer
;
21 import be
.nikiroo
.fanfix
.output
.BasicOutput
;
22 import be
.nikiroo
.fanfix
.output
.BasicOutput
.OutputType
;
23 import be
.nikiroo
.fanfix
.reader
.BasicReader
;
24 import be
.nikiroo
.fanfix
.reader
.Reader
;
25 import be
.nikiroo
.fanfix
.reader
.Reader
.ReaderType
;
26 import be
.nikiroo
.fanfix
.searchable
.BasicSearchable
;
27 import be
.nikiroo
.fanfix
.supported
.BasicSupport
;
28 import be
.nikiroo
.fanfix
.supported
.SupportType
;
29 import be
.nikiroo
.utils
.Progress
;
30 import be
.nikiroo
.utils
.Version
;
31 import be
.nikiroo
.utils
.serial
.server
.ServerObject
;
34 * Main program entry point.
39 private enum MainAction
{
40 IMPORT
, EXPORT
, CONVERT
, READ
, READ_URL
, LIST
, HELP
, SET_READER
, START
, VERSION
, SERVER
, STOP_SERVER
, REMOTE
, SET_SOURCE
, SET_TITLE
, SET_AUTHOR
, SEARCH
, SEARCH_TAG
44 * Main program entry point.
46 * Known environment variables:
48 * <li>NOUTF: if set to 1 or 'true', the program will prefer non-unicode
49 * {@link String}s when possible</li>
50 * <li>CONFIG_DIR: a path where to look for the <tt>.properties</tt> files
51 * before taking the usual ones; they will also be saved/updated into this
52 * path when the program starts</li>
53 * <li>DEBUG: if set to 1 or 'true', the program will override the DEBUG_ERR
54 * configuration value with 'true'</li>
58 * <li>--import [URL]: import into library</li>
59 * <li>--export [id] [output_type] [target]: export story to target</li>
60 * <li>--convert [URL] [output_type] [target] (+info): convert URL into
62 * <li>--read [id] ([chapter number]): read the given story from the library
64 * <li>--read-url [URL] ([chapter number]): convert on the fly and read the
65 * story, without saving it</li>
66 * <li>--search: list the supported websites (where)</li>
67 * <li>--search [where] [keywords] (page [page]) (item [item]): search on
68 * the supported website and display the given results page of stories it
69 * found, or the story details if asked</li>
70 * <li>--search-tag [where]: list all the tags supported by this website</li>
71 * <li>--search-tag [index 1]... (page [page]) (item [item]): search for the
72 * given stories or subtags, tag by tag, and display information about a
73 * specific page of results or about a specific item if requested</li>
74 * <li>--list ([type]): list the stories present in the library</li>
75 * <li>--set-source [id] [new source]: change the source of the given story</li>
76 * <li>--set-title [id] [new title]: change the title of the given story</li>
77 * <li>--set-author [id] [new author]: change the author of the given story</li>
78 * <li>--set-reader [reader type]: set the reader type to CLI, TUI or LOCAL
79 * for this command</li>
80 * <li>--version: get the version of the program</li>
81 * <li>--server: start the server mode (see config file for parameters)</li>
82 * <li>--stop-server: stop the running server on this port if any</li>
83 * <li>--remote [key] [host] [port]: use a the given remote library</li>
87 * see method description
89 public static void main(String
[] args
) {
90 String urlString
= null;
92 String sourceString
= null;
93 String titleString
= null;
94 String authorString
= null;
95 String chapString
= null;
98 MainAction action
= MainAction
.START
;
99 Boolean plusInfo
= null;
102 SupportType searchOn
= null;
103 String search
= null;
104 List
<Integer
> tags
= new ArrayList
<Integer
>();
108 boolean noMoreActions
= false;
111 for (int i
= 0; exitCode
== 0 && i
< args
.length
; i
++) {
112 // Action (--) handling:
113 if (!noMoreActions
&& args
[i
].startsWith("--")) {
114 if (args
[i
].equals("--")) {
115 noMoreActions
= true;
118 action
= MainAction
.valueOf(args
[i
].substring(2)
119 .toUpperCase().replace("-", "_"));
120 } catch (Exception e
) {
121 Instance
.getTraceHandler().error(
122 new IllegalArgumentException("Unknown action: "
133 if (urlString
== null) {
142 } else if (sourceString
== null) {
143 sourceString
= args
[i
];
144 } else if (target
== null) {
151 if (urlString
== null) {
153 } else if (sourceString
== null) {
154 sourceString
= args
[i
];
155 } else if (target
== null) {
157 } else if (plusInfo
== null) {
158 if ("+info".equals(args
[i
])) {
168 if (sourceString
== null) {
169 sourceString
= args
[i
];
177 } else if (sourceString
== null) {
178 sourceString
= args
[i
];
186 } else if (sourceString
== null) {
187 titleString
= args
[i
];
195 } else if (sourceString
== null) {
196 authorString
= args
[i
];
204 } else if (chapString
== null) {
205 chapString
= args
[i
];
211 if (urlString
== null) {
213 } else if (chapString
== null) {
214 chapString
= args
[i
];
220 if (searchOn
== null) {
221 searchOn
= SupportType
.valueOfAllOkUC(args
[i
]);
223 if (searchOn
== null) {
224 Instance
.getTraceHandler().error(
225 "Website not known: <" + args
[i
] + ">");
230 if (BasicSearchable
.getSearchable(searchOn
) == null) {
231 Instance
.getTraceHandler().error(
232 "Website not supported: " + searchOn
);
236 } else if (search
== null) {
238 } else if (page
!= null && page
== -1) {
240 page
= Integer
.parseInt(args
[i
]);
241 } catch (Exception e
) {
244 } else if (item
!= null && item
== -1) {
246 item
= Integer
.parseInt(args
[i
]);
247 } catch (Exception e
) {
250 } else if (page
== null || item
== null) {
251 if (page
== null && "page".equals(args
[i
])) {
253 } else if (item
== null && "item".equals(args
[i
])) {
263 if (searchOn
== null) {
264 searchOn
= SupportType
.valueOfAllOkUC(args
[i
]);
266 if (searchOn
== null) {
267 Instance
.getTraceHandler().error(
268 "Website not known: <" + args
[i
] + ">");
272 if (BasicSearchable
.getSearchable(searchOn
) == null) {
273 Instance
.getTraceHandler().error(
274 "Website not supported: " + searchOn
);
277 } else if (page
== null && item
== null) {
278 if ("page".equals(args
[i
])) {
280 } else if ("item".equals(args
[i
])) {
284 int index
= Integer
.parseInt(args
[i
]);
286 } catch (NumberFormatException e
) {
287 Instance
.getTraceHandler().error(
288 "Invalid tag index: " + args
[i
]);
292 } else if (page
!= null && page
== -1) {
294 page
= Integer
.parseInt(args
[i
]);
295 } catch (Exception e
) {
298 } else if (item
!= null && item
== -1) {
300 item
= Integer
.parseInt(args
[i
]);
301 } catch (Exception e
) {
304 } else if (page
== null || item
== null) {
305 if (page
== null && "page".equals(args
[i
])) {
307 } else if (item
== null && "item".equals(args
[i
])) {
320 exitCode
= setReaderType(args
[i
]);
321 action
= MainAction
.START
;
324 exitCode
= 255; // not supposed to be selected by user
327 exitCode
= 255; // no arguments for this option
330 exitCode
= 255; // no arguments for this option
333 exitCode
= 255; // no arguments for this option
338 } else if (host
== null) {
340 } else if (port
== null) {
341 port
= Integer
.parseInt(args
[i
]);
343 BasicLibrary lib
= new RemoteLibrary(key
, host
, port
);
344 lib
= new CacheLibrary(Instance
.getRemoteDir(host
), lib
);
346 BasicReader
.setDefaultLibrary(lib
);
348 action
= MainAction
.START
;
356 final Progress mainProgress
= new Progress(0, 80);
357 mainProgress
.addProgressListener(new Progress
.ProgressListener() {
358 private int current
= mainProgress
.getMin();
361 public void progress(Progress progress
, String name
) {
362 int diff
= progress
.getProgress() - current
;
368 StringBuilder builder
= new StringBuilder();
369 for (int i
= 0; i
< diff
; i
++) {
373 System
.err
.print(builder
.toString());
375 if (progress
.isDone()) {
376 System
.err
.println("");
380 Progress pg
= new Progress();
381 mainProgress
.addProgress(pg
, mainProgress
.getMax());
383 VersionCheck updates
= VersionCheck
.check();
384 if (updates
.isNewVersionAvailable()) {
385 // Sent to syserr so not to cause problem if one tries to capture a
386 // story content in text mode
388 .println("A new version of the program is available at https://github.com/nikiroo/fanfix/releases");
389 System
.err
.println("");
390 for (Version v
: updates
.getNewer()) {
391 System
.err
.println("\tVersion " + v
);
392 System
.err
.println("\t-------------");
393 System
.err
.println("");
394 for (String it
: updates
.getChanges().get(v
)) {
395 System
.err
.println("\t- " + it
);
397 System
.err
.println("");
404 exitCode
= imprt(urlString
, pg
);
405 updates
.ok(); // we consider it read
408 exitCode
= export(luid
, sourceString
, target
, pg
);
409 updates
.ok(); // we consider it read
412 exitCode
= convert(urlString
, sourceString
, target
,
413 plusInfo
== null ?
false : plusInfo
, pg
);
414 updates
.ok(); // we consider it read
417 if (BasicReader
.getReader() == null) {
418 Instance
.getTraceHandler()
419 .error(new Exception(
420 "No reader type has been configured"));
424 exitCode
= list(sourceString
);
428 Instance
.getLibrary().changeSource(luid
, sourceString
, pg
);
429 } catch (IOException e1
) {
430 Instance
.getTraceHandler().error(e1
);
436 Instance
.getLibrary().changeTitle(luid
, titleString
, pg
);
437 } catch (IOException e1
) {
438 Instance
.getTraceHandler().error(e1
);
444 Instance
.getLibrary().changeAuthor(luid
, authorString
, pg
);
445 } catch (IOException e1
) {
446 Instance
.getTraceHandler().error(e1
);
451 if (BasicReader
.getReader() == null) {
452 Instance
.getTraceHandler()
453 .error(new Exception(
454 "No reader type has been configured"));
458 exitCode
= read(luid
, chapString
, true);
461 if (BasicReader
.getReader() == null) {
462 Instance
.getTraceHandler()
463 .error(new Exception(
464 "No reader type has been configured"));
468 exitCode
= read(urlString
, chapString
, false);
471 page
= page
== null ?
1 : page
;
473 Instance
.getTraceHandler().error("Incorrect page number");
478 item
= item
== null ?
0 : item
;
480 Instance
.getTraceHandler().error("Incorrect item number");
485 if (BasicReader
.getReader() == null) {
486 Instance
.getTraceHandler()
487 .error(new Exception(
488 "No reader type has been configured"));
494 if (searchOn
== null) {
495 BasicReader
.getReader().search(true);
496 } else if (search
!= null) {
498 BasicReader
.getReader().search(searchOn
, search
, page
,
503 } catch (IOException e1
) {
504 Instance
.getTraceHandler().error(e1
);
510 if (searchOn
== null) {
515 page
= page
== null ?
1 : page
;
517 Instance
.getTraceHandler().error("Incorrect page number");
522 item
= item
== null ?
0 : item
;
524 Instance
.getTraceHandler().error("Incorrect item number");
529 if (BasicReader
.getReader() == null) {
530 Instance
.getTraceHandler()
531 .error(new Exception(
532 "No reader type has been configured"));
538 BasicReader
.getReader().searchTag(searchOn
, page
, item
,
539 true, tags
.toArray(new Integer
[] {}));
540 } catch (IOException e1
) {
541 Instance
.getTraceHandler().error(e1
);
554 .println(String
.format("Fanfix version %s"
555 + "%nhttps://github.com/nikiroo/fanfix/"
556 + "%n\tWritten by Nikiroo",
557 Version
.getCurrentVersion()));
558 updates
.ok(); // we consider it read
561 if (BasicReader
.getReader() == null) {
562 Instance
.getTraceHandler()
563 .error(new Exception(
564 "No reader type has been configured"));
569 BasicReader
.getReader().browse(null);
570 } catch (IOException e
) {
571 Instance
.getTraceHandler().error(e
);
576 key
= Instance
.getConfig().getString(Config
.SERVER_KEY
);
577 port
= Instance
.getConfig().getInteger(Config
.SERVER_PORT
);
579 System
.err
.println("No port configured in the config file");
584 ServerObject server
= new RemoteLibraryServer(key
, port
);
585 server
.setTraceHandler(Instance
.getTraceHandler());
587 } catch (IOException e
) {
588 Instance
.getTraceHandler().error(e
);
592 key
= Instance
.getConfig().getString(Config
.SERVER_KEY
);
593 port
= Instance
.getConfig().getInteger(Config
.SERVER_PORT
);
595 System
.err
.println("No port configured in the config file");
600 new RemoteLibrary(key
, host
, port
).exit();
601 } catch (SSLException e
) {
602 Instance
.getTraceHandler().error(
603 "Bad access key for remote library");
605 } catch (IOException e
) {
606 Instance
.getTraceHandler().error(e
);
612 exitCode
= 255; // should not be reachable (REMOTE -> START)
618 Instance
.getTempFiles().close();
619 } catch (IOException e
) {
620 Instance
.getTraceHandler()
621 .error(new IOException(
622 "Cannot dispose of the temporary files", e
));
625 if (exitCode
== 255) {
629 System
.exit(exitCode
);
633 * Import the given resource into the {@link LocalLibrary}.
636 * the resource to import
638 * the optional progress reporter
640 * @return the exit return code (0 = success)
642 public static int imprt(String urlString
, Progress pg
) {
644 Story story
= Instance
.getLibrary().imprt(
645 BasicReader
.getUrl(urlString
), pg
);
646 System
.out
.println(story
.getMeta().getLuid() + ": \""
647 + story
.getMeta().getTitle() + "\" imported.");
648 } catch (IOException e
) {
649 Instance
.getTraceHandler().error(e
);
657 * Export the {@link Story} from the {@link LocalLibrary} to the given
663 * the {@link OutputType} to use
667 * the optional progress reporter
669 * @return the exit return code (0 = success)
671 public static int export(String luid
, String typeString
, String target
,
673 OutputType type
= OutputType
.valueOfNullOkUC(typeString
, null);
675 Instance
.getTraceHandler().error(
676 new Exception(trans(StringId
.OUTPUT_DESC
, typeString
)));
681 Instance
.getLibrary().export(luid
, type
, target
, pg
);
682 } catch (IOException e
) {
683 Instance
.getTraceHandler().error(e
);
691 * List the stories of the given source from the {@link LocalLibrary}
692 * (unless NULL is passed, in which case all stories will be listed).
695 * the source to list the known stories of, or NULL to list all
698 * @return the exit return code (0 = success)
700 private static int list(String source
) {
701 BasicReader
.setDefaultReaderType(ReaderType
.CLI
);
703 BasicReader
.getReader().browse(source
);
704 } catch (IOException e
) {
705 Instance
.getTraceHandler().error(e
);
713 * Start the current reader for this {@link Story}.
716 * the LUID of the {@link Story} in the {@link LocalLibrary}
717 * <b>or</b> the {@link Story} {@link URL}
719 * which {@link Chapter} to read (starting at 1), or NULL to get
720 * the {@link Story} description
722 * TRUE if the source is the {@link Story} LUID, FALSE if it is a
725 * @return the exit return code (0 = success)
727 private static int read(String story
, String chapString
, boolean library
) {
729 Reader reader
= BasicReader
.getReader();
731 reader
.setMeta(story
);
733 reader
.setMeta(BasicReader
.getUrl(story
), null);
736 if (chapString
!= null) {
738 reader
.setChapter(Integer
.parseInt(chapString
));
740 } catch (NumberFormatException e
) {
741 Instance
.getTraceHandler().error(
742 new IOException("Chapter number cannot be parsed: "
749 } catch (IOException e
) {
750 Instance
.getTraceHandler().error(e
);
758 * Convert the {@link Story} into another format.
761 * the source {@link Story} to convert
763 * the {@link OutputType} to convert to
767 * TRUE to also export the cover and info file, even if the given
768 * {@link OutputType} does not usually save them
770 * the optional progress reporter
772 * @return the exit return code (0 = success)
774 public static int convert(String urlString
, String typeString
,
775 String target
, boolean infoCover
, Progress pg
) {
778 Instance
.getTraceHandler().trace("Convert: " + urlString
);
779 String sourceName
= urlString
;
781 URL source
= BasicReader
.getUrl(urlString
);
782 sourceName
= source
.toString();
783 if (source
.toString().startsWith("file://")) {
784 sourceName
= sourceName
.substring("file://".length());
787 OutputType type
= OutputType
.valueOfAllOkUC(typeString
, null);
789 Instance
.getTraceHandler().error(
790 new IOException(trans(StringId
.ERR_BAD_OUTPUT_TYPE
,
796 BasicSupport support
= BasicSupport
.getSupport(source
);
798 if (support
!= null) {
799 Instance
.getTraceHandler().trace(
800 "Support found: " + support
.getClass());
801 Progress pgIn
= new Progress();
802 Progress pgOut
= new Progress();
805 pg
.addProgress(pgIn
, 1);
806 pg
.addProgress(pgOut
, 1);
809 Story story
= support
.process(pgIn
);
811 target
= new File(target
).getAbsolutePath();
812 BasicOutput
.getOutput(type
, infoCover
, infoCover
)
813 .process(story
, target
, pgOut
);
814 } catch (IOException e
) {
815 Instance
.getTraceHandler().error(
816 new IOException(trans(StringId
.ERR_SAVING
,
821 Instance
.getTraceHandler().error(
822 new IOException(trans(
823 StringId
.ERR_NOT_SUPPORTED
, source
)));
827 } catch (IOException e
) {
828 Instance
.getTraceHandler().error(
829 new IOException(trans(StringId
.ERR_LOADING
,
834 } catch (MalformedURLException e
) {
835 Instance
.getTraceHandler()
836 .error(new IOException(trans(StringId
.ERR_BAD_URL
,
845 * Simple shortcut method to call {link Instance#getTrans()#getString()}.
848 * the ID to translate
850 * @return the translated result
852 private static String
trans(StringId id
, Object
... params
) {
853 return Instance
.getTrans().getString(id
, params
);
857 * Display the correct syntax of the program to the user to stdout, or an
858 * error message if the syntax used was wrong on stderr.
861 * TRUE to show the syntax help, FALSE to show "syntax error"
863 private static void syntax(boolean showHelp
) {
865 StringBuilder builder
= new StringBuilder();
866 for (SupportType type
: SupportType
.values()) {
867 builder
.append(trans(StringId
.ERR_SYNTAX_TYPE
, type
.toString(),
869 builder
.append('\n');
872 String typesIn
= builder
.toString();
873 builder
.setLength(0);
875 for (OutputType type
: OutputType
.values()) {
876 builder
.append(trans(StringId
.ERR_SYNTAX_TYPE
, type
.toString(),
877 type
.getDesc(true)));
878 builder
.append('\n');
881 String typesOut
= builder
.toString();
883 System
.out
.println(trans(StringId
.HELP_SYNTAX
, typesIn
, typesOut
));
885 System
.err
.println(trans(StringId
.ERR_SYNTAX
));
890 * Set the default reader type for this session only (it can be changed in
891 * the configuration file, too, but this value will override it).
893 * @param readerTypeString
896 private static int setReaderType(String readerTypeString
) {
898 ReaderType readerType
= ReaderType
.valueOf(readerTypeString
900 BasicReader
.setDefaultReaderType(readerType
);
902 } catch (IllegalArgumentException e
) {
903 Instance
.getTraceHandler().error(
904 new IOException("Unknown reader type: " + readerTypeString
,