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