Merge branch 'subtree'
[fanfix.git] / src / be / nikiroo / fanfix / searchable / MangaLel.java
index c8a522c896d17849ab1cc223d86c468aa15934fa..354edb435ef281a0dab3a31f55218f63c2ab6586 100644 (file)
@@ -50,12 +50,6 @@ class MangaLel extends BasicSearchable {
                // Tags are always complete
        }
 
-       @Override
-       public int searchPages(String search) throws IOException {
-               // No pagination
-               return 1;
-       }
-
        @Override
        public List<MetaData> search(String search, int page) throws IOException {
                String url = BASE_URL + "?nomProjet="
@@ -76,56 +70,104 @@ class MangaLel extends BasicSearchable {
                return getResults(url);
        }
 
+       @Override
+       public int searchPages(String search) throws IOException {
+               // No pagination
+               return 1;
+       }
+
+       @Override
+       public int searchPages(SearchableTag tag) throws IOException {
+               if (tag.isLeaf()) {
+                       // No pagination
+                       return 1;
+               }
+
+               return 0;
+       }
+
        private List<MetaData> getResults(String sourceUrl) throws IOException {
                List<MetaData> metas = new ArrayList<MetaData>();
 
-               Document doc = DataUtil.load(
-                               Instance.getCache().open(new URL(sourceUrl), getSupport(),
-                                               false), "UTF-8", sourceUrl);
+               Document doc = DataUtil.load(Instance.getInstance().getCache().open(new URL(sourceUrl), getSupport(), false),
+                               "UTF-8", sourceUrl);
 
                for (Element result : doc.getElementsByClass("rechercheAffichage")) {
                        Element a = result.getElementsByTag("a").first();
                        if (a != null) {
+                               int projectId = -1;
+
                                MetaData meta = new MetaData();
-                               meta.setUrl(a.absUrl("href"));
-                               Element img = result.getElementsByTag("img").first();
-                               if (img != null) {
-                                       String coverUrl = img.absUrl("src");
 
-                                       try {
-                                               InputStream in = Instance.getCache().open(
-                                                               new URL(coverUrl), getSupport(), true);
-                                               try {
-                                                       meta.setCover(new Image(in));
-                                               } finally {
-                                                       in.close();
-                                               }
-                                       } catch (Exception e) {
-                                               Instance.getTraceHandler()
-                                                               .error(new Exception(
-                                                                               "Cannot download cover for MangaLEL story in search mode",
-                                                                               e));
-                                       }
-                               }
+                               // Target:
+                               // http://mangas-lecture-en-ligne.fr/index_lel.php?page=presentationProjet&idProjet=218
+
+                               // a.absUrl("href"):
+                               // http://mangas-lecture-en-ligne.fr/index_lel?onCommence=oui&idChapitre=2805
+
+                               // ...but we need the PROJECT id, not the CHAPTER id -> use
+                               // <IMG>
 
                                Elements infos = result.getElementsByClass("texte");
                                if (infos != null) {
                                        String[] tab = infos.outerHtml().split("<br>");
 
+                                       meta.setLang("fr");
+                                       meta.setSource(getType().getSourceName());
+                                       meta.setPublisher(getType().getSourceName());
+                                       meta.setType(getType().toString());
+                                       meta.setSubject("manga");
+                                       meta.setImageDocument(true);
                                        meta.setTitle(getVal(tab, 0));
                                        meta.setAuthor(getVal(tab, 1));
                                        meta.setTags(Arrays.asList(getVal(tab, 2).split(" ")));
 
-                                       meta.setResume(getSupport()
-                                                       .makeChapter(
-                                                                       new URL(sourceUrl),
-                                                                       0,
-                                                                       Instance.getTrans().getString(
-                                                                                       StringId.DESCRIPTION),
-                                                                       getVal(tab, 5)));
+                                       meta.setResume(getSupport().makeChapter(new URL(sourceUrl), 0,
+                                                       Instance.getInstance().getTrans().getString(StringId.DESCRIPTION), getVal(tab, 5)));
                                }
 
-                               metas.add(meta);
+                               Element img = result.getElementsByTag("img").first();
+                               if (img != null) {
+                                       try {
+                                               String[] tab = img.attr("src").split("/");
+                                               String str = tab[tab.length - 1];
+                                               tab = str.split("\\.");
+                                               str = tab[0];
+                                               projectId = Integer.parseInt(str);
+
+                                               String coverUrl = img.absUrl("src");
+                                               try {
+                                                       InputStream in = Instance.getInstance().getCache().open(new URL(coverUrl), getSupport(),
+                                                                       true);
+                                                       try {
+                                                               Image ii = new Image(in);
+                                                               if (ii.getSize() == 0) {
+                                                                       ii.close();
+                                                                       throw new IOException(
+                                                                                       "Empty image not accepted");
+                                                               }
+                                                               meta.setCover(ii);
+                                                       } finally {
+                                                               in.close();
+                                                       }
+                                               } catch (Exception e) {
+                                                       // Happen often on MangaLEL...
+                                                       Instance.getInstance().getTraceHandler().trace(
+                                                                       "Cannot download cover for MangaLEL story in search mode: "     + meta.getTitle());
+                                               }
+                                       } catch (Exception e) {
+                                               // no project id... cannot use the story :(
+                                               Instance.getInstance().getTraceHandler()
+                                                               .error("Cannot find ProjectId for MangaLEL story in search mode: " + meta.getTitle());
+                                       }
+                               }
+
+                               if (projectId >= 0) {
+                                       meta.setUrl("http://mangas-lecture-en-ligne.fr/index_lel.php?page=presentationProjet&idProjet="
+                                                       + projectId);
+                                       meta.setUuid(meta.getUrl());
+                                       metas.add(meta);
+                               }
                        }
                }