--list was causing problems + move reader
authorNiki Roo <niki@nikiroo.be>
Sat, 11 Feb 2017 22:12:44 +0000 (23:12 +0100)
committerNiki Roo <niki@nikiroo.be>
Sat, 11 Feb 2017 22:12:44 +0000 (23:12 +0100)
Fix a bug in Cache.java which made it impossible to download,
even from the local cache, if the current referer was not set
+ move CliReader into a new package and use an interface,
so it will work better with the second reader (simple HTML
sent to the local browser, still not ready)

src/be/nikiroo/fanfix/Cache.java
src/be/nikiroo/fanfix/Main.java
src/be/nikiroo/fanfix/package-info.java
src/be/nikiroo/fanfix/reader/FanfixReader.java [new file with mode: 0644]
src/be/nikiroo/fanfix/reader/cli/CliReader.java [moved from src/be/nikiroo/fanfix/reader/CliReader.java with 53% similarity]

index 75a0f5d52e2c194a8d7b614b005387c38a088824..20d2885df2f5fd43ecebe72c2dbb80f222aa65bb 100644 (file)
@@ -302,7 +302,7 @@ public class Cache {
                conn.setRequestProperty("User-Agent", UA);
                conn.setRequestProperty("Cookie", generateCookies(support));
                conn.setRequestProperty("Accept-Encoding", "gzip");
-               if (support != null) {
+               if (support != null && support.getCurrentReferer() != null) {
                        conn.setRequestProperty("Referer", support.getCurrentReferer()
                                        .toString());
                        conn.setRequestProperty("Host", support.getCurrentReferer()
index 45b87c429bd9aca166a5c9558c01e95571d71109..3ecc612eb2f111f14cd39a5c7e853ed447fcd32c 100644 (file)
@@ -10,7 +10,8 @@ import be.nikiroo.fanfix.data.Chapter;
 import be.nikiroo.fanfix.data.Story;
 import be.nikiroo.fanfix.output.BasicOutput;
 import be.nikiroo.fanfix.output.BasicOutput.OutputType;
-import be.nikiroo.fanfix.reader.CliReader;
+import be.nikiroo.fanfix.reader.FanfixReader;
+import be.nikiroo.fanfix.reader.cli.CliReader;
 import be.nikiroo.fanfix.supported.BasicSupport;
 import be.nikiroo.fanfix.supported.BasicSupport.SupportType;
 
@@ -191,7 +192,7 @@ public class Main {
                        return 1;
                }
 
-               CliReader.list(type);
+               new CliReader().start(type);
 
                return 0;
        }
@@ -213,11 +214,11 @@ public class Main {
         */
        private static int read(String story, String chap, boolean library) {
                try {
-                       CliReader reader;
+                       FanfixReader reader = new CliReader();
                        if (library) {
-                               reader = new CliReader(story);
+                               reader.setStory(story);
                        } else {
-                               reader = new CliReader(getUrl(story));
+                               reader.setStory(getUrl(story));
                        }
 
                        if (chap != null) {
index e1043395c2be8d99f55cc76daa0dc702fccc8745..4423974f6c014d7a7db92c9d9fe7dc3079051a6e 100644 (file)
@@ -1,5 +1,5 @@
 /**
- * Fanfic Reader is a program that can support a few different websites from 
+ * Fanfix is a program that can support a few different websites from 
  * which to retrieve stories, then process them into <tt>epub</tt> (or other)
  * files that you can read anywhere.
  * <p>
diff --git a/src/be/nikiroo/fanfix/reader/FanfixReader.java b/src/be/nikiroo/fanfix/reader/FanfixReader.java
new file mode 100644 (file)
index 0000000..009271e
--- /dev/null
@@ -0,0 +1,96 @@
+package be.nikiroo.fanfix.reader;
+
+import java.io.IOException;
+import java.net.URL;
+
+import be.nikiroo.fanfix.Instance;
+import be.nikiroo.fanfix.Library;
+import be.nikiroo.fanfix.data.Story;
+import be.nikiroo.fanfix.supported.BasicSupport;
+import be.nikiroo.fanfix.supported.BasicSupport.SupportType;
+
+/**
+ * Command line {@link Story} reader.
+ * <p>
+ * Will output stories to the console.
+ * 
+ * @author niki
+ */
+public abstract class FanfixReader {
+       private Story story;
+
+       /**
+        * Return the current {@link Story}.
+        * 
+        * @return the {@link Story}
+        */
+       public Story getStory() {
+               return story;
+       }
+
+       /**
+        * Create a new {@link FanfixReader} for a {@link Story} in the
+        * {@link Library} .
+        * 
+        * @param luid
+        *            the {@link Story} ID
+        * @throws IOException
+        *             in case of I/O error
+        */
+       public void setStory(String luid) throws IOException {
+               story = Instance.getLibrary().getStory(luid);
+               if (story == null) {
+                       throw new IOException("Cannot retrieve story from library: " + luid);
+               }
+       }
+
+       /**
+        * Create a new {@link FanfixReader} for an external {@link Story}.
+        * 
+        * @param source
+        *            the {@link Story} {@link URL}
+        * @throws IOException
+        *             in case of I/O error
+        */
+       public void setStory(URL source) throws IOException {
+               BasicSupport support = BasicSupport.getSupport(source);
+               if (support == null) {
+                       throw new IOException("URL not supported: " + source.toString());
+               }
+
+               story = support.process(source);
+               if (story == null) {
+                       throw new IOException(
+                                       "Cannot retrieve story from external source: "
+                                                       + source.toString());
+
+               }
+       }
+
+       /**
+        * Start the {@link Story} Reading.
+        * 
+        * @throws IOException
+        *             in case of I/O error or if the {@link Story} was not
+        *             previously set
+        */
+       public abstract void read() throws IOException;
+
+       /**
+        * Read the selected chapter (starting at 1).
+        * 
+        * @param chapter
+        *            the chapter
+        */
+       public abstract void read(int chapter);
+
+       /**
+        * Start the reader in browse mode for the given type (or pass NULL for all
+        * types).
+        * 
+        * @param type
+        *            the type of {@link Story} to take into account, or NULL for
+        *            all
+        */
+       public abstract void start(SupportType type);
+}
similarity index 53%
rename from src/be/nikiroo/fanfix/reader/CliReader.java
rename to src/be/nikiroo/fanfix/reader/cli/CliReader.java
index 52a5ea4321f7ef6aa8a1607605b09e39f7708b77..e040a210cb3aaf5e4dbc7ef46721e86b38c5a86b 100644 (file)
@@ -1,18 +1,15 @@
-package be.nikiroo.fanfix.reader;
+package be.nikiroo.fanfix.reader.cli;
 
 import java.io.IOException;
-import java.net.URL;
 import java.util.List;
 
 import be.nikiroo.fanfix.Instance;
-import be.nikiroo.fanfix.Library;
 import be.nikiroo.fanfix.bundles.StringId;
 import be.nikiroo.fanfix.data.Chapter;
 import be.nikiroo.fanfix.data.MetaData;
 import be.nikiroo.fanfix.data.Paragraph;
 import be.nikiroo.fanfix.data.Story;
-import be.nikiroo.fanfix.output.BasicOutput.OutputType;
-import be.nikiroo.fanfix.supported.BasicSupport;
+import be.nikiroo.fanfix.reader.FanfixReader;
 import be.nikiroo.fanfix.supported.BasicSupport.SupportType;
 
 /**
@@ -22,56 +19,23 @@ import be.nikiroo.fanfix.supported.BasicSupport.SupportType;
  * 
  * @author niki
  */
-public class CliReader {
-       private Story story;
-
+public class CliReader extends FanfixReader {
        /**
-        * Create a new {@link CliReader} for a {@link Story} in the {@link Library}
-        * .
+        * Start the {@link Story} Reading.
         * 
-        * @param luid
-        *            the {@link Story} ID
         * @throws IOException
-        *             in case of I/O error
+        *             in case of I/O error or if the {@link Story} was not
+        *             previously set
         */
-       public CliReader(String luid) throws IOException {
-               story = Instance.getLibrary().getStory(luid);
-               if (story == null) {
-                       throw new IOException("Cannot retrieve story from library: " + luid);
+       public void read() throws IOException {
+               if (getStory() == null) {
+                       throw new IOException("No story to read");
                }
-       }
-
-       /**
-        * Create a new {@link CliReader} for an external {@link Story}.
-        * 
-        * @param source
-        *            the {@link Story} {@link URL}
-        * @throws IOException
-        *             in case of I/O error
-        */
-       public CliReader(URL source) throws IOException {
-               BasicSupport support = BasicSupport.getSupport(source);
-               if (support == null) {
-                       throw new IOException("URL not supported: " + source.toString());
-               }
-
-               story = support.process(source);
-               if (story == null) {
-                       throw new IOException(
-                                       "Cannot retrieve story from external source: "
-                                                       + source.toString());
 
-               }
-       }
-
-       /**
-        * Read the information about the {@link Story}.
-        */
-       public void read() {
                String title = "";
                String author = "";
 
-               MetaData meta = story.getMeta();
+               MetaData meta = getStory().getMeta();
                if (meta != null) {
                        if (meta.getTitle() != null) {
                                title = meta.getTitle();
@@ -89,7 +53,7 @@ public class CliReader {
                System.out.println(author);
                System.out.println("");
 
-               for (Chapter chap : story) {
+               for (Chapter chap : getStory()) {
                        if (chap.getName() != null && !chap.getName().isEmpty()) {
                                System.out.println(Instance.getTrans().getString(
                                                StringId.CHAPTER_NAMED, chap.getNumber(),
@@ -108,10 +72,10 @@ public class CliReader {
         *            the chapter
         */
        public void read(int chapter) {
-               if (chapter > story.getChapters().size()) {
+               if (chapter > getStory().getChapters().size()) {
                        System.err.println("Chapter " + chapter + ": no such chapter");
                } else {
-                       Chapter chap = story.getChapters().get(chapter - 1);
+                       Chapter chap = getStory().getChapters().get(chapter - 1);
                        System.out.println("Chapter " + chap.getNumber() + ": "
                                        + chap.getName());
 
@@ -122,14 +86,8 @@ public class CliReader {
                }
        }
 
-       /**
-        * List all the stories available in the {@link Library} by
-        * {@link OutputType} (or all of them if the given type is NULL)
-        * 
-        * @param type
-        *            the {@link OutputType} or NULL for all stories
-        */
-       public static void list(SupportType type) {
+       @Override
+       public void start(SupportType type) {
                List<MetaData> stories;
                stories = Instance.getLibrary().getList(type);