do not allow empty cover images
[fanfix.git] / src / be / nikiroo / fanfix / searchable / Fanfiction.java
index 6814e63648ba933d7c199fe115a8c64fabae747d..cbc927bcc2d981b028311be59fa2d1ba7c152542 100644 (file)
@@ -126,11 +126,11 @@ class Fanfiction extends BasicSearchable {
                                                                nr = nr.substring(0, nr.length() - 1);
                                                        }
                                                        nr = nr.trim();
-                                                       
-                                                       //TODO: fix toNumber/fromNumber
+
+                                                       // TODO: fix toNumber/fromNumber
                                                        nr = nr.replaceAll("\\.[0-9]*", "");
-                                                       
-                                                       subtag.setCount(toNumber(nr));
+
+                                                       subtag.setCount(StringUtils.toNumber(nr));
                                                }
                                        }
                                }
@@ -140,38 +140,13 @@ class Fanfiction extends BasicSearchable {
                tag.setComplete(true);
        }
 
-       /**
-        * @deprecated use {@link StringUtils} when updated
-        */
-       @Deprecated
-       private static long toNumber(String value) {
-               // TODO: use StringUtils instead after update
-               long count = 0l;
-               if (value != null) {
-                       try {
-                               if (value.toLowerCase().endsWith("m")) {
-                                       count = Long.parseLong(value.substring(0,
-                                                       value.length() - 1).trim());
-                                       count *= 1000000;
-                               } else if (value.toLowerCase().endsWith("k")) {
-                                       count = Long.parseLong(value.substring(0,
-                                                       value.length() - 1).trim());
-                                       count *= 1000;
-                               } else {
-                                       count = Long.parseLong(value);
-                               }
-                       } catch (NumberFormatException pe) {
-                       }
-               }
-
-               return count;
-       }
-
        @Override
-       public List<MetaData> search(String search) throws IOException {
+       public List<MetaData> search(String search, int page) throws IOException {
                String encoded = URLEncoder.encode(search.toLowerCase(), "utf-8");
-               return getStories(BASE_URL + "search/?ready=1&type=story&keywords="
-                               + encoded, null, null);
+               String url = BASE_URL + "search/?ready=1&type=story&keywords="
+                               + encoded + "&ppage=" + page;
+
+               return getStories(url, null, null);
        }
 
        @Override
@@ -190,7 +165,7 @@ class Fanfiction extends BasicSearchable {
                                        url += "&p=" + page;
                                }
                        }
-                       
+
                        Document doc = load(url, false);
 
                        // Update the pages number if needed
@@ -213,6 +188,25 @@ class Fanfiction extends BasicSearchable {
                return metas;
        }
 
+       @Override
+       public int searchPages(String search) throws IOException {
+               String encoded = URLEncoder.encode(search.toLowerCase(), "utf-8");
+               String url = BASE_URL + "search/?ready=1&type=story&keywords="
+                               + encoded;
+
+               return getPages(load(url, false));
+       }
+
+       @Override
+       public int searchPages(SearchableTag tag) throws IOException {
+               if (tag.isLeaf()) {
+                       String url = tag.getId();
+                       return getPages(load(url, false));
+               }
+
+               return 0;
+       }
+
        /**
         * Return the number of pages in this stories result listing.
         * 
@@ -220,11 +214,8 @@ class Fanfiction extends BasicSearchable {
         *            the document
         * 
         * @return the number of pages or -1 if unknown
-        * 
-        * @throws IOException
-        *             in case of I/O errors
         */
-       private int getPages(Document doc) throws IOException {
+       private int getPages(Document doc) {
                int pages = -1;
 
                if (doc != null) {
@@ -279,30 +270,37 @@ class Fanfiction extends BasicSearchable {
                        MetaData meta = new MetaData();
                        meta.setImageDocument(false);
                        meta.setSource(getType().getSourceName());
+                       meta.setPublisher(getType().getSourceName());
+                       meta.setType(getType().toString());
 
                        // Title, URL, Cover
                        Element stitle = story.getElementsByClass("stitle").first();
                        if (stitle != null) {
                                meta.setTitle(stitle.text());
                                meta.setUrl(stitle.absUrl("href"));
+                               meta.setUuid(meta.getUrl());
                                Element cover = stitle.getElementsByTag("img").first();
                                if (cover != null) {
                                        // note: see data-original if needed?
                                        String coverUrl = cover.absUrl("src");
 
                                        try {
-                                               InputStream in = Instance.getCache().open(
-                                                               new URL(coverUrl), getSupport(), true);
+                                               InputStream in = Instance.getInstance().getCache().open(new URL(coverUrl), getSupport(), true);
                                                try {
-                                                       meta.setCover(new Image(in));
+                                                       Image img = new Image(in);
+                                                       if (img.getSize() == 0) {
+                                                               img.close();
+                                                               throw new IOException(
+                                                                               "Empty image not accepted");
+                                                       }
+                                                       meta.setCover(img);
                                                } finally {
                                                        in.close();
                                                }
                                        } catch (Exception e) {
-                                               Instance.getTraceHandler()
-                                                               .error(new Exception(
-                                                                               "Cannot download cover for Fanfiction story in search mode",
-                                                                               e));
+                                               // Should not happen on Fanfiction.net
+                                               Instance.getInstance().getTraceHandler().error(new Exception(
+                                                               "Cannot download cover for Fanfiction story in search mode: " + meta.getTitle(), e));
                                        }
                                }
                        }
@@ -342,8 +340,7 @@ class Fanfiction extends BasicSearchable {
                                }
 
                                meta.setResume(getSupport().makeChapter(new URL(sourceUrl), 0,
-                                               Instance.getTrans().getString(StringId.DESCRIPTION),
-                                               resume));
+                                               Instance.getInstance().getTrans().getString(StringId.DESCRIPTION), resume));
                        }
 
                        // How are the tags ordered?