search: fixes, CLI now working
authorNiki Roo <niki@nikiroo.be>
Fri, 12 Apr 2019 07:26:39 +0000 (09:26 +0200)
committerNiki Roo <niki@nikiroo.be>
Fri, 12 Apr 2019 07:26:39 +0000 (09:26 +0200)
src/be/nikiroo/fanfix/Main.java
src/be/nikiroo/fanfix/reader/cli/CliReader.java
src/be/nikiroo/fanfix/searchable/Fanfiction.java

index bbb335e97afb9395b8cabb7196e5f1845ca0fcf7..d5a5debe253fb4a6d570c9791d1e5fbdda57d917 100644 (file)
@@ -466,15 +466,15 @@ public class Main {
                                page = 1;
 
                                try {
-                                       item = Integer.parseInt(tags.get(tags.size()-1));
+                                       page = Integer.parseInt(tags.get(tags.size()-1));
                                        tags.remove(tags.size() - 1);
                                        
                                        try {
                                                int tmp = Integer.parseInt(tags.get(tags.size()-1));
                                                tags.remove(tags.size() - 1);
                                                
-                                               page = item;
-                                               item = tmp;
+                                               item = page;
+                                               page = tmp;
                                        } catch (Exception e) {
                                        }
                                } catch (Exception e) {
index 7a4abfb28c91490f92dd50140c10eb9e1404e134..22d11cbdfbbb87f9f41b28844d8317087070af1b 100644 (file)
@@ -99,28 +99,29 @@ class CliReader extends BasicReader {
                                        + author);
                }
        }
-       
+
        @Override
