code cleanup
authorNiki Roo <niki@nikiroo.be>
Thu, 11 Apr 2019 05:09:59 +0000 (07:09 +0200)
committerNiki Roo <niki@nikiroo.be>
Thu, 11 Apr 2019 05:09:59 +0000 (07:09 +0200)
src/be/nikiroo/fanfix/reader/ui/GuiReaderBookInfo.java
src/be/nikiroo/fanfix/searchable/Fanfiction.java
src/be/nikiroo/fanfix/searchable/SearchableTag.java

index 23d4c3160169e0244a1070720d376fc0aba06ab6..eb6dfbf36294a1f8711b173b394d25187222ba8a 100644 (file)
@@ -5,6 +5,7 @@ import be.nikiroo.fanfix.data.MetaData;
 import be.nikiroo.fanfix.data.Story;
 import be.nikiroo.fanfix.library.BasicLibrary;
 import be.nikiroo.utils.Image;
+import be.nikiroo.utils.StringUtils;
 
 /**
  * Some meta information related to a "book" (which can either be a
@@ -220,8 +221,12 @@ public class GuiReaderBookInfo {
         *            the number to parse
         * 
         * @return the displayable version of the number
+        * 
+        * @deprecated use {@link StringUtils} after update instead
         */
+       @Deprecated
        static private String formatNumber(long number) {
+               // TODO: replace with StringUtils after update
                String displayNumber;
                if (number >= 4000) {
                        displayNumber = "" + (number / 1000) + "k";
index 362b5433d7d3fc81e163a03a08273e9f2319f03f..bcc4759d1390f4f765963136eeb23b890fd07559 100644 (file)
@@ -20,6 +20,7 @@ import be.nikiroo.fanfix.bundles.StringId;
 import be.nikiroo.fanfix.data.MetaData;
 import be.nikiroo.fanfix.supported.SupportType;
 import be.nikiroo.utils.Image;
+import be.nikiroo.utils.StringUtils;
 
 /**
  * A {@link BasicSearchable} for Fanfiction.NET.
@@ -125,24 +126,7 @@ class Fanfiction extends BasicSearchable {
                                                                nr = nr.substring(0, nr.length() - 1);
                                                        }
                                                        nr = nr.trim();
-
-                                                       long count = 0;
-                                                       try {
-                                                               if (nr.toLowerCase().endsWith("m")) {
-                                                                       count = Long.parseLong(nr.substring(0,
-                                                                                       nr.length() - 1).trim());
-                                                                       count *= 1000000;
-                                                               } else if (nr.toLowerCase().endsWith("k")) {
-                                                                       count = Long.parseLong(nr.substring(0,
-                                                                                       nr.length() - 1).trim());
-                                                                       count *= 1000;
-                                                               } else {
-                                                                       count = Long.parseLong(nr);
-                                                               }
-                                                       } catch (NumberFormatException pe) {
-                                                       }
-
-                                                       subtag.setCount(count);
+                                                       subtag.setCount(toNumber(nr));
                                                }
                                        }
                                }
@@ -152,12 +136,38 @@ 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 {
                String encoded = URLEncoder.encode(search.toLowerCase(), "utf-8");
-               return getStories(
-                               "http://fanfiction.net/search/?ready=1&type=story&keywords="
-                                               + encoded, null, null);
+               return getStories(BASE_URL + "search/?ready=1&type=story&keywords="
+                               + encoded, null, null);
        }
 
        @Override
@@ -266,9 +276,7 @@ class Fanfiction extends BasicSearchable {
                        meta.setImageDocument(false);
                        meta.setSource(getType().getSourceName());
 
-                       String subject = mainSubject == null ? "" : mainSubject;
-                       List<String> tagList = new ArrayList<String>();
-
+                       // Title, URL, Cover
                        Element stitle = story.getElementsByClass("stitle").first();
                        if (stitle != null) {
                                meta.setTitle(stitle.text());
@@ -295,13 +303,15 @@ class Fanfiction extends BasicSearchable {
                                }
                        }
 
+                       // Author
                        Elements as = story.getElementsByTag("a");
                        if (as.size() > 1) {
                                meta.setAuthor(as.get(1).text());
                        }
 
+                       // Tags (concatenated text), published date, updated date, Resume
                        String tags = "";
-
+                       List<String> tagList = new ArrayList<String>();
                        Elements divs = story.getElementsByTag("div");
                        if (divs.size() > 1 && divs.get(1).childNodeSize() > 0) {
                                String resume = divs.get(1).text();
@@ -336,25 +346,24 @@ class Fanfiction extends BasicSearchable {
                        // We have "Rated: xx", then the language, then all other tags
                        // If the subject(s) is/are present, they are before "Rated: xx"
 
-                       // /////////////
+                       // ////////////
                        // Examples: //
-                       // /////////////
+                       // ////////////
 
                        // Search (Luna) Tags: [Harry Potter, Rated: T, English, Chapters:
                        // 1, Words: 270, Reviews: 2, Published: 2/19/2013, Luna L.]
 
                        // Normal (MLP) Tags: [Rated: T, Spanish, Drama/Suspense, Chapters:
                        // 2, Words: 8,686, Reviews: 1, Favs: 1, Follows: 1, Updated: 4/7,
-                       // Published:
-                       // 4/2]
+                       // Published: 4/2]
 
                        // Crossover (MLP/Who) Tags: [Rated: K+, English, Adventure/Romance,
                        // Chapters: 8, Words: 7,788, Reviews: 2, Favs: 2, Follows: 1,
-                       // Published:
-                       // 9/1/2016]
+                       // Published: 9/1/2016]
 
                        boolean rated = false;
                        boolean isLang = false;
+                       String subject = mainSubject == null ? "" : mainSubject;
                        String[] tab = tags.split("  *-  *");
                        for (int i = 0; i < tab.length; i++) {
                                String tag = tab[i];
@@ -384,6 +393,7 @@ class Fanfiction extends BasicSearchable {
                                                        tagList.add(tag);
                                                }
                                        } else {
+                                               // Normal tags are "/"-separated
                                                for (String t : tag.split("/")) {
                                                        tagList.add(t);
                                                }
index eebbe09849520b9b681639132e24d7a946425797..c12b3c68eeeb5b0d3c2135b521bf5bbd354dbfa3 100644 (file)
@@ -135,31 +135,6 @@ public class SearchableTag {
                return count;
        }
 
-       /**
-        * The number of items that can be found with this tag if it is searched,
-        * displayable format.
-        * <p>
-        * Will report the number of subtags by default.
-        * 
-        * @return the number of items
-        */
-       public String getCountDisplay() {
-               long count = this.count;
-               if (count <= 0) {
-                       count = children.size();
-               }
-
-               if (count > 999999) {
-                       return count / 1000000 + "M";
-               }
-
-               if (count > 2000) {
-                       return count / 1000 + "k";
-               }
-
-               return Long.toString(count);
-       }
-
        /**
         * The number of items that can be found with this tag if it is searched.
         * 
@@ -263,7 +238,7 @@ public class SearchableTag {
                }
 
                if (getCount() > 0) {
-                       rep += " (" + getCountDisplay() + ")";
+                       rep += " (" + getCount() + ")";
                }
 
                if (!children.isEmpty()) {