Commit | Line | Data |
---|---|---|
4749f2c5 NR |
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 | ||
4749f2c5 NR |
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 | ||
81acd363 NR |
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 | ||
4749f2c5 NR |
89 | private List<MetaData> getResults(String sourceUrl) throws IOException { |
90 | List<MetaData> metas = new ArrayList<MetaData>(); | |
91 | ||
d66deb8d NR |
92 | Document doc = DataUtil.load(Instance.getInstance().getCache().open(new URL(sourceUrl), getSupport(), false), |
93 | "UTF-8", sourceUrl); | |
4749f2c5 NR |
94 | |
95 | for (Element result : doc.getElementsByClass("rechercheAffichage")) { | |
96 | Element a = result.getElementsByTag("a").first(); | |
97 | if (a != null) { | |
12443642 NR |
98 | int projectId = -1; |
99 | ||
4749f2c5 | 100 | MetaData meta = new MetaData(); |
4749f2c5 | 101 | |
12443642 NR |
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> | |
4749f2c5 NR |
110 | |
111 | Elements infos = result.getElementsByClass("texte"); | |
112 | if (infos != null) { | |
113 | String[] tab = infos.outerHtml().split("<br>"); | |
114 | ||
d16065ec NR |
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); | |
4749f2c5 NR |
121 | meta.setTitle(getVal(tab, 0)); |
122 | meta.setAuthor(getVal(tab, 1)); | |
123 | meta.setTags(Arrays.asList(getVal(tab, 2).split(" "))); | |
124 | ||
d66deb8d NR |
125 | meta.setResume(getSupport().makeChapter(new URL(sourceUrl), 0, |
126 | Instance.getInstance().getTrans().getString(StringId.DESCRIPTION), getVal(tab, 5))); | |
4749f2c5 NR |
127 | } |
128 | ||
12443642 NR |
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 { | |
d66deb8d NR |
140 | InputStream in = Instance.getInstance().getCache().open(new URL(coverUrl), getSupport(), |
141 | true); | |
12443642 | 142 | try { |
002972e9 NR |
143 | Image ii = new Image(in); |
144 | if (ii.getSize() == 0) { | |
145 | ii.close(); | |
146 | throw new IOException( | |
147 | "Empty image not accepted"); | |
148 | } | |
149 | meta.setCover(ii); | |
12443642 NR |
150 | } finally { |
151 | in.close(); | |
152 | } | |
153 | } catch (Exception e) { | |
154 | // Happen often on MangaLEL... | |
d66deb8d NR |
155 | Instance.getInstance().getTraceHandler().trace( |
156 | "Cannot download cover for MangaLEL story in search mode: " + meta.getTitle()); | |
12443642 NR |
157 | } |
158 | } catch (Exception e) { | |
159 | // no project id... cannot use the story :( | |
d66deb8d NR |
160 | Instance.getInstance().getTraceHandler() |
161 | .error("Cannot find ProjectId for MangaLEL story in search mode: " + meta.getTitle()); | |
12443642 NR |
162 | } |
163 | } | |
164 | ||
165 | if (projectId >= 0) { | |
166 | meta.setUrl("http://mangas-lecture-en-ligne.fr/index_lel.php?page=presentationProjet&idProjet=" | |
167 | + projectId); | |
d16065ec | 168 | meta.setUuid(meta.getUrl()); |
12443642 NR |
169 | metas.add(meta); |
170 | } | |
4749f2c5 NR |
171 | } |
172 | } | |
173 | ||
174 | return metas; | |
175 | } | |
176 | ||
177 | private String getVal(String[] tab, int i) { | |
178 | String val = ""; | |
179 | ||
180 | if (i < tab.length) { | |
181 | val = StringUtils.unhtml(tab[i]); | |
182 | int pos = val.indexOf(":"); | |
183 | if (pos >= 0) { | |
184 | val = val.substring(pos + 1).trim(); | |
185 | } | |
186 | } | |
187 | ||
188 | return val; | |
189 | } | |
190 | } |