fix warnings
[fanfix.git] / src / be / nikiroo / fanfix / supported / BasicSupport.java
CommitLineData
08fe2e33
NR
1package be.nikiroo.fanfix.supported;
2
08fe2e33
NR
3import java.io.IOException;
4import java.io.InputStream;
08fe2e33 5import java.net.URL;
08fe2e33 6import java.util.ArrayList;
793f1071 7import java.util.Date;
08fe2e33
NR
8import java.util.HashMap;
9import java.util.List;
10import java.util.Map;
11import java.util.Map.Entry;
0ffa4754
NR
12
13import org.jsoup.helper.DataUtil;
14import org.jsoup.nodes.Document;
15import org.jsoup.nodes.Element;
16import org.jsoup.nodes.Node;
08fe2e33
NR
17
18import be.nikiroo.fanfix.Instance;
08fe2e33
NR
19import be.nikiroo.fanfix.bundles.StringId;
20import be.nikiroo.fanfix.data.Chapter;
21import be.nikiroo.fanfix.data.MetaData;
9252c65e 22import be.nikiroo.fanfix.data.Story;
3b2b638f 23import be.nikiroo.utils.Progress;
08fe2e33
NR
24import be.nikiroo.utils.StringUtils;
25
26/**
27 * This class is the base class used by the other support classes. It can be
28 * used outside of this package, and have static method that you can use to get
29 * access to the correct support class.
30 * <p>
31 * It will be used with 'resources' (usually web pages or files).
32 *
33 * @author niki
34 */
35public abstract class BasicSupport {
0ffa4754
NR
36 private Document sourceNode;
37 private URL source;
08fe2e33 38 private SupportType type;
22848428 39 private URL currentReferer; // with only one 'r', as in 'HTTP'...
08fe2e33 40
08fe2e33
NR
41 /**
42 * The name of this support class.
43 *
44 * @return the name
45 */
46 protected abstract String getSourceName();
47
48 /**
49 * Check if the given resource is supported by this {@link BasicSupport}.
50 *
51 * @param url
52 * the resource to check for
53 *
54 * @return TRUE if it is
55 */
56 protected abstract boolean supports(URL url);
57
58 /**
59 * Return TRUE if the support will return HTML encoded content values for
60 * the chapters content.
61 *
62 * @return TRUE for HTML
63 */
64 protected abstract boolean isHtml();
65
0efd25e3
NR
66 /**
67 * Return the {@link MetaData} of this story.
68 *
776ad3c6 69 * @return the associated {@link MetaData}, never NULL
0efd25e3
NR
70 *
71 * @throws IOException
72 * in case of I/O error
73 */
0ffa4754 74 protected abstract MetaData getMeta() throws IOException;
08fe2e33
NR
75
76 /**
77 * Return the story description.
78 *
08fe2e33
NR
79 * @return the description
80 *
81 * @throws IOException
82 * in case of I/O error
83 */
0ffa4754 84 protected abstract String getDesc() throws IOException;
08fe2e33 85
08fe2e33 86 /**
826e4569 87 * Return the list of chapters (name and resource).
0ffa4754
NR
88 * <p>
89 * Can be NULL if this {@link BasicSupport} do no use chapters.
08fe2e33 90 *
ed08c171
NR
91 * @param pg
92 * the optional progress reporter
08fe2e33 93 *
0ffa4754 94 * @return the chapters or NULL
08fe2e33
NR
95 *
96 * @throws IOException
97 * in case of I/O error
98 */
0ffa4754
NR
99 protected abstract List<Entry<String, URL>> getChapters(Progress pg)
100 throws IOException;
08fe2e33
NR
101
102 /**
103 * Return the content of the chapter (possibly HTML encoded, if
104 * {@link BasicSupport#isHtml()} is TRUE).
105 *
0ffa4754
NR
106 * @param chapUrl
107 * the chapter {@link URL}
08fe2e33
NR
108 * @param number
109 * the chapter number
ed08c171
NR
110 * @param pg
111 * the optional progress reporter
08fe2e33
NR
112 *
113 * @return the content
114 *
115 * @throws IOException
116 * in case of I/O error
117 */
0ffa4754
NR
118 protected abstract String getChapterContent(URL chapUrl, int number,
119 Progress pg) throws IOException;
6e06d2cc 120
08fe2e33
NR
121 /**
122 * Return the list of cookies (values included) that must be used to
123 * correctly fetch the resources.
124 * <p>
125 * You are expected to call the super method implementation if you override
126 * it.
127 *
128 * @return the cookies
129 */
315f14ae 130 public Map<String, String> getCookies() {
08fe2e33
NR
131 return new HashMap<String, String>();
132 }
133
315f14ae
NR
134 /**
135 * OAuth authorisation (aka, "bearer XXXXXXX").
136 *
137 * @return the OAuth string
138 */
139 public String getOAuth() {
140 return null;
141 }
142
a4143cd7
NR
143 /**
144 * Return the canonical form of the main {@link URL}.
145 *
146 * @param source
0ffa4754
NR
147 * the source {@link URL}, which can be NULL
148 *
149 * @return the canonical form of this {@link URL} or NULL if the source was
150 * NULL
151 */
152 protected URL getCanonicalUrl(URL source) {
153 return source;
154 }
155
156 /**
157 * The main {@link Node} for this {@link Story}.
158 *
159 * @return the node
160 */
161 protected Element getSourceNode() {
162 return sourceNode;
163 }
164
165 /**
166 * The main {@link URL} for this {@link Story}.
167 *
168 * @return the URL
169 */
170 protected URL getSource() {
171 return source;
172 }
173
174 /**
175 * The current referer {@link URL} (only one 'r', as in 'HTML'...), i.e.,
176 * the current {@link URL} we work on.
177 *
178 * @return the referer
179 */
180 public URL getCurrentReferer() {
181 return currentReferer;
182 }
183
184 /**
185 * The current referer {@link URL} (only one 'r', as in 'HTML'...), i.e.,
186 * the current {@link URL} we work on.
187 *
188 * @param currentReferer
189 * the new referer
190 */
191 protected void setCurrentReferer(URL currentReferer) {
192 this.currentReferer = currentReferer;
193 }
194
195 /**
196 * The support type.
197 *
198 * @return the type
199 */
200 public SupportType getType() {
201 return type;
202 }
203
204 /**
205 * The support type.
206 *
207 * @param type
208 * the new type
209 */
210 protected void setType(SupportType type) {
211 this.type = type;
212 }
213
214 /**
215 * Open an input link that will be used for the support.
216 * <p>
7445f856
NR
217 * Can return NULL, in which case you are supposed to work without a source
218 * node.
0ffa4754
NR
219 *
220 * @param source
a4143cd7
NR
221 * the source {@link URL}
222 *
0ffa4754
NR
223 * @return the {@link InputStream}
224 *
225 * @throws IOException
226 * in case of I/O error
227 */
228 protected Document loadDocument(URL source) throws IOException {
229 String url = getCanonicalUrl(source).toString();
230 return DataUtil.load(Instance.getCache().open(source, this, false),
231 "UTF-8", url.toString());
232 }
233
234 /**
235 * Log into the support (can be a no-op depending upon the support).
a4143cd7
NR
236 *
237 * @throws IOException
238 * in case of I/O error
239 */
0ffa4754
NR
240 protected void login() throws IOException {
241 }
242
0ffa4754
NR
243 /**
244 * Now that we have processed the {@link Story}, close the resources if any.
245 */
246 protected void close() {
247 setCurrentReferer(null);
a4143cd7
NR
248 }
249
08fe2e33
NR
250 /**
251 * Process the given story resource into a partially filled {@link Story}
252 * object containing the name and metadata.
253 *
0efd25e3
NR
254 * @param getDesc
255 * retrieve the description of the story, or not
ed08c171
NR
256 * @param pg
257 * the optional progress reporter
08fe2e33 258 *
776ad3c6 259 * @return the {@link Story}, never NULL
08fe2e33
NR
260 *
261 * @throws IOException
262 * in case of I/O error
263 */
0ffa4754
NR
264 protected Story processMeta(boolean getDesc, Progress pg)
265 throws IOException {
ed08c171
NR
266 if (pg == null) {
267 pg = new Progress();
268 } else {
269 pg.setMinMax(0, 100);
270 }
271
0ffa4754 272 pg.setProgress(30);
ed08c171 273
0ffa4754
NR
274 Story story = new Story();
275 MetaData meta = getMeta();
276 if (meta.getCreationDate() == null || meta.getCreationDate().isEmpty()) {
277 meta.setCreationDate(StringUtils.fromTime(new Date().getTime()));
278 }
279 story.setMeta(meta);
ed08c171 280
0ffa4754 281 pg.setProgress(50);
08fe2e33 282
0ffa4754
NR
283 if (meta.getCover() == null) {
284 meta.setCover(BasicSupportHelper.getDefaultCover(meta.getSubject()));
285 }
08fe2e33 286
0ffa4754 287 pg.setProgress(60);
a4143cd7 288
0ffa4754
NR
289 if (getDesc) {
290 String descChapterName = Instance.getTrans().getString(
291 StringId.DESCRIPTION);
292 story.getMeta().setResume(
293 BasicSupportPara.makeChapter(this, source, 0,
294 descChapterName, //
295 getDesc(), isHtml(), null));
08fe2e33 296 }
0ffa4754
NR
297
298 pg.setProgress(100);
299 return story;
08fe2e33
NR
300 }
301
9005532f 302 /**
826e4569
NR
303 * Process the given story resource into a fully filled {@link Story}
304 * object.
9005532f
NR
305 *
306 * @param pg
307 * the optional progress reporter
308 *
309 * @return the {@link Story}, never NULL
310 *
311 * @throws IOException
312 * in case of I/O error
313 */
826e4569 314 // ADD final when BasicSupport_Deprecated is gone
9005532f
NR
315 public Story process(Progress pg) throws IOException {
316 setCurrentReferer(source);
317 login();
318 sourceNode = loadDocument(source);
319
320 try {
321 return doProcess(pg);
322 } finally {
323 close();
324 }
325 }
326
08fe2e33 327 /**
826e4569
NR
328 * Actual processing step, without the calls to other methods.
329 * <p>
330 * Will convert the story resource into a fully filled {@link Story} object.
08fe2e33 331 *
92fb0719
NR
332 * @param pg
333 * the optional progress reporter
08fe2e33 334 *
776ad3c6 335 * @return the {@link Story}, never NULL
08fe2e33
NR
336 *
337 * @throws IOException
338 * in case of I/O error
339 */
826e4569 340 protected Story doProcess(Progress pg) throws IOException {
92fb0719
NR
341 if (pg == null) {
342 pg = new Progress();
343 } else {
344 pg.setMinMax(0, 100);
345 }
346
92fb0719 347 pg.setProgress(1);
9005532f
NR
348 Progress pgMeta = new Progress();
349 pg.addProgress(pgMeta, 10);
350 Story story = processMeta(true, pgMeta);
351 if (!pgMeta.isDone()) {
352 pgMeta.setProgress(pgMeta.getMax()); // 10%
353 }
ed08c171 354
9005532f 355 pg.setName("Retrieving " + story.getMeta().getTitle());
754a5bc2 356
9005532f
NR
357 Progress pgGetChapters = new Progress();
358 pg.addProgress(pgGetChapters, 10);
359 story.setChapters(new ArrayList<Chapter>());
360 List<Entry<String, URL>> chapters = getChapters(pgGetChapters);
361 if (!pgGetChapters.isDone()) {
362 pgGetChapters.setProgress(pgGetChapters.getMax()); // 20%
363 }
364
365 if (chapters != null) {
366 Progress pgChaps = new Progress("Extracting chapters", 0,
367 chapters.size() * 300);
368 pg.addProgress(pgChaps, 80);
369
370 long words = 0;
371 int i = 1;
372 for (Entry<String, URL> chap : chapters) {
373 pgChaps.setName("Extracting chapter " + i);
374 URL chapUrl = chap.getValue();
375 String chapName = chap.getKey();
376 if (chapUrl != null) {
377 setCurrentReferer(chapUrl);
378 }
379
380 pgChaps.setProgress(i * 100);
381 Progress pgGetChapterContent = new Progress();
382 Progress pgMakeChapter = new Progress();
383 pgChaps.addProgress(pgGetChapterContent, 100);
384 pgChaps.addProgress(pgMakeChapter, 100);
385
386 String content = getChapterContent(chapUrl, i,
387 pgGetChapterContent);
388 if (!pgGetChapterContent.isDone()) {
389 pgGetChapterContent.setProgress(pgGetChapterContent
390 .getMax());
391 }
08fe2e33 392
9005532f
NR
393 Chapter cc = BasicSupportPara.makeChapter(this, chapUrl, i,
394 chapName, content, isHtml(), pgMakeChapter);
395 if (!pgMakeChapter.isDone()) {
396 pgMakeChapter.setProgress(pgMakeChapter.getMax());
08fe2e33 397 }
ed08c171 398
9005532f
NR
399 words += cc.getWords();
400 story.getChapters().add(cc);
401 story.getMeta().setWords(words);
402
403 i++;
08fe2e33
NR
404 }
405
9005532f
NR
406 pgChaps.setName("Extracting chapters");
407 } else {
408 pg.setProgress(80);
08fe2e33 409 }
9005532f
NR
410
411 return story;
08fe2e33
NR
412 }
413
414 /**
0ffa4754
NR
415 * Return a {@link BasicSupport} implementation supporting the given
416 * resource if possible.
08fe2e33 417 *
0ffa4754
NR
418 * @param url
419 * the story resource
08fe2e33 420 *
0ffa4754 421 * @return an implementation that supports it, or NULL
08fe2e33 422 */
0ffa4754
NR
423 public static BasicSupport getSupport(URL url) {
424 if (url == null) {
425 return null;
426 }
08fe2e33 427
0ffa4754
NR
428 // TEXT and INFO_TEXT always support files (not URLs though)
429 for (SupportType type : SupportType.values()) {
430 if (type != SupportType.TEXT && type != SupportType.INFO_TEXT) {
431 BasicSupport support = getSupport(type, url);
432 if (support != null && support.supports(url)) {
433 return support;
434 }
435 }
436 }
08fe2e33 437
0ffa4754
NR
438 for (SupportType type : new SupportType[] { SupportType.INFO_TEXT,
439 SupportType.TEXT }) {
440 BasicSupport support = getSupport(type, url);
441 if (support != null && support.supports(url)) {
442 return support;
443 }
444 }
445
446 return null;
08fe2e33
NR
447 }
448
449 /**
0ffa4754 450 * Return a {@link BasicSupport} implementation supporting the given type.
08fe2e33 451 *
0ffa4754
NR
452 * @param type
453 * the type
454 * @param url
455 * the {@link URL} to support (can be NULL to get an
456 * "abstract support")
08fe2e33 457 *
0ffa4754 458 * @return an implementation that supports it, or NULL
08fe2e33 459 */
0ffa4754
NR
460 public static BasicSupport getSupport(SupportType type, URL url) {
461 BasicSupport support = null;
08fe2e33 462
08fe2e33
NR
463 switch (type) {
464 case EPUB:
0ffa4754
NR
465 support = new Epub();
466 break;
08fe2e33 467 case INFO_TEXT:
0ffa4754
NR
468 support = new InfoText();
469 break;
08fe2e33 470 case FIMFICTION:
315f14ae
NR
471 try {
472 // Can fail if no client key or NO in options
0ffa4754 473 support = new FimfictionApi();
315f14ae 474 } catch (IOException e) {
0ffa4754 475 support = new Fimfiction();
315f14ae 476 }
0ffa4754 477 break;
08fe2e33 478 case FANFICTION:
0ffa4754
NR
479 support = new Fanfiction();
480 break;
08fe2e33 481 case TEXT:
0ffa4754
NR
482 support = new Text();
483 break;
08fe2e33 484 case MANGAFOX:
0ffa4754
NR
485 support = new MangaFox();
486 break;
08fe2e33 487 case E621:
0ffa4754
NR
488 support = new E621();
489 break;
a4143cd7 490 case YIFFSTAR:
0ffa4754
NR
491 support = new YiffStar();
492 break;
f0608ab1 493 case E_HENTAI:
0ffa4754
NR
494 support = new EHentai();
495 break;
af1f506f
NR
496 case MANGA_LEL:
497 support = new MangaLel();
498 break;
08fe2e33 499 case CBZ:
0ffa4754
NR
500 support = new Cbz();
501 break;
373da363 502 case HTML:
0ffa4754
NR
503 support = new Html();
504 break;
68686a37
NR
505 }
506
0ffa4754
NR
507 if (support != null) {
508 support.setType(type);
509 support.source = support.getCanonicalUrl(url);
315f14ae
NR
510 }
511
0ffa4754 512 return support;
315f14ae 513 }
08fe2e33 514}