Version 1.5.1: FimFiction 4, small fix on eHentai
[fanfix.git] / src / be / nikiroo / fanfix / supported / Fimfiction.java
index 61f61d2f5856c4732c87f27df1f00b96e7f6f0f2..1faf615207154501361164721d7be8a2178855a9 100644 (file)
@@ -1,5 +1,7 @@
 package be.nikiroo.fanfix.supported;
 
+import java.awt.image.BufferedImage;
+import java.io.IOException;
 import java.io.InputStream;
 import java.net.MalformedURLException;
 import java.net.URL;
@@ -11,6 +13,9 @@ import java.util.Map.Entry;
 import java.util.Scanner;
 
 import be.nikiroo.fanfix.Instance;
+import be.nikiroo.fanfix.data.MetaData;
+import be.nikiroo.utils.Progress;
+import be.nikiroo.utils.StringUtils;
 
 /**
  * Support class for <a href="http://www.fimfiction.net/">FimFiction.net</a>
@@ -30,8 +35,25 @@ class Fimfiction extends BasicSupport {
        }
 
        @Override
-       protected String getSubject(URL source, InputStream in) {
-               return "MLP";
+       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("MLP");
+               meta.setType(getType().toString());
+               meta.setImageDocument(false);
+               meta.setCover(getCover(reset(in)));
+
+               return meta;
        }
 
        @Override
@@ -41,8 +63,7 @@ class Fimfiction extends BasicSupport {
                return cookies;
        }
 
-       @Override
-       protected List<String> getTags(URL source, InputStream in) {
+       private List<String> getTags(InputStream in) {
                List<String> tags = new ArrayList<String>();
                tags.add("MLP");
 
@@ -71,8 +92,7 @@ class Fimfiction extends BasicSupport {
                return tags;
        }
 
-       @Override
-       protected String getTitle(URL source, InputStream in) {
+       private String getTitle(InputStream in) {
                String line = getLine(in, " property=\"og:title\"", 0);
                if (line != null) {
                        int pos = -1;
@@ -84,7 +104,7 @@ class Fimfiction 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();
                                }
                        }
                }
@@ -92,8 +112,7 @@ class Fimfiction extends BasicSupport {
                return null;
        }
 
-       @Override
-       protected String getAuthor(URL source, InputStream in) {
+       private String getAuthor(InputStream in) {
                String line = getLine(in, " href=\"/user/", 0);
                if (line != null) {
                        int pos = line.indexOf('"');
@@ -114,8 +133,7 @@ class Fimfiction extends BasicSupport {
                return null;
        }
 
-       @Override
-       protected String getDate(URL source, InputStream in) {
+       private String getDate(InputStream in) {
                String line = getLine(in, "<span class=\"date\">", 0);
                if (line != null) {
                        int pos = -1;
@@ -138,13 +156,12 @@ class Fimfiction extends BasicSupport {
        @Override
        protected String getDesc(URL source, InputStream in) {
                // the og: meta version is the SHORT resume, this is the LONG resume
-               return getLine(in, "class=\"more_button hidden\"", -1);
+               return getLine(in, "class=\"description-text bbcode\"", 1);
        }
 
-       @Override
-       protected URL getCover(URL url, InputStream in) {
+       private BufferedImage getCover(InputStream in) {
                // Note: the 'og:image' is the SMALL cover, not the full version
-               String cover = getLine(in, "<div class=\"story_image\">", 1);
+               String cover = getLine(in, "class=\"story_container__story_image\"", 1);
                if (cover != null) {
                        int pos = cover.indexOf('"');
                        if (pos >= 0) {
@@ -156,66 +173,68 @@ class Fimfiction extends BasicSupport {
                        }
                }
 
-               if (cover != null) {
-                       try {
-                               return new URL(cover);
-                       } catch (MalformedURLException e) {
-                               Instance.syserr(e);
-                       }
-               }
-
-               return null;
+               return getImage(this, null, cover);
        }
 
        @Override
-       protected List<Entry<String, URL>> getChapters(URL source, InputStream in) {
+       protected List<Entry<String, URL>> getChapters(URL source, InputStream in,
+                       Progress pg) {
                List<Entry<String, URL>> urls = new ArrayList<Entry<String, URL>>();
                @SuppressWarnings("resource")
                Scanner scan = new Scanner(in, "UTF-8");
                scan.useDelimiter("\\n");
+               boolean started = false;
                while (scan.hasNext()) {
-                       String line = scan.next();
-                       if (line.contains("class=\"chapter_link\"")
-                                       || line.contains("class='chapter_link'")) {
-                               // Chapter name
-                               String name = line;
-                               int pos = name.indexOf('>');
-                               if (pos >= 0) {
-                                       name = name.substring(pos + 1);
-                                       pos = name.indexOf('<');
+                       String line = scan.next().trim();
+
+                       if (!started) {
+                               started = line.equals("<!--Chapters-->");
+                       } else {
+                               if (line.equals("</form>")) {
+                                       break;
+                               }
+
+                               if (line.startsWith("<a href=")) {
+                                       // Chapter name
+                                       String name = line;
+                                       int pos = name.indexOf('>');
                                        if (pos >= 0) {
-                                               name = name.substring(0, pos);
+                                               name = name.substring(pos + 1);
+                                               pos = name.indexOf('<');
+                                               if (pos >= 0) {
+                                                       name = name.substring(0, pos);
+                                               }
                                        }
-                               }
-                               // Chapter content
-                               pos = line.indexOf('/');
-                               if (pos >= 0) {
-                                       line = line.substring(pos); // we take the /, not +1
-                                       pos = line.indexOf('"');
+                                       // Chapter content
+                                       pos = line.indexOf('/');
                                        if (pos >= 0) {
-                                               line = line.substring(0, pos);
+                                               line = line.substring(pos); // we take the /, not +1
+                                               pos = line.indexOf('"');
+                                               if (pos >= 0) {
+                                                       line = line.substring(0, pos);
+                                               }
                                        }
-                               }
 
-                               try {
-                                       final String key = name;
-                                       final URL value = new URL("http://www.fimfiction.net"
-                                                       + line);
-                                       urls.add(new Entry<String, URL>() {
-                                               public URL setValue(URL value) {
-                                                       return null;
-                                               }
+                                       try {
+                                               final String key = name;
+                                               final URL value = new URL("http://www.fimfiction.net"
+                                                               + line);
+                                               urls.add(new Entry<String, URL>() {
+                                                       public URL setValue(URL value) {
+                                                               return null;
+                                                       }
 
-                                               public String getKey() {
-                                                       return key;
-                                               }
+                                                       public String getKey() {
+                                                               return key;
+                                                       }
 
-                                               public URL getValue() {
-                                                       return value;
-                                               }
-                                       });
-                               } catch (MalformedURLException e) {
-                                       Instance.syserr(e);
+                                                       public URL getValue() {
+                                                               return value;
+                                                       }
+                                               });
+                                       } catch (MalformedURLException e) {
+                                               Instance.syserr(e);
+                                       }
                                }
                        }
                }
@@ -224,8 +243,9 @@ class Fimfiction extends BasicSupport {
        }
 
        @Override
-       protected String getChapterContent(URL source, InputStream in, int number) {
-               return getLine(in, "<div id=\"chapter_container\">", 1);
+       protected String getChapterContent(URL source, InputStream in, int number,
+                       Progress pg) {
+               return getLine(in, "<div class=\"bbcode\">", 1);
        }
 
        @Override