tests: allow testing live URLs
[nikiroo-utils.git] / src / be / nikiroo / fanfix / reader / cli / CliReader.java
CommitLineData
16a81ef7 1package be.nikiroo.fanfix.reader.cli;
08fe2e33
NR
2
3import java.io.IOException;
08fe2e33
NR
4import java.util.List;
5
6import be.nikiroo.fanfix.Instance;
08fe2e33
NR
7import be.nikiroo.fanfix.bundles.StringId;
8import be.nikiroo.fanfix.data.Chapter;
9import be.nikiroo.fanfix.data.MetaData;
10import be.nikiroo.fanfix.data.Paragraph;
11import be.nikiroo.fanfix.data.Story;
16a81ef7 12import be.nikiroo.fanfix.reader.BasicReader;
91b82a5c
NR
13import be.nikiroo.fanfix.searchable.BasicSearchable;
14import be.nikiroo.fanfix.searchable.SearchableTag;
15import be.nikiroo.fanfix.supported.SupportType;
74a43961 16import be.nikiroo.utils.StringUtils;
08fe2e33
NR
17
18/**
19 * Command line {@link Story} reader.
20 * <p>
21 * Will output stories to the console.
22 *
23 * @author niki
24 */
3727aae2 25class CliReader extends BasicReader {
211f7ddb 26 @Override
350bc060 27 public void read(boolean sync) throws IOException {
bc2ea776
NR
28 MetaData meta = getMeta();
29
30 if (meta == null) {
89cb07a6 31 throw new IOException("No story to read");
08fe2e33 32 }
08fe2e33 33
08fe2e33
NR
34 String title = "";
35 String author = "";
36
bc2ea776
NR
37 if (meta.getTitle() != null) {
38 title = meta.getTitle();
39 }
08fe2e33 40
bc2ea776
NR
41 if (meta.getAuthor() != null) {
42 author = "©" + meta.getAuthor();
43 if (meta.getDate() != null && !meta.getDate().isEmpty()) {
44 author = author + " (" + meta.getDate() + ")";
08fe2e33
NR
45 }
46 }
47
48 System.out.println(title);
49 System.out.println(author);
50 System.out.println("");
51
bc2ea776
NR
52 // TODO: progress?
53 for (Chapter chap : getStory(null)) {
08fe2e33
NR
54 if (chap.getName() != null && !chap.getName().isEmpty()) {
55 System.out.println(Instance.getTrans().getString(
56 StringId.CHAPTER_NAMED, chap.getNumber(),
57 chap.getName()));
58 } else {
59 System.out.println(Instance.getTrans().getString(
60 StringId.CHAPTER_UNNAMED, chap.getNumber()));
61 }
62 }
63 }
64
edd46289 65 public void read(int chapter) throws IOException {
bc2ea776
NR
66 MetaData meta = getMeta();
67
68 if (meta == null) {
edd46289
NR
69 throw new IOException("No story to read");
70 }
71
bc2ea776
NR
72 // TODO: progress?
73 if (chapter > getStory(null).getChapters().size()) {
08fe2e33
NR
74 System.err.println("Chapter " + chapter + ": no such chapter");
75 } else {
bc2ea776 76 Chapter chap = getStory(null).getChapters().get(chapter - 1);
08fe2e33
NR
77 System.out.println("Chapter " + chap.getNumber() + ": "
78 + chap.getName());
79
80 for (Paragraph para : chap) {
81 System.out.println(para.getContent());
82 System.out.println("");
83 }
84 }
85 }
86
211f7ddb 87 @Override
0bb51c9c
NR
88 public void browse(String source) throws IOException {
89 List<MetaData> stories = getLibrary().getListBySource(source);
08fe2e33
NR
90
91 for (MetaData story : stories) {
92 String author = "";
93 if (story.getAuthor() != null && !story.getAuthor().isEmpty()) {
94 author = " (" + story.getAuthor() + ")";
95 }
96
97 System.out.println(story.getLuid() + ": " + story.getTitle()
98 + author);
99 }
100 }
b3b9a1cd 101
b31a0db0
NR
102 @Override
103 public void search(boolean sync) throws IOException {
104 for (SupportType type : SupportType.values()) {
105 if (BasicSearchable.getSearchable(type) != null) {
106 System.out.println(type);
107 }
108 }
109 }
110
91b82a5c 111 @Override
f76de465
NR
112 public void search(SupportType searchOn, String keywords, int page,
113 int item, boolean sync) throws IOException {
124442f1
NR
114 BasicSearchable search = BasicSearchable.getSearchable(searchOn);
115
116 if (page == 0) {
117 System.out.println(search.searchPages(keywords));
118 } else {
119 List<MetaData> metas = search.search(keywords, page);
120
121 if (item == 0) {
122 System.out.println("Page " + page + " of stories for: "
123 + keywords);
124 displayStories(metas);
125 } else {
126 // ! 1-based index !
e92e4ae3 127 if (item <= 0 || item > metas.size()) {
124442f1
NR
128 throw new IOException("Index out of bounds: " + item);
129 }
130
131 MetaData meta = metas.get(item - 1);
132 displayStory(meta);
133 }
134 }
91b82a5c 135 }
b3b9a1cd 136
91b82a5c 137 @Override
b3b9a1cd 138 public void searchTag(SupportType searchOn, int page, int item,
f76de465 139 boolean sync, Integer... tags) throws IOException {
b3b9a1cd 140
aaeabf3a
NR
141 BasicSearchable search = BasicSearchable.getSearchable(searchOn);
142 SearchableTag stag = search.getTag(tags);
b3b9a1cd 143
aaeabf3a
NR
144 if (stag == null) {
145 // TODO i18n
146 System.out.println("Known tags: ");
147 int i = 1;
148 for (SearchableTag s : search.getTags()) {
149 System.out.println(String.format("%d: %s", i, s.getName()));
150 i++;
91b82a5c 151 }
aaeabf3a 152 } else {
91b82a5c
NR
153 if (page <= 0) {
154 if (stag.isLeaf()) {
81acd363 155 System.out.println(search.searchPages(stag));
91b82a5c
NR
156 } else {
157 System.out.println(stag.getCount());
158 }
159 } else {
160 List<MetaData> metas = null;
161 List<SearchableTag> subtags = null;
162 int count;
b3b9a1cd 163
91b82a5c
NR
164 if (stag.isLeaf()) {
165 metas = search.search(stag, page);
166 count = metas.size();
167 } else {
168 subtags = stag.getChildren();
169 count = subtags.size();
170 }
b3b9a1cd 171
91b82a5c
NR
172 if (item > 0) {
173 if (item <= count) {
174 if (metas != null) {
175 MetaData meta = metas.get(item - 1);
124442f1 176 displayStory(meta);
91b82a5c
NR
177 } else {
178 SearchableTag subtag = subtags.get(item - 1);
aaeabf3a 179 displayTag(subtag);
91b82a5c
NR
180 }
181 } else {
b3b9a1cd
NR
182 System.out.println("Invalid item: only " + count
183 + " items found");
91b82a5c
NR
184 }
185 } else {
186 if (metas != null) {
8b153400
NR
187 // TODO i18n
188 System.out.println(String.format("Content of %s: ",
aaeabf3a 189 stag.getFqName()));
124442f1 190 displayStories(metas);
91b82a5c 191 } else {
8b153400
NR
192 // TODO i18n
193 System.out.println(String.format("Subtags of %s: ",
aaeabf3a
NR
194 stag.getFqName()));
195 displayTags(subtags);
91b82a5c
NR
196 }
197 }
198 }
91b82a5c
NR
199 }
200 }
124442f1 201
aaeabf3a
NR
202 private void displayTag(SearchableTag subtag) {
203 // TODO: i18n
204 String stories = "stories";
205 String num = StringUtils.formatNumber(subtag.getCount());
206 System.out.println(String.format("%s (%s), %s %s", subtag.getName(),
207 subtag.getFqName(), num, stories));
208 }
209
124442f1
NR
210 private void displayStory(MetaData meta) {
211 System.out.println(meta.getTitle());
212 System.out.println();
213 System.out.println(meta.getUrl());
214 System.out.println();
215 System.out.println("Tags: " + meta.getTags());
216 System.out.println();
217 for (Paragraph para : meta.getResume()) {
218 System.out.println(para.getContent());
219 System.out.println("");
220 }
221 }
222
aaeabf3a
NR
223 private void displayTags(List<SearchableTag> subtags) {
224 int i = 1;
225 for (SearchableTag subtag : subtags) {
226 String total = "";
227 if (subtag.getCount() > 0) {
228 total = StringUtils.formatNumber(subtag.getCount());
229 }
230
231 if (total.isEmpty()) {
232 System.out
233 .println(String.format("%d: %s", i, subtag.getName()));
234 } else {
235 System.out.println(String.format("%d: %s (%s)", i,
236 subtag.getName(), total));
237 }
238
239 i++;
240 }
241 }
242
124442f1
NR
243 private void displayStories(List<MetaData> metas) {
244 int i = 1;
245 for (MetaData meta : metas) {
246 System.out.println(i + ": " + meta.getTitle());
247 i++;
248 }
249 }
08fe2e33 250}