Fanfiction step2 + SearchableTags
[nikiroo-utils.git] / src / be / nikiroo / fanfix / searchable / BasicSearchable.java
CommitLineData
fd69647f
NR
1package be.nikiroo.fanfix.searchable;
2
3import java.io.IOException;
4import java.net.URL;
5import java.util.List;
6
7import org.jsoup.helper.DataUtil;
8import org.jsoup.nodes.Document;
9
10import be.nikiroo.fanfix.Instance;
11import be.nikiroo.fanfix.data.MetaData;
12import be.nikiroo.fanfix.supported.BasicSupport;
13import 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 */
22public 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 protected 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 *
119 * @return a list of stories that satisfy that search term
120 *
121 * @throws IOException
122 * in case of I/O error
123 */
124 abstract public List<MetaData> search(SearchableTag tag) throws IOException;
125
126 /**
127 * Load a document from its url.
128 *
129 * @param url
130 * the URL to load
76ec935e
NR
131 * @param stable
132 * TRUE for more stable resources, FALSE when they often change
133 *
fd69647f
NR
134 * @return the document
135 *
136 * @throws IOException
137 * in case of I/O error
138 */
76ec935e
NR
139 protected Document load(String url, boolean stable) throws IOException {
140 return load(new URL(url), stable);
fd69647f
NR
141 }
142
143 /**
144 * Load a document from its url.
145 *
146 * @param url
147 * the URL to load
76ec935e
NR
148 * @param stable
149 * TRUE for more stable resources, FALSE when they often change
150 *
fd69647f
NR
151 * @return the document
152 *
153 * @throws IOException
154 * in case of I/O error
155 */
76ec935e
NR
156 protected Document load(URL url, boolean stable) throws IOException {
157 return DataUtil.load(Instance.getCache().open(url, support, stable),
fd69647f
NR
158 "UTF-8", url.toString());
159 }
160
161 /**
162 * Return a {@link BasicSearchable} implementation supporting the given
163 * type, or NULL if it does not exist.
164 *
165 * @param type
166 * the type, must not be NULL
167 *
168 * @return an implementation that supports it, or NULL
169 */
170 public static BasicSearchable getSearchable(SupportType type) {
171 BasicSearchable support = null;
172
173 switch (type) {
174 case FIMFICTION:
175 // TODO
176 break;
177 case FANFICTION:
178 support = new Fanfiction(type);
179 break;
180 case MANGAFOX:
181 // TODO
182 break;
183 case E621:
184 // TODO
185 break;
186 case YIFFSTAR:
187 // TODO
188 break;
189 case E_HENTAI:
190 // TODO
191 break;
192 case MANGA_LEL:
193 // TODO
194 break;
195 case CBZ:
196 case HTML:
197 case INFO_TEXT:
198 case TEXT:
199 case EPUB:
200 break;
201 }
202
203 return support;
204 }
205}