- protected Story processMeta(URL url, boolean close, boolean getDesc,
- Progress pg) throws IOException {
- if (pg == null) {
- pg = new Progress();
- } else {
- pg.setMinMax(0, 100);
- }
-
- login();
- pg.setProgress(10);
-
- url = getCanonicalUrl(url);
-
- setCurrentReferer(url);
-
- in = openInput(url); // NULL allowed here
- try {
- preprocess(url, getInput());
- pg.setProgress(30);
-
- Story story = new Story();
- MetaData meta = getMeta(url, getInput());
- if (meta.getCreationDate() == null
- || meta.getCreationDate().isEmpty()) {
- meta.setCreationDate(StringUtils.fromTime(new Date().getTime()));
- }
- story.setMeta(meta);
-
- pg.setProgress(50);
-
- if (meta.getCover() == null) {
- meta.setCover(getDefaultCover(meta.getSubject()));
- }
-
- pg.setProgress(60);
-
- if (getDesc) {
- String descChapterName = Instance.getTrans().getString(
- StringId.DESCRIPTION);
- story.getMeta().setResume(
- makeChapter(url, 0, descChapterName,
- getDesc(url, getInput()), null));
- }
-
- pg.setProgress(100);
- return story;
- } finally {
- if (close) {
- try {
- close();
- } catch (IOException e) {
- Instance.getTraceHandler().error(e);
- }
-
- if (in != null) {
- in.close();
- }
- }
-
- setCurrentReferer(null);
- }
- }
-
- /**
- * Process the given story resource into a fully filled {@link Story}
- * object.
- *
- * @param url
- * the story resource
- * @param pg
- * the optional progress reporter
- *
- * @return the {@link Story}
- *
- * @throws IOException
- * in case of I/O error
- */
- public Story process(URL url, Progress pg) throws IOException {
- if (pg == null) {
- pg = new Progress();
- } else {
- pg.setMinMax(0, 100);
- }
-
- url = getCanonicalUrl(url);
- pg.setProgress(1);
- try {
- Progress pgMeta = new Progress();
- pg.addProgress(pgMeta, 10);
- Story story = processMeta(url, false, true, pgMeta);
- if (!pgMeta.isDone()) {
- pgMeta.setProgress(pgMeta.getMax()); // 10%
- }
-
- if (story == null) {
- pg.setProgress(90);
- return null;
- }
-
- pg.setName("Retrieving " + story.getMeta().getTitle());
-
- setCurrentReferer(url);
-
- Progress pgGetChapters = new Progress();
- pg.addProgress(pgGetChapters, 10);
- story.setChapters(new ArrayList<Chapter>());
- List<Entry<String, URL>> chapters = getChapters(url, getInput(),
- pgGetChapters);
- if (!pgGetChapters.isDone()) {
- pgGetChapters.setProgress(pgGetChapters.getMax()); // 20%
- }
-
- if (chapters != null) {
- Progress pgChaps = new Progress("Extracting chapters", 0,
- chapters.size() * 300);
- pg.addProgress(pgChaps, 80);
-
- long words = 0;
- int i = 1;
- for (Entry<String, URL> chap : chapters) {
- pgChaps.setName("Extracting chapter " + i);
- InputStream chapIn = null;
- if (chap.getValue() != null) {
- setCurrentReferer(chap.getValue());
- chapIn = Instance.getCache().open(chap.getValue(),
- this, true);
- }
- pgChaps.setProgress(i * 100);
- try {
- Progress pgGetChapterContent = new Progress();
- Progress pgMakeChapter = new Progress();
- pgChaps.addProgress(pgGetChapterContent, 100);
- pgChaps.addProgress(pgMakeChapter, 100);
-
- String content = getChapterContent(url, chapIn, i,
- pgGetChapterContent);
- if (!pgGetChapterContent.isDone()) {
- pgGetChapterContent.setProgress(pgGetChapterContent
- .getMax());
- }
-
- Chapter cc = makeChapter(url, i, chap.getKey(),
- content, pgMakeChapter);
- if (!pgMakeChapter.isDone()) {
- pgMakeChapter.setProgress(pgMakeChapter.getMax());
- }
-
- words += cc.getWords();
- story.getChapters().add(cc);
- if (story.getMeta() != null) {
- story.getMeta().setWords(words);
- }
- } finally {
- if (chapIn != null) {
- chapIn.close();
- }
- }
-
- i++;
- }
-
- pgChaps.setName("Extracting chapters");
- } else {
- pg.setProgress(80);
- }
-
- return story;
-
- } finally {
- try {
- close();
- } catch (IOException e) {
- Instance.getTraceHandler().error(e);
- }
-
- if (in != null) {
- in.close();
- }
-
- setCurrentReferer(null);
- }
- }
-
- /**
- * The support type.
- *
- * @return the type
- */
- public SupportType getType() {
- return type;