search: cleanup
[nikiroo-utils.git] / src / be / nikiroo / fanfix / searchable / BasicSearchable.java
1 package be.nikiroo.fanfix.searchable;
2
3 import java.io.IOException;
4 import java.net.URL;
5 import java.util.List;
6
7 import org.jsoup.helper.DataUtil;
8 import org.jsoup.nodes.Document;
9
10 import be.nikiroo.fanfix.Instance;
11 import be.nikiroo.fanfix.data.MetaData;
12 import be.nikiroo.fanfix.supported.BasicSupport;
13 import be.nikiroo.fanfix.supported.SupportType;
14
15 /**
16 * This class supports browsing through stories on the supported websites. It
17 * will fetch some {@link MetaData} that satisfy a search query or some tags if
18 * supported.
19 *
20 * @author niki
21 */
22 public abstract class BasicSearchable {
23 private SupportType type;
24 private BasicSupport support;
25
26 /**
27 * Create a new {@link BasicSearchable} of the given type.
28 *
29 * @param type
30 * the type, must not be NULL
31 */
32 public BasicSearchable(SupportType type) {
33 setType(type);
34 support = BasicSupport.getSupport(getType(), null);
35 }
36
37 /**
38 * Find the given tag by its hierarchical IDs.
39 * <p>
40 * I.E., it will take the tag A, subtag B, subsubtag C...
41 *
42 * @param ids
43 * the IDs to look for
44 *
45 * @return the appropriate tag fully filled, or NULL if not found
46 *
47 * @throws IOException
48 * in case of I/O error
49 */
50 public SearchableTag getTag(Integer... ids) throws IOException {
51 SearchableTag tag = null;
52 List<SearchableTag> tags = getTags();
53
54 for (Integer tagIndex : ids) {
55 // ! 1-based index !
56 if (tagIndex == null || tags == null || tagIndex <= 0
57 || tagIndex > tags.size()) {
58 return null;
59 }
60
61 tag = tags.get(tagIndex - 1);
62 fillTag(tag);
63 tags = tag.getChildren();
64 }
65
66 return tag;
67 }
68
69 /**
70 * The support type.
71 *
72 * @return the type
73 */
74 public SupportType getType() {
75 return type;
76 }
77
78 /**
79 * The support type.
80 *
81 * @param type
82 * the new type
83 */
84 protected void setType(SupportType type) {
85 this.type = type;
86 }
87
88 /**
89 * The associated {@link BasicSupport}.
90 * <p>
91 * Mostly used to download content.
92 *
93 * @return the support
94 */
95 protected BasicSupport getSupport() {
96 return support;
97 }
98
99 /**
100 * Get a list of tags that can be browsed here.
101 *
102 * @return the list of tags
103 *
104 * @throws IOException
105 * in case of I/O error
106 */
107 abstract public List<SearchableTag> getTags() throws IOException;
108
109 /**
110 * Fill the tag (set it 'complete') with more information from the support.
111 *
112 * @param tag
113 * the tag to fill
114 *
115 * @throws IOException
116 * in case of I/O error
117 */
118 abstract public void fillTag(SearchableTag tag) throws IOException;
119
120 /**
121 * Search for the given term and return the number of pages of results of
122 * stories satisfying this search term.
123 *
124 * @param search
125 * the term to search for
126 *
127 * @return a number of pages
128 *
129 * @throws IOException
130 * in case of I/O error
131 */
132 abstract public int searchPages(String search) throws IOException;
133
134 /**
135 * Search for the given term and return a list of stories satisfying this
136 * search term.
137 * <p>
138 * Not that the returned stories will <b>NOT</b> be complete, but will only
139 * contain enough information to present them to the user and retrieve them.
140 * <p>
141 * URL is guaranteed to be usable, LUID will always be NULL.
142 *
143 * @param search
144 * the term to search for
145 * @param page
146 * the page to use for result pagination, index is 1-based
147 *
148 * @return a list of stories that satisfy that search term
149 *
150 * @throws IOException
151 * in case of I/O error
152 */
153 abstract public List<MetaData> search(String search, int page)
154 throws IOException;
155
156 /**
157 * Search for the given tag and return a list of stories satisfying this
158 * tag.
159 * <p>
160 * Not that the returned stories will <b>NOT</b> be complete, but will only
161 * contain enough information to present them to the user and retrieve them.
162 * <p>
163 * URL is guaranteed to be usable, LUID will always be NULL.
164 *
165 * @param tag
166 * the tag to search for
167 * @param page
168 * the page to use for result pagination (see
169 * {@link SearchableTag#getPages()}, remember to check for -1),
170 * index is 1-based
171 *
172 * @return a list of stories that satisfy that search term
173 *
174 * @throws IOException
175 * in case of I/O error
176 */
177 abstract public List<MetaData> search(SearchableTag tag, int page)
178 throws IOException;
179
180 /**
181 * Load a document from its url.
182 *
183 * @param url
184 * the URL to load
185 * @param stable
186 * TRUE for more stable resources, FALSE when they often change
187 *
188 * @return the document
189 *
190 * @throws IOException
191 * in case of I/O error
192 */
193 protected Document load(String url, boolean stable) throws IOException {
194 return load(new URL(url), stable);
195 }
196
197 /**
198 * Load a document from its url.
199 *
200 * @param url
201 * the URL to load
202 * @param stable
203 * TRUE for more stable resources, FALSE when they often change
204 *
205 * @return the document
206 *
207 * @throws IOException
208 * in case of I/O error
209 */
210 protected Document load(URL url, boolean stable) throws IOException {
211 return DataUtil.load(Instance.getCache().open(url, support, stable),
212 "UTF-8", url.toString());
213 }
214
215 /**
216 * Return a {@link BasicSearchable} implementation supporting the given
217 * type, or NULL if it does not exist.
218 *
219 * @param type
220 * the type, can be NULL (will just return NULL, since we do not
221 * support it)
222 *
223 * @return an implementation that supports it, or NULL
224 */
225 static public BasicSearchable getSearchable(SupportType type) {
226 BasicSearchable support = null;
227
228 if (type != null) {
229 switch (type) {
230 case FIMFICTION:
231 // TODO
232 break;
233 case FANFICTION:
234 support = new Fanfiction(type);
235 break;
236 case MANGAFOX:
237 // TODO
238 break;
239 case E621:
240 // TODO
241 break;
242 case YIFFSTAR:
243 // TODO
244 break;
245 case E_HENTAI:
246 // TODO
247 break;
248 case MANGA_LEL:
249 support = new MangaLel();
250 break;
251 case CBZ:
252 case HTML:
253 case INFO_TEXT:
254 case TEXT:
255 case EPUB:
256 break;
257 }
258 }
259
260 return support;
261 }
262 }