Fix MangaFox tome sorting
authorNiki Roo <niki@nikiroo.be>
Sun, 25 Mar 2018 11:35:39 +0000 (13:35 +0200)
committerNiki Roo <niki@nikiroo.be>
Sun, 25 Mar 2018 11:35:39 +0000 (13:35 +0200)
src/be/nikiroo/fanfix/supported/BasicSupportHelper.java
src/be/nikiroo/fanfix/supported/Html.java
src/be/nikiroo/fanfix/supported/MangaFox.java

index 0c9e199fa2695af13d15493e1a121aa37a04f270..2c3c4fe68187ff3588574e3a874cd4a8f126f7bd 100644 (file)
@@ -80,8 +80,7 @@ class BasicSupportHelper {
                        }
                        InputStream in = null;
                        try {
-                               in = Instance.getCache().open(url,
-                                               BasicSupport.getSupport(url), true);
+                               in = Instance.getCache().open(url, support, true);
                                return new Image(in);
                        } catch (IOException e) {
                        } finally {
index f66032bfce20032f784b6df0ab4b51ce7883fce7..1e960bf84978bf1125aececd6d59a47eff38f9f9 100644 (file)
@@ -3,7 +3,6 @@ package be.nikiroo.fanfix.supported;
 import java.io.File;
 import java.io.IOException;
 import java.net.MalformedURLException;
-import java.net.URISyntaxException;
 import java.net.URL;
 
 import be.nikiroo.fanfix.Instance;
@@ -22,25 +21,38 @@ class Html extends InfoText {
 
        @Override
        protected boolean supports(URL url) {
-               try {
-                       File file = new File(url.toURI());
-                       if (file.getName().equals("index.html")) {
-                               file = file.getParentFile();
-                       }
-
-                       file = new File(file, file.getName());
-
-                       return super.supports(file.toURI().toURL());
-               } catch (URISyntaxException e) {
-               } catch (MalformedURLException e) {
-               }
-
-               return false;
+               File txt = getTxt(url);
+               return txt != null && txt.exists();
        }
 
        @Override
        public URL getCanonicalUrl(URL source) {
+               File txt = getTxt(source);
+               if (txt != null) {
+                       try {
+                               source = txt.toURI().toURL();
+                       } catch (MalformedURLException e) {
+                               Instance.getTraceHandler().error(
+                                               new IOException("Cannot convert the right URL for "
+                                                               + source, e));
+                       }
+               } else {
+                       Instance.getTraceHandler().error(
+                                       new IOException("Cannot find the right URL for " + source));
+               }
+
+               return source;
+       }
 
+       /**
+        * Return the associated TXT source file if it can be found.
+        * 
+        * @param source
+        *            the source URL
+        * 
+        * @return the supported source text file or NULL
+        */
+       private static File getTxt(URL source) {
                try {
                        File fakeFile = new File(source.toURI());
                        if (fakeFile.getName().equals("index.html")) { // "story/index.html"
@@ -51,13 +63,12 @@ class Html extends InfoText {
                                fakeFile = new File(fakeFile, fakeFile.getName() + ".txt"); // "story/story.txt"
                        }
 
-                       return fakeFile.toURI().toURL();
+                       if (fakeFile.getName().endsWith(".txt")) {
+                               return fakeFile;
+                       }
                } catch (Exception e) {
-                       Instance.getTraceHandler().error(
-                                       new IOException("Cannot find the right URL for " + source,
-                                                       e));
                }
 
-               return source;
+               return null;
        }
 }
index b6d3fe133ce1a70ace47f92ac4bab21805172ac6..f1226076315e844d6b55718fe26394bc556c1ae2 100644 (file)
@@ -9,6 +9,8 @@ import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 import java.util.Map.Entry;
+import java.util.SortedMap;
+import java.util.TreeMap;
 
 import org.jsoup.helper.DataUtil;
 import org.jsoup.nodes.Element;
@@ -140,6 +142,9 @@ class MangaFox extends BasicSupport {
        protected List<Entry<String, URL>> getChapters(Progress pg) {
                List<Entry<String, URL>> urls = new ArrayList<Entry<String, URL>>();
 
+               String prefix = null; // each chapter starts with this prefix, then a
+                                                               // chapter number (including "x.5"), then name
+
                Element doc = getSourceNode();
                for (Element li : doc.getElementsByTag("li")) {
                        Element el = li.getElementsByTag("h4").first();
@@ -160,6 +165,32 @@ class MangaFox extends BasicSupport {
                                                        url += "/";
                                                }
 
+                                               if (prefix == null || !prefix.isEmpty()) {
+                                                       StringBuilder possiblePrefix = new StringBuilder(
+                                                                       StringUtils.unhtml(a.text()).trim());
+                                                       while (possiblePrefix.length() > 0) {
+                                                               char car = possiblePrefix.charAt(possiblePrefix
+                                                                               .length() - 1);
+                                                               boolean punctuation = (car == '.' || car == ' ');
+                                                               boolean digit = (car >= '0' && car <= '9');
+                                                               if (!punctuation && !digit) {
+                                                                       break;
+                                                               }
+
+                                                               possiblePrefix.setLength(possiblePrefix
+                                                                               .length() - 1);
+                                                       }
+
+                                                       if (prefix == null) {
+                                                               prefix = possiblePrefix.toString();
+                                                       }
+
+                                                       if (!prefix.equalsIgnoreCase(possiblePrefix
+                                                                       .toString())) {
+                                                               prefix = ""; // prefix not ok
+                                                       }
+                                               }
+
                                                urls.add(new AbstractMap.SimpleEntry<String, URL>(
                                                                title, new URL(url)));
                                        } catch (Exception e) {
@@ -169,8 +200,44 @@ class MangaFox extends BasicSupport {
                        }
                }
 
-               // the chapters are in reversed order
-               Collections.reverse(urls);
+               if (prefix != null && !prefix.isEmpty()) {
+                       try {
+                               // We found a prefix, so everything should be sortable
+                               SortedMap<Double, Entry<String, URL>> map = new TreeMap<Double, Entry<String, URL>>();
+                               for (Entry<String, URL> entry : urls) {
+                                       String num = entry.getKey().substring(prefix.length() + 1)
+                                                       .trim();
+                                       String name = "";
+                                       int pos = num.indexOf(' ');
+                                       if (pos >= 0) {
+                                               name = num.substring(pos).trim();
+                                               num = num.substring(0, pos).trim();
+                                       }
+
+                                       if (!name.isEmpty()) {
+                                               name = "Tome " + num + ": " + name;
+                                       } else {
+                                               name = "Tome " + num;
+                                       }
+
+                                       double key = Double.parseDouble(num);
+
+                                       map.put(key, new AbstractMap.SimpleEntry<String, URL>(name,
+                                                       entry.getValue()));
+                               }
+                               urls = new ArrayList<Entry<String, URL>>(map.values());
+                       } catch (NumberFormatException e) {
+                               Instance.getTraceHandler()
+                                               .error(new IOException(
+                                                               "Cannot find a tome number, revert to default sorting",
+                                                               e));
+                               // by default, the chapters are in reversed order
+                               Collections.reverse(urls);
+                       }
+               } else {
+                       // by default, the chapters are in reversed order
+                       Collections.reverse(urls);
+               }
 
                return urls;
        }
@@ -231,7 +298,7 @@ class MangaFox extends BasicSupport {
        }
 
        /**
-        * Refresh the {@link URL} by calling {@link MangaFoxNew#openEx(String)}.
+        * Refresh the {@link URL} by calling {@link MangaFox#openEx(String)}.
         * 
         * @param url
         *            the URL to refresh