Change BasicSupport to use jsoup
[nikiroo-utils.git] / src / be / nikiroo / fanfix / supported / MangaFox.java
index fb72bf531d00d9e6b20d88f127b9a19e98dc64e3..8fc1965dfe20839fe9c406a07f12ad52c3a5527a 100644 (file)
@@ -11,9 +11,12 @@ import java.util.Map.Entry;
 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;
 
-class MangaFox extends BasicSupport {
+class MangaFox extends BasicSupport_Deprecated {
        @Override
        protected boolean isHtml() {
                return true;
@@ -21,22 +24,32 @@ class MangaFox extends BasicSupport {
 
        @Override
        public String getSourceName() {
-               return "MangaFox.met";
+               return "MangaFox.me";
        }
 
        @Override
-       protected String getSubject(URL source, InputStream in) {
-               return "manga";
+       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("manga");
+               meta.setType(getType().toString());
+               meta.setImageDocument(true);
+               meta.setCover(getCover(reset(in)));
+
+               return meta;
        }
 
-       @Override
-       public boolean isImageDocument(URL source, InputStream in)
-                       throws IOException {
-               return true;
-       }
-
-       @Override
-       protected List<String> getTags(URL source, InputStream in) {
+       private List<String> getTags(InputStream in) {
                List<String> tags = new ArrayList<String>();
 
                String line = getLine(in, "/genres/", 0);
@@ -53,8 +66,7 @@ class MangaFox 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;
@@ -74,8 +86,7 @@ class MangaFox extends BasicSupport {
                return null;
        }
 
-       @Override
-       protected String getAuthor(URL source, InputStream in) {
+       private String getAuthor(InputStream in) {
                List<String> authors = new ArrayList<String>();
 
                String line = getLine(in, "/author/", 0, false);
@@ -91,7 +102,7 @@ class MangaFox extends BasicSupport {
                try {
                        in.reset();
                } catch (IOException e) {
-                       Instance.syserr(e);
+                       Instance.getTraceHandler().error(e);
                }
 
                line = getLine(in, "/artist/", 0, false);
@@ -106,22 +117,21 @@ class MangaFox extends BasicSupport {
 
                if (authors.isEmpty()) {
                        return null;
-               } else {
-                       StringBuilder builder = new StringBuilder();
-                       for (String author : authors) {
-                               if (builder.length() > 0) {
-                                       builder.append(", ");
-                               }
+               }
 
-                               builder.append(author);
+               StringBuilder builder = new StringBuilder();
+               for (String author : authors) {
+                       if (builder.length() > 0) {
+                               builder.append(", ");
                        }
 
-                       return builder.toString();
+                       builder.append(author);
                }
+
+               return builder.toString();
        }
 
-       @Override
-       protected String getDate(URL source, InputStream in) {
+       private String getDate(InputStream in) {
                String line = getLine(in, "/released/", 0);
                if (line != null) {
                        line = StringUtils.unhtml(line);
@@ -152,8 +162,7 @@ class MangaFox extends BasicSupport {
                return null;
        }
 
-       @Override
-       protected URL getCover(URL url, InputStream in) {
+       private Image getCover(InputStream in) {
                String line = getLine(in, " property=\"og:image\"", 0);
                String cover = null;
                if (line != null) {
@@ -172,10 +181,15 @@ class MangaFox extends BasicSupport {
                }
 
                if (cover != null) {
+                       InputStream coverIn;
                        try {
-                               return new URL(cover);
-                       } catch (MalformedURLException e) {
-                               Instance.syserr(e);
+                               coverIn = openEx(cover);
+                               try {
+                                       return new Image(coverIn);
+                               } finally {
+                                       coverIn.close();
+                               }
+                       } catch (IOException e) {
                        }
                }
 
@@ -183,7 +197,8 @@ class MangaFox extends BasicSupport {
        }
 
        @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>>();
 
                String volumeAt = "<h3 class=\"volume\">";
@@ -226,31 +241,44 @@ class MangaFox extends BasicSupport {
                                        }
                                }
 
-                               // to help with the retry and the originalUrl
-                               refresh(url);
-
                                try {
                                        final String key = name;
                                        final URL value = new URL(url);
                                        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);
                                }
                        }
                }
 
+               if (pg == null) {
+                       pg = new Progress(0, urls.size());
+               } else {
+                       pg.setMinMax(0, urls.size());
+               }
+
+               int i = 1;
+               for (Entry<String, URL> entry : urls) {
+                       // to help with the retry and the originalUrl
+                       refresh(entry.getValue().toString());
+                       pg.setProgress(i++);
+               }
+
                // the chapters are in reversed order
                Collections.reverse(urls);
 
@@ -258,19 +286,29 @@ class MangaFox extends BasicSupport {
        }
 
        @Override
-       protected String getChapterContent(URL source, InputStream in, int number) {
+       protected String getChapterContent(URL source, InputStream in, int number,
+                       Progress pg) {
+               if (pg == null) {
+                       pg = new Progress();
+               } else {
+                       // Since we have no idea how many images we have, we cycle from 0
+                       // to max, then again, then again...
+                       pg.setMinMax(0, 20);
+               }
+
                StringBuilder builder = new StringBuilder();
                String base = getCurrentReferer().toString();
                int pos = base.lastIndexOf('/');
                base = base.substring(0, pos + 1); // including the '/' at the end
 
+               int i = 1;
                boolean close = false;
                while (in != null) {
                        String linkNextLine = getLine(in, "return enlarge()", 0);
                        try {
                                in.reset();
                        } catch (IOException e) {
-                               Instance.syserr(e);
+                               Instance.getTraceHandler().error(e);
                        }
 
                        String linkImageLine = getLine(in, "return enlarge()", 1);
@@ -298,17 +336,18 @@ class MangaFox extends BasicSupport {
                                builder.append("[");
                                // to help with the retry and the originalUrl, part 1
                                builder.append(withoutQuery(linkImage));
-                               builder.append("]\n");
+                               builder.append("]<br/>");
                        }
 
                        // to help with the retry and the originalUrl, part 2
                        refresh(linkImage);
+                       pg.setProgress((i++) % pg.getMax());
 
                        if (close) {
                                try {
                                        in.close();
                                } catch (IOException e) {
-                                       Instance.syserr(e);
+                                       Instance.getTraceHandler().error(e);
                                }
                        }
 
@@ -319,10 +358,12 @@ class MangaFox extends BasicSupport {
                                        url = new URL(base + linkNext);
                                        in = openEx(base + linkNext);
                                        setCurrentReferer(url);
+                                       pg.setProgress((i++) % pg.getMax());
                                } catch (IOException e) {
-                                       Instance.syserr(new IOException(
-                                                       "Cannot get the next manga page which is: "
-                                                                       + linkNext, e));
+                                       Instance.getTraceHandler().error(
+                                                       new IOException(
+                                                                       "Cannot get the next manga page which is: "
+                                                                                       + linkNext, e));
                                }
                        }
 
@@ -335,6 +376,9 @@ class MangaFox extends BasicSupport {
 
        @Override
        protected boolean supports(URL url) {
+               // Broken code (see MangaFoxNew)
+               if (true)
+                       return false;
                return "mangafox.me".equals(url.getHost())
                                || "www.mangafox.me".equals(url.getHost());
        }