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