| 1 | package be.nikiroo.fanfix.searchable; |
| 2 | |
| 3 | import java.io.IOException; |
| 4 | import java.io.InputStream; |
| 5 | import java.net.URL; |
| 6 | import java.net.URLEncoder; |
| 7 | import java.util.ArrayList; |
| 8 | import java.util.Arrays; |
| 9 | import java.util.List; |
| 10 | |
| 11 | import org.jsoup.helper.DataUtil; |
| 12 | import org.jsoup.nodes.Document; |
| 13 | import org.jsoup.nodes.Element; |
| 14 | import org.jsoup.select.Elements; |
| 15 | |
| 16 | import be.nikiroo.fanfix.Instance; |
| 17 | import be.nikiroo.fanfix.bundles.StringId; |
| 18 | import be.nikiroo.fanfix.data.MetaData; |
| 19 | import be.nikiroo.fanfix.supported.SupportType; |
| 20 | import be.nikiroo.utils.Image; |
| 21 | import be.nikiroo.utils.StringUtils; |
| 22 | |
| 23 | class MangaLel extends BasicSearchable { |
| 24 | private String BASE_URL = "http://mangas-lecture-en-ligne.fr/index_lel.php"; |
| 25 | |
| 26 | public MangaLel() { |
| 27 | super(SupportType.MANGA_LEL); |
| 28 | } |
| 29 | |
| 30 | @Override |
| 31 | public List<SearchableTag> getTags() throws IOException { |
| 32 | List<SearchableTag> tags = new ArrayList<SearchableTag>(); |
| 33 | |
| 34 | String url = BASE_URL + "?page=recherche"; |
| 35 | Document doc = load(url, false); |
| 36 | |
| 37 | Element genre = doc.getElementsByClass("genre").first(); |
| 38 | if (genre != null) { |
| 39 | for (Element el : genre.getElementsByAttributeValueStarting("for", |
| 40 | "genre")) { |
| 41 | tags.add(new SearchableTag(el.attr("for"), el.text(), true)); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | return tags; |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public void fillTag(SearchableTag tag) throws IOException { |
| 50 | // Tags are always complete |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public List<MetaData> search(String search, int page) throws IOException { |
| 55 | String url = BASE_URL + "?nomProjet=" |
| 56 | + URLEncoder.encode(search, "utf-8") |
| 57 | + "&nomAuteur=&nomTeam=&page=recherche&truc=truc"; |
| 58 | |
| 59 | // No pagination |
| 60 | return getResults(url); |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public List<MetaData> search(SearchableTag tag, int page) |
| 65 | throws IOException { |
| 66 | String url = BASE_URL + "?nomProjet=&nomAuteur=&nomTeam=&" |
| 67 | + tag.getId() + "=on&page=recherche&truc=truc"; |
| 68 | |
| 69 | // No pagination |
| 70 | return getResults(url); |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public int searchPages(String search) throws IOException { |
| 75 | // No pagination |
| 76 | return 1; |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public int searchPages(SearchableTag tag) throws IOException { |
| 81 | if (tag.isLeaf()) { |
| 82 | // No pagination |
| 83 | return 1; |
| 84 | } |
| 85 | |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | private List<MetaData> getResults(String sourceUrl) throws IOException { |
| 90 | List<MetaData> metas = new ArrayList<MetaData>(); |
| 91 | |
| 92 | Document doc = DataUtil.load(Instance.getInstance().getCache().open(new URL(sourceUrl), getSupport(), false), |
| 93 | "UTF-8", sourceUrl); |
| 94 | |
| 95 | for (Element result : doc.getElementsByClass("rechercheAffichage")) { |
| 96 | Element a = result.getElementsByTag("a").first(); |
| 97 | if (a != null) { |
| 98 | int projectId = -1; |
| 99 | |
| 100 | MetaData meta = new MetaData(); |
| 101 | |
| 102 | // Target: |
| 103 | // http://mangas-lecture-en-ligne.fr/index_lel.php?page=presentationProjet&idProjet=218 |
| 104 | |
| 105 | // a.absUrl("href"): |
| 106 | // http://mangas-lecture-en-ligne.fr/index_lel?onCommence=oui&idChapitre=2805 |
| 107 | |
| 108 | // ...but we need the PROJECT id, not the CHAPTER id -> use |
| 109 | // <IMG> |
| 110 | |
| 111 | Elements infos = result.getElementsByClass("texte"); |
| 112 | if (infos != null) { |
| 113 | String[] tab = infos.outerHtml().split("<br>"); |
| 114 | |
| 115 | meta.setLang("fr"); |
| 116 | meta.setSource(getType().getSourceName()); |
| 117 | meta.setPublisher(getType().getSourceName()); |
| 118 | meta.setType(getType().toString()); |
| 119 | meta.setSubject("manga"); |
| 120 | meta.setImageDocument(true); |
| 121 | meta.setTitle(getVal(tab, 0)); |
| 122 | meta.setAuthor(getVal(tab, 1)); |
| 123 | meta.setTags(Arrays.asList(getVal(tab, 2).split(" "))); |
| 124 | |
| 125 | meta.setResume(getSupport().makeChapter(new URL(sourceUrl), 0, |
| 126 | Instance.getInstance().getTrans().getString(StringId.DESCRIPTION), getVal(tab, 5))); |
| 127 | } |
| 128 | |
| 129 | Element img = result.getElementsByTag("img").first(); |
| 130 | if (img != null) { |
| 131 | try { |
| 132 | String[] tab = img.attr("src").split("/"); |
| 133 | String str = tab[tab.length - 1]; |
| 134 | tab = str.split("\\."); |
| 135 | str = tab[0]; |
| 136 | projectId = Integer.parseInt(str); |
| 137 | |
| 138 | String coverUrl = img.absUrl("src"); |
| 139 | try { |
| 140 | InputStream in = Instance.getInstance().getCache().open(new URL(coverUrl), getSupport(), |
| 141 | true); |
| 142 | try { |
| 143 | meta.setCover(new Image(in)); |
| 144 | } finally { |
| 145 | in.close(); |
| 146 | } |
| 147 | } catch (Exception e) { |
| 148 | // Happen often on MangaLEL... |
| 149 | Instance.getInstance().getTraceHandler().trace( |
| 150 | "Cannot download cover for MangaLEL story in search mode: " + meta.getTitle()); |
| 151 | } |
| 152 | } catch (Exception e) { |
| 153 | // no project id... cannot use the story :( |
| 154 | Instance.getInstance().getTraceHandler() |
| 155 | .error("Cannot find ProjectId for MangaLEL story in search mode: " + meta.getTitle()); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | if (projectId >= 0) { |
| 160 | meta.setUrl("http://mangas-lecture-en-ligne.fr/index_lel.php?page=presentationProjet&idProjet=" |
| 161 | + projectId); |
| 162 | meta.setUuid(meta.getUrl()); |
| 163 | metas.add(meta); |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | return metas; |
| 169 | } |
| 170 | |
| 171 | private String getVal(String[] tab, int i) { |
| 172 | String val = ""; |
| 173 | |
| 174 | if (i < tab.length) { |
| 175 | val = StringUtils.unhtml(tab[i]); |
| 176 | int pos = val.indexOf(":"); |
| 177 | if (pos >= 0) { |
| 178 | val = val.substring(pos + 1).trim(); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | return val; |
| 183 | } |
| 184 | } |