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'...
42 * Check if the given resource is supported by this {@link BasicSupport}.
45 * the resource to check for
47 * @return TRUE if it is
49 protected abstract boolean supports(URL url
);
52 * Return TRUE if the support will return HTML encoded content values for
53 * the chapters content.
55 * @return TRUE for HTML
57 protected abstract boolean isHtml();
60 * Return the {@link MetaData} of this story.
62 * @return the associated {@link MetaData}, never NULL
65 * in case of I/O error
67 protected abstract MetaData
getMeta() throws IOException
;
70 * Return the story description.
72 * @return the description
75 * in case of I/O error
77 protected abstract String
getDesc() throws IOException
;
80 * Return the list of chapters (name and resource).
82 * Can be NULL if this {@link BasicSupport} do no use chapters.
85 * the optional progress reporter
87 * @return the chapters or NULL
90 * in case of I/O error
92 protected abstract List
<Entry
<String
, URL
>> getChapters(Progress pg
)
96 * Return the content of the chapter (possibly HTML encoded, if
97 * {@link BasicSupport#isHtml()} is TRUE).
100 * the chapter {@link URL}
104 * the optional progress reporter
106 * @return the content
108 * @throws IOException
109 * in case of I/O error
111 protected abstract String
getChapterContent(URL chapUrl
, int number
,
112 Progress pg
) throws IOException
;
115 * Return the list of cookies (values included) that must be used to
116 * correctly fetch the resources.
118 * You are expected to call the super method implementation if you override
121 * @return the cookies
123 public Map
<String
, String
> getCookies() {
124 return new HashMap
<String
, String
>();
128 * OAuth authorisation (aka, "bearer XXXXXXX").
130 * @return the OAuth string
132 public String
getOAuth() {
137 * Return the canonical form of the main {@link URL}.
140 * the source {@link URL}, which can be NULL
142 * @return the canonical form of this {@link URL} or NULL if the source was
145 protected URL
getCanonicalUrl(URL source
) {
150 * The main {@link Node} for this {@link Story}.
154 protected Element
getSourceNode() {
159 * The main {@link URL} for this {@link Story}.
163 protected URL
getSource() {
168 * The current referer {@link URL} (only one 'r', as in 'HTML'...), i.e.,
169 * the current {@link URL} we work on.
171 * @return the referer
173 public URL
getCurrentReferer() {
174 return currentReferer
;
178 * The current referer {@link URL} (only one 'r', as in 'HTML'...), i.e.,
179 * the current {@link URL} we work on.
181 * @param currentReferer
184 protected void setCurrentReferer(URL currentReferer
) {
185 this.currentReferer
= currentReferer
;
193 public SupportType
getType() {
203 protected void setType(SupportType type
) {
208 * Open an input link that will be used for the support.
210 * Can return NULL, in which case you are supposed to work without a source
214 * the source {@link URL}
216 * @return the {@link InputStream}
218 * @throws IOException
219 * in case of I/O error
221 protected Document
loadDocument(URL source
) throws IOException
{
222 String url
= getCanonicalUrl(source
).toString();
223 return DataUtil
.load(Instance
.getCache().open(source
, this, false),
224 "UTF-8", url
.toString());
228 * Log into the support (can be a no-op depending upon the support).
230 * @throws IOException
231 * in case of I/O error
233 protected void login() throws IOException
{
237 * Now that we have processed the {@link Story}, close the resources if any.
239 protected void close() {
240 setCurrentReferer(null);
244 * Process the given story resource into a partially filled {@link Story}
245 * object containing the name and metadata.
248 * retrieve the description of the story, or not
250 * the optional progress reporter
252 * @return the {@link Story}, never NULL
254 * @throws IOException
255 * in case of I/O error
257 protected Story
processMeta(boolean getDesc
, Progress pg
)
262 pg
.setMinMax(0, 100);
267 Story story
= new Story();
268 MetaData meta
= getMeta();
269 if (meta
.getCreationDate() == null || meta
.getCreationDate().isEmpty()) {
270 meta
.setCreationDate(StringUtils
.fromTime(new Date().getTime()));
276 if (meta
.getCover() == null) {
277 meta
.setCover(BasicSupportHelper
.getDefaultCover(meta
.getSubject()));
283 String descChapterName
= Instance
.getTrans().getString(
284 StringId
.DESCRIPTION
);
285 story
.getMeta().setResume(
286 BasicSupportPara
.makeChapter(this, source
, 0,
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 // 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
= BasicSupportPara
.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 BasicSupportPara
.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 MangaFox();
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
);