-       public void search(SupportType searchOn, String keywords, int page, int item) throws IOException {
-               
+       public void search(SupportType searchOn, String keywords, int page, int item)
+                       throws IOException {
+
        }
-       
+
        @Override
-       public void searchTag(SupportType searchOn, int page, int item, String... tags) throws IOException {
+       public void searchTag(SupportType searchOn, int page, int item,
+                       String... tags) throws IOException {
                BasicSearchable search = BasicSearchable.getSearchable(searchOn);
                List<SearchableTag> stags = search.getTags();
-               
-               
+
                SearchableTag stag = null;
                for (String tag : tags) {
                        stag = null;
-                       for (int i = 0 ; i < stags.size() ; i++) {
+                       for (int i = 0; i < stags.size(); i++) {
                                if (stags.get(i).getName().equalsIgnoreCase(tag)) {
                                        stag = stags.get(i);
                                        break;
                                }
                        }
-                       
+
                        if (stag != null) {
                                search.fillTag(stag);
                                stags = stag.getChildren();
@@ -133,6 +134,7 @@ class CliReader extends BasicReader {
                if (stag != null) {
                        if (page <= 0) {
                                if (stag.isLeaf()) {
+                                       search.search(stag, 1);
                                        System.out.println(stag.getPages());
                                } else {
                                        System.out.println(stag.getCount());
@@ -141,7 +143,7 @@ class CliReader extends BasicReader {
                                List<MetaData> metas = null;
                                List<SearchableTag> subtags = null;
                                int count;
-                               
+
                                if (stag.isLeaf()) {
                                        metas = search.search(stag, page);
                                        count = metas.size();
@@ -149,12 +151,14 @@ class CliReader extends BasicReader {
                                        subtags = stag.getChildren();
                                        count = subtags.size();
                                }
-                               
+
                                if (item > 0) {
                                        if (item <= count) {
                                                if (metas != null) {
                                                        MetaData meta = metas.get(item - 1);
-                                                       System.out.println(item + ": " + meta.getTitle());
+                                                       System.out.println(page + "/" + item + ": "
+                                                                       + meta.getTitle());
+                                                       System.out.println();
                                                        System.out.println(meta.getUrl());
                                                        System.out.println();
                                                        System.out.println("Tags: " + meta.getTags());
@@ -165,26 +169,58 @@ class CliReader extends BasicReader {
                                                        }
                                                } else {
                                                        SearchableTag subtag = subtags.get(item - 1);
-                                                       // TODO: display fixed info, not debug
-                                                       System.out.println(subtag);
+
+                                                       String sp = "";
+                                                       if (subtag.getParent() != null) {
+                                                               List<String> parents = new ArrayList<String>();
+                                                               for (SearchableTag parent = subtag.getParent(); parent != null; parent = parent
+                                                                               .getParent()) {
+                                                                       parents.add(parent.getName());
+                                                               }
+                                                               for (String parent : parents) {
+                                                                       if (!sp.isEmpty()) {
+                                                                               sp += " / ";
+                                                                       }
+                                                                       sp += parent;
+                                                               }
+                                                       }
+
+                                                       // TODO: i18n
+                                                       if (sp.isEmpty()) {
+                                                               System.out.println(String.format(
+                                                                               "%d/%d: %s, %d %s", page, item,
+                                                                               subtag.getName(), subtag.getCount(),
+                                                                               "stories"));
+                                                       } else {
+                                                               System.out.println(String.format(
+                                                                               "%d/%d: %s (%s), %d %s", page, item,
+                                                                               subtag.getName(), sp,
+                                                                               subtag.getCount(), "stories"));
+                                                       }
                                                }
                                        } else {
-                                               System.out.println("Invalid item: only " + count + " items found");
+                                               System.out.println("Invalid item: only " + count
+                                                               + " items found");
                                        }
                                } else {
                                        if (metas != null) {
                                                int i = 0;
                                                for (MetaData meta : metas) {
-                                                       System.out.println((i + 1) + ": " + meta.getTitle());
+                                                       System.out
+                                                                       .println((i + 1) + ": " + meta.getTitle());
                                                        i++;
                                                }
                                        } else {
+                                               int i = 1;
                                                for (SearchableTag subtag : subtags) {
+                                                       String total = "";
                                                        if (subtag.getCount() > 0) {
-                                                               System.out.println(subtag.getName() + " (" + subtag.getCount() + ")");
-                                                       } else {
-                                                               System.out.println(subtag.getName());
+                                                               // TODO: use StringUtils fromNumber
+                                                               total = " (" + subtag.getCount() + ")";
                                                        }
+                                                       System.out.println(i + ": " + subtag.getName()
+                                                                       + total);
+                                                       i++;
                                                }
                                        }
                                }
index 71732fc5d5d16ba178e775d6fff87bfd2eff1b4c..6814e63648ba933d7c199fe115a8c64fabae747d 100644 (file)
@@ -77,7 +77,7 @@ class Fanfiction extends BasicSearchable {
                if (storiesName != null) {
                        SearchableTag tag = new SearchableTag(null, storiesName, false);
                        for (String id : stories.keySet()) {
-                               tag.add(new SearchableTag(id, stories.get(id), true, false));
+                               tag.add(new SearchableTag(id, stories.get(id), false, false));
                        }
                        tags.add(tag);
                }
@@ -126,6 +126,10 @@ class Fanfiction extends BasicSearchable {
                                                                nr = nr.substring(0, nr.length() - 1);
                                                        }
                                                        nr = nr.trim();
+                                                       
+                                                       //TODO: fix toNumber/fromNumber
+                                                       nr = nr.replaceAll("\\.[0-9]*", "");
+                                                       
                                                        subtag.setCount(toNumber(nr));
                                                }
                                        }
@@ -186,11 +190,11 @@ class Fanfiction extends BasicSearchable {
                                        url += "&p=" + page;
                                }
                        }
-
+                       
                        Document doc = load(url, false);
 
                        // Update the pages number if needed
-                       if (tag.getPages() < 0) {
+                       if (tag.getPages() < 0 && tag.isLeaf()) {
                                tag.setPages(getPages(doc));
                        }