fix cache, MangaLEL +search
[nikiroo-utils.git] / src / be / nikiroo / fanfix / supported / MangaFox.java
index b6d3fe133ce1a70ace47f92ac4bab21805172ac6..dae2d314f900d79a70c5d6237667d007a9a616dd 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;
@@ -26,11 +28,6 @@ class MangaFox extends BasicSupport {
                return true;
        }
 
-       @Override
-       public String getSourceName() {
-               return "MangaFox.me";
-       }
-
        @Override
        protected MetaData getMeta() throws IOException {
                MetaData meta = new MetaData();
@@ -61,9 +58,9 @@ class MangaFox extends BasicSupport {
                        meta.setDate(StringUtils.unhtml(table.get(0).text()).trim());
                        meta.setTags(explode(table.get(3).text()));
                }
-               meta.setSource(getSourceName());
+               meta.setSource(getType().getSourceName());
                meta.setUrl(getSource().toString());
-               meta.setPublisher(getSourceName());
+               meta.setPublisher(getType().getSourceName());
                meta.setUuid(getSource().toString());
                meta.setLuid("");
                meta.setLang("en");
@@ -104,7 +101,7 @@ class MangaFox extends BasicSupport {
                Element doc = getSourceNode();
                Element title = doc.getElementsByClass("summary").first();
                if (title != null) {
-                       StringUtils.unhtml(title.text()).trim();
+                       return StringUtils.unhtml(title.text()).trim();
                }
 
                return null;
@@ -140,6 +137,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 +160,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 +195,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;
        }
@@ -194,8 +256,13 @@ class MangaFox extends BasicSupport {
                        // note: when used, the base URL can be an ad-page
                        imageIn = openEx(url + "1.html");
                        imageDoc = DataUtil.load(imageIn, "UTF-8", url + "1.html");
+               } catch (IOException e) {
+                       Instance.getTraceHandler().error(
+                                       new IOException("Cannot get image " + 1 + " of manga", e));
                } finally {
-                       imageIn.close();
+                       if (imageIn != null) {
+                               imageIn.close();
+                       }
                }
                Element select = imageDoc.getElementsByClass("m").first();
                Elements options = select.getElementsByTag("option");
@@ -205,33 +272,40 @@ class MangaFox extends BasicSupport {
 
                // 2. list them
                for (int i = 1; i <= size; i++) {
-                       if (i > 1) { // because fist one was opened for size
+                       if (i > 1) { // because first one was opened for size
                                try {
                                        imageIn = openEx(url + i + ".html");
                                        imageDoc = DataUtil.load(imageIn, "UTF-8", url + i
                                                        + ".html");
+
+                                       String linkImage = imageDoc.getElementById("image").absUrl(
+                                                       "src");
+                                       if (linkImage != null) {
+                                               builder.append("[");
+                                               // to help with the retry and the originalUrl, part 1
+                                               builder.append(withoutQuery(linkImage));
+                                               builder.append("]<br/>");
+                                       }
+
+                                       // to help with the retry and the originalUrl, part 2
+                                       refresh(linkImage);
+                               } catch (IOException e) {
+                                       Instance.getTraceHandler().error(
+                                                       new IOException("Cannot get image " + i
+                                                                       + " of manga", e));
                                } finally {
-                                       imageIn.close();
+                                       if (imageIn != null) {
+                                               imageIn.close();
+                                       }
                                }
                        }
-
-                       String linkImage = imageDoc.getElementById("image").absUrl("src");
-                       if (linkImage != null) {
-                               builder.append("[");
-                               // to help with the retry and the originalUrl, part 1
-                               builder.append(withoutQuery(linkImage));
-                               builder.append("]<br/>");
-                       }
-
-                       // to help with the retry and the originalUrl, part 2
-                       refresh(linkImage);
                }
 
                return builder.toString();
        }
 
        /**
-        * 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
@@ -262,8 +336,8 @@ class MangaFox extends BasicSupport {
         */
        private InputStream openEx(String url) throws IOException {
                try {
-                       return Instance.getCache().open(new URL(url), this, true,
-                                       withoutQuery(url));
+                       return Instance.getCache().open(new URL(url), withoutQuery(url),
+                                       this, true);
                } catch (Exception e) {
                        // second chance
                        try {
@@ -271,8 +345,8 @@ class MangaFox extends BasicSupport {
                        } catch (InterruptedException ee) {
                        }
 
-                       return Instance.getCache().open(new URL(url), this, true,
-                                       withoutQuery(url));
+                       return Instance.getCache().open(new URL(url), withoutQuery(url),
+                                       this, true);
                }
        }