tests: fix NPE, add BasicSupportUtilities tests
[nikiroo-utils.git] / src / be / nikiroo / fanfix / supported / Fanfiction.java
index cbbc0851621d56f04288fe4539ed4019651c1fd8..0dcd7903e077d2bb6afa268247e04ea39a04722a 100644 (file)
@@ -5,6 +5,7 @@ import java.io.InputStream;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.text.SimpleDateFormat;
+import java.util.AbstractMap;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
@@ -12,6 +13,10 @@ import java.util.Map.Entry;
 import java.util.Scanner;
 
 import be.nikiroo.fanfix.Instance;
+import be.nikiroo.fanfix.bundles.Config;
+import be.nikiroo.fanfix.data.MetaData;
+import be.nikiroo.utils.Image;
+import be.nikiroo.utils.Progress;
 import be.nikiroo.utils.StringUtils;
 
 /**
@@ -21,19 +26,35 @@ import be.nikiroo.utils.StringUtils;
  * 
  * @author niki
  */
-class Fanfiction extends BasicSupport {
+class Fanfiction extends BasicSupport_Deprecated {
        @Override
        protected boolean isHtml() {
                return true;
        }
 
        @Override
-       public String getSourceName() {
-               return "Fanfiction.net";
+       protected MetaData getMeta(URL source, InputStream in) throws IOException {
+               MetaData meta = new MetaData();
+
+               meta.setTitle(getTitle(reset(in)));
+               meta.setAuthor(getAuthor(reset(in)));
+               meta.setDate(getDate(reset(in)));
+               meta.setTags(getTags(reset(in)));
+               meta.setSource(getType().getSourceName());
+               meta.setUrl(source.toString());
+               meta.setPublisher(getType().getSourceName());
+               meta.setUuid(source.toString());
+               meta.setLuid("");
+               meta.setLang("en"); // TODO!
+               meta.setSubject(getSubject(reset(in)));
+               meta.setType(getType().toString());
+               meta.setImageDocument(false);
+               meta.setCover(getCover(source, reset(in)));
+
+               return meta;
        }
 
-       @Override
-       protected String getSubject(URL source, InputStream in) {
+       private String getSubject(InputStream in) {
                String line = getLine(in, "id=pre_story_links", 0);
                if (line != null) {
                        int pos = line.lastIndexOf('"');
@@ -41,7 +62,7 @@ class Fanfiction extends BasicSupport {
                                line = line.substring(pos + 1);
                                pos = line.indexOf('<');
                                if (pos >= 0) {
-                                       return line.substring(0, pos);
+                                       return StringUtils.unhtml(line.substring(0, pos)).trim();
                                }
                        }
                }
@@ -49,10 +70,8 @@ class Fanfiction extends BasicSupport {
                return null;
        }
 
-       @Override
-       protected List<String> getTags(URL source, InputStream in)
-                       throws IOException {
-               List<String> tags = super.getTags(source, in);
+       private List<String> getTags(InputStream in) {
+               List<String> tags = new ArrayList<String>();
 
                String key = "title=\"Send Private Message\"";
                String line = getLine(in, key, 2);
@@ -71,7 +90,7 @@ class Fanfiction extends BasicSupport {
                                        }
 
                                        for (String tag : line.split("-")) {
-                                               tags.add(tag.trim());
+                                               tags.add(StringUtils.unhtml(tag).trim());
                                        }
                                }
                        }
@@ -80,8 +99,7 @@ class Fanfiction extends BasicSupport {
                return tags;
        }
 
-       @Override
-       protected String getTitle(URL source, InputStream in) {
+       private String getTitle(InputStream in) {
                int i = 0;
                @SuppressWarnings("resource")
                Scanner scan = new Scanner(in, "UTF-8");
@@ -95,16 +113,17 @@ class Fanfiction extends BasicSupport {
                                                line = line.substring("Follow/Fav".length()).trim();
                                        }
 
-                                       return line;
+                                       return StringUtils.unhtml(line).trim();
                                }
                        }
                }
 
-               return null;
+               return "";
        }
 
-       @Override
-       protected String getAuthor(URL source, InputStream in) {
+       private String getAuthor(InputStream in) {
+               String author = null;
+
                int i = 0;
                @SuppressWarnings("resource")
                Scanner scan = new Scanner(in, "UTF-8");
@@ -113,16 +132,16 @@ class Fanfiction extends BasicSupport {
                        String line = scan.next();
                        if (line.contains("xcontrast_txt")) {
                                if ((++i) == 3) {
-                                       return StringUtils.unhtml(line).trim();
+                                       author = StringUtils.unhtml(line).trim();
+                                       break;
                                }
                        }
                }
 
-               return null;
+               return bsHelper.fixAuthor(author);
        }
 
-       @Override
-       protected String getDate(URL source, InputStream in) {
+       private String getDate(InputStream in) {
                String key = "Published: <span data-xutime='";
                String line = getLine(in, key, 0);
                if (line != null) {
@@ -138,8 +157,10 @@ class Fanfiction extends BasicSupport {
                                                return sdf
                                                                .format(new Date(1000 * Long.parseLong(line)));
                                        } catch (NumberFormatException e) {
-                                               Instance.syserr(new IOException(
-                                                               "Cannot convert publication date: " + line, e));
+                                               Instance.getTraceHandler().error(
+                                                               new IOException(
+                                                                               "Cannot convert publication date: "
+                                                                                               + line, e));
                                        }
                                }
                        }
@@ -153,8 +174,7 @@ class Fanfiction extends BasicSupport {
                return getLine(in, "title=\"Send Private Message\"", 1);
        }
 
-       @Override
-       protected URL getCover(URL url, InputStream in) {
+       private Image getCover(URL url, InputStream in) {
                String key = "class='cimage";
                String line = getLine(in, key, 0);
                if (line != null) {
@@ -179,11 +199,7 @@ class Fanfiction extends BasicSupport {
                                                                        + "/" + url.getPath() + "/" + line;
                                                }
 
-                                               try {
-                                                       return new URL(line);
-                                               } catch (MalformedURLException e) {
-                                                       Instance.syserr(e);
-                                               }
+                                               return getImage(this, null, line);
                                        }
                                }
                        }
@@ -193,7 +209,8 @@ class Fanfiction extends BasicSupport {
        }
 
        @Override
-       protected List<Entry<String, URL>> getChapters(URL source, InputStream in) {
+       protected List<Entry<String, URL>> getChapters(URL source, InputStream in,
+                       Progress pg) {
                List<Entry<String, URL>> urls = new ArrayList<Entry<String, URL>>();
 
                String base = source.toString();
@@ -207,46 +224,62 @@ class Fanfiction extends BasicSupport {
                String line = getLine(in, "id=chap_select", 0);
                String key = "<option  value=";
                int i = 1;
-               for (pos = line.indexOf(key); pos >= 0; pos = line.indexOf(key, pos), i++) {
-                       pos = line.indexOf('>', pos);
-                       if (pos >= 0) {
-                               int endOfName = line.indexOf('<', pos);
-                               if (endOfName >= 0) {
-                                       String name = line.substring(pos + 1, endOfName);
-                                       String chapNum = i + ".";
-                                       if (name.startsWith(chapNum)) {
-                                               name = name.substring(chapNum.length(), name.length());
-                                       }
-
-                                       try {
-                                               final String chapName = name.trim();
-                                               final URL chapURL = new URL(base + i + suffix);
-                                               urls.add(new Entry<String, URL>() {
-                                                       public URL setValue(URL value) {
-                                                               return null;
-                                                       }
 
-                                                       public URL getValue() {
-                                                               return chapURL;
-                                                       }
+               if (line != null) {
+                       for (pos = line.indexOf(key); pos >= 0; pos = line
+                                       .indexOf(key, pos), i++) {
+                               pos = line.indexOf('>', pos);
+                               if (pos >= 0) {
+                                       int endOfName = line.indexOf('<', pos);
+                                       if (endOfName >= 0) {
+                                               String name = line.substring(pos + 1, endOfName);
+                                               String chapNum = i + ".";
+                                               if (name.startsWith(chapNum)) {
+                                                       name = name.substring(chapNum.length(),
+                                                                       name.length());
+                                               }
 
-                                                       public String getKey() {
-                                                               return chapName;
-                                                       }
-                                               });
-                                       } catch (MalformedURLException e) {
-                                               Instance.syserr(new IOException("Cannot parse chapter "
-                                                               + i + " url: " + (base + i + suffix), e));
+                                               try {
+                                                       urls.add(new AbstractMap.SimpleEntry<String, URL>(
+                                                                       name.trim(), new URL(base + i + suffix)));
+                                               } catch (MalformedURLException e) {
+                                                       Instance.getTraceHandler()
+                                                                       .error(new IOException(
+                                                                                       "Cannot parse chapter " + i
+                                                                                                       + " url: "
+                                                                                                       + (base + i + suffix), e));
+                                               }
                                        }
                                }
                        }
+               } else {
+                       // only one chapter:
+                       final String chapName = getTitle(reset(in));
+                       final URL chapURL = source;
+                       urls.add(new Entry<String, URL>() {
+                               @Override
+                               public URL setValue(URL value) {
+                                       return null;
+                               }
+
+                               @Override
+                               public URL getValue() {
+                                       return chapURL;
+                               }
+
+                               @Override
+                               public String getKey() {
+                                       return chapName;
+                               }
+                       });
                }
 
                return urls;
        }
 
        @Override
-       protected String getChapterContent(URL source, InputStream in, int number) {
+       protected String getChapterContent(URL source, InputStream in, int number,
+                       Progress pg) {
                StringBuilder builder = new StringBuilder();
                String startAt = "class='storytext ";
                String endAt1 = "function review_init";
@@ -270,11 +303,26 @@ class Fanfiction extends BasicSupport {
                                if (builder.length() == 0) {
                                        int pos = line.indexOf("<hr");
                                        if (pos >= 0) {
-                                               line = line.substring(pos);
+                                               boolean chaptered = false;
+                                               for (String lang : Instance.getConfig().getList(
+                                                               Config.CONF_CHAPTER)) {
+                                                       String chapterWord = Instance.getConfig()
+                                                                       .getStringX(Config.CONF_CHAPTER, lang);
+                                                       int posChap = line.indexOf(chapterWord + " ");
+                                                       if (posChap < pos) {
+                                                               chaptered = true;
+                                                               break;
+                                                       }
+                                               }
+
+                                               if (chaptered) {
+                                                       line = line.substring(pos);
+                                               }
                                        }
                                }
 
                                builder.append(line);
+                               builder.append(' ');
                        }
                }