From: Niki Roo Date: Sat, 13 Apr 2019 12:09:22 +0000 (+0200) Subject: search should now be OK (CLI, Fanfiction only) X-Git-Url: https://git.nikiroo.be/?a=commitdiff_plain;h=124442f1bc9bfc67f74b2c20cdeea90bec3cd89c;p=nikiroo-utils.git search should now be OK (CLI, Fanfiction only) --- diff --git a/src/be/nikiroo/fanfix/Main.java b/src/be/nikiroo/fanfix/Main.java index 2437cae..876fe55 100644 --- a/src/be/nikiroo/fanfix/Main.java +++ b/src/be/nikiroo/fanfix/Main.java @@ -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; diff --git a/src/be/nikiroo/fanfix/reader/cli/CliReader.java b/src/be/nikiroo/fanfix/reader/cli/CliReader.java index 701ea48..d8d3a6c 100644 --- a/src/be/nikiroo/fanfix/reader/cli/CliReader.java +++ b/src/be/nikiroo/fanfix/reader/cli/CliReader.java @@ -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 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 metas) { + int i = 1; + for (MetaData meta : metas) { + System.out.println(i + ": " + meta.getTitle()); + i++; + } + } } diff --git a/src/be/nikiroo/fanfix/searchable/BasicSearchable.java b/src/be/nikiroo/fanfix/searchable/BasicSearchable.java index 979bf5e..1e5e9da 100644 --- a/src/be/nikiroo/fanfix/searchable/BasicSearchable.java +++ b/src/be/nikiroo/fanfix/searchable/BasicSearchable.java @@ -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. diff --git a/src/be/nikiroo/fanfix/searchable/Fanfiction.java b/src/be/nikiroo/fanfix/searchable/Fanfiction.java index 90f25ee..d0b6f99 100644 --- a/src/be/nikiroo/fanfix/searchable/Fanfiction.java +++ b/src/be/nikiroo/fanfix/searchable/Fanfiction.java @@ -143,8 +143,19 @@ class Fanfiction extends BasicSearchable { @Override public List 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