X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2Fsupported%2FBasicSupport.java;h=e3b6fab345faa1886e491f6d22742624d6415c19;hb=754a5bc205f6a50f3fe3fe7c2dfb09d8a8dd09bb;hp=129182208f29f0ce70d46ffb70053231d0792a30;hpb=a4143cd74a90e17a811a4581cbeb213fed1f6304;p=fanfix.git diff --git a/src/be/nikiroo/fanfix/supported/BasicSupport.java b/src/be/nikiroo/fanfix/supported/BasicSupport.java index 1291822..e3b6fab 100644 --- a/src/be/nikiroo/fanfix/supported/BasicSupport.java +++ b/src/be/nikiroo/fanfix/supported/BasicSupport.java @@ -10,6 +10,7 @@ import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; +import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -243,6 +244,16 @@ public abstract class BasicSupport { protected abstract String getChapterContent(URL source, InputStream in, int number) throws IOException; + /** + * Log into the support (can be a no-op depending upon the support). + * + * @throws IOException + * in case of I/O error + */ + public void login() throws IOException { + + } + /** * Return the list of cookies (values included) that must be used to * correctly fetch the resources. @@ -251,8 +262,11 @@ public abstract class BasicSupport { * it. * * @return the cookies + * + * @throws IOException + * in case of I/O error */ - public Map getCookies() { + public Map getCookies() throws IOException { return new HashMap(); } @@ -304,6 +318,8 @@ public abstract class BasicSupport { */ protected Story processMeta(URL url, boolean close, boolean getDesc) throws IOException { + login(); + url = getCanonicalUrl(url); setCurrentReferer(url); @@ -318,6 +334,10 @@ public abstract class BasicSupport { 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); if (meta != null && meta.getCover() == null) { @@ -381,6 +401,8 @@ public abstract class BasicSupport { return null; } + pg.setName("Retrieving " + story.getMeta().getTitle()); + setCurrentReferer(url); story.setChapters(new ArrayList()); @@ -393,14 +415,19 @@ public abstract class BasicSupport { Progress pgChaps = new Progress(0, chapters.size()); pg.addProgress(pgChaps, 80); + long words = 0; for (Entry chap : chapters) { setCurrentReferer(chap.getValue()); InputStream chapIn = Instance.getCache().open( chap.getValue(), this, true); try { - story.getChapters().add( - makeChapter(url, i, chap.getKey(), - getChapterContent(url, chapIn, i))); + Chapter cc = makeChapter(url, i, chap.getKey(), + getChapterContent(url, chapIn, i)); + words += cc.getWords(); + story.getChapters().add(cc); + if (story.getMeta() != null) { + story.getMeta().setWords(words); + } } finally { chapIn.close(); } @@ -539,7 +566,13 @@ public abstract class BasicSupport { Chapter chap = new Chapter(number, chapterName); if (content != null) { - chap.setParagraphs(makeParagraphs(source, content)); + List paras = makeParagraphs(source, content); + long words = 0; + for (Paragraph para : paras) { + words += para.getWords(); + } + chap.setParagraphs(paras); + chap.setWords(words); } return chap; @@ -914,7 +947,8 @@ public abstract class BasicSupport { if (!singleQ && !doubleQ) { line = openDoubleQuote + line + closeDoubleQuote; - newParas.add(new Paragraph(ParagraphType.QUOTE, line)); + newParas.add(new Paragraph(ParagraphType.QUOTE, line, para + .getWords())); } else { char open = singleQ ? openQuote : openDoubleQuote; char close = singleQ ? closeQuote : closeDoubleQuote; @@ -937,7 +971,13 @@ public abstract class BasicSupport { if (posDot >= 0) { String rest = line.substring(posDot + 1).trim(); line = line.substring(0, posDot + 1).trim(); - newParas.add(new Paragraph(ParagraphType.QUOTE, line)); + long words = 1; + for (char car : line.toCharArray()) { + if (car == ' ') { + words++; + } + } + newParas.add(new Paragraph(ParagraphType.QUOTE, line, words)); if (!rest.isEmpty()) { newParas.addAll(requotify(processPara(rest))); } @@ -971,6 +1011,7 @@ public abstract class BasicSupport { boolean tentativeCloseQuote = false; char prev = '\0'; int dashCount = 0; + long words = 1; StringBuilder builder = new StringBuilder(); for (char car : line.toCharArray()) { @@ -1004,6 +1045,10 @@ public abstract class BasicSupport { case '\t': case '\n': // just in case case '\r': // just in case + if (builder.length() > 0 + && builder.charAt(builder.length() - 1) != ' ') { + words++; + } builder.append(' '); break; @@ -1156,7 +1201,7 @@ public abstract class BasicSupport { type = ParagraphType.QUOTE; } - return new Paragraph(type, line); + return new Paragraph(type, line, words); } /**