Merge branch 'subtree'
[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;
5cf61f35 5import java.net.MalformedURLException;
08fe2e33 6import java.net.URL;
08fe2e33 7import java.util.ArrayList;
793f1071 8import java.util.Date;
08fe2e33
NR
9import java.util.HashMap;
10import java.util.List;
11import java.util.Map;
5cf61f35 12import java.util.Scanner;
08fe2e33 13import java.util.Map.Entry;
0ffa4754 14
8831d290 15import org.json.JSONException;
5cf61f35 16import org.json.JSONObject;
0ffa4754
NR
17import org.jsoup.helper.DataUtil;
18import org.jsoup.nodes.Document;
19import org.jsoup.nodes.Element;
20import org.jsoup.nodes.Node;
08fe2e33
NR
21
22import be.nikiroo.fanfix.Instance;
08fe2e33
NR
23import be.nikiroo.fanfix.bundles.StringId;
24import be.nikiroo.fanfix.data.Chapter;
25import be.nikiroo.fanfix.data.MetaData;
9252c65e 26import be.nikiroo.fanfix.data.Story;
3b2b638f 27import be.nikiroo.utils.Progress;
08fe2e33
NR
28import be.nikiroo.utils.StringUtils;
29
30/**
31 * This class is the base class used by the other support classes. It can be
32 * used outside of this package, and have static method that you can use to get
33 * access to the correct support class.
34 * <p>
35 * It will be used with 'resources' (usually web pages or files).
36 *
37 * @author niki
38 */
39public abstract class BasicSupport {
0ffa4754
NR
40 private Document sourceNode;
41 private URL source;
08fe2e33 42 private SupportType type;
22848428 43 private URL currentReferer; // with only one 'r', as in 'HTTP'...
8d59ce07
NR
44
45 static protected BasicSupportHelper bsHelper = new BasicSupportHelper();
46 static protected BasicSupportImages bsImages = new BasicSupportImages();
47 static protected BasicSupportPara bsPara = new BasicSupportPara(new BasicSupportHelper(), new BasicSupportImages());
08fe2e33 48
08fe2e33
NR
49 /**
50 * Check if the given resource is supported by this {@link BasicSupport}.
51 *
52 * @param url
53 * the resource to check for
54 *
55 * @return TRUE if it is
56 */
57 protected abstract boolean supports(URL url);
58
59 /**
60 * Return TRUE if the support will return HTML encoded content values for
61 * the chapters content.
62 *
63 * @return TRUE for HTML
64 */
65 protected abstract boolean isHtml();
66
0efd25e3
NR
67 /**
68 * Return the {@link MetaData} of this story.
69 *
776ad3c6 70 * @return the associated {@link MetaData}, never NULL
0efd25e3
NR
71 *
72 * @throws IOException
73 * in case of I/O error
74 */
0ffa4754 75 protected abstract MetaData getMeta() throws IOException;
08fe2e33
NR
76
77 /**
78 * Return the story description.
79 *
08fe2e33
NR
80 * @return the description
81 *
82 * @throws IOException
83 * in case of I/O error
84 */
0ffa4754 85 protected abstract String getDesc() throws IOException;
08fe2e33 86
08fe2e33 87 /**
826e4569 88 * Return the list of chapters (name and resource).
0ffa4754
NR
89 * <p>
90 * Can be NULL if this {@link BasicSupport} do no use chapters.
08fe2e33 91 *
ed08c171
NR
92 * @param pg
93 * the optional progress reporter
08fe2e33 94 *
0ffa4754 95 * @return the chapters or NULL
08fe2e33
NR
96 *
97 * @throws IOException
98 * in case of I/O error
99 */
0ffa4754
NR
100 protected abstract List<Entry<String, URL>> getChapters(Progress pg)
101 throws IOException;
08fe2e33
NR
102
103 /**
104 * Return the content of the chapter (possibly HTML encoded, if
105 * {@link BasicSupport#isHtml()} is TRUE).
106 *
0ffa4754
NR
107 * @param chapUrl
108 * the chapter {@link URL}
08fe2e33
NR
109 * @param number
110 * the chapter number
ed08c171
NR
111 * @param pg
112 * the optional progress reporter
08fe2e33
NR
113 *
114 * @return the content
115 *
116 * @throws IOException
117 * in case of I/O error
118 */
0ffa4754
NR
119 protected abstract String getChapterContent(URL chapUrl, int number,
120 Progress pg) throws IOException;
6e06d2cc 121
08fe2e33
NR
122 /**
123 * Return the list of cookies (values included) that must be used to
124 * correctly fetch the resources.
125 * <p>
126 * You are expected to call the super method implementation if you override
127 * it.
128 *
129 * @return the cookies
130 */
315f14ae 131 public Map<String, String> getCookies() {
08fe2e33
NR
132 return new HashMap<String, String>();
133 }
134
315f14ae
NR
135 /**
136 * OAuth authorisation (aka, "bearer XXXXXXX").
137 *
138 * @return the OAuth string
139 */
140 public String getOAuth() {
141 return null;
142 }
143
a4143cd7
NR
144 /**
145 * Return the canonical form of the main {@link URL}.
146 *
147 * @param source
0ffa4754
NR
148 * the source {@link URL}, which can be NULL
149 *
150 * @return the canonical form of this {@link URL} or NULL if the source was
151 * NULL
152 */
153 protected URL getCanonicalUrl(URL source) {
154 return source;
155 }
156
157 /**
158 * The main {@link Node} for this {@link Story}.
159 *
160 * @return the node
161 */
162 protected Element getSourceNode() {
163 return sourceNode;
164 }
165
166 /**
167 * The main {@link URL} for this {@link Story}.
168 *
169 * @return the URL
170 */
171 protected URL getSource() {
172 return source;
173 }
174
175 /**
176 * The current referer {@link URL} (only one 'r', as in 'HTML'...), i.e.,
177 * the current {@link URL} we work on.
178 *
179 * @return the referer
180 */
181 public URL getCurrentReferer() {
182 return currentReferer;
183 }
184
185 /**
186 * The current referer {@link URL} (only one 'r', as in 'HTML'...), i.e.,
187 * the current {@link URL} we work on.
188 *
189 * @param currentReferer
190 * the new referer
191 */
192 protected void setCurrentReferer(URL currentReferer) {
193 this.currentReferer = currentReferer;
194 }
195
196 /**
197 * The support type.
198 *
199 * @return the type
200 */
201 public SupportType getType() {
202 return type;
203 }
204
205 /**
206 * The support type.
207 *
208 * @param type
209 * the new type
210 */
211 protected void setType(SupportType type) {
212 this.type = type;
213 }
214
215 /**
216 * Open an input link that will be used for the support.
217 * <p>
7445f856
NR
218 * Can return NULL, in which case you are supposed to work without a source
219 * node.
0ffa4754
NR
220 *
221 * @param source
a4143cd7
NR
222 * the source {@link URL}
223 *
0ffa4754
NR
224 * @return the {@link InputStream}
225 *
226 * @throws IOException
227 * in case of I/O error
228 */
229 protected Document loadDocument(URL source) throws IOException {
230 String url = getCanonicalUrl(source).toString();
d66deb8d 231 return DataUtil.load(Instance.getInstance().getCache().open(source, this, false), "UTF-8", url.toString());
0ffa4754
NR
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 274 Story story = new Story();
cfdaf605 275
0ffa4754 276 MetaData meta = getMeta();
cfdaf605
NR
277 meta.setType(getType().toString());
278 meta.setSource(getType().getSourceName());
ee8686f0
NR
279 if (meta.getPublisher() == null) {
280 meta.setPublisher(getType().getSourceName());
281 }
282
bff19b54
NR
283 if (meta.getCreationDate() == null
284 || meta.getCreationDate().trim().isEmpty()) {
285 meta.setCreationDate(bsHelper
286 .formatDate(StringUtils.fromTime(new Date().getTime())));
0ffa4754
NR
287 }
288 story.setMeta(meta);
920af1c7 289 pg.put("meta", meta);
ed08c171 290
0ffa4754 291 pg.setProgress(50);
08fe2e33 292
0ffa4754 293 if (meta.getCover() == null) {
8d59ce07 294 meta.setCover(bsHelper.getDefaultCover(meta.getSubject()));
0ffa4754 295 }
08fe2e33 296
0ffa4754 297 pg.setProgress(60);
a4143cd7 298
0ffa4754 299 if (getDesc) {
d66deb8d
NR
300 String descChapterName = Instance.getInstance().getTrans().getString(StringId.DESCRIPTION);
301 story.getMeta().setResume(bsPara.makeChapter(this, source, 0, descChapterName, //
302 getDesc(), isHtml(), null));
08fe2e33 303 }
0ffa4754 304
fdc55375 305 pg.done();
0ffa4754 306 return story;
08fe2e33
NR
307 }
308
5cf61f35
NR
309 /**
310 * Utility method to convert the given URL into a JSON object.
311 * <p>
312 * Note that this method expects small JSON files (everything is copied into
313 * memory at least twice).
314 *
315 * @param url
316 * the URL to parse
317 * @param stable
318 * TRUE for more stable resources, FALSE when they often change
319 *
320 * @return the JSON object
321 *
322 * @throws IOException
323 * in case of I/O error
324 */
325 protected JSONObject getJson(String url, boolean stable)
326 throws IOException {
327 try {
328 return getJson(new URL(url), stable);
329 } catch (MalformedURLException e) {
330 throw new IOException("Malformed URL: " + url, e);
331 }
332 }
333
334 /**
335 * Utility method to convert the given URL into a JSON object.
336 * <p>
337 * Note that this method expects small JSON files (everything is copied into
338 * memory at least twice).
339 *
340 * @param url
341 * the URL to parse
342 * @param stable
343 * TRUE for more stable resources, FALSE when they often change
344 *
345 * @return the JSON object
346 *
347 * @throws IOException
348 * in case of I/O error
349 */
350 protected JSONObject getJson(URL url, boolean stable) throws IOException {
351 InputStream in = Instance.getInstance().getCache().open(url, null,
352 stable);
353 try {
354 Scanner scan = new Scanner(in);
355 scan.useDelimiter("\0");
356 try {
728191e8
NR
357 if(!scan.hasNext())
358 throw new IOException("The given input was empty");
359
5cf61f35 360 return new JSONObject(scan.next());
8831d290
NR
361 } catch (JSONException e) {
362 throw new IOException(e);
5cf61f35
NR
363 } finally {
364 scan.close();
365 }
366 } finally {
367 in.close();
368 }
369 }
370
9005532f 371 /**
826e4569
NR
372 * Process the given story resource into a fully filled {@link Story}
373 * object.
9005532f
NR
374 *
375 * @param pg
376 * the optional progress reporter
377 *
378 * @return the {@link Story}, never NULL
379 *
380 * @throws IOException
381 * in case of I/O error
382 */
6569afb4 383 // TODO: ADD final when BasicSupport_Deprecated is gone
9005532f
NR
384 public Story process(Progress pg) throws IOException {
385 setCurrentReferer(source);
386 login();
387 sourceNode = loadDocument(source);
388
389 try {
75a6a3ea
NR
390 Story story = doProcess(pg);
391
392 // Check for "no chapters" stories
393 if (story.getChapters().isEmpty()
394 && story.getMeta().getResume() != null
395 && !story.getMeta().getResume().getParagraphs().isEmpty()) {
396 Chapter resume = story.getMeta().getResume();
397 resume.setName("");
398 resume.setNumber(1);
399 story.getChapters().add(resume);
5d190880 400 story.getMeta().setWords(resume.getWords());
75a6a3ea
NR
401
402 String descChapterName = Instance.getInstance().getTrans()
403 .getString(StringId.DESCRIPTION);
404 resume = new Chapter(0, descChapterName);
405 story.getMeta().setResume(resume);
406 }
407
408 return story;
9005532f
NR
409 } finally {
410 close();
411 }
412 }
413
08fe2e33 414 /**
826e4569
NR
415 * Actual processing step, without the calls to other methods.
416 * <p>
417 * Will convert the story resource into a fully filled {@link Story} object.
08fe2e33 418 *
92fb0719
NR
419 * @param pg
420 * the optional progress reporter
08fe2e33 421 *
776ad3c6 422 * @return the {@link Story}, never NULL
08fe2e33
NR
423 *
424 * @throws IOException
425 * in case of I/O error
426 */
826e4569 427 protected Story doProcess(Progress pg) throws IOException {
92fb0719
NR
428 if (pg == null) {
429 pg = new Progress();
430 } else {
431 pg.setMinMax(0, 100);
432 }
3b039231
NR
433
434 pg.setName("Initialising");
92fb0719 435
92fb0719 436 pg.setProgress(1);
9005532f
NR
437 Progress pgMeta = new Progress();
438 pg.addProgress(pgMeta, 10);
439 Story story = processMeta(true, pgMeta);
68328e17 440 pgMeta.done(); // 10%
920af1c7 441 pg.put("meta", story.getMeta());
ed08c171 442
9005532f
NR
443 Progress pgGetChapters = new Progress();
444 pg.addProgress(pgGetChapters, 10);
445 story.setChapters(new ArrayList<Chapter>());
446 List<Entry<String, URL>> chapters = getChapters(pgGetChapters);
68328e17 447 pgGetChapters.done(); // 20%
5d190880 448
9005532f
NR
449 if (chapters != null) {
450 Progress pgChaps = new Progress("Extracting chapters", 0,
451 chapters.size() * 300);
452 pg.addProgress(pgChaps, 80);
453
454 long words = 0;
455 int i = 1;
456 for (Entry<String, URL> chap : chapters) {
457 pgChaps.setName("Extracting chapter " + i);
458 URL chapUrl = chap.getValue();
459 String chapName = chap.getKey();
460 if (chapUrl != null) {
461 setCurrentReferer(chapUrl);
462 }
463
464 pgChaps.setProgress(i * 100);
465 Progress pgGetChapterContent = new Progress();
466 Progress pgMakeChapter = new Progress();
467 pgChaps.addProgress(pgGetChapterContent, 100);
468 pgChaps.addProgress(pgMakeChapter, 100);
469
470 String content = getChapterContent(chapUrl, i,
471 pgGetChapterContent);
68328e17 472 pgGetChapterContent.done();
8d59ce07 473 Chapter cc = bsPara.makeChapter(this, chapUrl, i,
9005532f 474 chapName, content, isHtml(), pgMakeChapter);
68328e17 475 pgMakeChapter.done();
ed08c171 476
9005532f
NR
477 words += cc.getWords();
478 story.getChapters().add(cc);
9005532f
NR
479
480 i++;
08fe2e33 481 }
5d190880
NR
482
483 story.getMeta().setWords(words);
08fe2e33 484
9005532f 485 pgChaps.setName("Extracting chapters");
fdc55375 486 pgChaps.done();
08fe2e33 487 }
9005532f 488
68328e17
NR
489 pg.done();
490
9005532f 491 return story;
08fe2e33
NR
492 }
493
99d71bd7
NR
494 /**
495 * Create a chapter from the given data.
496 *
497 * @param source
498 * the source URL for this content, which can be used to try and
499 * find images if images are present in the format [image-url]
500 * @param number
501 * the chapter number (0 = description)
502 * @param name
503 * the chapter name
504 * @param content
505 * the content of the chapter
75a6a3ea
NR
506 *
507 * @return the {@link Chapter}, never NULL
99d71bd7
NR
508 *
509 * @throws IOException
510 * in case of I/O error
511 */
512 public Chapter makeChapter(URL source, int number, String name,
513 String content) throws IOException {
8d59ce07 514 return bsPara.makeChapter(this, source, number, name,
99d71bd7
NR
515 content, isHtml(), null);
516 }
517
08fe2e33 518 /**
0ffa4754
NR
519 * Return a {@link BasicSupport} implementation supporting the given
520 * resource if possible.
08fe2e33 521 *
0ffa4754
NR
522 * @param url
523 * the story resource
08fe2e33 524 *
0ffa4754 525 * @return an implementation that supports it, or NULL
08fe2e33 526 */
0ffa4754
NR
527 public static BasicSupport getSupport(URL url) {
528 if (url == null) {
529 return null;
530 }
08fe2e33 531
0ffa4754
NR
532 // TEXT and INFO_TEXT always support files (not URLs though)
533 for (SupportType type : SupportType.values()) {
534 if (type != SupportType.TEXT && type != SupportType.INFO_TEXT) {
535 BasicSupport support = getSupport(type, url);
536 if (support != null && support.supports(url)) {
537 return support;
538 }
539 }
540 }
08fe2e33 541
0ffa4754
NR
542 for (SupportType type : new SupportType[] { SupportType.INFO_TEXT,
543 SupportType.TEXT }) {
544 BasicSupport support = getSupport(type, url);
545 if (support != null && support.supports(url)) {
546 return support;
547 }
548 }
549
550 return null;
08fe2e33
NR
551 }
552
553 /**
0ffa4754 554 * Return a {@link BasicSupport} implementation supporting the given type.
08fe2e33 555 *
0ffa4754 556 * @param type
99d71bd7 557 * the type, must not be NULL
0ffa4754
NR
558 * @param url
559 * the {@link URL} to support (can be NULL to get an
727108fe
NR
560 * "abstract support"; if not NULL, will be used as the source
561 * URL)
08fe2e33 562 *
0ffa4754 563 * @return an implementation that supports it, or NULL
08fe2e33 564 */
0ffa4754
NR
565 public static BasicSupport getSupport(SupportType type, URL url) {
566 BasicSupport support = null;
08fe2e33 567
08fe2e33
NR
568 switch (type) {
569 case EPUB:
0ffa4754
NR
570 support = new Epub();
571 break;
08fe2e33 572 case INFO_TEXT:
0ffa4754
NR
573 support = new InfoText();
574 break;
08fe2e33 575 case FIMFICTION:
315f14ae
NR
576 try {
577 // Can fail if no client key or NO in options
0ffa4754 578 support = new FimfictionApi();
315f14ae 579 } catch (IOException e) {
0ffa4754 580 support = new Fimfiction();
315f14ae 581 }
0ffa4754 582 break;
08fe2e33 583 case FANFICTION:
0ffa4754
NR
584 support = new Fanfiction();
585 break;
08fe2e33 586 case TEXT:
0ffa4754
NR
587 support = new Text();
588 break;
413bcc29
NR
589 case MANGAHUB:
590 support = new MangaHub();
0ffa4754 591 break;
08fe2e33 592 case E621:
0ffa4754
NR
593 support = new E621();
594 break;
a4143cd7 595 case YIFFSTAR:
0ffa4754
NR
596 support = new YiffStar();
597 break;
f0608ab1 598 case E_HENTAI:
0ffa4754
NR
599 support = new EHentai();
600 break;
af1f506f
NR
601 case MANGA_LEL:
602 support = new MangaLel();
603 break;
08fe2e33 604 case CBZ:
0ffa4754
NR
605 support = new Cbz();
606 break;
373da363 607 case HTML:
0ffa4754
NR
608 support = new Html();
609 break;
68686a37
NR
610 }
611
0ffa4754
NR
612 if (support != null) {
613 support.setType(type);
614 support.source = support.getCanonicalUrl(url);
315f14ae
NR
615 }
616
0ffa4754 617 return support;
315f14ae 618 }
08fe2e33 619}