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()));
279 if (meta
.getCover() == null) {
280 meta
.setCover(bsHelper
.getDefaultCover(meta
.getSubject()));
286 String descChapterName
= Instance
.getInstance().getTrans().getString(StringId
.DESCRIPTION
);
287 story
.getMeta().setResume(bsPara
.makeChapter(this, source
, 0, descChapterName
, //
288 getDesc(), isHtml(), null));
296 * Process the given story resource into a fully filled {@link Story}
300 * the optional progress reporter
302 * @return the {@link Story}, never NULL
304 * @throws IOException
305 * in case of I/O error
307 // TODO: ADD final when BasicSupport_Deprecated is gone
308 public Story
process(Progress pg
) throws IOException
{
309 setCurrentReferer(source
);
311 sourceNode
= loadDocument(source
);
314 return doProcess(pg
);
321 * Actual processing step, without the calls to other methods.
323 * Will convert the story resource into a fully filled {@link Story} object.
326 * the optional progress reporter
328 * @return the {@link Story}, never NULL
330 * @throws IOException
331 * in case of I/O error
333 protected Story
doProcess(Progress pg
) throws IOException
{
337 pg
.setMinMax(0, 100);
341 Progress pgMeta
= new Progress();
342 pg
.addProgress(pgMeta
, 10);
343 Story story
= processMeta(true, pgMeta
);
344 pgMeta
.done(); // 10%
346 pg
.setName("Retrieving " + story
.getMeta().getTitle());
348 Progress pgGetChapters
= new Progress();
349 pg
.addProgress(pgGetChapters
, 10);
350 story
.setChapters(new ArrayList
<Chapter
>());
351 List
<Entry
<String
, URL
>> chapters
= getChapters(pgGetChapters
);
352 pgGetChapters
.done(); // 20%
354 if (chapters
!= null) {
355 Progress pgChaps
= new Progress("Extracting chapters", 0,
356 chapters
.size() * 300);
357 pg
.addProgress(pgChaps
, 80);
361 for (Entry
<String
, URL
> chap
: chapters
) {
362 pgChaps
.setName("Extracting chapter " + i
);
363 URL chapUrl
= chap
.getValue();
364 String chapName
= chap
.getKey();
365 if (chapUrl
!= null) {
366 setCurrentReferer(chapUrl
);
369 pgChaps
.setProgress(i
* 100);
370 Progress pgGetChapterContent
= new Progress();
371 Progress pgMakeChapter
= new Progress();
372 pgChaps
.addProgress(pgGetChapterContent
, 100);
373 pgChaps
.addProgress(pgMakeChapter
, 100);
375 String content
= getChapterContent(chapUrl
, i
,
376 pgGetChapterContent
);
377 pgGetChapterContent
.done();
378 Chapter cc
= bsPara
.makeChapter(this, chapUrl
, i
,
379 chapName
, content
, isHtml(), pgMakeChapter
);
380 pgMakeChapter
.done();
382 words
+= cc
.getWords();
383 story
.getChapters().add(cc
);
384 story
.getMeta().setWords(words
);
389 pgChaps
.setName("Extracting chapters");
399 * Create a chapter from the given data.
402 * the source URL for this content, which can be used to try and
403 * find images if images are present in the format [image-url]
405 * the chapter number (0 = description)
409 * the content of the chapter
410 * @return the {@link Chapter}
412 * @throws IOException
413 * in case of I/O error
415 public Chapter
makeChapter(URL source
, int number
, String name
,
416 String content
) throws IOException
{
417 return bsPara
.makeChapter(this, source
, number
, name
,
418 content
, isHtml(), null);
422 * Return a {@link BasicSupport} implementation supporting the given
423 * resource if possible.
428 * @return an implementation that supports it, or NULL
430 public static BasicSupport
getSupport(URL url
) {
435 // TEXT and INFO_TEXT always support files (not URLs though)
436 for (SupportType type
: SupportType
.values()) {
437 if (type
!= SupportType
.TEXT
&& type
!= SupportType
.INFO_TEXT
) {
438 BasicSupport support
= getSupport(type
, url
);
439 if (support
!= null && support
.supports(url
)) {
445 for (SupportType type
: new SupportType
[] { SupportType
.INFO_TEXT
,
446 SupportType
.TEXT
}) {
447 BasicSupport support
= getSupport(type
, url
);
448 if (support
!= null && support
.supports(url
)) {
457 * Return a {@link BasicSupport} implementation supporting the given type.
460 * the type, must not be NULL
462 * the {@link URL} to support (can be NULL to get an
463 * "abstract support"; if not NULL, will be used as the source
466 * @return an implementation that supports it, or NULL
468 public static BasicSupport
getSupport(SupportType type
, URL url
) {
469 BasicSupport support
= null;
473 support
= new Epub();
476 support
= new InfoText();
480 // Can fail if no client key or NO in options
481 support
= new FimfictionApi();
482 } catch (IOException e
) {
483 support
= new Fimfiction();
487 support
= new Fanfiction();
490 support
= new Text();
493 support
= new MangaHub();
496 support
= new E621();
499 support
= new YiffStar();
502 support
= new EHentai();
505 support
= new MangaLel();
511 support
= new Html();
515 if (support
!= null) {
516 support
.setType(type
);
517 support
.source
= support
.getCanonicalUrl(url
);