code cleanup / jdoc
[nikiroo-utils.git] / src / be / nikiroo / fanfix / supported / MangaLel.java
index d42e74318ddaa4b75f16d34a6f5f424735a12f4c..47efad8594a71830a33e5de698e47256c3d87616 100644 (file)
@@ -2,11 +2,11 @@ package be.nikiroo.fanfix.supported;
 
 import java.io.IOException;
 import java.io.InputStream;
-import java.net.MalformedURLException;
 import java.net.URL;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
 import java.util.AbstractMap;
 import java.util.ArrayList;
-import java.util.Collections;
 import java.util.List;
 import java.util.Map.Entry;
 
@@ -30,20 +30,15 @@ class MangaLel extends BasicSupport {
        protected MetaData getMeta() throws IOException {
                MetaData meta = new MetaData();
 
-               String[] authorDateTag = getAuthorDateTag();
-
                meta.setTitle(getTitle());
-               meta.setAuthor(authorDateTag[0]);
-               meta.setDate(authorDateTag[1]);
-               meta.setTags(explode(authorDateTag[2]));
-               meta.setSource(getType().getSourceName());
+               meta.setAuthor(getAuthor());
+               meta.setDate(bsHelper.formatDate(getDate()));
+               meta.setTags(getTags());
                meta.setUrl(getSource().toString());
-               meta.setPublisher(getType().getSourceName());
                meta.setUuid(getSource().toString());
                meta.setLuid("");
                meta.setLang("fr");
                meta.setSubject("manga");
-               meta.setType(getType().toString());
                meta.setImageDocument(true);
                meta.setCover(getCover());
 
@@ -52,131 +47,149 @@ class MangaLel extends BasicSupport {
 
        private String getTitle() {
                Element doc = getSourceNode();
-               Element h2 = doc.getElementsByClass("widget-title").first();
-               if (h2 != null) {
-                       return StringUtils.unhtml(h2.text()).trim();
+               Element h4 = doc.getElementsByTag("h4").first();
+               if (h4 != null) {
+                       return StringUtils.unhtml(h4.text()).trim();
                }
 
                return null;
        }
 
-       // 0 = author
-       // 1 = date
-       // 2 = tags
-       private String[] getAuthorDateTag() {
-               String[] tab = new String[3];
-
+       private String getAuthor() {
                Element doc = getSourceNode();
-               Element tabEls = doc.getElementsByClass("dl-horizontal").first();
-               int prevOk = 0;
-               for (Element tabEl : tabEls.children()) {
-                       String txt = tabEl.text().trim();
-                       if (prevOk > 0) {
-                               if (tab[prevOk - 1] == null) {
-                                       tab[prevOk - 1] = "";
-                               } else {
-                                       tab[prevOk - 1] += ", ";
-                               }
+               Element tabEls = doc.getElementsByClass("presentation-projet").first();
+               if (tabEls != null) {
+                       String[] tab = tabEls.outerHtml().split("<br>");
+                       return getVal(tab, 1);
+               }
 
-                               tab[prevOk - 1] += txt;
-                               prevOk = 0;
-                       } else {
-                               if (txt.equals("Auteur(s)") || txt.equals("Artist(s)")) {
-                                       prevOk = 1;
-                               } else if (txt.equals("Date de sortie")) {
-                                       prevOk = 2;
-                               } else if (txt.equals("Type") || txt.equals("Catégories")) {
-                                       prevOk = 3;
-                               } else {
-                                       prevOk = 0;
-                               }
+               return "";
+       }
+
+       private List<String> getTags() {
+               Element doc = getSourceNode();
+               Element tabEls = doc.getElementsByClass("presentation-projet").first();
+               if (tabEls != null) {
+                       String[] tab = tabEls.outerHtml().split("<br>");
+                       List<String> tags = new ArrayList<String>();
+                       for (String tag : getVal(tab, 3).split(" ")) {
+                               tags.add(tag);
                        }
+                       return tags;
                }
 
-               for (int i = 0; i < 3; i++) {
-                       String list = "";
-                       for (String item : explode(tab[i])) {
-                               if (!list.isEmpty()) {
-                                       list = list + ", ";
+               return new ArrayList<String>();
+
+       }
+
+       private String getDate() {
+               Element doc = getSourceNode();
+               Element table = doc.getElementsByClass("table").first();
+
+               // We take the first date we find
+               String value = "";
+               if (table != null) {
+                       Elements els;
+                       els = table.getElementsByTag("tr");
+                       if (els.size() >= 2) {
+                               els = els.get(1).getElementsByTag("td");
+                               if (els.size() >= 3) {
+                                       value = StringUtils.unhtml(els.get(2).text()).trim();
                                }
-                               list += item;
                        }
-                       tab[i] = list;
                }
 
-               return tab;
+               return value;
        }
 
        @Override
        protected String getDesc() {
-               String desc = null;
-
                Element doc = getSourceNode();
-               Element title = doc.getElementsByClass("well").first();
-               if (title != null) {
-                       desc = StringUtils.unhtml(title.text()).trim();
-                       if (desc.startsWith("Résumé")) {
-                               desc = desc.substring("Résumé".length()).trim();
-                       }
+               Element tabEls = doc.getElementsByClass("presentation-projet").first();
+               if (tabEls != null) {
+                       String[] tab = tabEls.outerHtml().split("<br>");
+                       return getVal(tab, 4);
                }
 
-               return desc;
+               return "";
        }
 
        private Image getCover() {
                Element doc = getSourceNode();
-               Element cover = doc.getElementsByClass("img-responsive").first();
+               Element container = doc.getElementsByClass("container").first();
+
+               if (container != null) {
+
+                       Elements imgs = container.getElementsByTag("img");
+                       Element img = null;
+                       if (imgs.size() >= 1) {
+                               img = imgs.get(0);
+                               if (img.hasClass("banniere-team-projet")) {
+                                       img = null;
+                                       if (imgs.size() >= 2) {
+                                               img = imgs.get(1);
+                                       }
+                               }
+                       }
 
-               if (cover != null) {
-                       String coverUrl = cover.absUrl("src");
+                       if (img != null) {
+                               String coverUrl = img.absUrl("src");
 
-                       InputStream coverIn;
-                       try {
-                               coverIn = Instance.getCache().open(new URL(coverUrl), this,
-                                               true);
                                try {
-                                       return new Image(coverIn);
-                               } finally {
-                                       coverIn.close();
+                                       InputStream coverIn = Instance.getInstance().getCache()
+                                                       .open(new URL(coverUrl), this, true);
+                                       try {
+                                               Image ii = new Image(coverIn);
+                                               if (ii.getSize() == 0) {
+                                                       ii.close();
+                                                       throw new IOException("Empty image not accepted");
+                                               }
+                                               
+                                               return ii;
+                                       } finally {
+                                               coverIn.close();
+                                       }
+                               } catch (IOException e) {
+                                       Instance.getInstance().getTraceHandler().error(e);
                                }
-                       } catch (IOException e) {
-                               Instance.getTraceHandler().error(e);
                        }
                }
 
                return null;
        }
 
+       private String getVal(String[] tab, int i) {
+               String val = "";
+
+               if (i < tab.length) {
+                       val = StringUtils.unhtml(tab[i]);
+                       int pos = val.indexOf(":");
+                       if (pos >= 0) {
+                               val = val.substring(pos + 1).trim();
+                       }
+               }
+
+               return val;
+       }
+
        @Override
-       protected List<Entry<String, URL>> getChapters(Progress pg) {
+       protected List<Entry<String, URL>> getChapters(Progress pg)
+                       throws IOException {
                List<Entry<String, URL>> urls = new ArrayList<Entry<String, URL>>();
 
-               int i = 0;
                Element doc = getSourceNode();
-               Elements chapEls = doc.getElementsByClass("chapters").first()
-                               .getElementsByTag("li");
-               for (Element chapEl : chapEls) {
-                       Element titleEl = chapEl.getElementsByTag("h5").first();
-                       String title = StringUtils.unhtml(titleEl.text()).trim();
-
-                       // because Atril does not support strange file names
-                       title = Integer.toString(chapEls.size() - i);
-
-                       Element linkEl = chapEl.getElementsByTag("h5").first()
-                                       .getElementsByTag("a").first();
-                       String link = linkEl.absUrl("href");
-
-                       try {
-                               urls.add(new AbstractMap.SimpleEntry<String, URL>(title,
-                                               new URL(link)));
-                       } catch (MalformedURLException e) {
-                               Instance.getTraceHandler().error(e);
+               Element table = doc.getElementsByClass("table").first();
+               if (table != null) {
+                       for (Element tr : table.getElementsByTag("tr")) {
+                               Element a = tr.getElementsByTag("a").first();
+                               if (a != null) {
+                                       String name = StringUtils.unhtml(a.text()).trim();
+                                       URL url = new URL(a.absUrl("href"));
+                                       urls.add(new AbstractMap.SimpleEntry<String, URL>(name, url));
+                               }
                        }
-
-                       i++;
                }
 
-               Collections.reverse(urls);
                return urls;
        }
 
@@ -189,16 +202,19 @@ class MangaLel extends BasicSupport {
 
                StringBuilder builder = new StringBuilder();
 
-               InputStream in = Instance.getCache().open(chapUrl, this, false);
+               InputStream in = Instance.getInstance().getCache().open(chapUrl, this, false);
                try {
                        Element pageDoc = DataUtil.load(in, "UTF-8", chapUrl.toString());
-                       Elements linkEls = pageDoc.getElementsByClass("img-responsive");
+                       Element content = pageDoc.getElementById("content");
+                       Elements linkEls = content.getElementsByTag("img");
                        for (Element linkEl : linkEls) {
-                               if (linkEl.hasAttr("data-src")) {
-                                       builder.append("[");
-                                       builder.append(linkEl.absUrl("data-src").trim());
-                                       builder.append("]<br/>");
+                               if (linkEl.absUrl("src").isEmpty()) {
+                                       continue;
                                }
+
+                               builder.append("[");
+                               builder.append(linkEl.absUrl("src"));
+                               builder.append("]<br/>");
                        }
 
                } finally {
@@ -208,32 +224,11 @@ class MangaLel extends BasicSupport {
                return builder.toString();
        }
 
-       /**
-        * Explode an HTML comma-separated list of values into a non-duplicate text
-        * {@link List} .
-        * 
-        * @param values
-        *            the comma-separated values in HTML format
-        * 
-        * @return the full list with no duplicate in text format
-        */
-       private List<String> explode(String values) {
-               List<String> list = new ArrayList<String>();
-               if (values != null && !values.isEmpty()) {
-                       for (String auth : values.split(",")) {
-                               String a = StringUtils.unhtml(auth).trim();
-                               if (!a.isEmpty() && !list.contains(a.trim())) {
-                                       list.add(a);
-                               }
-                       }
-               }
-
-               return list;
-       }
-
        @Override
        protected boolean supports(URL url) {
-               return "manga-lel.com".equals(url.getHost())
-                               || "www.manga-lel.com".equals(url.getHost());
+               // URL structure (the projectId is the manga key):
+               // http://mangas-lecture-en-ligne.fr/index_lel.php?page=presentationProjet&idProjet=999
+
+               return "mangas-lecture-en-ligne.fr".equals(url.getHost());
        }
 }