X-Git-Url: http://git.nikiroo.be/?p=fanfix.git;a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2Fsupported%2FFanfiction.java;h=7d4285b33e86109650e397c1d58358d54a9633a9;hp=cbbc0851621d56f04288fe4539ed4019651c1fd8;hb=b4dc6ab518ded2dd92e4cbb02ac615b1d57e8e6d;hpb=08fe2e33007063e30fe22dc1d290f8afaa18eb1d diff --git a/src/be/nikiroo/fanfix/supported/Fanfiction.java b/src/be/nikiroo/fanfix/supported/Fanfiction.java index cbbc085..7d4285b 100644 --- a/src/be/nikiroo/fanfix/supported/Fanfiction.java +++ b/src/be/nikiroo/fanfix/supported/Fanfiction.java @@ -1,5 +1,6 @@ package be.nikiroo.fanfix.supported; +import java.awt.image.BufferedImage; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; @@ -12,6 +13,7 @@ import java.util.Map.Entry; import java.util.Scanner; import be.nikiroo.fanfix.Instance; +import be.nikiroo.fanfix.data.MetaData; import be.nikiroo.utils.StringUtils; /** @@ -33,7 +35,28 @@ class Fanfiction extends BasicSupport { } @Override - protected String getSubject(URL source, InputStream in) { + protected MetaData getMeta(URL source, InputStream in) throws IOException { + MetaData meta = new MetaData(); + + meta.setTitle(getTitle(reset(in))); + meta.setAuthor(getAuthor(reset(in))); + meta.setDate(getDate(reset(in))); + meta.setTags(getTags(reset(in))); + meta.setSource(getSourceName()); + meta.setUrl(source.toString()); + meta.setPublisher(getSourceName()); + meta.setUuid(source.toString()); + meta.setLuid(""); + meta.setLang("EN"); + meta.setSubject(getSubject(reset(in))); + meta.setType(getType().toString()); + meta.setImageDocument(false); + meta.setCover(getCover(source, reset(in))); + + return meta; + } + + private String getSubject(InputStream in) { String line = getLine(in, "id=pre_story_links", 0); if (line != null) { int pos = line.lastIndexOf('"'); @@ -41,7 +64,7 @@ class Fanfiction extends BasicSupport { line = line.substring(pos + 1); pos = line.indexOf('<'); if (pos >= 0) { - return line.substring(0, pos); + return StringUtils.unhtml(line.substring(0, pos)).trim(); } } } @@ -49,10 +72,8 @@ class Fanfiction extends BasicSupport { return null; } - @Override - protected List getTags(URL source, InputStream in) - throws IOException { - List tags = super.getTags(source, in); + private List getTags(InputStream in) throws IOException { + List tags = new ArrayList(); String key = "title=\"Send Private Message\""; String line = getLine(in, key, 2); @@ -71,7 +92,7 @@ class Fanfiction extends BasicSupport { } for (String tag : line.split("-")) { - tags.add(tag.trim()); + tags.add(StringUtils.unhtml(tag).trim()); } } } @@ -80,8 +101,7 @@ class Fanfiction extends BasicSupport { return tags; } - @Override - protected String getTitle(URL source, InputStream in) { + private String getTitle(InputStream in) { int i = 0; @SuppressWarnings("resource") Scanner scan = new Scanner(in, "UTF-8"); @@ -95,7 +115,7 @@ class Fanfiction extends BasicSupport { line = line.substring("Follow/Fav".length()).trim(); } - return line; + return StringUtils.unhtml(line).trim(); } } } @@ -103,8 +123,9 @@ class Fanfiction extends BasicSupport { return null; } - @Override - protected String getAuthor(URL source, InputStream in) { + private String getAuthor(InputStream in) { + String author = null; + int i = 0; @SuppressWarnings("resource") Scanner scan = new Scanner(in, "UTF-8"); @@ -113,16 +134,16 @@ class Fanfiction extends BasicSupport { String line = scan.next(); if (line.contains("xcontrast_txt")) { if ((++i) == 3) { - return StringUtils.unhtml(line).trim(); + author = StringUtils.unhtml(line).trim(); + break; } } } - return null; + return fixAuthor(author); } - @Override - protected String getDate(URL source, InputStream in) { + private String getDate(InputStream in) { String key = "Published: = 0; pos = line.indexOf(key, pos), i++) { - pos = line.indexOf('>', pos); - if (pos >= 0) { - int endOfName = line.indexOf('<', pos); - if (endOfName >= 0) { - String name = line.substring(pos + 1, endOfName); - String chapNum = i + "."; - if (name.startsWith(chapNum)) { - name = name.substring(chapNum.length(), name.length()); - } - try { - final String chapName = name.trim(); - final URL chapURL = new URL(base + i + suffix); - urls.add(new Entry() { - public URL setValue(URL value) { - return null; - } - - public URL getValue() { - return chapURL; - } - - public String getKey() { - return chapName; - } - }); - } catch (MalformedURLException e) { - Instance.syserr(new IOException("Cannot parse chapter " - + i + " url: " + (base + i + suffix), e)); + if (line != null) { + for (pos = line.indexOf(key); pos >= 0; pos = line + .indexOf(key, pos), i++) { + pos = line.indexOf('>', pos); + if (pos >= 0) { + int endOfName = line.indexOf('<', pos); + if (endOfName >= 0) { + String name = line.substring(pos + 1, endOfName); + String chapNum = i + "."; + if (name.startsWith(chapNum)) { + name = name.substring(chapNum.length(), + name.length()); + } + + try { + final String chapName = name.trim(); + final URL chapURL = new URL(base + i + suffix); + urls.add(new Entry() { + public URL setValue(URL value) { + return null; + } + + public URL getValue() { + return chapURL; + } + + public String getKey() { + return chapName; + } + }); + } catch (MalformedURLException e) { + Instance.syserr(new IOException( + "Cannot parse chapter " + i + " url: " + + (base + i + suffix), e)); + } } } } + } else { + // only one chapter: + final String chapName = getTitle(reset(in)); + final URL chapURL = source; + urls.add(new Entry() { + public URL setValue(URL value) { + return null; + } + + public URL getValue() { + return chapURL; + } + + public String getKey() { + return chapName; + } + }); } return urls;