From 89cb07a69f3ee217f9ea6a4284bec0df94ef77fa Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Sat, 11 Feb 2017 23:12:44 +0100 Subject: [PATCH] --list was causing problems + move reader 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 | 2 +- src/be/nikiroo/fanfix/Main.java | 11 ++- src/be/nikiroo/fanfix/package-info.java | 2 +- .../nikiroo/fanfix/reader/FanfixReader.java | 96 +++++++++++++++++++ .../fanfix/reader/{ => cli}/CliReader.java | 72 +++----------- 5 files changed, 119 insertions(+), 64 deletions(-) create mode 100644 src/be/nikiroo/fanfix/reader/FanfixReader.java rename src/be/nikiroo/fanfix/reader/{ => cli}/CliReader.java (53%) diff --git a/src/be/nikiroo/fanfix/Cache.java b/src/be/nikiroo/fanfix/Cache.java index 75a0f5d5..20d2885d 100644 --- a/src/be/nikiroo/fanfix/Cache.java +++ b/src/be/nikiroo/fanfix/Cache.java @@ -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() diff --git a/src/be/nikiroo/fanfix/Main.java b/src/be/nikiroo/fanfix/Main.java index 45b87c42..3ecc612e 100644 --- a/src/be/nikiroo/fanfix/Main.java +++ b/src/be/nikiroo/fanfix/Main.java @@ -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) { diff --git a/src/be/nikiroo/fanfix/package-info.java b/src/be/nikiroo/fanfix/package-info.java index e1043395..4423974f 100644 --- a/src/be/nikiroo/fanfix/package-info.java +++ b/src/be/nikiroo/fanfix/package-info.java @@ -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 epub (or other) * files that you can read anywhere. *

diff --git a/src/be/nikiroo/fanfix/reader/FanfixReader.java b/src/be/nikiroo/fanfix/reader/FanfixReader.java new file mode 100644 index 00000000..009271e5 --- /dev/null +++ b/src/be/nikiroo/fanfix/reader/FanfixReader.java @@ -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. + *

+ * 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); +} diff --git a/src/be/nikiroo/fanfix/reader/CliReader.java b/src/be/nikiroo/fanfix/reader/cli/CliReader.java similarity index 53% rename from src/be/nikiroo/fanfix/reader/CliReader.java rename to src/be/nikiroo/fanfix/reader/cli/CliReader.java index 52a5ea43..e040a210 100644 --- a/src/be/nikiroo/fanfix/reader/CliReader.java +++ b/src/be/nikiroo/fanfix/reader/cli/CliReader.java @@ -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 stories; stories = Instance.getLibrary().getList(type); -- 2.27.0