search should now be OK (CLI, Fanfiction only)
[fanfix.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 * The support type.
39 *
40 * @return the type
41 */
42 public SupportType getType() {
43 return type;
44 }
45
46 /**
47 * The support type.
48 *
49 * @param type
50 * the new type
51 */
52 protected void setType(SupportType type) {
53 this.type = type;
54 }
55
56 /**
57 * The associated {@link BasicSupport}.
58 * <p>
59 * Mostly used to download content.
60 *
61 * @return the support
62 */
63 protected BasicSupport getSupport() {
64 return support;
65 }
66
67 /**
68 * Get a list of tags that can be browsed here.
69 *
70 * @return the list of tags
71 *
72 * @throws IOException
73 * in case of I/O error
74 */
75 abstract public List<SearchableTag> getTags() throws IOException;
76
77 /**
78 * Fill the tag (set it 'complete') with more information from the support.
79 *
80 * @param tag
81 * the tag to fill
82 *
83 * @throws IOException
84 * in case of I/O error
85 */
86 abstract public void fillTag(SearchableTag tag) throws IOException;
87
88 /**
89 * Search for the given term and return the number of pages of results of
90 * stories satisfying this search term.
91 *
92 * @param search
93 * the term to search for
94 *
95 * @return a number of pages
96 *
97 * @throws IOException
98 * in case of I/O error
99 */
100 abstract public int searchPages(String search) throws IOException;
101
102 /**
103 * Search for the given term and return a list of stories satisfying this
104 * search term.
105 * <p>
106 * Not that the returned stories will <b>NOT</b> be complete, but will only
107 * contain enough information to present them to the user and retrieve them.
108 * <p>
109 * URL is guaranteed to be usable, LUID will always be NULL.
110 *
111 * @param search
112 * the term to search for
113 * @param page
114 * the page to use for result pagination, index is 1-based
115 *
116 * @return a list of stories that satisfy that search term
117 *
118 * @throws IOException
119 * in case of I/O error
120 */
121 abstract public List<MetaData> search(String search, int page)
122 throws IOException;
123
124 /**
125 * Search for the given tag and return a list of stories satisfying this
126 * tag.
127 * <p>
128 * Not that the returned stories will <b>NOT</b> be complete, but will only
129 * contain enough information to present them to the user and retrieve them.
130 * <p>
131 * URL is guaranteed to be usable, LUID will always be NULL.
132 *
133 * @param tag
134 * the tag to search for
135 * @param page
136 * the page to use for result pagination (see
137 * {@link SearchableTag#getPages()}, remember to check for -1),
138 * index is 1-based
139 *
140 * @return a list of stories that satisfy that search term
141 *
142 * @throws IOException
143 * in case of I/O error
144 */
145 abstract public List<MetaData> search(SearchableTag tag, int page)
146 throws IOException;
147
148 /**
149 * Load a document from its url.
150 *
151 * @param url
152 * the URL to load
153 * @param stable
154 * TRUE for more stable resources, FALSE when they often change
155 *
156 * @return the document
157 *
158 * @throws IOException
159 * in case of I/O error
160 */
161 protected Document load(String url, boolean stable) throws IOException {
162 return load(new URL(url), stable);
163 }
164
165 /**
166 * Load a document from its url.
167 *
168 * @param url
169 * the URL to load
170 * @param stable
171 * TRUE for more stable resources, FALSE when they often change
172 *
173 * @return the document
174 *
175 * @throws IOException
176 * in case of I/O error
177 */
178 protected Document load(URL url, boolean stable) throws IOException {
179 return DataUtil.load(Instance.getCache().open(url, support, stable),
180 "UTF-8", url.toString());
181 }
182
183 /**
184 * Return a {@link BasicSearchable} implementation supporting the given
185 * type, or NULL if it does not exist.
186 *
187 * @param type
188 * the type, must not be NULL
189 *
190 * @return an implementation that supports it, or NULL
191 */
192 public static BasicSearchable getSearchable(SupportType type) {
193 BasicSearchable support = null;
194
195 switch (type) {
196 case FIMFICTION:
197 // TODO
198 break;
199 case FANFICTION:
200 support = new Fanfiction(type);
201 break;
202 case MANGAFOX:
203 // TODO
204 break;
205 case E621:
206 // TODO
207 break;
208 case YIFFSTAR:
209 // TODO
210 break;
211 case E_HENTAI:
212 // TODO
213 break;
214 case MANGA_LEL:
215 // TODO
216 break;
217 case CBZ:
218 case HTML:
219 case INFO_TEXT:
220 case TEXT:
221 case EPUB:
222 break;
223 }
224
225 return support;
226 }
227 }