e621: fix order for pools
[nikiroo-utils.git] / src / be / nikiroo / fanfix / supported / E621.java
index 316ac2c2bcfbad54411467af2f34713e37772d8d..de754c8b843da881bf1e34419a7491997ac8d201 100644 (file)
@@ -3,6 +3,7 @@ package be.nikiroo.fanfix.supported;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.UnsupportedEncodingException;
+import java.net.MalformedURLException;
 import java.net.URL;
 import java.net.URLDecoder;
 import java.util.AbstractMap;
@@ -105,7 +106,11 @@ class E621 extends BasicSupport {
                } else if (isSearchOrSet(getSource())) {
                        String baseUrl = "https://e621.net/posts/?page=";
                        String search = "&tags=" + getTagsFromUrl(getSource());
-                       return getChapters(getSource(), pg, baseUrl, search);
+                       // sets are sorted in reverse order on the website
+                       List<Entry<String, URL>> urls = getChapters(getSource(), pg,
+                                       baseUrl, search);
+                       Collections.reverse(urls);
+                       return urls;
                }
 
                return new LinkedList<Entry<String, URL>>();
@@ -122,7 +127,7 @@ class E621 extends BasicSupport {
                for (int i = 1; true; i++) {
                        URL url = new URL(baseUrl + i + parameters);
                        try {
-                               InputStream pageI = Instance.getCache().open(url, this, false);
+                               InputStream pageI = Instance.getInstance().getCache().open(url, this, false);
                                try {
                                        if (IOUtils.readSmallStream(pageI).contains("Nobody here but us chickens!")) {
                                                break;
@@ -136,8 +141,6 @@ class E621 extends BasicSupport {
                        }
                }
 
-               // They are sorted in reverse order on the website
-               Collections.reverse(urls);
                return urls;
        }
 
@@ -158,7 +161,7 @@ class E621 extends BasicSupport {
        protected URL getCanonicalUrl(URL source) {
                if (isSetOriginalUrl(source)) {
                        try {
-                               Document doc = DataUtil.load(Instance.getCache().open(source, this, false), "UTF-8", source.toString());
+                               Document doc = DataUtil.load(Instance.getInstance().getCache().open(source, this, false), "UTF-8", source.toString());
                                for (Element shortname : doc.getElementsByClass("set-shortname")) {
                                        for (Element el : shortname.getElementsByTag("a")) {
                                                if (!el.attr("href").isEmpty())
@@ -166,7 +169,14 @@ class E621 extends BasicSupport {
                                        }
                                }
                        } catch (IOException e) {
-                               Instance.getTraceHandler().error(e);
+                               Instance.getInstance().getTraceHandler().error(e);
+                       }
+               }
+
+               if (isPool(source)) {
+                       try {
+                               return new URL(source.toString().replace("/pool/show/", "/pools/"));
+                       } catch (MalformedURLException e) {
                        }
                }
 
@@ -274,8 +284,12 @@ class E621 extends BasicSupport {
                Image image = null;
                List<Entry<String, URL>> chapters = getChapters(null);
                if (!chapters.isEmpty()) {
-                       URL url = chapters.get(0).getValue();
-                       image = bsImages.getImage(this, url);
+                       URL chap1Url = chapters.get(0).getValue();
+                       String imgsChap1 = getChapterContent(chap1Url, 1, null);
+                       if (!imgsChap1.isEmpty()) {
+                               imgsChap1 = imgsChap1.split("]")[0].substring(1).trim();
+                               image = bsImages.getImage(this, new URL(imgsChap1));
+                       }
                }
 
                return image;
@@ -287,7 +301,7 @@ class E621 extends BasicSupport {
        }
 
        private boolean isPool(URL url) {
-               return url.getPath().startsWith("/pools/");
+               return url.getPath().startsWith("/pools/") || url.getPath().startsWith("/pool/show/");
        }
 
        // set will be renamed into search by canonical url