WIP: convert local "supports" to new BasicSupport
[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 /**
0ffa4754
NR
87 * Return the list of chapters (name and resource). *
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
243 /**
244 * Prepare the support if needed before processing.
245 *
246 * @throws IOException
247 * on I/O error
248 */
0ffa4754
NR
249 protected void preprocess() throws IOException {
250 }
251
252 /**
253 * Now that we have processed the {@link Story}, close the resources if any.
254 */
255 protected void close() {
256 setCurrentReferer(null);
a4143cd7
NR
257 }
258
08fe2e33
NR
259 /**
260 * Process the given story resource into a partially filled {@link Story}
261 * object containing the name and metadata, except for the description.
262 *
08fe2e33
NR
263 * @return the {@link Story}
264 *
265 * @throws IOException
266 * in case of I/O error
267 */
0ffa4754
NR
268 public Story processMeta() throws IOException {
269 Story story = null;
270
271 preprocess();
272 try {
273 story = processMeta(false, null);
274 } finally {
275 close();
276 }
277
278 return story;
08fe2e33
NR
279 }
280
281 /**
282 * Process the given story resource into a partially filled {@link Story}
283 * object containing the name and metadata.
284 *
0efd25e3
NR
285 * @param getDesc
286 * retrieve the description of the story, or not
ed08c171
NR
287 * @param pg
288 * the optional progress reporter
08fe2e33 289 *
776ad3c6 290 * @return the {@link Story}, never NULL
08fe2e33
NR
291 *
292 * @throws IOException
293 * in case of I/O error
294 */
0ffa4754
NR
295 protected Story processMeta(boolean getDesc, Progress pg)
296 throws IOException {
ed08c171
NR
297 if (pg == null) {
298 pg = new Progress();
299 } else {
300 pg.setMinMax(0, 100);
301 }
302
0ffa4754 303 pg.setProgress(30);
ed08c171 304
0ffa4754
NR
305 Story story = new Story();
306 MetaData meta = getMeta();
307 if (meta.getCreationDate() == null || meta.getCreationDate().isEmpty()) {
308 meta.setCreationDate(StringUtils.fromTime(new Date().getTime()));
309 }
310 story.setMeta(meta);
ed08c171 311
0ffa4754 312 pg.setProgress(50);
08fe2e33 313
0ffa4754
NR
314 if (meta.getCover() == null) {
315 meta.setCover(BasicSupportHelper.getDefaultCover(meta.getSubject()));
316 }
08fe2e33 317
0ffa4754 318 pg.setProgress(60);
a4143cd7 319
0ffa4754
NR
320 if (getDesc) {
321 String descChapterName = Instance.getTrans().getString(
322 StringId.DESCRIPTION);
323 story.getMeta().setResume(
324 BasicSupportPara.makeChapter(this, source, 0,
325 descChapterName, //
326 getDesc(), isHtml(), null));
08fe2e33 327 }
0ffa4754
NR
328
329 pg.setProgress(100);
330 return story;
08fe2e33
NR
331 }
332
333 /**
334 * Process the given story resource into a fully filled {@link Story}
335 * object.
336 *
92fb0719
NR
337 * @param pg
338 * the optional progress reporter
08fe2e33 339 *
776ad3c6 340 * @return the {@link Story}, never NULL
08fe2e33
NR
341 *
342 * @throws IOException
343 * in case of I/O error
344 */
0ffa4754 345 public Story process(Progress pg) throws IOException {
92fb0719
NR
346 if (pg == null) {
347 pg = new Progress();
348 } else {
349 pg.setMinMax(0, 100);
350 }
351
0ffa4754
NR
352 setCurrentReferer(source);
353 login();
354 sourceNode = loadDocument(source);
355
92fb0719 356 pg.setProgress(1);
08fe2e33 357 try {
ed08c171
NR
358 Progress pgMeta = new Progress();
359 pg.addProgress(pgMeta, 10);
0ffa4754
NR
360 preprocess();
361 Story story = processMeta(true, pgMeta);
ed08c171
NR
362 if (!pgMeta.isDone()) {
363 pgMeta.setProgress(pgMeta.getMax()); // 10%
364 }
365
754a5bc2
NR
366 pg.setName("Retrieving " + story.getMeta().getTitle());
367
ed08c171
NR
368 Progress pgGetChapters = new Progress();
369 pg.addProgress(pgGetChapters, 10);
08fe2e33 370 story.setChapters(new ArrayList<Chapter>());
0ffa4754 371 List<Entry<String, URL>> chapters = getChapters(pgGetChapters);
ed08c171
NR
372 if (!pgGetChapters.isDone()) {
373 pgGetChapters.setProgress(pgGetChapters.getMax()); // 20%
374 }
08fe2e33 375
08fe2e33 376 if (chapters != null) {
ed08c171
NR
377 Progress pgChaps = new Progress("Extracting chapters", 0,
378 chapters.size() * 300);
92fb0719
NR
379 pg.addProgress(pgChaps, 80);
380
793f1071 381 long words = 0;
ed08c171 382 int i = 1;
08fe2e33 383 for (Entry<String, URL> chap : chapters) {
ed08c171 384 pgChaps.setName("Extracting chapter " + i);
0ffa4754
NR
385 URL chapUrl = chap.getValue();
386 String chapName = chap.getKey();
387 if (chapUrl != null) {
388 setCurrentReferer(chapUrl);
315f14ae 389 }
ed08c171 390
0ffa4754
NR
391 pgChaps.setProgress(i * 100);
392 Progress pgGetChapterContent = new Progress();
393 Progress pgMakeChapter = new Progress();
394 pgChaps.addProgress(pgGetChapterContent, 100);
395 pgChaps.addProgress(pgMakeChapter, 100);
396
397 String content = getChapterContent(chapUrl, i,
398 pgGetChapterContent);
399 if (!pgGetChapterContent.isDone()) {
400 pgGetChapterContent.setProgress(pgGetChapterContent
401 .getMax());
402 }
ed08c171 403
0ffa4754
NR
404 Chapter cc = BasicSupportPara.makeChapter(this, chapUrl, i,
405 chapName, content, isHtml(), pgMakeChapter);
406 if (!pgMakeChapter.isDone()) {
407 pgMakeChapter.setProgress(pgMakeChapter.getMax());
08fe2e33 408 }
a6395bef 409
0ffa4754
NR
410 words += cc.getWords();
411 story.getChapters().add(cc);
412 story.getMeta().setWords(words);
413
ed08c171 414 i++;
08fe2e33 415 }
ed08c171
NR
416
417 pgChaps.setName("Extracting chapters");
92fb0719 418 } else {
ed08c171 419 pg.setProgress(80);
08fe2e33
NR
420 }
421
422 return story;
08fe2e33 423 } finally {
0ffa4754 424 close();
08fe2e33
NR
425 }
426 }
427
428 /**
0ffa4754
NR
429 * Return a {@link BasicSupport} implementation supporting the given
430 * resource if possible.
08fe2e33 431 *
0ffa4754
NR
432 * @param url
433 * the story resource
08fe2e33 434 *
0ffa4754 435 * @return an implementation that supports it, or NULL
08fe2e33 436 */
0ffa4754
NR
437 public static BasicSupport getSupport(URL url) {
438 if (url == null) {
439 return null;
440 }
08fe2e33 441
0ffa4754
NR
442 // TEXT and INFO_TEXT always support files (not URLs though)
443 for (SupportType type : SupportType.values()) {
444 if (type != SupportType.TEXT && type != SupportType.INFO_TEXT) {
445 BasicSupport support = getSupport(type, url);
446 if (support != null && support.supports(url)) {
447 return support;
448 }
449 }
450 }
08fe2e33 451
0ffa4754
NR
452 for (SupportType type : new SupportType[] { SupportType.INFO_TEXT,
453 SupportType.TEXT }) {
454 BasicSupport support = getSupport(type, url);
455 if (support != null && support.supports(url)) {
456 return support;
457 }
458 }
459
460 return null;
08fe2e33
NR
461 }
462
463 /**
0ffa4754 464 * Return a {@link BasicSupport} implementation supporting the given type.
08fe2e33 465 *
0ffa4754
NR
466 * @param type
467 * the type
468 * @param url
469 * the {@link URL} to support (can be NULL to get an
470 * "abstract support")
08fe2e33 471 *
0ffa4754 472 * @return an implementation that supports it, or NULL
08fe2e33 473 */
0ffa4754
NR
474 public static BasicSupport getSupport(SupportType type, URL url) {
475 BasicSupport support = null;
08fe2e33 476
08fe2e33
NR
477 switch (type) {
478 case EPUB:
0ffa4754
NR
479 support = new Epub();
480 break;
08fe2e33 481 case INFO_TEXT:
0ffa4754
NR
482 support = new InfoText();
483 break;
08fe2e33 484 case FIMFICTION:
315f14ae
NR
485 try {
486 // Can fail if no client key or NO in options
0ffa4754 487 support = new FimfictionApi();
315f14ae 488 } catch (IOException e) {
0ffa4754 489 support = new Fimfiction();
315f14ae 490 }
0ffa4754 491 break;
08fe2e33 492 case FANFICTION:
0ffa4754
NR
493 support = new Fanfiction();
494 break;
08fe2e33 495 case TEXT:
0ffa4754
NR
496 support = new Text();
497 break;
08fe2e33 498 case MANGAFOX:
0ffa4754
NR
499 support = new MangaFox();
500 break;
08fe2e33 501 case E621:
0ffa4754
NR
502 support = new E621();
503 break;
a4143cd7 504 case YIFFSTAR:
0ffa4754
NR
505 support = new YiffStar();
506 break;
f0608ab1 507 case E_HENTAI:
0ffa4754
NR
508 support = new EHentai();
509 break;
08fe2e33 510 case CBZ:
0ffa4754
NR
511 support = new Cbz();
512 break;
373da363 513 case HTML:
0ffa4754
NR
514 support = new Html();
515 break;
68686a37
NR
516 }
517
0ffa4754
NR
518 if (support != null) {
519 support.setType(type);
520 support.source = support.getCanonicalUrl(url);
315f14ae
NR
521 }
522
0ffa4754 523 return support;
315f14ae 524 }
08fe2e33 525}