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
] + ">");
229 if (BasicSearchable
.getSearchable(searchOn
) == null) {
230 Instance
.getTraceHandler().error(
231 "Website not supported: " + searchOn
);
235 } else if (search
== null) {
237 } else if (page
!= null && page
== -1) {
239 page
= Integer
.parseInt(args
[i
]);
240 } catch (Exception e
) {
243 } else if (item
!= null && item
== -1) {
245 item
= Integer
.parseInt(args
[i
]);
246 } catch (Exception e
) {
249 } else if (page
== null || item
== null) {
250 if (page
== null && "page".equals(args
[i
])) {
252 } else if (item
== null && "item".equals(args
[i
])) {
262 if (searchOn
== null) {
263 searchOn
= SupportType
.valueOfAllOkUC(args
[i
]);
265 if (searchOn
== null) {
266 Instance
.getTraceHandler().error(
267 "Website not known: <" + args
[i
] + ">");
271 if (BasicSearchable
.getSearchable(searchOn
) == null) {
272 Instance
.getTraceHandler().error(
273 "Website not supported: " + searchOn
);
276 } else if (page
== null && item
== null) {
277 if ("page".equals(args
[i
])) {
279 } else if ("item".equals(args
[i
])) {
283 int index
= Integer
.parseInt(args
[i
]);
285 } catch (NumberFormatException e
) {
286 Instance
.getTraceHandler().error(
287 "Invalid tag index: " + args
[i
]);
291 } else if (page
!= null && page
== -1) {
293 page
= Integer
.parseInt(args
[i
]);
294 } catch (Exception e
) {
297 } else if (item
!= null && item
== -1) {
299 item
= Integer
.parseInt(args
[i
]);
300 } catch (Exception e
) {
303 } else if (page
== null || item
== null) {
304 if (page
== null && "page".equals(args
[i
])) {
306 } else if (item
== null && "item".equals(args
[i
])) {
319 exitCode
= setReaderType(args
[i
]);
320 action
= MainAction
.START
;
323 exitCode
= 255; // not supposed to be selected by user
326 exitCode
= 255; // no arguments for this option
332 } else if (port
== null) {
333 port
= Integer
.parseInt(args
[i
]);
341 } else if (host
== null) {
343 } else if (port
== null) {
344 port
= Integer
.parseInt(args
[i
]);
346 BasicLibrary lib
= new RemoteLibrary(key
, host
, port
);
347 lib
= new CacheLibrary(Instance
.getRemoteDir(host
), lib
);
349 BasicReader
.setDefaultLibrary(lib
);
351 action
= MainAction
.START
;
359 final Progress mainProgress
= new Progress(0, 80);
360 mainProgress
.addProgressListener(new Progress
.ProgressListener() {
361 private int current
= mainProgress
.getMin();
364 public void progress(Progress progress
, String name
) {
365 int diff
= progress
.getProgress() - current
;
371 StringBuilder builder
= new StringBuilder();
372 for (int i
= 0; i
< diff
; i
++) {
376 System
.err
.print(builder
.toString());
378 if (progress
.isDone()) {
379 System
.err
.println("");
383 Progress pg
= new Progress();
384 mainProgress
.addProgress(pg
, mainProgress
.getMax());
386 VersionCheck updates
= VersionCheck
.check();
387 if (updates
.isNewVersionAvailable()) {
388 // Sent to syserr so not to cause problem if one tries to capture a
389 // story content in text mode
391 .println("A new version of the program is available at https://github.com/nikiroo/fanfix/releases");
392 System
.err
.println("");
393 for (Version v
: updates
.getNewer()) {
394 System
.err
.println("\tVersion " + v
);
395 System
.err
.println("\t-------------");
396 System
.err
.println("");
397 for (String it
: updates
.getChanges().get(v
)) {
398 System
.err
.println("\t- " + it
);
400 System
.err
.println("");
407 exitCode
= imprt(urlString
, pg
);
408 updates
.ok(); // we consider it read
411 exitCode
= export(luid
, sourceString
, target
, pg
);
412 updates
.ok(); // we consider it read
415 exitCode
= convert(urlString
, sourceString
, target
,
416 plusInfo
== null ?
false : plusInfo
, pg
);
417 updates
.ok(); // we consider it read
420 if (BasicReader
.getReader() == null) {
421 Instance
.getTraceHandler()
422 .error(new Exception(
423 "No reader type has been configured"));
427 exitCode
= list(sourceString
);
431 Instance
.getLibrary().changeSource(luid
, sourceString
, pg
);
432 } catch (IOException e1
) {
433 Instance
.getTraceHandler().error(e1
);
439 Instance
.getLibrary().changeTitle(luid
, titleString
, pg
);
440 } catch (IOException e1
) {
441 Instance
.getTraceHandler().error(e1
);
447 Instance
.getLibrary().changeAuthor(luid
, authorString
, pg
);
448 } catch (IOException e1
) {
449 Instance
.getTraceHandler().error(e1
);
454 if (BasicReader
.getReader() == null) {
455 Instance
.getTraceHandler()
456 .error(new Exception(
457 "No reader type has been configured"));
461 exitCode
= read(luid
, chapString
, true);
464 if (BasicReader
.getReader() == null) {
465 Instance
.getTraceHandler()
466 .error(new Exception(
467 "No reader type has been configured"));
471 exitCode
= read(urlString
, chapString
, false);
474 page
= page
== null ?
1 : page
;
476 Instance
.getTraceHandler().error("Incorrect page number");
481 item
= item
== null ?
0 : item
;
483 Instance
.getTraceHandler().error("Incorrect item number");
488 if (BasicReader
.getReader() == null) {
489 Instance
.getTraceHandler()
490 .error(new Exception(
491 "No reader type has been configured"));
497 if (searchOn
== null) {
498 BasicReader
.getReader().search(true);
499 } else if (search
!= null) {
501 BasicReader
.getReader().search(searchOn
, search
, page
,
506 } catch (IOException e1
) {
507 Instance
.getTraceHandler().error(e1
);
513 if (searchOn
== null) {
518 page
= page
== null ?
1 : page
;
520 Instance
.getTraceHandler().error("Incorrect page number");
525 item
= item
== null ?
0 : item
;
527 Instance
.getTraceHandler().error("Incorrect item number");
532 if (BasicReader
.getReader() == null) {
533 Instance
.getTraceHandler()
534 .error(new Exception(
535 "No reader type has been configured"));
541 BasicReader
.getReader().searchTag(searchOn
, page
, item
,
542 true, tags
.toArray(new Integer
[] {}));
543 } catch (IOException e1
) {
544 Instance
.getTraceHandler().error(e1
);
557 .println(String
.format("Fanfix version %s"
558 + "%nhttps://github.com/nikiroo/fanfix/"
559 + "%n\tWritten by Nikiroo",
560 Version
.getCurrentVersion()));
561 updates
.ok(); // we consider it read
564 if (BasicReader
.getReader() == null) {
565 Instance
.getTraceHandler()
566 .error(new Exception(
567 "No reader type has been configured"));
571 BasicReader
.getReader().browse(null);
579 ServerObject server
= new RemoteLibraryServer(key
, port
);
580 server
.setTraceHandler(Instance
.getTraceHandler());
582 } catch (IOException e
) {
583 Instance
.getTraceHandler().error(e
);
592 new RemoteLibrary(key
, host
, port
).exit();
595 exitCode
= 255; // should not be reachable (REMOTE -> START)
601 Instance
.getTempFiles().close();
602 } catch (IOException e
) {
603 Instance
.getTraceHandler()
604 .error(new IOException(
605 "Cannot dispose of the temporary files", e
));
608 if (exitCode
== 255) {
612 System
.exit(exitCode
);
616 * Import the given resource into the {@link LocalLibrary}.
619 * the resource to import
621 * the optional progress reporter
623 * @return the exit return code (0 = success)
625 public static int imprt(String urlString
, Progress pg
) {
627 Story story
= Instance
.getLibrary().imprt(
628 BasicReader
.getUrl(urlString
), pg
);
629 System
.out
.println(story
.getMeta().getLuid() + ": \""
630 + story
.getMeta().getTitle() + "\" imported.");
631 } catch (IOException e
) {
632 Instance
.getTraceHandler().error(e
);
640 * Export the {@link Story} from the {@link LocalLibrary} to the given
646 * the {@link OutputType} to use
650 * the optional progress reporter
652 * @return the exit return code (0 = success)
654 public static int export(String luid
, String typeString
, String target
,
656 OutputType type
= OutputType
.valueOfNullOkUC(typeString
, null);
658 Instance
.getTraceHandler().error(
659 new Exception(trans(StringId
.OUTPUT_DESC
, typeString
)));
664 Instance
.getLibrary().export(luid
, type
, target
, pg
);
665 } catch (IOException e
) {
666 Instance
.getTraceHandler().error(e
);
674 * List the stories of the given source from the {@link LocalLibrary}
675 * (unless NULL is passed, in which case all stories will be listed).
678 * the source to list the known stories of, or NULL to list all
681 * @return the exit return code (0 = success)
683 private static int list(String source
) {
684 List
<MetaData
> stories
;
685 stories
= BasicReader
.getReader().getLibrary().getListBySource(source
);
687 for (MetaData story
: stories
) {
689 if (story
.getAuthor() != null && !story
.getAuthor().isEmpty()) {
690 author
= " (" + story
.getAuthor() + ")";
693 System
.out
.println(story
.getLuid() + ": " + story
.getTitle()
700 * Start the current reader for this {@link Story}.
703 * the LUID of the {@link Story} in the {@link LocalLibrary}
704 * <b>or</b> the {@link Story} {@link URL}
706 * which {@link Chapter} to read (starting at 1), or NULL to get
707 * the {@link Story} description
709 * TRUE if the source is the {@link Story} LUID, FALSE if it is a
712 * @return the exit return code (0 = success)
714 private static int read(String story
, String chapString
, boolean library
) {
716 Reader reader
= BasicReader
.getReader();
718 reader
.setMeta(story
);
720 reader
.setMeta(BasicReader
.getUrl(story
), null);
723 if (chapString
!= null) {
725 reader
.setChapter(Integer
.parseInt(chapString
));
727 } catch (NumberFormatException e
) {
728 Instance
.getTraceHandler().error(
729 new IOException("Chapter number cannot be parsed: "
736 } catch (IOException e
) {
737 Instance
.getTraceHandler().error(e
);
745 * Convert the {@link Story} into another format.
748 * the source {@link Story} to convert
750 * the {@link OutputType} to convert to
754 * TRUE to also export the cover and info file, even if the given
755 * {@link OutputType} does not usually save them
757 * the optional progress reporter
759 * @return the exit return code (0 = success)
761 public static int convert(String urlString
, String typeString
,
762 String target
, boolean infoCover
, Progress pg
) {
765 Instance
.getTraceHandler().trace("Convert: " + urlString
);
766 String sourceName
= urlString
;
768 URL source
= BasicReader
.getUrl(urlString
);
769 sourceName
= source
.toString();
770 if (source
.toString().startsWith("file://")) {
771 sourceName
= sourceName
.substring("file://".length());
774 OutputType type
= OutputType
.valueOfAllOkUC(typeString
, null);
776 Instance
.getTraceHandler().error(
777 new IOException(trans(StringId
.ERR_BAD_OUTPUT_TYPE
,
783 BasicSupport support
= BasicSupport
.getSupport(source
);
785 if (support
!= null) {
786 Instance
.getTraceHandler().trace(
787 "Support found: " + support
.getClass());
788 Progress pgIn
= new Progress();
789 Progress pgOut
= new Progress();
792 pg
.addProgress(pgIn
, 1);
793 pg
.addProgress(pgOut
, 1);
796 Story story
= support
.process(pgIn
);
798 target
= new File(target
).getAbsolutePath();
799 BasicOutput
.getOutput(type
, infoCover
, infoCover
)
800 .process(story
, target
, pgOut
);
801 } catch (IOException e
) {
802 Instance
.getTraceHandler().error(
803 new IOException(trans(StringId
.ERR_SAVING
,
808 Instance
.getTraceHandler().error(
809 new IOException(trans(
810 StringId
.ERR_NOT_SUPPORTED
, source
)));
814 } catch (IOException e
) {
815 Instance
.getTraceHandler().error(
816 new IOException(trans(StringId
.ERR_LOADING
,
821 } catch (MalformedURLException e
) {
822 Instance
.getTraceHandler()
823 .error(new IOException(trans(StringId
.ERR_BAD_URL
,
832 * Simple shortcut method to call {link Instance#getTrans()#getString()}.
835 * the ID to translate
837 * @return the translated result
839 private static String
trans(StringId id
, Object
... params
) {
840 return Instance
.getTrans().getString(id
, params
);
844 * Display the correct syntax of the program to the user to stdout, or an
845 * error message if the syntax used was wrong on stderr.
848 * TRUE to show the syntax help, FALSE to show "syntax error"
850 private static void syntax(boolean showHelp
) {
852 StringBuilder builder
= new StringBuilder();
853 for (SupportType type
: SupportType
.values()) {
854 builder
.append(trans(StringId
.ERR_SYNTAX_TYPE
, type
.toString(),
856 builder
.append('\n');
859 String typesIn
= builder
.toString();
860 builder
.setLength(0);
862 for (OutputType type
: OutputType
.values()) {
863 builder
.append(trans(StringId
.ERR_SYNTAX_TYPE
, type
.toString(),
864 type
.getDesc(true)));
865 builder
.append('\n');
868 String typesOut
= builder
.toString();
870 System
.out
.println(trans(StringId
.HELP_SYNTAX
, typesIn
, typesOut
));
872 System
.err
.println(trans(StringId
.ERR_SYNTAX
));
877 * Set the default reader type for this session only (it can be changed in
878 * the configuration file, too, but this value will override it).
880 * @param readerTypeString
883 private static int setReaderType(String readerTypeString
) {
885 ReaderType readerType
= ReaderType
.valueOf(readerTypeString
887 BasicReader
.setDefaultReaderType(readerType
);
889 } catch (IllegalArgumentException e
) {
890 Instance
.getTraceHandler().error(
891 new IOException("Unknown reader type: " + readerTypeString
,