improve progress code
[fanfix.git] / src / be / nikiroo / fanfix / supported / BasicSupport.java
1 package be.nikiroo.fanfix.supported;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.net.URL;
6 import java.util.ArrayList;
7 import java.util.Date;
8 import java.util.HashMap;
9 import java.util.List;
10 import java.util.Map;
11 import java.util.Map.Entry;
12
13 import org.jsoup.helper.DataUtil;
14 import org.jsoup.nodes.Document;
15 import org.jsoup.nodes.Element;
16 import org.jsoup.nodes.Node;
17
18 import be.nikiroo.fanfix.Instance;
19 import be.nikiroo.fanfix.bundles.StringId;
20 import be.nikiroo.fanfix.data.Chapter;
21 import be.nikiroo.fanfix.data.MetaData;
22 import be.nikiroo.fanfix.data.Story;
23 import be.nikiroo.utils.Progress;
24 import 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 */
35 public abstract class BasicSupport {
36 private Document sourceNode;
37 private URL source;
38 private SupportType type;
39 private URL currentReferer; // with only one 'r', as in 'HTTP'...
40
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
66 /**
67 * Return the {@link MetaData} of this story.
68 *
69 * @return the associated {@link MetaData}, never NULL
70 *
71 * @throws IOException
72 * in case of I/O error
73 */
74 protected abstract MetaData getMeta() throws IOException;
75
76 /**
77 * Return the story description.
78 *
79 * @return the description
80 *
81 * @throws IOException
82 * in case of I/O error
83 */
84 protected abstract String getDesc() throws IOException;
85
86 /**
87 * Return the list of chapters (name and resource).
88 * <p>
89 * Can be NULL if this {@link BasicSupport} do no use chapters.
90 *
91 * @param pg
92 * the optional progress reporter
93 *
94 * @return the chapters or NULL
95 *
96 * @throws IOException
97 * in case of I/O error
98 */
99 protected abstract List<Entry<String, URL>> getChapters(Progress pg)
100 throws IOException;
101
102 /**
103 * Return the content of the chapter (possibly HTML encoded, if
104 * {@link BasicSupport#isHtml()} is TRUE).
105 *
106 * @param chapUrl
107 * the chapter {@link URL}
108 * @param number
109 * the chapter number
110 * @param pg
111 * the optional progress reporter
112 *
113 * @return the content
114 *
115 * @throws IOException
116 * in case of I/O error
117 */
118 protected abstract String getChapterContent(URL chapUrl, int number,
119 Progress pg) throws IOException;
120
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 */
130 public Map<String, String> getCookies() {
131 return new HashMap<String, String>();
132 }
133
134 /**
135 * OAuth authorisation (aka, "bearer XXXXXXX").
136 *
137 * @return the OAuth string
138 */
139 public String getOAuth() {
140 return null;
141 }
142
143 /**
144 * Return the canonical form of the main {@link URL}.
145 *
146 * @param source
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>
217 * Can return NULL, in which case you are supposed to work without a source
218 * node.
219 *
220 * @param source
221 * the source {@link URL}
222 *
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).
236 *
237 * @throws IOException
238 * in case of I/O error
239 */
240 protected void login() throws IOException {
241 }
242
243 /**
244 * Now that we have processed the {@link Story}, close the resources if any.
245 */
246 protected void close() {
247 setCurrentReferer(null);
248 }
249
250 /**
251 * Process the given story resource into a partially filled {@link Story}
252 * object containing the name and metadata.
253 *
254 * @param getDesc
255 * retrieve the description of the story, or not
256 * @param pg
257 * the optional progress reporter
258 *
259 * @return the {@link Story}, never NULL
260 *
261 * @throws IOException
262 * in case of I/O error
263 */
264 protected Story processMeta(boolean getDesc, Progress pg)
265 throws IOException {
266 if (pg == null) {
267 pg = new Progress();
268 } else {
269 pg.setMinMax(0, 100);
270 }
271
272 pg.setProgress(30);
273
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);
280
281 pg.setProgress(50);
282
283 if (meta.getCover() == null) {
284 meta.setCover(BasicSupportHelper.getDefaultCover(meta.getSubject()));
285 }
286
287 pg.setProgress(60);
288
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));
296 }
297
298 pg.setProgress(100);
299 return story;
300 }
301
302 /**
303 * Process the given story resource into a fully filled {@link Story}
304 * object.
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 */
314 // ADD final when BasicSupport_Deprecated is gone
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
327 /**
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.
331 *
332 * @param pg
333 * the optional progress reporter
334 *
335 * @return the {@link Story}, never NULL
336 *
337 * @throws IOException
338 * in case of I/O error
339 */
340 protected Story doProcess(Progress pg) throws IOException {
341 if (pg == null) {
342 pg = new Progress();
343 } else {
344 pg.setMinMax(0, 100);
345 }
346
347 pg.setProgress(1);
348 Progress pgMeta = new Progress();
349 pg.addProgress(pgMeta, 10);
350 Story story = processMeta(true, pgMeta);
351 pgMeta.done(); // 10%
352
353 pg.setName("Retrieving " + story.getMeta().getTitle());
354
355 Progress pgGetChapters = new Progress();
356 pg.addProgress(pgGetChapters, 10);
357 story.setChapters(new ArrayList<Chapter>());
358 List<Entry<String, URL>> chapters = getChapters(pgGetChapters);
359 pgGetChapters.done(); // 20%
360
361 if (chapters != null) {
362 Progress pgChaps = new Progress("Extracting chapters", 0,
363 chapters.size() * 300);
364 pg.addProgress(pgChaps, 80);
365
366 long words = 0;
367 int i = 1;
368 for (Entry<String, URL> chap : chapters) {
369 pgChaps.setName("Extracting chapter " + i);
370 URL chapUrl = chap.getValue();
371 String chapName = chap.getKey();
372 if (chapUrl != null) {
373 setCurrentReferer(chapUrl);
374 }
375
376 pgChaps.setProgress(i * 100);
377 Progress pgGetChapterContent = new Progress();
378 Progress pgMakeChapter = new Progress();
379 pgChaps.addProgress(pgGetChapterContent, 100);
380 pgChaps.addProgress(pgMakeChapter, 100);
381
382 String content = getChapterContent(chapUrl, i,
383 pgGetChapterContent);
384 pgGetChapterContent.done();
385 Chapter cc = BasicSupportPara.makeChapter(this, chapUrl, i,
386 chapName, content, isHtml(), pgMakeChapter);
387 pgMakeChapter.done();
388
389 words += cc.getWords();
390 story.getChapters().add(cc);
391 story.getMeta().setWords(words);
392
393 i++;
394 }
395
396 pgChaps.setName("Extracting chapters");
397 }
398
399 pg.done();
400
401 return story;
402 }
403
404 /**
405 * Return a {@link BasicSupport} implementation supporting the given
406 * resource if possible.
407 *
408 * @param url
409 * the story resource
410 *
411 * @return an implementation that supports it, or NULL
412 */
413 public static BasicSupport getSupport(URL url) {
414 if (url == null) {
415 return null;
416 }
417
418 // TEXT and INFO_TEXT always support files (not URLs though)
419 for (SupportType type : SupportType.values()) {
420 if (type != SupportType.TEXT && type != SupportType.INFO_TEXT) {
421 BasicSupport support = getSupport(type, url);
422 if (support != null && support.supports(url)) {
423 return support;
424 }
425 }
426 }
427
428 for (SupportType type : new SupportType[] { SupportType.INFO_TEXT,
429 SupportType.TEXT }) {
430 BasicSupport support = getSupport(type, url);
431 if (support != null && support.supports(url)) {
432 return support;
433 }
434 }
435
436 return null;
437 }
438
439 /**
440 * Return a {@link BasicSupport} implementation supporting the given type.
441 *
442 * @param type
443 * the type
444 * @param url
445 * the {@link URL} to support (can be NULL to get an
446 * "abstract support")
447 *
448 * @return an implementation that supports it, or NULL
449 */
450 public static BasicSupport getSupport(SupportType type, URL url) {
451 BasicSupport support = null;
452
453 switch (type) {
454 case EPUB:
455 support = new Epub();
456 break;
457 case INFO_TEXT:
458 support = new InfoText();
459 break;
460 case FIMFICTION:
461 try {
462 // Can fail if no client key or NO in options
463 support = new FimfictionApi();
464 } catch (IOException e) {
465 support = new Fimfiction();
466 }
467 break;
468 case FANFICTION:
469 support = new Fanfiction();
470 break;
471 case TEXT:
472 support = new Text();
473 break;
474 case MANGAFOX:
475 support = new MangaFox();
476 break;
477 case E621:
478 support = new E621();
479 break;
480 case YIFFSTAR:
481 support = new YiffStar();
482 break;
483 case E_HENTAI:
484 support = new EHentai();
485 break;
486 case MANGA_LEL:
487 support = new MangaLel();
488 break;
489 case CBZ:
490 support = new Cbz();
491 break;
492 case HTML:
493 support = new Html();
494 break;
495 }
496
497 if (support != null) {
498 support.setType(type);
499 support.source = support.getCanonicalUrl(url);
500 }
501
502 return support;
503 }
504 }