- 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);
- setCurrentReferer(chap.getValue());
- InputStream 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 {
- chapIn.close();
- }
-
- i++;
- }
-
- pgChaps.setName("Extracting chapters");
- } else {
- pg.setProgress(80);
- }
-
- return story;
-
- } finally {
- try {
- close();
- } catch (IOException e) {
- Instance.syserr(e);
- }
-
- if (in != null) {
- in.close();
- }
-
- setCurrentReferer(null);
- }
- }
-
- /**
- * The support type.
- *
- * @return the type
- */
- public SupportType getType() {
- return type;