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 * The name of this support class.
46 protected abstract String
getSourceName();
49 * Check if the given resource is supported by this {@link BasicSupport}.
52 * the resource to check for
54 * @return TRUE if it is
56 protected abstract boolean supports(URL url
);
59 * Return TRUE if the support will return HTML encoded content values for
60 * the chapters content.
62 * @return TRUE for HTML
64 protected abstract boolean isHtml();
67 * Return the {@link MetaData} of this story.
69 * @return the associated {@link MetaData}, never NULL
72 * in case of I/O error
74 protected abstract MetaData
getMeta() throws IOException
;
77 * Return the story description.
79 * @return the description
82 * in case of I/O error
84 protected abstract String
getDesc() throws IOException
;
87 * Return the list of chapters (name and resource). *
89 * Can be NULL if this {@link BasicSupport} do no use chapters.
92 * the optional progress reporter
94 * @return the chapters or NULL
97 * in case of I/O error
99 protected abstract List
<Entry
<String
, URL
>> getChapters(Progress pg
)
103 * Return the content of the chapter (possibly HTML encoded, if
104 * {@link BasicSupport#isHtml()} is TRUE).
107 * the chapter {@link URL}
111 * the optional progress reporter
113 * @return the content
115 * @throws IOException
116 * in case of I/O error
118 protected abstract String
getChapterContent(URL chapUrl
, int number
,
119 Progress pg
) throws IOException
;
122 * Return the list of cookies (values included) that must be used to
123 * correctly fetch the resources.
125 * You are expected to call the super method implementation if you override
128 * @return the cookies
130 public Map
<String
, String
> getCookies() {
131 return new HashMap
<String
, String
>();
135 * OAuth authorisation (aka, "bearer XXXXXXX").
137 * @return the OAuth string
139 public String
getOAuth() {
144 * Return the canonical form of the main {@link URL}.
147 * the source {@link URL}, which can be NULL
149 * @return the canonical form of this {@link URL} or NULL if the source was
152 protected URL
getCanonicalUrl(URL source
) {
157 * The main {@link Node} for this {@link Story}.
161 protected Element
getSourceNode() {
166 * The main {@link URL} for this {@link Story}.
170 protected URL
getSource() {
175 * The current referer {@link URL} (only one 'r', as in 'HTML'...), i.e.,
176 * the current {@link URL} we work on.
178 * @return the referer
180 public URL
getCurrentReferer() {
181 return currentReferer
;
185 * The current referer {@link URL} (only one 'r', as in 'HTML'...), i.e.,
186 * the current {@link URL} we work on.
188 * @param currentReferer
191 protected void setCurrentReferer(URL currentReferer
) {
192 this.currentReferer
= currentReferer
;
200 public SupportType
getType() {
210 protected void setType(SupportType type
) {
215 * Open an input link that will be used for the support.
217 * Can return NULL, in which case you are supposed to work without a source
221 * the source {@link URL}
223 * @return the {@link InputStream}
225 * @throws IOException
226 * in case of I/O error
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());
235 * Log into the support (can be a no-op depending upon the support).
237 * @throws IOException
238 * in case of I/O error
240 protected void login() throws IOException
{
244 * Now that we have processed the {@link Story}, close the resources if any.
246 protected void close() {
247 setCurrentReferer(null);
251 * Process the given story resource into a partially filled {@link Story}
252 * object containing the name and metadata, except for the description.
254 * @return the {@link Story}
256 * @throws IOException
257 * in case of I/O error
259 public final Story
processMeta() throws IOException
{
263 story
= processMeta(false, null);
272 * Process the given story resource into a partially filled {@link Story}
273 * object containing the name and metadata.
276 * retrieve the description of the story, or not
278 * the optional progress reporter
280 * @return the {@link Story}, never NULL
282 * @throws IOException
283 * in case of I/O error
285 protected Story
processMeta(boolean getDesc
, Progress pg
)
290 pg
.setMinMax(0, 100);
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()));
304 if (meta
.getCover() == null) {
305 meta
.setCover(BasicSupportHelper
.getDefaultCover(meta
.getSubject()));
311 String descChapterName
= Instance
.getTrans().getString(
312 StringId
.DESCRIPTION
);
313 story
.getMeta().setResume(
314 BasicSupportPara
.makeChapter(this, source
, 0,
316 getDesc(), isHtml(), null));
324 * Actual processing step, without the calls to other methods.
326 * Will convert the story resource into a fully filled {@link Story} object.
329 * the optional progress reporter
331 * @return the {@link Story}, never NULL
333 * @throws IOException
334 * in case of I/O error
337 public Story
process(Progress pg
) throws IOException
{
338 setCurrentReferer(source
);
340 sourceNode
= loadDocument(source
);
343 return doProcess(pg
);
350 * Process the given story resource into a fully filled {@link Story}
354 * the optional progress reporter
356 * @return the {@link Story}, never NULL
358 * @throws IOException
359 * in case of I/O error
361 public Story
doProcess(Progress pg
) throws IOException
{
365 pg
.setMinMax(0, 100);
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%
376 pg
.setName("Retrieving " + story
.getMeta().getTitle());
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%
386 if (chapters
!= null) {
387 Progress pgChaps
= new Progress("Extracting chapters", 0,
388 chapters
.size() * 300);
389 pg
.addProgress(pgChaps
, 80);
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
);
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);
407 String content
= getChapterContent(chapUrl
, i
,
408 pgGetChapterContent
);
409 if (!pgGetChapterContent
.isDone()) {
410 pgGetChapterContent
.setProgress(pgGetChapterContent
414 Chapter cc
= BasicSupportPara
.makeChapter(this, chapUrl
, i
,
415 chapName
, content
, isHtml(), pgMakeChapter
);
416 if (!pgMakeChapter
.isDone()) {
417 pgMakeChapter
.setProgress(pgMakeChapter
.getMax());
420 words
+= cc
.getWords();
421 story
.getChapters().add(cc
);
422 story
.getMeta().setWords(words
);
427 pgChaps
.setName("Extracting chapters");
436 * Return a {@link BasicSupport} implementation supporting the given
437 * resource if possible.
442 * @return an implementation that supports it, or NULL
444 public static BasicSupport
getSupport(URL url
) {
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
)) {
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
)) {
471 * Return a {@link BasicSupport} implementation supporting the given type.
476 * the {@link URL} to support (can be NULL to get an
477 * "abstract support")
479 * @return an implementation that supports it, or NULL
481 public static BasicSupport
getSupport(SupportType type
, URL url
) {
482 BasicSupport support
= null;
486 support
= new Epub();
489 support
= new InfoText();
493 // Can fail if no client key or NO in options
494 support
= new FimfictionApi();
495 } catch (IOException e
) {
496 support
= new Fimfiction();
500 support
= new Fanfiction();
503 support
= new Text();
506 support
= new MangaFox();
509 support
= new E621();
512 support
= new YiffStar();
515 support
= new EHentai();
518 support
= new MangaLel();
524 support
= new Html();
528 if (support
!= null) {
529 support
.setType(type
);
530 support
.source
= support
.getCanonicalUrl(url
);