1 package be
.nikiroo
.fanfix
;
4 import java
.io
.IOException
;
5 import java
.net
.MalformedURLException
;
7 import java
.util
.ArrayList
;
10 import be
.nikiroo
.fanfix
.bundles
.StringId
;
11 import be
.nikiroo
.fanfix
.data
.Chapter
;
12 import be
.nikiroo
.fanfix
.data
.MetaData
;
13 import be
.nikiroo
.fanfix
.data
.Story
;
14 import be
.nikiroo
.fanfix
.library
.BasicLibrary
;
15 import be
.nikiroo
.fanfix
.library
.CacheLibrary
;
16 import be
.nikiroo
.fanfix
.library
.LocalLibrary
;
17 import be
.nikiroo
.fanfix
.library
.RemoteLibrary
;
18 import be
.nikiroo
.fanfix
.library
.RemoteLibraryServer
;
19 import be
.nikiroo
.fanfix
.output
.BasicOutput
;
20 import be
.nikiroo
.fanfix
.output
.BasicOutput
.OutputType
;
21 import be
.nikiroo
.fanfix
.reader
.BasicReader
;
22 import be
.nikiroo
.fanfix
.reader
.Reader
;
23 import be
.nikiroo
.fanfix
.reader
.Reader
.ReaderType
;
24 import be
.nikiroo
.fanfix
.searchable
.BasicSearchable
;
25 import be
.nikiroo
.fanfix
.supported
.BasicSupport
;
26 import be
.nikiroo
.fanfix
.supported
.SupportType
;
27 import be
.nikiroo
.utils
.Progress
;
28 import be
.nikiroo
.utils
.Version
;
29 import be
.nikiroo
.utils
.serial
.server
.ServerObject
;
32 * Main program entry point.
37 private enum MainAction
{
38 IMPORT
, EXPORT
, CONVERT
, READ
, READ_URL
, LIST
, HELP
, SET_READER
, START
, VERSION
, SERVER
, STOP_SERVER
, REMOTE
, SET_SOURCE
, SET_TITLE
, SET_AUTHOR
, SEARCH
, TAG
42 * Main program entry point.
44 * Known environment variables:
46 * <li>NOUTF: if set to 1 or 'true', the program will prefer non-unicode
47 * {@link String}s when possible</li>
48 * <li>CONFIG_DIR: a path where to look for the <tt>.properties</tt> files
49 * before taking the usual ones; they will also be saved/updated into this
50 * path when the program starts</li>
51 * <li>DEBUG: if set to 1 or 'true', the program will override the DEBUG_ERR
52 * configuration value with 'true'</li>
56 * <li>--import [URL]: import into library</li>
57 * <li>--export [id] [output_type] [target]: export story to target</li>
58 * <li>--convert [URL] [output_type] [target] (+info): convert URL into
60 * <li>--read [id] ([chapter number]): read the given story from the library
62 * <li>--read-url [URL] ([chapter number]): convert on the fly and read the
63 * story, without saving it</li>
64 * <li>--search: list the supported websites (where)</li>
65 * <li>--search [where] [keywords] (page [page]) (item [item]): search on
66 * the supported website and display the given results page of stories it
67 * found, or the story details if asked</li>
68 * <li>--tag [where]: list all the tags supported by this website</li>
69 * <li>--tag [index 1]... (page [page]) (item [item]): search for the given
70 * stories or subtags, tag by tag, and display information about a specific
71 * page of results or about a specific item if requested</li>
72 * <li>--list ([type]): list the stories present in the library</li>
73 * <li>--set-source [id] [new source]: change the source of the given story</li>
74 * <li>--set-title [id] [new title]: change the title of the given story</li>
75 * <li>--set-author [id] [new author]: change the author of the given story</li>
76 * <li>--set-reader [reader type]: set the reader type to CLI, TUI or LOCAL
77 * for this command</li>
78 * <li>--version: get the version of the program</li>
79 * <li>--server [key] [port]: start a server on this port</li>
80 * <li>--stop-server [key] [port]: stop the running server on this port if
82 * <li>--remote [key] [host] [port]: use a the given remote library</li>
86 * see method description
88 public static void main(String
[] args
) {
89 String urlString
= null;
91 String sourceString
= null;
92 String titleString
= null;
93 String authorString
= null;
94 String chapString
= null;
97 MainAction action
= MainAction
.START
;
98 Boolean plusInfo
= null;
101 SupportType searchOn
= null;
102 String search
= null;
103 List
<Integer
> tags
= new ArrayList
<Integer
>();
107 boolean noMoreActions
= false;
110 for (int i
= 0; exitCode
== 0 && i
< args
.length
; i
++) {
111 // Action (--) handling:
112 if (!noMoreActions
&& args
[i
].startsWith("--")) {
113 if (args
[i
].equals("--")) {
114 noMoreActions
= true;
117 action
= MainAction
.valueOf(args
[i
].substring(2)
118 .toUpperCase().replace("-", "_"));
119 } catch (Exception e
) {
120 Instance
.getTraceHandler().error(
121 new IllegalArgumentException("Unknown action: "
132 if (urlString
== null) {
141 } else if (sourceString
== null) {
142 sourceString
= args
[i
];
143 } else if (target
== null) {
150 if (urlString
== null) {
152 } else if (sourceString
== null) {
153 sourceString
= args
[i
];
154 } else if (target
== null) {
156 } else if (plusInfo
== null) {
157 if ("+info".equals(args
[i
])) {
167 if (sourceString
== null) {
168 sourceString
= args
[i
];
176 } else if (sourceString
== null) {
177 sourceString
= args
[i
];
185 } else if (sourceString
== null) {
186 titleString
= args
[i
];
194 } else if (sourceString
== null) {
195 authorString
= args
[i
];
203 } else if (chapString
== null) {
204 chapString
= args
[i
];
210 if (urlString
== null) {
212 } else if (chapString
== null) {
213 chapString
= args
[i
];
219 if (searchOn
== null) {
220 searchOn
= SupportType
.valueOfAllOkUC(args
[i
]);
222 if (searchOn
== null) {
223 Instance
.getTraceHandler().error(
224 "Website not known: <" + args
[i
] + ">");
228 if (BasicSearchable
.getSearchable(searchOn
) == null) {
229 Instance
.getTraceHandler().error(
230 "Website not supported: " + searchOn
);
233 } else if (search
== null) {
235 } else if (page
!= null && page
== -1) {
237 page
= Integer
.parseInt(args
[i
]);
238 } catch (Exception e
) {
241 } else if (item
!= null && item
== -1) {
243 item
= Integer
.parseInt(args
[i
]);
244 } catch (Exception e
) {
247 } else if (page
== null || item
== null) {
248 if (page
== null && "page".equals(args
[i
])) {
250 } else if (item
== null && "item".equals(args
[i
])) {
260 if (searchOn
== null) {
261 searchOn
= SupportType
.valueOfAllOkUC(args
[i
]);
263 if (searchOn
== null) {
264 Instance
.getTraceHandler().error(
265 "Website not known: <" + args
[i
] + ">");
269 if (BasicSearchable
.getSearchable(searchOn
) == null) {
270 Instance
.getTraceHandler().error(
271 "Website not supported: " + searchOn
);
274 } else if (page
== null && item
== null) {
275 if ("page".equals(args
[i
])) {
277 } else if ("item".equals(args
[i
])) {
281 int index
= Integer
.parseInt(args
[i
]);
283 } catch (NumberFormatException e
) {
284 Instance
.getTraceHandler().error(
285 "Invalid tag index: " + args
[i
]);
289 } else if (page
!= null && page
== -1) {
291 page
= Integer
.parseInt(args
[i
]);
292 } catch (Exception e
) {
295 } else if (item
!= null && item
== -1) {
297 item
= Integer
.parseInt(args
[i
]);
298 } catch (Exception e
) {
301 } else if (page
== null || item
== null) {
302 if (page
== null && "page".equals(args
[i
])) {
304 } else if (item
== null && "item".equals(args
[i
])) {
317 exitCode
= setReaderType(args
[i
]);
318 action
= MainAction
.START
;
321 exitCode
= 255; // not supposed to be selected by user
324 exitCode
= 255; // no arguments for this option
330 } else if (port
== null) {
331 port
= Integer
.parseInt(args
[i
]);
339 } else if (host
== null) {
341 } else if (port
== null) {
342 port
= Integer
.parseInt(args
[i
]);
344 BasicLibrary lib
= new RemoteLibrary(key
, host
, port
);
345 lib
= new CacheLibrary(Instance
.getRemoteDir(host
), lib
);
347 BasicReader
.setDefaultLibrary(lib
);
349 action
= MainAction
.START
;
357 final Progress mainProgress
= new Progress(0, 80);
358 mainProgress
.addProgressListener(new Progress
.ProgressListener() {
359 private int current
= mainProgress
.getMin();
362 public void progress(Progress progress
, String name
) {
363 int diff
= progress
.getProgress() - current
;
369 StringBuilder builder
= new StringBuilder();
370 for (int i
= 0; i
< diff
; i
++) {
374 System
.err
.print(builder
.toString());
376 if (progress
.isDone()) {
377 System
.err
.println("");
381 Progress pg
= new Progress();
382 mainProgress
.addProgress(pg
, mainProgress
.getMax());
384 VersionCheck updates
= VersionCheck
.check();
385 if (updates
.isNewVersionAvailable()) {
386 // Sent to syserr so not to cause problem if one tries to capture a
387 // story content in text mode
389 .println("A new version of the program is available at https://github.com/nikiroo/fanfix/releases");
390 System
.err
.println("");
391 for (Version v
: updates
.getNewer()) {
392 System
.err
.println("\tVersion " + v
);
393 System
.err
.println("\t-------------");
394 System
.err
.println("");
395 for (String it
: updates
.getChanges().get(v
)) {
396 System
.err
.println("\t- " + it
);
398 System
.err
.println("");
402 if (exitCode
!= 255) {
405 exitCode
= imprt(urlString
, pg
);
406 updates
.ok(); // we consider it read
409 exitCode
= export(luid
, sourceString
, target
, pg
);
410 updates
.ok(); // we consider it read
413 exitCode
= convert(urlString
, sourceString
, target
,
414 plusInfo
== null ?
false : plusInfo
, pg
);
415 updates
.ok(); // we consider it read
418 if (BasicReader
.getReader() == null) {
419 Instance
.getTraceHandler()
420 .error(new Exception(
421 "No reader type has been configured"));
425 exitCode
= list(sourceString
);
429 Instance
.getLibrary().changeSource(luid
, sourceString
, pg
);
430 } catch (IOException e1
) {
431 Instance
.getTraceHandler().error(e1
);
437 Instance
.getLibrary().changeTitle(luid
, titleString
, pg
);
438 } catch (IOException e1
) {
439 Instance
.getTraceHandler().error(e1
);
445 Instance
.getLibrary().changeAuthor(luid
, authorString
, pg
);
446 } catch (IOException e1
) {
447 Instance
.getTraceHandler().error(e1
);
452 if (BasicReader
.getReader() == null) {
453 Instance
.getTraceHandler()
454 .error(new Exception(
455 "No reader type has been configured"));
459 exitCode
= read(luid
, chapString
, true);
462 if (BasicReader
.getReader() == null) {
463 Instance
.getTraceHandler()
464 .error(new Exception(
465 "No reader type has been configured"));
469 exitCode
= read(urlString
, chapString
, false);
472 page
= page
== null ?
1 : page
;
474 Instance
.getTraceHandler().error("Incorrect page number");
479 item
= item
== null ?
0 : item
;
481 Instance
.getTraceHandler().error("Incorrect item number");
486 if (BasicReader
.getReader() == null) {
487 Instance
.getTraceHandler()
488 .error(new Exception(
489 "No reader type has been configured"));
494 if (searchOn
== null) {
495 // TODO: do on reader!!!
496 for (SupportType type
: SupportType
.values()) {
497 if (BasicSearchable
.getSearchable(type
) != null) {
498 System
.out
.println(type
);
501 } else if (search
!= null) {
503 BasicReader
.getReader().search(searchOn
, search
, page
,
505 } catch (IOException e1
) {
506 Instance
.getTraceHandler().error(e1
);
514 if (searchOn
== null) {
519 page
= page
== null ?
1 : page
;
521 Instance
.getTraceHandler().error("Incorrect page number");
526 item
= item
== null ?
0 : item
;
528 Instance
.getTraceHandler().error("Incorrect item number");
533 if (BasicReader
.getReader() == null) {
534 Instance
.getTraceHandler()
535 .error(new Exception(
536 "No reader type has been configured"));
542 BasicReader
.getReader().searchTag(searchOn
, page
, item
,
543 true, tags
.toArray(new Integer
[] {}));
544 } catch (IOException e1
) {
545 Instance
.getTraceHandler().error(e1
);
558 .println(String
.format("Fanfix version %s"
559 + "%nhttps://github.com/nikiroo/fanfix/"
560 + "%n\tWritten by Nikiroo",
561 Version
.getCurrentVersion()));
562 updates
.ok(); // we consider it read
565 if (BasicReader
.getReader() == null) {
566 Instance
.getTraceHandler()
567 .error(new Exception(
568 "No reader type has been configured"));
572 BasicReader
.getReader().browse(null);
580 ServerObject server
= new RemoteLibraryServer(key
, port
);
581 server
.setTraceHandler(Instance
.getTraceHandler());
583 } catch (IOException e
) {
584 Instance
.getTraceHandler().error(e
);
593 new RemoteLibrary(key
, host
, port
).exit();
596 exitCode
= 255; // should not be reachable (REMOTE -> START)
602 Instance
.getTempFiles().close();
603 } catch (IOException e
) {
604 Instance
.getTraceHandler()
605 .error(new IOException(
606 "Cannot dispose of the temporary files", e
));
609 if (exitCode
== 255) {
613 System
.exit(exitCode
);
617 * Import the given resource into the {@link LocalLibrary}.
620 * the resource to import
622 * the optional progress reporter
624 * @return the exit return code (0 = success)
626 public static int imprt(String urlString
, Progress pg
) {
628 Story story
= Instance
.getLibrary().imprt(
629 BasicReader
.getUrl(urlString
), pg
);
630 System
.out
.println(story
.getMeta().getLuid() + ": \""
631 + story
.getMeta().getTitle() + "\" imported.");
632 } catch (IOException e
) {
633 Instance
.getTraceHandler().error(e
);
641 * Export the {@link Story} from the {@link LocalLibrary} to the given
647 * the {@link OutputType} to use
651 * the optional progress reporter
653 * @return the exit return code (0 = success)
655 public static int export(String luid
, String typeString
, String target
,
657 OutputType type
= OutputType
.valueOfNullOkUC(typeString
, null);
659 Instance
.getTraceHandler().error(
660 new Exception(trans(StringId
.OUTPUT_DESC
, typeString
)));
665 Instance
.getLibrary().export(luid
, type
, target
, pg
);
666 } catch (IOException e
) {
667 Instance
.getTraceHandler().error(e
);
675 * List the stories of the given source from the {@link LocalLibrary}
676 * (unless NULL is passed, in which case all stories will be listed).
679 * the source to list the known stories of, or NULL to list all
682 * @return the exit return code (0 = success)
684 private static int list(String source
) {
685 List
<MetaData
> stories
;
686 stories
= BasicReader
.getReader().getLibrary().getListBySource(source
);
688 for (MetaData story
: stories
) {
690 if (story
.getAuthor() != null && !story
.getAuthor().isEmpty()) {
691 author
= " (" + story
.getAuthor() + ")";
694 System
.out
.println(story
.getLuid() + ": " + story
.getTitle()
701 * Start the current reader for this {@link Story}.
704 * the LUID of the {@link Story} in the {@link LocalLibrary}
705 * <b>or</b> the {@link Story} {@link URL}
707 * which {@link Chapter} to read (starting at 1), or NULL to get
708 * the {@link Story} description
710 * TRUE if the source is the {@link Story} LUID, FALSE if it is a
713 * @return the exit return code (0 = success)
715 private static int read(String story
, String chapString
, boolean library
) {
717 Reader reader
= BasicReader
.getReader();
719 reader
.setMeta(story
);
721 reader
.setMeta(BasicReader
.getUrl(story
), null);
724 if (chapString
!= null) {
726 reader
.setChapter(Integer
.parseInt(chapString
));
728 } catch (NumberFormatException e
) {
729 Instance
.getTraceHandler().error(
730 new IOException("Chapter number cannot be parsed: "
737 } catch (IOException e
) {
738 Instance
.getTraceHandler().error(e
);
746 * Convert the {@link Story} into another format.
749 * the source {@link Story} to convert
751 * the {@link OutputType} to convert to
755 * TRUE to also export the cover and info file, even if the given
756 * {@link OutputType} does not usually save them
758 * the optional progress reporter
760 * @return the exit return code (0 = success)
762 public static int convert(String urlString
, String typeString
,
763 String target
, boolean infoCover
, Progress pg
) {
766 Instance
.getTraceHandler().trace("Convert: " + urlString
);
767 String sourceName
= urlString
;
769 URL source
= BasicReader
.getUrl(urlString
);
770 sourceName
= source
.toString();
771 if (source
.toString().startsWith("file://")) {
772 sourceName
= sourceName
.substring("file://".length());
775 OutputType type
= OutputType
.valueOfAllOkUC(typeString
, null);
777 Instance
.getTraceHandler().error(
778 new IOException(trans(StringId
.ERR_BAD_OUTPUT_TYPE
,
784 BasicSupport support
= BasicSupport
.getSupport(source
);
786 if (support
!= null) {
787 Instance
.getTraceHandler().trace(
788 "Support found: " + support
.getClass());
789 Progress pgIn
= new Progress();
790 Progress pgOut
= new Progress();
793 pg
.addProgress(pgIn
, 1);
794 pg
.addProgress(pgOut
, 1);
797 Story story
= support
.process(pgIn
);
799 target
= new File(target
).getAbsolutePath();
800 BasicOutput
.getOutput(type
, infoCover
, infoCover
)
801 .process(story
, target
, pgOut
);
802 } catch (IOException e
) {
803 Instance
.getTraceHandler().error(
804 new IOException(trans(StringId
.ERR_SAVING
,
809 Instance
.getTraceHandler().error(
810 new IOException(trans(
811 StringId
.ERR_NOT_SUPPORTED
, source
)));
815 } catch (IOException e
) {
816 Instance
.getTraceHandler().error(
817 new IOException(trans(StringId
.ERR_LOADING
,
822 } catch (MalformedURLException e
) {
823 Instance
.getTraceHandler()
824 .error(new IOException(trans(StringId
.ERR_BAD_URL
,
833 * Simple shortcut method to call {link Instance#getTrans()#getString()}.
836 * the ID to translate
838 * @return the translated result
840 private static String
trans(StringId id
, Object
... params
) {
841 return Instance
.getTrans().getString(id
, params
);
845 * Display the correct syntax of the program to the user to stdout, or an
846 * error message if the syntax used was wrong on stderr.
849 * TRUE to show the syntax help, FALSE to show "syntax error"
851 private static void syntax(boolean showHelp
) {
853 StringBuilder builder
= new StringBuilder();
854 for (SupportType type
: SupportType
.values()) {
855 builder
.append(trans(StringId
.ERR_SYNTAX_TYPE
, type
.toString(),
857 builder
.append('\n');
860 String typesIn
= builder
.toString();
861 builder
.setLength(0);
863 for (OutputType type
: OutputType
.values()) {
864 builder
.append(trans(StringId
.ERR_SYNTAX_TYPE
, type
.toString(),
865 type
.getDesc(true)));
866 builder
.append('\n');
869 String typesOut
= builder
.toString();
871 System
.out
.println(trans(StringId
.HELP_SYNTAX
, typesIn
, typesOut
));
873 System
.err
.println(trans(StringId
.ERR_SYNTAX
));
878 * Set the default reader type for this session only (it can be changed in
879 * the configuration file, too, but this value will override it).
881 * @param readerTypeString
884 private static int setReaderType(String readerTypeString
) {
886 ReaderType readerType
= ReaderType
.valueOf(readerTypeString
888 BasicReader
.setDefaultReaderType(readerType
);
890 } catch (IllegalArgumentException e
) {
891 Instance
.getTraceHandler().error(
892 new IOException("Unknown reader type: " + readerTypeString
,