Change BasicSupport to use jsoup
[nikiroo-utils.git] / src / be / nikiroo / fanfix / supported / Fimfiction.java
index 1faf615207154501361164721d7be8a2178855a9..ed9c9adf867439cd3154d3f6d00f0e5d30479403 100644 (file)
@@ -1,6 +1,5 @@
 package be.nikiroo.fanfix.supported;
 
-import java.awt.image.BufferedImage;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.MalformedURLException;
@@ -14,6 +13,7 @@ import java.util.Scanner;
 
 import be.nikiroo.fanfix.Instance;
 import be.nikiroo.fanfix.data.MetaData;
+import be.nikiroo.utils.Image;
 import be.nikiroo.utils.Progress;
 import be.nikiroo.utils.StringUtils;
 
@@ -23,7 +23,7 @@ import be.nikiroo.utils.StringUtils;
  * 
  * @author niki
  */
-class Fimfiction extends BasicSupport {
+class Fimfiction extends BasicSupport_Deprecated {
        @Override
        protected boolean isHtml() {
                return true;
@@ -70,22 +70,36 @@ class Fimfiction extends BasicSupport {
                @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("story_category") && !line.contains("title=")) {
-                               int pos = line.indexOf('>');
-                               if (pos >= 0) {
-                                       line = line.substring(pos + 1);
-                                       pos = line.indexOf('<');
-                                       if (pos >= 0) {
-                                               line = line.substring(0, pos);
-                                       }
+
+                       if (!started) {
+                               started = line.contains("\"story_container\"");
+                       }
+
+                       if (started && line.contains("class=\"tag-")) {
+                               if (line.contains("index.php")) {
+                                       break; // end of *this story* tags
                                }
 
-                               line = line.trim();
-                               if (!tags.contains(line)) {
-                                       tags.add(line);
+                               String keyword = "title=\"";
+                               Scanner tagScanner = new Scanner(line);
+                               tagScanner.useDelimiter(keyword);
+                               if (tagScanner.hasNext()) {
+                                       tagScanner.next();// Ignore first one
+                               }
+                               while (tagScanner.hasNext()) {
+                                       String tag = tagScanner.next();
+                                       if (tag.contains("\"")) {
+                                               tag = tag.split("\"")[0];
+                                               tag = StringUtils.unhtml(tag).trim();
+                                               if (!tag.isEmpty() && !tags.contains(tag)) {
+                                                       tags.add(tag);
+                                               }
+                                       }
                                }
+                               tagScanner.close();
                        }
                }
 
@@ -159,7 +173,7 @@ class Fimfiction extends BasicSupport {
                return getLine(in, "class=\"description-text bbcode\"", 1);
        }
 
-       private BufferedImage getCover(InputStream in) {
+       private Image getCover(InputStream in) {
                // Note: the 'og:image' is the SMALL cover, not the full version
                String cover = getLine(in, "class=\"story_container__story_image\"", 1);
                if (cover != null) {
@@ -194,7 +208,8 @@ class Fimfiction extends BasicSupport {
                                        break;
                                }
 
-                               if (line.startsWith("<a href=")) {
+                               if (line.startsWith("<a href=")
+                                               || line.contains("class=\"chapter-title\"")) {
                                        // Chapter name
                                        String name = line;
                                        int pos = name.indexOf('>');
@@ -220,20 +235,23 @@ class Fimfiction extends BasicSupport {
                                                final URL value = new URL("http://www.fimfiction.net"
                                                                + line);
                                                urls.add(new Entry<String, URL>() {
+                                                       @Override
                                                        public URL setValue(URL value) {
                                                                return null;
                                                        }
 
+                                                       @Override
                                                        public String getKey() {
                                                                return key;
                                                        }
 
+                                                       @Override
                                                        public URL getValue() {
                                                                return value;
                                                        }
                                                });
                                        } catch (MalformedURLException e) {
-                                               Instance.syserr(e);
+                                               Instance.getTraceHandler().error(e);
                                        }
                                }
                        }