X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=searchable%2FMangaLel.java;fp=searchable%2FMangaLel.java;h=3e2924f31d5d84a5082fec20b0058a7091c86f5d;hb=0fc81e6465aa9c1f1dfc19b532082220d609768a;hp=0000000000000000000000000000000000000000;hpb=505be508ae7d3fb48122be548b310a238cfb91eb;p=fanfix.git diff --git a/searchable/MangaLel.java b/searchable/MangaLel.java new file mode 100644 index 0000000..3e2924f --- /dev/null +++ b/searchable/MangaLel.java @@ -0,0 +1,192 @@ +package be.nikiroo.fanfix.searchable; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.net.URLEncoder; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.jsoup.helper.DataUtil; +import org.jsoup.nodes.Document; +import org.jsoup.nodes.Element; +import org.jsoup.select.Elements; + +import be.nikiroo.fanfix.Instance; +import be.nikiroo.fanfix.bundles.StringId; +import be.nikiroo.fanfix.data.MetaData; +import be.nikiroo.fanfix.supported.SupportType; +import be.nikiroo.utils.Image; +import be.nikiroo.utils.StringUtils; + +class MangaLel extends BasicSearchable { + private String BASE_URL = "http://mangas-lecture-en-ligne.fr/index_lel.php"; + + public MangaLel() { + super(SupportType.MANGA_LEL); + } + + @Override + public List getTags() throws IOException { + List tags = new ArrayList(); + + String url = BASE_URL + "?page=recherche"; + Document doc = load(url, false); + + Element genre = doc.getElementsByClass("genre").first(); + if (genre != null) { + for (Element el : genre.getElementsByAttributeValueStarting("for", + "genre")) { + tags.add(new SearchableTag(el.attr("for"), el.text(), true)); + } + } + + return tags; + } + + @Override + public void fillTag(SearchableTag tag) throws IOException { + // Tags are always complete + } + + @Override + public List search(String search, int page) throws IOException { + String url = BASE_URL + "?nomProjet=" + + URLEncoder.encode(search, "utf-8") + + "&nomAuteur=&nomTeam=&page=recherche&truc=truc"; + + // No pagination + return getResults(url); + } + + @Override + public List search(SearchableTag tag, int page) + throws IOException { + String url = BASE_URL + "?nomProjet=&nomAuteur=&nomTeam=&" + + tag.getId() + "=on&page=recherche&truc=truc"; + + // No pagination + 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 getResults(String sourceUrl) throws IOException { + List metas = new ArrayList(); + + Document doc = DataUtil.load( + Instance.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(); + + // 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 + // + + Elements infos = result.getElementsByClass("texte"); + if (infos != null) { + String[] tab = infos.outerHtml().split("
"); + + 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))); + } + + 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.getCache().open( + new URL(coverUrl), getSupport(), true); + try { + meta.setCover(new Image(in)); + } finally { + in.close(); + } + } catch (Exception e) { + // Happen often on MangaLEL... + Instance.getTraceHandler().trace( + "Cannot download cover for MangaLEL story in search mode: " + + meta.getTitle()); + } + } catch (Exception e) { + // no project id... cannot use the story :( + Instance.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); + } + } + } + + return metas; + } + + 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; + } +}