1 package be
.nikiroo
.fanfix
.supported
;
3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
6 import java
.util
.ArrayList
;
8 import java
.util
.HashMap
;
11 import java
.util
.Map
.Entry
;
13 import org
.jsoup
.helper
.DataUtil
;
14 import org
.jsoup
.nodes
.Document
;
15 import org
.jsoup
.nodes
.Element
;
16 import org
.jsoup
.nodes
.Node
;
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
;
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.
31 * It will be used with 'resources' (usually web pages or files).
35 public abstract class BasicSupport
{
36 private Document sourceNode
;
38 private SupportType type
;
39 private URL currentReferer
; // with only one 'r', as in 'HTTP'...
41 static protected BasicSupportHelper bsHelper
= new BasicSupportHelper();
42 static protected BasicSupportImages bsImages
= new BasicSupportImages();
43 static protected BasicSupportPara bsPara
= new BasicSupportPara(new BasicSupportHelper(), new BasicSupportImages());
46 * Check if the given resource is supported by this {@link BasicSupport}.
49 * the resource to check for
51 * @return TRUE if it is
53 protected abstract boolean supports(URL url
);
56 * Return TRUE if the support will return HTML encoded content values for
57 * the chapters content.
59 * @return TRUE for HTML
61 protected abstract boolean isHtml();
64 * Return the {@link MetaData} of this story.
66 * @return the associated {@link MetaData}, never NULL
69 * in case of I/O error
71 protected abstract MetaData
getMeta() throws IOException
;
74 * Return the story description.
76 * @return the description
79 * in case of I/O error
81 protected abstract String
getDesc() throws IOException
;
84 * Return the list of chapters (name and resource).
86 * Can be NULL if this {@link BasicSupport} do no use chapters.
89 * the optional progress reporter
91 * @return the chapters or NULL
94 * in case of I/O error
96 protected abstract List
<Entry
<String
, URL
>> getChapters(Progress pg
)
100 * Return the content of the chapter (possibly HTML encoded, if
101 * {@link BasicSupport#isHtml()} is TRUE).
104 * the chapter {@link URL}
108 * the optional progress reporter
110 * @return the content
112 * @throws IOException
113 * in case of I/O error
115 protected abstract String
getChapterContent(URL chapUrl
, int number
,
116 Progress pg
) throws IOException
;
119 * Return the list of cookies (values included) that must be used to
120 * correctly fetch the resources.
122 * You are expected to call the super method implementation if you override
125 * @return the cookies
127 public Map
<String
, String
> getCookies() {
128 return new HashMap
<String
, String
>();
132 * OAuth authorisation (aka, "bearer XXXXXXX").
134 * @return the OAuth string
136 public String
getOAuth() {
141 * Return the canonical form of the main {@link URL}.
144 * the source {@link URL}, which can be NULL
146 * @return the canonical form of this {@link URL} or NULL if the source was
149 protected URL
getCanonicalUrl(URL source
) {
154 * The main {@link Node} for this {@link Story}.
158 protected Element
getSourceNode() {
163 * The main {@link URL} for this {@link Story}.
167 protected URL
getSource() {
172 * The current referer {@link URL} (only one 'r', as in 'HTML'...), i.e.,
173 * the current {@link URL} we work on.
175 * @return the referer
177 public URL
getCurrentReferer() {
178 return currentReferer
;
182 * The current referer {@link URL} (only one 'r', as in 'HTML'...), i.e.,
183 * the current {@link URL} we work on.
185 * @param currentReferer
188 protected void setCurrentReferer(URL currentReferer
) {
189 this.currentReferer
= currentReferer
;
197 public SupportType
getType() {
207 protected void setType(SupportType type
) {
212 * Open an input link that will be used for the support.
214 * Can return NULL, in which case you are supposed to work without a source
218 * the source {@link URL}
220 * @return the {@link InputStream}
222 * @throws IOException
223 * in case of I/O error
225 protected Document
loadDocument(URL source
) throws IOException
{
226 String url
= getCanonicalUrl(source
).toString();
227 return DataUtil
.load(Instance
.getCache().open(source
, this, false),
228 "UTF-8", url
.toString());
232 * Log into the support (can be a no-op depending upon the support).
234 * @throws IOException
235 * in case of I/O error
237 protected void login() throws IOException
{
241 * Now that we have processed the {@link Story}, close the resources if any.
243 protected void close() {
244 setCurrentReferer(null);
248 * Process the given story resource into a partially filled {@link Story}
249 * object containing the name and metadata.
252 * retrieve the description of the story, or not
254 * the optional progress reporter
256 * @return the {@link Story}, never NULL
258 * @throws IOException
259 * in case of I/O error
261 protected Story
processMeta(boolean getDesc
, Progress pg
)
266 pg
.setMinMax(0, 100);
271 Story story
= new Story();
272 MetaData meta
= getMeta();
273 if (meta
.getCreationDate() == null || meta
.getCreationDate().isEmpty()) {
274 meta
.setCreationDate(StringUtils
.fromTime(new Date().getTime()));
280 if (meta
.getCover() == null) {
281 meta
.setCover(bsHelper
.getDefaultCover(meta
.getSubject()));
287 String descChapterName
= Instance
.getTrans().getString(
288 StringId
.DESCRIPTION
);
289 story
.getMeta().setResume(
290 bsPara
.makeChapter(this, source
, 0,
292 getDesc(), isHtml(), null));
300 * Process the given story resource into a fully filled {@link Story}
304 * the optional progress reporter
306 * @return the {@link Story}, never NULL
308 * @throws IOException
309 * in case of I/O error
311 // TODO: ADD final when BasicSupport_Deprecated is gone
312 public Story
process(Progress pg
) throws IOException
{
313 setCurrentReferer(source
);
315 sourceNode
= loadDocument(source
);
318 return doProcess(pg
);
325 * Actual processing step, without the calls to other methods.
327 * Will convert the story resource into a fully filled {@link Story} object.
330 * the optional progress reporter
332 * @return the {@link Story}, never NULL
334 * @throws IOException
335 * in case of I/O error
337 protected Story
doProcess(Progress pg
) throws IOException
{
341 pg
.setMinMax(0, 100);
345 Progress pgMeta
= new Progress();
346 pg
.addProgress(pgMeta
, 10);
347 Story story
= processMeta(true, pgMeta
);
348 pgMeta
.done(); // 10%
350 pg
.setName("Retrieving " + story
.getMeta().getTitle());
352 Progress pgGetChapters
= new Progress();
353 pg
.addProgress(pgGetChapters
, 10);
354 story
.setChapters(new ArrayList
<Chapter
>());
355 List
<Entry
<String
, URL
>> chapters
= getChapters(pgGetChapters
);
356 pgGetChapters
.done(); // 20%
358 if (chapters
!= null) {
359 Progress pgChaps
= new Progress("Extracting chapters", 0,
360 chapters
.size() * 300);
361 pg
.addProgress(pgChaps
, 80);
365 for (Entry
<String
, URL
> chap
: chapters
) {
366 pgChaps
.setName("Extracting chapter " + i
);
367 URL chapUrl
= chap
.getValue();
368 String chapName
= chap
.getKey();
369 if (chapUrl
!= null) {
370 setCurrentReferer(chapUrl
);
373 pgChaps
.setProgress(i
* 100);
374 Progress pgGetChapterContent
= new Progress();
375 Progress pgMakeChapter
= new Progress();
376 pgChaps
.addProgress(pgGetChapterContent
, 100);
377 pgChaps
.addProgress(pgMakeChapter
, 100);
379 String content
= getChapterContent(chapUrl
, i
,
380 pgGetChapterContent
);
381 pgGetChapterContent
.done();
382 Chapter cc
= bsPara
.makeChapter(this, chapUrl
, i
,
383 chapName
, content
, isHtml(), pgMakeChapter
);
384 pgMakeChapter
.done();
386 words
+= cc
.getWords();
387 story
.getChapters().add(cc
);
388 story
.getMeta().setWords(words
);
393 pgChaps
.setName("Extracting chapters");
403 * Create a chapter from the given data.
406 * the source URL for this content, which can be used to try and
407 * find images if images are present in the format [image-url]
409 * the chapter number (0 = description)
413 * the content of the chapter
414 * @return the {@link Chapter}
416 * @throws IOException
417 * in case of I/O error
419 public Chapter
makeChapter(URL source
, int number
, String name
,
420 String content
) throws IOException
{
421 return bsPara
.makeChapter(this, source
, number
, name
,
422 content
, isHtml(), null);
426 * Return a {@link BasicSupport} implementation supporting the given
427 * resource if possible.
432 * @return an implementation that supports it, or NULL
434 public static BasicSupport
getSupport(URL url
) {
439 // TEXT and INFO_TEXT always support files (not URLs though)
440 for (SupportType type
: SupportType
.values()) {
441 if (type
!= SupportType
.TEXT
&& type
!= SupportType
.INFO_TEXT
) {
442 BasicSupport support
= getSupport(type
, url
);
443 if (support
!= null && support
.supports(url
)) {
449 for (SupportType type
: new SupportType
[] { SupportType
.INFO_TEXT
,
450 SupportType
.TEXT
}) {
451 BasicSupport support
= getSupport(type
, url
);
452 if (support
!= null && support
.supports(url
)) {
461 * Return a {@link BasicSupport} implementation supporting the given type.
464 * the type, must not be NULL
466 * the {@link URL} to support (can be NULL to get an
467 * "abstract support"; if not NULL, will be used as the source
470 * @return an implementation that supports it, or NULL
472 public static BasicSupport
getSupport(SupportType type
, URL url
) {
473 BasicSupport support
= null;
477 support
= new Epub();
480 support
= new InfoText();
484 // Can fail if no client key or NO in options
485 support
= new FimfictionApi();
486 } catch (IOException e
) {
487 support
= new Fimfiction();
491 support
= new Fanfiction();
494 support
= new Text();
497 support
= new MangaFox();
500 support
= new E621();
503 support
= new YiffStar();
506 support
= new EHentai();
509 support
= new MangaLel();
515 support
= new Html();
519 if (support
!= null) {
520 support
.setType(type
);
521 support
.source
= support
.getCanonicalUrl(url
);