From: Niki Roo Date: Thu, 11 Apr 2019 05:09:59 +0000 (+0200) Subject: code cleanup X-Git-Url: http://git.nikiroo.be/?p=fanfix.git;a=commitdiff_plain;h=596ed3d65b2585c4358935d6fbb130f50e237787 code cleanup --- diff --git a/src/be/nikiroo/fanfix/reader/ui/GuiReaderBookInfo.java b/src/be/nikiroo/fanfix/reader/ui/GuiReaderBookInfo.java index 23d4c31..eb6dfbf 100644 --- a/src/be/nikiroo/fanfix/reader/ui/GuiReaderBookInfo.java +++ b/src/be/nikiroo/fanfix/reader/ui/GuiReaderBookInfo.java @@ -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"; diff --git a/src/be/nikiroo/fanfix/searchable/Fanfiction.java b/src/be/nikiroo/fanfix/searchable/Fanfiction.java index 362b543..bcc4759 100644 --- a/src/be/nikiroo/fanfix/searchable/Fanfiction.java +++ b/src/be/nikiroo/fanfix/searchable/Fanfiction.java @@ -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 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 tagList = new ArrayList(); - + // 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 tagList = new ArrayList(); 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); } diff --git a/src/be/nikiroo/fanfix/searchable/SearchableTag.java b/src/be/nikiroo/fanfix/searchable/SearchableTag.java index eebbe09..c12b3c6 100644 --- a/src/be/nikiroo/fanfix/searchable/SearchableTag.java +++ b/src/be/nikiroo/fanfix/searchable/SearchableTag.java @@ -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. - *

- * 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()) {