From 2206ef66ee00ad42d806f04a7b7ad6f8cb2d8828 Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Sun, 12 Feb 2017 23:19:48 +0100 Subject: [PATCH] Add URL into .info and MetaData, work on Library --- src/be/nikiroo/fanfix/Instance.java | 6 +- src/be/nikiroo/fanfix/Library.java | 115 +++++++++++++++--- src/be/nikiroo/fanfix/bundles/Config.java | 4 +- .../nikiroo/fanfix/bundles/config.properties | 4 +- src/be/nikiroo/fanfix/data/MetaData.java | 28 ++++- src/be/nikiroo/fanfix/output/BasicOutput.java | 27 +++- src/be/nikiroo/fanfix/output/Cbz.java | 2 +- src/be/nikiroo/fanfix/output/Epub.java | 2 +- src/be/nikiroo/fanfix/output/Html.java | 6 + src/be/nikiroo/fanfix/output/InfoCover.java | 9 +- src/be/nikiroo/fanfix/output/InfoText.java | 2 +- src/be/nikiroo/fanfix/output/LaTeX.java | 2 +- src/be/nikiroo/fanfix/output/Text.java | 2 +- src/be/nikiroo/fanfix/supported/E621.java | 1 + src/be/nikiroo/fanfix/supported/Epub.java | 1 + .../nikiroo/fanfix/supported/Fanfiction.java | 1 + .../nikiroo/fanfix/supported/Fimfiction.java | 1 + .../nikiroo/fanfix/supported/InfoReader.java | 1 + src/be/nikiroo/fanfix/supported/MangaFox.java | 1 + src/be/nikiroo/fanfix/supported/Text.java | 3 +- 20 files changed, 172 insertions(+), 46 deletions(-) create mode 100644 src/be/nikiroo/fanfix/output/Html.java diff --git a/src/be/nikiroo/fanfix/Instance.java b/src/be/nikiroo/fanfix/Instance.java index ae96d481..9c20682f 100644 --- a/src/be/nikiroo/fanfix/Instance.java +++ b/src/be/nikiroo/fanfix/Instance.java @@ -6,6 +6,7 @@ import java.io.IOException; import be.nikiroo.fanfix.bundles.Config; import be.nikiroo.fanfix.bundles.ConfigBundle; import be.nikiroo.fanfix.bundles.StringIdBundle; +import be.nikiroo.fanfix.output.BasicOutput.OutputType; import be.nikiroo.utils.resources.Bundles; /** @@ -56,12 +57,13 @@ public class Instance { trans = new StringIdBundle(getLang()); try { - lib = new Library(getFile(Config.LIBRARY_DIR)); + lib = new Library(getFile(Config.LIBRARY_DIR), + OutputType.INFO_TEXT, OutputType.CBZ); } catch (Exception e) { syserr(new IOException("Cannot create library for directory: " + getFile(Config.LIBRARY_DIR), e)); } - + debug = Instance.getConfig().getBoolean(Config.DEBUG_ERR, false); coverDir = getFile(Config.DEFAULT_COVERS_DIR); File tmp = getFile(Config.CACHE_DIR); diff --git a/src/be/nikiroo/fanfix/Library.java b/src/be/nikiroo/fanfix/Library.java index 1b9419a6..304f19f4 100644 --- a/src/be/nikiroo/fanfix/Library.java +++ b/src/be/nikiroo/fanfix/Library.java @@ -29,17 +29,25 @@ public class Library { private File baseDir; private Map stories; private int lastId; + private OutputType text; + private OutputType image; /** * Create a new {@link Library} with the given backend directory. * * @param dir - * the directoy where to find the {@link Story} objects + * the directory where to find the {@link Story} objects + * @param text + * the {@link OutputType} to save the text-focused stories into + * @param image + * the {@link OutputType} to save the images-focused stories into */ - public Library(File dir) { + public Library(File dir, OutputType text, OutputType image) { this.baseDir = dir; this.stories = new HashMap(); this.lastId = 0; + this.text = text; + this.image = image; dir.mkdirs(); } @@ -67,6 +75,26 @@ public class Library { return list; } + /** + * Retrieve a {@link File} corresponding to the given {@link Story}. + * + * @param luid + * the Library UID of the story + * + * @return the corresponding {@link Story} + */ + public File getFile(String luid) { + if (luid != null) { + for (Entry entry : getStories().entrySet()) { + if (luid.equals(entry.getKey().getLuid())) { + return entry.getValue(); + } + } + } + + return null; + } + /** * Retrieve a specific {@link Story}. * @@ -121,12 +149,7 @@ public class Library { throw new IOException("URL not supported: " + url.toString()); } - getStories(); // refresh lastId - Story story = support.process(url); - story.getMeta().setLuid(String.format("%03d", (++lastId))); - save(story); - - return story; + return save(support.process(url), null); } /** @@ -155,37 +178,61 @@ public class Library { } /** - * Save a story as-is to the {@link Library} -- the LUID must be - * correct. + * Save a {@link Story} to the {@link Library}. + * + * @param story + * the {@link Story} to save + * + * @return the same {@link Story}, whose LUID may have changed + * + * @throws IOException + * in case of I/O error + */ + public Story save(Story story) throws IOException { + return save(story, null); + } + + /** + * Save a {@link Story} to the {@link Library} -- the LUID must be + * correct, or NULL to get the next free one. * * @param story * the {@link Story} to save + * @param luid + * the correct LUID or NULL to get the next free one + * + * @return the same {@link Story}, whose LUID may have changed * * @throws IOException * in case of I/O error */ - private void save(Story story) throws IOException { + private Story save(Story story, String luid) throws IOException { MetaData key = story.getMeta(); + if (luid == null || luid.isEmpty()) { + getStories(); // refresh lastId if needed + key.setLuid(String.format("%03d", (++lastId))); + } else { + key.setLuid(luid); + } + getDir(key).mkdirs(); if (!getDir(key).exists()) { throw new IOException("Cannot create library dir"); } OutputType out; - SupportType in; if (key != null && key.isImageDocument()) { - in = SupportType.CBZ; - out = OutputType.CBZ; + out = image; } else { - in = SupportType.INFO_TEXT; - out = OutputType.INFO_TEXT; + out = text; } + BasicOutput it = BasicOutput.getOutput(out, true); File file = it.process(story, getFile(key).getPath()); - getStories().put( - BasicSupport.getSupport(in).processMeta(file.toURI().toURL()) - .getMeta(), file); + getStories().put(story.getMeta(), file); + + return story; } /** @@ -225,11 +272,12 @@ public class Library { if (stories.isEmpty()) { lastId = 0; + String ext = ".info"; for (File dir : baseDir.listFiles()) { if (dir.isDirectory()) { for (File file : dir.listFiles()) { try { - if (file.getPath().toLowerCase().endsWith(".info")) { + if (file.getPath().toLowerCase().endsWith(ext)) { MetaData meta = InfoReader.readMeta(file); try { int id = Integer.parseInt(meta.getLuid()); @@ -237,6 +285,17 @@ public class Library { lastId = id; } + // Replace .info with whatever is needed: + String path = file.getPath(); + path = path.substring(0, path.length() + - ext.length()); + + String newExt = getOutputType(meta) + .getDefaultExtension(); + + file = new File(path + newExt); + // + stories.put(meta, file); } catch (Exception e) { @@ -261,4 +320,20 @@ public class Library { return stories; } + + /** + * Return the {@link OutputType} for this {@link Story}. + * + * @param meta + * the {@link Story} {@link MetaData} + * + * @return the type + */ + private OutputType getOutputType(MetaData meta) { + if (meta != null && meta.isImageDocument()) { + return image; + } else { + return text; + } + } } diff --git a/src/be/nikiroo/fanfix/bundles/Config.java b/src/be/nikiroo/fanfix/bundles/Config.java index 7a663e40..060bc812 100644 --- a/src/be/nikiroo/fanfix/bundles/Config.java +++ b/src/be/nikiroo/fanfix/bundles/Config.java @@ -38,9 +38,9 @@ public enum Config { LATEX_LANG_EN, // @Meta(what = "LaTeX output language", where = "LaTeX", format = "", info = "LaTeX full name for French") LATEX_LANG_FR, // - @Meta(what = "other 'by' prefixes before author name", where = "", format = "coma-separated list", info = "used to identify the author") + @Meta(what = "other 'by' prefixes before author name", where = "", format = "comma-separated list", info = "used to identify the author") BYS, // - @Meta(what = "Chapter identification languages", where = "", format = "coma-separated list", info = "used to identify a starting chapter in text mode") + @Meta(what = "Chapter identification languages", where = "", format = "comma-separated list", info = "used to identify a starting chapter in text mode") CHAPTER, // @Meta(what = "Chapter identification string", where = "", format = "", info = "used to identify a starting chapter in text mode") CHAPTER_EN, // diff --git a/src/be/nikiroo/fanfix/bundles/config.properties b/src/be/nikiroo/fanfix/bundles/config.properties index dd9fa1dc..67b5e7fc 100644 --- a/src/be/nikiroo/fanfix/bundles/config.properties +++ b/src/be/nikiroo/fanfix/bundles/config.properties @@ -47,10 +47,10 @@ LATEX_LANG_EN = english # (WHAT: LaTeX output language, WHERE: LaTeX) # LaTeX full name for French LATEX_LANG_FR = french -# (WHAT: other 'by' prefixes before author name, FORMAT: coma-separated list) +# (WHAT: other 'by' prefixes before author name, FORMAT: comma-separated list) # used to identify the author BYS = by,par,de,©,(c) -# (WHAT: Chapter identification languages, FORMAT: coma-separated list) +# (WHAT: Chapter identification languages, FORMAT: comma-separated list) # used to identify a starting chapter in text mode CHAPTER = EN,FR # (WHAT: Chapter identification string) diff --git a/src/be/nikiroo/fanfix/data/MetaData.java b/src/be/nikiroo/fanfix/data/MetaData.java index 76a9c7ba..5ec98016 100644 --- a/src/be/nikiroo/fanfix/data/MetaData.java +++ b/src/be/nikiroo/fanfix/data/MetaData.java @@ -17,6 +17,7 @@ public class MetaData { private BufferedImage cover; private String subject; private String source; + private String url; private String uuid; private String luid; private String lang; @@ -162,7 +163,7 @@ public class MetaData { } /** - * The source of this story (where it was downloaded from). + * The source of this story (which online library it was downloaded from). * * @return the source */ @@ -171,7 +172,7 @@ public class MetaData { } /** - * The source of this story (where it was downloaded from). + * The source of this story (which online library it was downloaded from). * * @param source * the source to set @@ -181,7 +182,26 @@ public class MetaData { } /** - * A unique value representing the story (it is often an URL). + * The original URL from which this {@link Story} was imported. + * + * @return the url + */ + public String getUrl() { + return url; + } + + /** + * The original URL from which this {@link Story} was imported. + * + * @param url + * the new url to set + */ + public void setUrl(String url) { + this.url = url; + } + + /** + * A unique value representing the story (it is often a URL). * * @return the uuid */ @@ -190,7 +210,7 @@ public class MetaData { } /** - * A unique value representing the story (it is often an URL). + * A unique value representing the story (it is often a URL). * * @param uuid * the uuid to set diff --git a/src/be/nikiroo/fanfix/output/BasicOutput.java b/src/be/nikiroo/fanfix/output/BasicOutput.java index e2bf2ff8..9b53859a 100644 --- a/src/be/nikiroo/fanfix/output/BasicOutput.java +++ b/src/be/nikiroo/fanfix/output/BasicOutput.java @@ -36,7 +36,12 @@ public abstract class BasicOutput { /** ZIP with (PNG) images */ CBZ, /** LaTeX file with "book" template */ - LATEX; + LATEX, + /** HTML files in a dedicated directory */ + HTML, + + ; + public String toString() { return super.toString().toLowerCase(); } @@ -58,6 +63,20 @@ public abstract class BasicOutput { return desc; } + /** + * The default extension to add to the output files. + * + * @return the extension + */ + public String getDefaultExtension() { + BasicOutput output = BasicOutput.getOutput(this, false); + if (output != null) { + return output.getDefaultExtension(); + } + + return null; + } + /** * Call {@link OutputType#valueOf(String.toUpperCase())}. * @@ -208,12 +227,10 @@ public abstract class BasicOutput { /** * The default extension to add to the output files. - *

- * Cannot be NULL! * * @return the extension */ - protected String getDefaultExtension() { + public String getDefaultExtension() { return ""; } @@ -423,6 +440,8 @@ public abstract class BasicOutput { return new Cbz().setType(type, infoCover, infoCover); case LATEX: return new LaTeX().setType(type, infoCover, infoCover); + case HTML: + return new Html().setType(type, false, false); } } diff --git a/src/be/nikiroo/fanfix/output/Cbz.java b/src/be/nikiroo/fanfix/output/Cbz.java index 67175b14..f618f59d 100644 --- a/src/be/nikiroo/fanfix/output/Cbz.java +++ b/src/be/nikiroo/fanfix/output/Cbz.java @@ -43,7 +43,7 @@ class Cbz extends BasicOutput { } @Override - protected String getDefaultExtension() { + public String getDefaultExtension() { return ".cbz"; } diff --git a/src/be/nikiroo/fanfix/output/Epub.java b/src/be/nikiroo/fanfix/output/Epub.java index 1706d8b1..f27391b4 100644 --- a/src/be/nikiroo/fanfix/output/Epub.java +++ b/src/be/nikiroo/fanfix/output/Epub.java @@ -58,7 +58,7 @@ class Epub extends BasicOutput { } @Override - protected String getDefaultExtension() { + public String getDefaultExtension() { return ".epub"; } diff --git a/src/be/nikiroo/fanfix/output/Html.java b/src/be/nikiroo/fanfix/output/Html.java new file mode 100644 index 00000000..4226bf84 --- /dev/null +++ b/src/be/nikiroo/fanfix/output/Html.java @@ -0,0 +1,6 @@ +package be.nikiroo.fanfix.output; + +//TODO: implement it for LocalReader +class Html extends Text { + +} diff --git a/src/be/nikiroo/fanfix/output/InfoCover.java b/src/be/nikiroo/fanfix/output/InfoCover.java index 1cf32a06..74505e7d 100644 --- a/src/be/nikiroo/fanfix/output/InfoCover.java +++ b/src/be/nikiroo/fanfix/output/InfoCover.java @@ -28,20 +28,17 @@ class InfoCover { } } - String lang = meta.getLang(); - if (lang != null) { - lang = lang.toLowerCase(); - } - writeMeta(infoWriter, "TITLE", meta.getTitle()); writeMeta(infoWriter, "AUTHOR", meta.getAuthor()); writeMeta(infoWriter, "DATE", meta.getDate()); writeMeta(infoWriter, "SUBJECT", meta.getSubject()); writeMeta(infoWriter, "SOURCE", meta.getSource()); + writeMeta(infoWriter, "URL", meta.getUrl()); writeMeta(infoWriter, "TAGS", tags); writeMeta(infoWriter, "UUID", meta.getUuid()); writeMeta(infoWriter, "LUID", meta.getLuid()); - writeMeta(infoWriter, "LANG", lang); + writeMeta(infoWriter, "LANG", meta.getLang() == null ? "" + : meta.getLang().toLowerCase()); writeMeta(infoWriter, "IMAGES_DOCUMENT", meta.isImageDocument() ? "true" : "false"); writeMeta(infoWriter, "TYPE", meta.getType()); diff --git a/src/be/nikiroo/fanfix/output/InfoText.java b/src/be/nikiroo/fanfix/output/InfoText.java index 69376856..dc00b8af 100644 --- a/src/be/nikiroo/fanfix/output/InfoText.java +++ b/src/be/nikiroo/fanfix/output/InfoText.java @@ -19,7 +19,7 @@ class InfoText extends Text { StringId.CLOSE_DOUBLE_QUOTE); @Override - protected String getDefaultExtension() { + public String getDefaultExtension() { return ""; } diff --git a/src/be/nikiroo/fanfix/output/LaTeX.java b/src/be/nikiroo/fanfix/output/LaTeX.java index a8d6d379..a4ed4d27 100644 --- a/src/be/nikiroo/fanfix/output/LaTeX.java +++ b/src/be/nikiroo/fanfix/output/LaTeX.java @@ -46,7 +46,7 @@ class LaTeX extends BasicOutput { } @Override - protected String getDefaultExtension() { + public String getDefaultExtension() { return ".tex"; } diff --git a/src/be/nikiroo/fanfix/output/Text.java b/src/be/nikiroo/fanfix/output/Text.java index 22056ed3..6db7b7be 100644 --- a/src/be/nikiroo/fanfix/output/Text.java +++ b/src/be/nikiroo/fanfix/output/Text.java @@ -39,7 +39,7 @@ class Text extends BasicOutput { } @Override - protected String getDefaultExtension() { + public String getDefaultExtension() { return ".txt"; } diff --git a/src/be/nikiroo/fanfix/supported/E621.java b/src/be/nikiroo/fanfix/supported/E621.java index bc6ba5b1..cb4ae465 100644 --- a/src/be/nikiroo/fanfix/supported/E621.java +++ b/src/be/nikiroo/fanfix/supported/E621.java @@ -39,6 +39,7 @@ class E621 extends BasicSupport { meta.setDate(""); meta.setTags(new ArrayList()); // TODDO ??? meta.setSource(getSourceName()); + meta.setUrl(source.toString()); meta.setPublisher(getSourceName()); meta.setUuid(source.toString()); meta.setLuid(""); diff --git a/src/be/nikiroo/fanfix/supported/Epub.java b/src/be/nikiroo/fanfix/supported/Epub.java index 32349a89..f6a0b410 100644 --- a/src/be/nikiroo/fanfix/supported/Epub.java +++ b/src/be/nikiroo/fanfix/supported/Epub.java @@ -152,6 +152,7 @@ class Epub extends InfoText { meta.setLang("EN"); meta.setTags(new ArrayList()); meta.setSource(getSourceName()); + meta.setUrl(source.toString()); } } diff --git a/src/be/nikiroo/fanfix/supported/Fanfiction.java b/src/be/nikiroo/fanfix/supported/Fanfiction.java index 9be098d6..2cbe6960 100644 --- a/src/be/nikiroo/fanfix/supported/Fanfiction.java +++ b/src/be/nikiroo/fanfix/supported/Fanfiction.java @@ -43,6 +43,7 @@ class Fanfiction extends BasicSupport { meta.setDate(getDate(reset(in))); meta.setTags(getTags(reset(in))); meta.setSource(getSourceName()); + meta.setUrl(source.toString()); meta.setPublisher(getSourceName()); meta.setUuid(source.toString()); meta.setLuid(""); diff --git a/src/be/nikiroo/fanfix/supported/Fimfiction.java b/src/be/nikiroo/fanfix/supported/Fimfiction.java index a6bd475f..03a7cc21 100644 --- a/src/be/nikiroo/fanfix/supported/Fimfiction.java +++ b/src/be/nikiroo/fanfix/supported/Fimfiction.java @@ -42,6 +42,7 @@ class Fimfiction extends BasicSupport { meta.setDate(getDate(reset(in))); meta.setTags(getTags(reset(in))); meta.setSource(getSourceName()); + meta.setUrl(source.toString()); meta.setPublisher(getSourceName()); meta.setUuid(source.toString()); meta.setLuid(""); diff --git a/src/be/nikiroo/fanfix/supported/InfoReader.java b/src/be/nikiroo/fanfix/supported/InfoReader.java index 1695ebde..f84f01b2 100644 --- a/src/be/nikiroo/fanfix/supported/InfoReader.java +++ b/src/be/nikiroo/fanfix/supported/InfoReader.java @@ -42,6 +42,7 @@ public class InfoReader { meta.setDate(getInfoTag(in, "DATE")); meta.setTags(getInfoTagList(in, "TAGS", ",")); meta.setSource(getInfoTag(in, "SOURCE")); + meta.setUrl(getInfoTag(in, "URL")); meta.setPublisher(getInfoTag(in, "PUBLISHER")); meta.setUuid(getInfoTag(in, "UUID")); meta.setLuid(getInfoTag(in, "LUID")); diff --git a/src/be/nikiroo/fanfix/supported/MangaFox.java b/src/be/nikiroo/fanfix/supported/MangaFox.java index 3e2dbadc..5455537b 100644 --- a/src/be/nikiroo/fanfix/supported/MangaFox.java +++ b/src/be/nikiroo/fanfix/supported/MangaFox.java @@ -37,6 +37,7 @@ class MangaFox extends BasicSupport { meta.setDate(getDate(reset(in))); meta.setTags(getTags(reset(in))); meta.setSource(getSourceName()); + meta.setUrl(source.toString()); meta.setPublisher(getSourceName()); meta.setUuid(source.toString()); meta.setLuid(""); diff --git a/src/be/nikiroo/fanfix/supported/Text.java b/src/be/nikiroo/fanfix/supported/Text.java index 4bb86fe7..f7ecadad 100644 --- a/src/be/nikiroo/fanfix/supported/Text.java +++ b/src/be/nikiroo/fanfix/supported/Text.java @@ -52,7 +52,8 @@ class Text extends BasicSupport { meta.setDate(getDate(reset(in))); meta.setTags(new ArrayList()); meta.setSource(getSourceName()); - meta.setPublisher(""); // often sourceName + meta.setUrl(source.toString()); + meta.setPublisher(""); meta.setUuid(source.toString()); meta.setLuid(""); meta.setLang(getLang(source, reset(in))); // default is EN -- 2.27.0