Add URL into .info and MetaData, work on Library
authorNiki Roo <niki@nikiroo.be>
Sun, 12 Feb 2017 22:19:48 +0000 (23:19 +0100)
committerNiki Roo <niki@nikiroo.be>
Sun, 12 Feb 2017 22:19:48 +0000 (23:19 +0100)
20 files changed:
src/be/nikiroo/fanfix/Instance.java
src/be/nikiroo/fanfix/Library.java
src/be/nikiroo/fanfix/bundles/Config.java
src/be/nikiroo/fanfix/bundles/config.properties
src/be/nikiroo/fanfix/data/MetaData.java
src/be/nikiroo/fanfix/output/BasicOutput.java
src/be/nikiroo/fanfix/output/Cbz.java
src/be/nikiroo/fanfix/output/Epub.java
src/be/nikiroo/fanfix/output/Html.java [new file with mode: 0644]
src/be/nikiroo/fanfix/output/InfoCover.java
src/be/nikiroo/fanfix/output/InfoText.java
src/be/nikiroo/fanfix/output/LaTeX.java
src/be/nikiroo/fanfix/output/Text.java
src/be/nikiroo/fanfix/supported/E621.java
src/be/nikiroo/fanfix/supported/Epub.java
src/be/nikiroo/fanfix/supported/Fanfiction.java
src/be/nikiroo/fanfix/supported/Fimfiction.java
src/be/nikiroo/fanfix/supported/InfoReader.java
src/be/nikiroo/fanfix/supported/MangaFox.java
src/be/nikiroo/fanfix/supported/Text.java

index ae96d481a930d60ffe9576e397eaf6493e05917d..9c20682fd8d6798da64eea8406ddfc86fa1c9dee 100644 (file)
@@ -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);
index 1b9419a6cb73804c3b509fff15322fc30288f8ce..304f19f462e1a05a899e4b01de430e6c283abfef 100644 (file)
@@ -29,17 +29,25 @@ public class Library {
        private File baseDir;
        private Map<MetaData, File> 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<MetaData, File>();
                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<MetaData, File> 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 <b>must</b> 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 <b>must</b> be
+        * correct, or NULL to get the next free one.
         * 
         * @param story
         *            the {@link Story} to save
+        * @param luid
+        *            the <b>correct</b> 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;
+               }
+       }
 }
index 7a663e403e626fe7a2568ef22b50cb0b72b582b4..060bc8126ac9dee202ce2c78a21b810c76c6c10b 100644 (file)
@@ -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, //
index dd9fa1dcac33d7042c5dab13cbed67bd6c148de2..67b5e7fcc93fdd8496badf884eda26f551eb615a 100644 (file)
@@ -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)
index 76a9c7ba97f43c687e932692ecd5eff44a4bcda0..5ec980163f5ed014018febc6a3fcd020870ddeff 100644 (file)
@@ -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
index e2bf2ff89fc0e28c382b8c8fa20db5a357b27e49..9b53859a199c05aa7742b8cd259a310230f5167d 100644 (file)
@@ -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.
-        * <p>
-        * 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);
                        }
                }
 
index 67175b143d007ce916a261d5ec00f7228f126692..f618f59da51a192065ea7cd4aa9d9c141dcbe9e3 100644 (file)
@@ -43,7 +43,7 @@ class Cbz extends BasicOutput {
        }
 
        @Override
-       protected String getDefaultExtension() {
+       public String getDefaultExtension() {
                return ".cbz";
        }
 
index 1706d8b1002c1009ecedb64208857dccf19eb6c6..f27391b4c1e7b6583304bdcb4b05ff45e2ac1850 100644 (file)
@@ -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 (file)
index 0000000..4226bf8
--- /dev/null
@@ -0,0 +1,6 @@
+package be.nikiroo.fanfix.output;
+
+//TODO: implement it for LocalReader
+class Html extends Text {
+
+}
index 1cf32a06adb75331ff7a2bf3759b05d96e7b75d0..74505e7dd72bc4b290feb801ab7c588166a0280e 100644 (file)
@@ -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());
index 69376856e83216c4c2f99e55e12c92a24b2abe55..dc00b8afca84d568a2d57bdfa5ae1a5135b180bf 100644 (file)
@@ -19,7 +19,7 @@ class InfoText extends Text {
                        StringId.CLOSE_DOUBLE_QUOTE);
 
        @Override
-       protected String getDefaultExtension() {
+       public String getDefaultExtension() {
                return "";
        }
 
index a8d6d379b790d0e3d1900a7d3e8e7c8b239371d8..a4ed4d27eaf37592c1eae3aa97a105ed59a96c5e 100644 (file)
@@ -46,7 +46,7 @@ class LaTeX extends BasicOutput {
        }
 
        @Override
-       protected String getDefaultExtension() {
+       public String getDefaultExtension() {
                return ".tex";
        }
 
index 22056ed3cf0185ab5407903ce42b4036a7b91590..6db7b7be8d3f4ee53395345acd61a29ba802181b 100644 (file)
@@ -39,7 +39,7 @@ class Text extends BasicOutput {
        }
 
        @Override
-       protected String getDefaultExtension() {
+       public String getDefaultExtension() {
                return ".txt";
        }
 
index bc6ba5b1501e83a1c1afbbdf38e6804d7147fdf7..cb4ae4657a7807c104cb5c7dbefe7324f015e9de 100644 (file)
@@ -39,6 +39,7 @@ class E621 extends BasicSupport {
                meta.setDate("");
                meta.setTags(new ArrayList<String>()); // TODDO ???
                meta.setSource(getSourceName());
+               meta.setUrl(source.toString());
                meta.setPublisher(getSourceName());
                meta.setUuid(source.toString());
                meta.setLuid("");
index 32349a89072a76b1cda38f6b9f7e73de2b247e22..f6a0b410338a3a1a7635601be402054fe5c761d1 100644 (file)
@@ -152,6 +152,7 @@ class Epub extends InfoText {
                        meta.setLang("EN");
                        meta.setTags(new ArrayList<String>());
                        meta.setSource(getSourceName());
+                       meta.setUrl(source.toString());
                }
        }
 
index 9be098d6b8bd67e5a419a10acd8fedc52ee5d695..2cbe696052dc16ec61e3ba333ac50c8180486aaf 100644 (file)
@@ -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("");
index a6bd475fad05e3ace03df18df271384e4cc934ee..03a7cc21dc7f94643326e7baa37c35ec860f121f 100644 (file)
@@ -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("");
index 1695ebdeadfb9792dcbe73011fb02540692e2fdc..f84f01b27ec721c971e2dfb18381be57fcaa3df9 100644 (file)
@@ -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"));
index 3e2dbadc9a7353f3f447dcdc47aec3e016a83ba1..5455537b340f36993e2a55915ce955c8e48aad04 100644 (file)
@@ -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("");
index 4bb86fe78d3d6308875dc7b856530f57b8b17468..f7ecadad15f8217dd5d1a1c43887d056f2d383e1 100644 (file)
@@ -52,7 +52,8 @@ class Text extends BasicSupport {
                meta.setDate(getDate(reset(in)));
                meta.setTags(new ArrayList<String>());
                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