allow pagination for search keywords
[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 a list of stories satisfying this
90 * search term.
91 * <p>
92 * Not that the returned stories will <b>NOT</b> be complete, but will only
93 * contain enough information to present them to the user and retrieve them.
94 * <p>
95 * URL is guaranteed to be usable, LUID will always be NULL.
96 *
97 * @param search
98 * the term to search for
99 * @param page
100 * the page to use for result pagination, index is 1-based
101 *
102 * @return a list of stories that satisfy that search term
103 *
104 * @throws IOException
105 * in case of I/O error
106 */
107 abstract public List<MetaData> search(String search, int page)
108 throws IOException;
109
110 /**
111 * Search for the given tag and return a list of stories satisfying this
112 * tag.
113 * <p>
114 * Not that the returned stories will <b>NOT</b> be complete, but will only
115 * contain enough information to present them to the user and retrieve them.
116 * <p>
117 * URL is guaranteed to be usable, LUID will always be NULL.
118 *
119 * @param tag
120 * the tag to search for
121 * @param page
122 * the page to use for result pagination (see
123 * {@link SearchableTag#getPages()}, remember to check for -1),
124 * index is 1-based
125 *
126 * @return a list of stories that satisfy that search term
127 *
128 * @throws IOException
129 * in case of I/O error
130 */
131 abstract public List<MetaData> search(SearchableTag tag, int page)
132 throws IOException;
133
134 /**
135 * Load a document from its url.
136 *
137 * @param url
138 * the URL to load
139 * @param stable
140 * TRUE for more stable resources, FALSE when they often change
141 *
142 * @return the document
143 *
144 * @throws IOException
145 * in case of I/O error
146 */
147 protected Document load(String url, boolean stable) throws IOException {
148 return load(new URL(url), stable);
149 }
150
151 /**
152 * Load a document from its url.
153 *
154 * @param url
155 * the URL to load
156 * @param stable
157 * TRUE for more stable resources, FALSE when they often change
158 *
159 * @return the document
160 *
161 * @throws IOException
162 * in case of I/O error
163 */
164 protected Document load(URL url, boolean stable) throws IOException {
165 return DataUtil.load(Instance.getCache().open(url, support, stable),
166 "UTF-8", url.toString());
167 }
168
169 /**
170 * Return a {@link BasicSearchable} implementation supporting the given
171 * type, or NULL if it does not exist.
172 *
173 * @param type
174 * the type, must not be NULL
175 *
176 * @return an implementation that supports it, or NULL
177 */
178 public static BasicSearchable getSearchable(SupportType type) {
179 BasicSearchable support = null;
180
181 switch (type) {
182 case FIMFICTION:
183 // TODO
184 break;
185 case FANFICTION:
186 support = new Fanfiction(type);
187 break;
188 case MANGAFOX:
189 // TODO
190 break;
191 case E621:
192 // TODO
193 break;
194 case YIFFSTAR:
195 // TODO
196 break;
197 case E_HENTAI:
198 // TODO
199 break;
200 case MANGA_LEL:
201 // TODO
202 break;
203 case CBZ:
204 case HTML:
205 case INFO_TEXT:
206 case TEXT:
207 case EPUB:
208 break;
209 }
210
211 return support;
212 }
213 }