Commit | Line | Data |
---|---|---|
fd69647f NR |
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 | */ | |
91b82a5c | 86 | abstract public void fillTag(SearchableTag tag) throws IOException; |
fd69647f | 87 | |
124442f1 NR |
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 | ||
fd69647f NR |
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 | |
8ffc8b73 NR |
113 | * @param page |
114 | * the page to use for result pagination, index is 1-based | |
fd69647f NR |
115 | * |
116 | * @return a list of stories that satisfy that search term | |
117 | * | |
118 | * @throws IOException | |
119 | * in case of I/O error | |
120 | */ | |
8ffc8b73 NR |
121 | abstract public List<MetaData> search(String search, int page) |
122 | throws IOException; | |
fd69647f NR |
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 | * | |
8ffc8b73 | 133 | * @param tag |
fd69647f | 134 | * the tag to search for |
e66c9078 NR |
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 | |
fd69647f NR |
139 | * |
140 | * @return a list of stories that satisfy that search term | |
141 | * | |
142 | * @throws IOException | |
143 | * in case of I/O error | |
144 | */ | |
e66c9078 NR |
145 | abstract public List<MetaData> search(SearchableTag tag, int page) |
146 | throws IOException; | |
fd69647f NR |
147 | |
148 | /** | |
149 | * Load a document from its url. | |
150 | * | |
151 | * @param url | |
152 | * the URL to load | |
76ec935e NR |
153 | * @param stable |
154 | * TRUE for more stable resources, FALSE when they often change | |
155 | * | |
fd69647f NR |
156 | * @return the document |
157 | * | |
158 | * @throws IOException | |
159 | * in case of I/O error | |
160 | */ | |
76ec935e NR |
161 | protected Document load(String url, boolean stable) throws IOException { |
162 | return load(new URL(url), stable); | |
fd69647f NR |
163 | } |
164 | ||
165 | /** | |
166 | * Load a document from its url. | |
167 | * | |
168 | * @param url | |
169 | * the URL to load | |
76ec935e NR |
170 | * @param stable |
171 | * TRUE for more stable resources, FALSE when they often change | |
172 | * | |
fd69647f NR |
173 | * @return the document |
174 | * | |
175 | * @throws IOException | |
176 | * in case of I/O error | |
177 | */ | |
76ec935e NR |
178 | protected Document load(URL url, boolean stable) throws IOException { |
179 | return DataUtil.load(Instance.getCache().open(url, support, stable), | |
fd69647f NR |
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 | } |