search should now be OK (CLI, Fanfiction only)
authorNiki Roo <niki@nikiroo.be>
Sat, 13 Apr 2019 12:09:22 +0000 (14:09 +0200)
committerNiki Roo <niki@nikiroo.be>
Sat, 13 Apr 2019 12:09:22 +0000 (14:09 +0200)
src/be/nikiroo/fanfix/Main.java
src/be/nikiroo/fanfix/reader/cli/CliReader.java
src/be/nikiroo/fanfix/searchable/BasicSearchable.java
src/be/nikiroo/fanfix/searchable/Fanfiction.java

index 2437cae567310700509c7c9a08a0a59212c7b1ec..876fe55c853d1a5359be41315b85c48e8ca1c201 100644 (file)
@@ -491,20 +491,22 @@ public class Main {
                                        break;
                                }
 
-                               if (searchOn == null || search == null) {
+                               if (searchOn == null) {
                                        // TODO: do on reader!!!
                                        for (SupportType type : SupportType.values()) {
                                                if (BasicSearchable.getSearchable(type) != null) {
                                                        System.out.println(type);
                                                }
                                        }
-                               } else {
+                               } else if (search != null) {
                                        try {
                                                BasicReader.getReader().search(searchOn, search, page,
                                                                item, true);
                                        } catch (IOException e1) {
                                                Instance.getTraceHandler().error(e1);
                                        }
+                               } else {
+                                       exitCode = 255;
                                }
 
                                break;
index 701ea481e40bf3ae6e2dba8a2d46d69f01b3a35e..d8d3a6c1e70b9a5b317d4c62e9f6bcf89783fb16 100644 (file)
@@ -104,7 +104,27 @@ class CliReader extends BasicReader {
        @Override
        public void search(SupportType searchOn, String keywords, int page,
                        int item, boolean sync) throws IOException {
-               // TODO
+               BasicSearchable search = BasicSearchable.getSearchable(searchOn);
+
+               if (page == 0) {
+                       System.out.println(search.searchPages(keywords));
+               } else {
+                       List<MetaData> metas = search.search(keywords, page);
+
+                       if (item == 0) {
+                               System.out.println("Page " + page + " of stories for: "
+                                               + keywords);
+                               displayStories(metas);
+                       } else {
+                               // ! 1-based index !
+                               if (item <= 0 | item > metas.size()) {
+                                       throw new IOException("Index out of bounds: " + item);
+                               }
+
+                               MetaData meta = metas.get(item - 1);
+                               displayStory(meta);
+                       }
+               }
        }
 
        @Override
@@ -160,17 +180,7 @@ class CliReader extends BasicReader {
                                        if (item <= count) {
                                                if (metas != null) {
                                                        MetaData meta = metas.get(item - 1);
-                                                       System.out.println(page + "/" + item + ": "
-                                                                       + meta.getTitle());
-                                                       System.out.println();
-                                                       System.out.println(meta.getUrl());
-                                                       System.out.println();
-                                                       System.out.println("Tags: " + meta.getTags());
-                                                       System.out.println();
-                                                       for (Paragraph para : meta.getResume()) {
-                                                               System.out.println(para.getContent());
-                                                               System.out.println("");
-                                                       }
+                                                       displayStory(meta);
                                                } else {
                                                        SearchableTag subtag = subtags.get(item - 1);
 
@@ -190,11 +200,7 @@ class CliReader extends BasicReader {
                                                // TODO i18n
                                                System.out.println(String.format("Content of %s: ",
                                                                fqnTag));
-                                               int i = 1;
-                                               for (MetaData meta : metas) {
-                                                       System.out.println(i + ": " + meta.getTitle());
-                                                       i++;
-                                               }
+                                               displayStories(metas);
                                        } else {
                                                // TODO i18n
                                                System.out.println(String.format("Subtags of %s: ",
@@ -230,4 +236,25 @@ class CliReader extends BasicReader {
                        }
                }
        }
+
+       private void displayStory(MetaData meta) {
+               System.out.println(meta.getTitle());
+               System.out.println();
+               System.out.println(meta.getUrl());
+               System.out.println();
+               System.out.println("Tags: " + meta.getTags());
+               System.out.println();
+               for (Paragraph para : meta.getResume()) {
+                       System.out.println(para.getContent());
+                       System.out.println("");
+               }
+       }
+
+       private void displayStories(List<MetaData> metas) {
+               int i = 1;
+               for (MetaData meta : metas) {
+                       System.out.println(i + ": " + meta.getTitle());
+                       i++;
+               }
+       }
 }
index 979bf5ecee225e2656afb8b862adb3c98b7e307f..1e5e9da67298e37874600f449700db154f0b1786 100644 (file)
@@ -85,6 +85,20 @@ public abstract class BasicSearchable {
         */
        abstract public void fillTag(SearchableTag tag) throws IOException;
 
+       /**
+        * Search for the given term and return the number of pages of results of
+        * stories satisfying this search term.
+        * 
+        * @param search
+        *            the term to search for
+        * 
+        * @return a number of pages
+        * 
+        * @throws IOException
+        *             in case of I/O error
+        */
+       abstract public int searchPages(String search) throws IOException;
+
        /**
         * Search for the given term and return a list of stories satisfying this
         * search term.
index 90f25eec473ba3657aac8de15224161b2faace11..d0b6f99bd903c071c5e6566140e731c15ca8a687 100644 (file)
@@ -143,8 +143,19 @@ class Fanfiction extends BasicSearchable {
        @Override
        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 + "&ppage=" + page, null, null);
+               String url = BASE_URL + "search/?ready=1&type=story&keywords="
+                               + encoded + "&ppage=" + page;
+
+               return getStories(url, null, null);
+       }
+
+       @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