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()
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;
return 1;
}
- CliReader.list(type);
+ new CliReader().start(type);
return 0;
}
*/
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) {
/**
- * 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>
--- /dev/null
+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);
+}
-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;
/**
*
* @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();
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(),
* 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());
}
}
- /**
- * 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);