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
.getInstance().getCache().open(source
, this, false), "UTF-8", url
.toString());
231 * Log into the support (can be a no-op depending upon the support).
233 * @throws IOException
234 * in case of I/O error
236 protected void login() throws IOException
{
240 * Now that we have processed the {@link Story}, close the resources if any.
242 protected void close() {
243 setCurrentReferer(null);
247 * Process the given story resource into a partially filled {@link Story}
248 * object containing the name and metadata.
251 * retrieve the description of the story, or not
253 * the optional progress reporter
255 * @return the {@link Story}, never NULL
257 * @throws IOException
258 * in case of I/O error
260 protected Story
processMeta(boolean getDesc
, Progress pg
)
265 pg
.setMinMax(0, 100);
270 Story story
= new Story();
271 MetaData meta
= getMeta();
272 if (meta
.getCreationDate() == null || meta
.getCreationDate().isEmpty()) {
273 meta
.setCreationDate(StringUtils
.fromTime(new Date().getTime()));
276 pg
.put("meta", meta
);
280 if (meta
.getCover() == null) {
281 meta
.setCover(bsHelper
.getDefaultCover(meta
.getSubject()));
287 String descChapterName
= Instance
.getInstance().getTrans().getString(StringId
.DESCRIPTION
);
288 story
.getMeta().setResume(bsPara
.makeChapter(this, source
, 0, descChapterName
, //
289 getDesc(), isHtml(), null));
297 * Process the given story resource into a fully filled {@link Story}
301 * the optional progress reporter
303 * @return the {@link Story}, never NULL
305 * @throws IOException
306 * in case of I/O error
308 // TODO: ADD final when BasicSupport_Deprecated is gone
309 public Story
process(Progress pg
) throws IOException
{
310 setCurrentReferer(source
);
312 sourceNode
= loadDocument(source
);
315 return doProcess(pg
);
322 * Actual processing step, without the calls to other methods.
324 * Will convert the story resource into a fully filled {@link Story} object.
327 * the optional progress reporter
329 * @return the {@link Story}, never NULL
331 * @throws IOException
332 * in case of I/O error
334 protected Story
doProcess(Progress pg
) throws IOException
{
338 pg
.setMinMax(0, 100);
341 pg
.setName("Initialising");
344 Progress pgMeta
= new Progress();
345 pg
.addProgress(pgMeta
, 10);
346 Story story
= processMeta(true, pgMeta
);
347 pgMeta
.done(); // 10%
348 pg
.put("meta", story
.getMeta());
350 Progress pgGetChapters
= new Progress();
351 pg
.addProgress(pgGetChapters
, 10);
352 story
.setChapters(new ArrayList
<Chapter
>());
353 List
<Entry
<String
, URL
>> chapters
= getChapters(pgGetChapters
);
354 pgGetChapters
.done(); // 20%
356 if (chapters
!= null) {
357 Progress pgChaps
= new Progress("Extracting chapters", 0,
358 chapters
.size() * 300);
359 pg
.addProgress(pgChaps
, 80);
363 for (Entry
<String
, URL
> chap
: chapters
) {
364 pgChaps
.setName("Extracting chapter " + i
);
365 URL chapUrl
= chap
.getValue();
366 String chapName
= chap
.getKey();
367 if (chapUrl
!= null) {
368 setCurrentReferer(chapUrl
);
371 pgChaps
.setProgress(i
* 100);
372 Progress pgGetChapterContent
= new Progress();
373 Progress pgMakeChapter
= new Progress();
374 pgChaps
.addProgress(pgGetChapterContent
, 100);
375 pgChaps
.addProgress(pgMakeChapter
, 100);
377 String content
= getChapterContent(chapUrl
, i
,
378 pgGetChapterContent
);
379 pgGetChapterContent
.done();
380 Chapter cc
= bsPara
.makeChapter(this, chapUrl
, i
,
381 chapName
, content
, isHtml(), pgMakeChapter
);
382 pgMakeChapter
.done();
384 words
+= cc
.getWords();
385 story
.getChapters().add(cc
);
386 story
.getMeta().setWords(words
);
391 pgChaps
.setName("Extracting chapters");
401 * Create a chapter from the given data.
404 * the source URL for this content, which can be used to try and
405 * find images if images are present in the format [image-url]
407 * the chapter number (0 = description)
411 * the content of the chapter
412 * @return the {@link Chapter}
414 * @throws IOException
415 * in case of I/O error
417 public Chapter
makeChapter(URL source
, int number
, String name
,
418 String content
) throws IOException
{
419 return bsPara
.makeChapter(this, source
, number
, name
,
420 content
, isHtml(), null);
424 * Return a {@link BasicSupport} implementation supporting the given
425 * resource if possible.
430 * @return an implementation that supports it, or NULL
432 public static BasicSupport
getSupport(URL url
) {
437 // TEXT and INFO_TEXT always support files (not URLs though)
438 for (SupportType type
: SupportType
.values()) {
439 if (type
!= SupportType
.TEXT
&& type
!= SupportType
.INFO_TEXT
) {
440 BasicSupport support
= getSupport(type
, url
);
441 if (support
!= null && support
.supports(url
)) {
447 for (SupportType type
: new SupportType
[] { SupportType
.INFO_TEXT
,
448 SupportType
.TEXT
}) {
449 BasicSupport support
= getSupport(type
, url
);
450 if (support
!= null && support
.supports(url
)) {
459 * Return a {@link BasicSupport} implementation supporting the given type.
462 * the type, must not be NULL
464 * the {@link URL} to support (can be NULL to get an
465 * "abstract support"; if not NULL, will be used as the source
468 * @return an implementation that supports it, or NULL
470 public static BasicSupport
getSupport(SupportType type
, URL url
) {
471 BasicSupport support
= null;
475 support
= new Epub();
478 support
= new InfoText();
482 // Can fail if no client key or NO in options
483 support
= new FimfictionApi();
484 } catch (IOException e
) {
485 support
= new Fimfiction();
489 support
= new Fanfiction();
492 support
= new Text();
495 support
= new MangaHub();
498 support
= new E621();
501 support
= new YiffStar();
504 support
= new EHentai();
507 support
= new MangaLel();
513 support
= new Html();
517 if (support
!= null) {
518 support
.setType(type
);
519 support
.source
= support
.getCanonicalUrl(url
);