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 || search
== null) {
495 // TODO: do on reader!!!
496 for (SupportType type
: SupportType
.values()) {
497 if (BasicSearchable
.getSearchable(type
) != null) {
498 System
.out
.println(type
);
503 BasicReader
.getReader().search(searchOn
, search
, page
,
505 } catch (IOException e1
) {
506 Instance
.getTraceHandler().error(e1
);
512 if (searchOn
== null) {
517 page
= page
== null ?
1 : page
;
519 Instance
.getTraceHandler().error("Incorrect page number");
524 item
= item
== null ?
0 : item
;
526 Instance
.getTraceHandler().error("Incorrect item number");
531 if (BasicReader
.getReader() == null) {
532 Instance
.getTraceHandler()
533 .error(new Exception(
534 "No reader type has been configured"));
540 BasicReader
.getReader().searchTag(searchOn
, page
, item
,
541 true, tags
.toArray(new Integer
[] {}));
542 } catch (IOException e1
) {
543 Instance
.getTraceHandler().error(e1
);
556 .println(String
.format("Fanfix version %s"
557 + "%nhttps://github.com/nikiroo/fanfix/"
558 + "%n\tWritten by Nikiroo",
559 Version
.getCurrentVersion()));
560 updates
.ok(); // we consider it read
563 if (BasicReader
.getReader() == null) {
564 Instance
.getTraceHandler()
565 .error(new Exception(
566 "No reader type has been configured"));
570 BasicReader
.getReader().browse(null);
578 ServerObject server
= new RemoteLibraryServer(key
, port
);
579 server
.setTraceHandler(Instance
.getTraceHandler());
581 } catch (IOException e
) {
582 Instance
.getTraceHandler().error(e
);
591 new RemoteLibrary(key
, host
, port
).exit();
594 exitCode
= 255; // should not be reachable (REMOTE -> START)
600 Instance
.getTempFiles().close();
601 } catch (IOException e
) {
602 Instance
.getTraceHandler()
603 .error(new IOException(
604 "Cannot dispose of the temporary files", e
));
607 if (exitCode
== 255) {
611 System
.exit(exitCode
);
615 * Import the given resource into the {@link LocalLibrary}.
618 * the resource to import
620 * the optional progress reporter
622 * @return the exit return code (0 = success)
624 public static int imprt(String urlString
, Progress pg
) {
626 Story story
= Instance
.getLibrary().imprt(
627 BasicReader
.getUrl(urlString
), pg
);
628 System
.out
.println(story
.getMeta().getLuid() + ": \""
629 + story
.getMeta().getTitle() + "\" imported.");
630 } catch (IOException e
) {
631 Instance
.getTraceHandler().error(e
);
639 * Export the {@link Story} from the {@link LocalLibrary} to the given
645 * the {@link OutputType} to use
649 * the optional progress reporter
651 * @return the exit return code (0 = success)
653 public static int export(String luid
, String typeString
, String target
,
655 OutputType type
= OutputType
.valueOfNullOkUC(typeString
, null);
657 Instance
.getTraceHandler().error(
658 new Exception(trans(StringId
.OUTPUT_DESC
, typeString
)));
663 Instance
.getLibrary().export(luid
, type
, target
, pg
);
664 } catch (IOException e
) {
665 Instance
.getTraceHandler().error(e
);
673 * List the stories of the given source from the {@link LocalLibrary}
674 * (unless NULL is passed, in which case all stories will be listed).
677 * the source to list the known stories of, or NULL to list all
680 * @return the exit return code (0 = success)
682 private static int list(String source
) {
683 List
<MetaData
> stories
;
684 stories
= BasicReader
.getReader().getLibrary().getListBySource(source
);
686 for (MetaData story
: stories
) {
688 if (story
.getAuthor() != null && !story
.getAuthor().isEmpty()) {
689 author
= " (" + story
.getAuthor() + ")";
692 System
.out
.println(story
.getLuid() + ": " + story
.getTitle()
699 * Start the current reader for this {@link Story}.
702 * the LUID of the {@link Story} in the {@link LocalLibrary}
703 * <b>or</b> the {@link Story} {@link URL}
705 * which {@link Chapter} to read (starting at 1), or NULL to get
706 * the {@link Story} description
708 * TRUE if the source is the {@link Story} LUID, FALSE if it is a
711 * @return the exit return code (0 = success)
713 private static int read(String story
, String chapString
, boolean library
) {
715 Reader reader
= BasicReader
.getReader();
717 reader
.setMeta(story
);
719 reader
.setMeta(BasicReader
.getUrl(story
), null);
722 if (chapString
!= null) {
724 reader
.setChapter(Integer
.parseInt(chapString
));
726 } catch (NumberFormatException e
) {
727 Instance
.getTraceHandler().error(
728 new IOException("Chapter number cannot be parsed: "
735 } catch (IOException e
) {
736 Instance
.getTraceHandler().error(e
);
744 * Convert the {@link Story} into another format.
747 * the source {@link Story} to convert
749 * the {@link OutputType} to convert to
753 * TRUE to also export the cover and info file, even if the given
754 * {@link OutputType} does not usually save them
756 * the optional progress reporter
758 * @return the exit return code (0 = success)
760 public static int convert(String urlString
, String typeString
,
761 String target
, boolean infoCover
, Progress pg
) {
764 Instance
.getTraceHandler().trace("Convert: " + urlString
);
765 String sourceName
= urlString
;
767 URL source
= BasicReader
.getUrl(urlString
);
768 sourceName
= source
.toString();
769 if (source
.toString().startsWith("file://")) {
770 sourceName
= sourceName
.substring("file://".length());
773 OutputType type
= OutputType
.valueOfAllOkUC(typeString
, null);
775 Instance
.getTraceHandler().error(
776 new IOException(trans(StringId
.ERR_BAD_OUTPUT_TYPE
,
782 BasicSupport support
= BasicSupport
.getSupport(source
);
784 if (support
!= null) {
785 Instance
.getTraceHandler().trace(
786 "Support found: " + support
.getClass());
787 Progress pgIn
= new Progress();
788 Progress pgOut
= new Progress();
791 pg
.addProgress(pgIn
, 1);
792 pg
.addProgress(pgOut
, 1);
795 Story story
= support
.process(pgIn
);
797 target
= new File(target
).getAbsolutePath();
798 BasicOutput
.getOutput(type
, infoCover
, infoCover
)
799 .process(story
, target
, pgOut
);
800 } catch (IOException e
) {
801 Instance
.getTraceHandler().error(
802 new IOException(trans(StringId
.ERR_SAVING
,
807 Instance
.getTraceHandler().error(
808 new IOException(trans(
809 StringId
.ERR_NOT_SUPPORTED
, source
)));
813 } catch (IOException e
) {
814 Instance
.getTraceHandler().error(
815 new IOException(trans(StringId
.ERR_LOADING
,
820 } catch (MalformedURLException e
) {
821 Instance
.getTraceHandler()
822 .error(new IOException(trans(StringId
.ERR_BAD_URL
,
831 * Simple shortcut method to call {link Instance#getTrans()#getString()}.
834 * the ID to translate
836 * @return the translated result
838 private static String
trans(StringId id
, Object
... params
) {
839 return Instance
.getTrans().getString(id
, params
);
843 * Display the correct syntax of the program to the user to stdout, or an
844 * error message if the syntax used was wrong on stderr.
847 * TRUE to show the syntax help, FALSE to show "syntax error"
849 private static void syntax(boolean showHelp
) {
851 StringBuilder builder
= new StringBuilder();
852 for (SupportType type
: SupportType
.values()) {
853 builder
.append(trans(StringId
.ERR_SYNTAX_TYPE
, type
.toString(),
855 builder
.append('\n');
858 String typesIn
= builder
.toString();
859 builder
.setLength(0);
861 for (OutputType type
: OutputType
.values()) {
862 builder
.append(trans(StringId
.ERR_SYNTAX_TYPE
, type
.toString(),
863 type
.getDesc(true)));
864 builder
.append('\n');
867 String typesOut
= builder
.toString();
869 System
.out
.println(trans(StringId
.HELP_SYNTAX
, typesIn
, typesOut
));
871 System
.err
.println(trans(StringId
.ERR_SYNTAX
));
876 * Set the default reader type for this session only (it can be changed in
877 * the configuration file, too, but this value will override it).
879 * @param readerTypeString
882 private static int setReaderType(String readerTypeString
) {
884 ReaderType readerType
= ReaderType
.valueOf(readerTypeString
886 BasicReader
.setDefaultReaderType(readerType
);
888 } catch (IllegalArgumentException e
) {
889 Instance
.getTraceHandler().error(
890 new IOException("Unknown reader type: " + readerTypeString
,