Version 1.2.4: fixes, new "Re-download" UI option
[fanfix.git] / src / be / nikiroo / fanfix / supported / Fanfiction.java
index 2cbe696052dc16ec61e3ba333ac50c8180486aaf..a73f7dee497142ee3af31212ca04b5c1f52c2211 100644 (file)
@@ -13,6 +13,7 @@ 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.StringUtils;
 
@@ -124,6 +125,8 @@ class Fanfiction extends BasicSupport {
        }
 
        private String getAuthor(InputStream in) {
+               String author = null;
+
                int i = 0;
                @SuppressWarnings("resource")
                Scanner scan = new Scanner(in, "UTF-8");
@@ -132,12 +135,13 @@ 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 fixAuthor(author);
        }
 
        private String getDate(InputStream in) {
@@ -196,7 +200,7 @@ class Fanfiction extends BasicSupport {
                                                                        + "/" + url.getPath() + "/" + line;
                                                }
 
-                                               return getImage(null, line);
+                                               return getImage(this, null, line);
                                        }
                                }
                        }
@@ -220,39 +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 {
+                                                       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;
+                                                               }
+
+                                                               public String getKey() {
+                                                                       return chapName;
+                                                               }
+                                                       });
+                                               } catch (MalformedURLException e) {
+                                                       Instance.syserr(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>() {
+                               public URL setValue(URL value) {
+                                       return null;
+                               }
+
+                               public URL getValue() {
+                                       return chapURL;
+                               }
+
+                               public String getKey() {
+                                       return chapName;
+                               }
+                       });
                }
 
                return urls;
@@ -283,7 +310,21 @@ 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()
+                                                               .getString(Config.CHAPTER).split(",")) {
+                                                       String chapterWord = Instance.getConfig()
+                                                                       .getStringX(Config.CHAPTER, lang);
+                                                       int posChap = line.indexOf(chapterWord + " ");
+                                                       if (posChap < pos) {
+                                                               chaptered = true;
+                                                               break;
+                                                       }
+                                               }
+
+                                               if (chaptered) {
+                                                       line = line.substring(pos);
+                                               }
                                        }
                                }