Commit | Line | Data |
---|---|---|
af1f506f NR |
1 | package be.nikiroo.fanfix.supported; |
2 | ||
3 | import java.io.IOException; | |
4 | import java.io.InputStream; | |
af1f506f | 5 | import java.net.URL; |
6fc76bae NR |
6 | import java.text.ParseException; |
7 | import java.text.SimpleDateFormat; | |
af1f506f NR |
8 | import java.util.AbstractMap; |
9 | import java.util.ArrayList; | |
af1f506f NR |
10 | import java.util.List; |
11 | import java.util.Map.Entry; | |
12 | ||
13 | import org.jsoup.helper.DataUtil; | |
14 | import org.jsoup.nodes.Element; | |
15 | import org.jsoup.select.Elements; | |
16 | ||
17 | import be.nikiroo.fanfix.Instance; | |
18 | import be.nikiroo.fanfix.data.MetaData; | |
19 | import be.nikiroo.utils.Image; | |
20 | import be.nikiroo.utils.Progress; | |
21 | import be.nikiroo.utils.StringUtils; | |
22 | ||
23 | class MangaLel extends BasicSupport { | |
24 | @Override | |
25 | protected boolean isHtml() { | |
26 | return true; | |
27 | } | |
28 | ||
af1f506f NR |
29 | @Override |
30 | protected MetaData getMeta() throws IOException { | |
31 | MetaData meta = new MetaData(); | |
32 | ||
af1f506f | 33 | meta.setTitle(getTitle()); |
6fc76bae | 34 | meta.setAuthor(getAuthor()); |
bff19b54 | 35 | meta.setDate(bsHelper.formatDate(getDate())); |
6fc76bae | 36 | meta.setTags(getTags()); |
af1f506f | 37 | meta.setUrl(getSource().toString()); |
af1f506f NR |
38 | meta.setUuid(getSource().toString()); |
39 | meta.setLuid(""); | |
40 | meta.setLang("fr"); | |
41 | meta.setSubject("manga"); | |
af1f506f NR |
42 | meta.setImageDocument(true); |
43 | meta.setCover(getCover()); | |
44 | ||
45 | return meta; | |
46 | } | |
47 | ||
48 | private String getTitle() { | |
49 | Element doc = getSourceNode(); | |
6fc76bae NR |
50 | Element h4 = doc.getElementsByTag("h4").first(); |
51 | if (h4 != null) { | |
52 | return StringUtils.unhtml(h4.text()).trim(); | |
af1f506f NR |
53 | } |
54 | ||
55 | return null; | |
56 | } | |
57 | ||
6fc76bae NR |
58 | private String getAuthor() { |
59 | Element doc = getSourceNode(); | |
12443642 NR |
60 | Element tabEls = doc.getElementsByClass("presentation-projet").first(); |
61 | if (tabEls != null) { | |
62 | String[] tab = tabEls.outerHtml().split("<br>"); | |
63 | return getVal(tab, 1); | |
6fc76bae NR |
64 | } |
65 | ||
12443642 | 66 | return ""; |
6fc76bae NR |
67 | } |
68 | ||
69 | private List<String> getTags() { | |
af1f506f | 70 | Element doc = getSourceNode(); |
12443642 NR |
71 | Element tabEls = doc.getElementsByClass("presentation-projet").first(); |
72 | if (tabEls != null) { | |
73 | String[] tab = tabEls.outerHtml().split("<br>"); | |
74 | List<String> tags = new ArrayList<String>(); | |
75 | for (String tag : getVal(tab, 3).split(" ")) { | |
76 | tags.add(tag); | |
af1f506f | 77 | } |
12443642 | 78 | return tags; |
af1f506f NR |
79 | } |
80 | ||
12443642 NR |
81 | return new ArrayList<String>(); |
82 | ||
6fc76bae NR |
83 | } |
84 | ||
85 | private String getDate() { | |
86 | Element doc = getSourceNode(); | |
87 | Element table = doc.getElementsByClass("table").first(); | |
88 | ||
89 | // We take the first date we find | |
90 | String value = ""; | |
91 | if (table != null) { | |
92 | Elements els; | |
93 | els = table.getElementsByTag("tr"); | |
94 | if (els.size() >= 2) { | |
95 | els = els.get(1).getElementsByTag("td"); | |
96 | if (els.size() >= 3) { | |
97 | value = StringUtils.unhtml(els.get(2).text()).trim(); | |
af1f506f | 98 | } |
af1f506f | 99 | } |
af1f506f NR |
100 | } |
101 | ||
6fc76bae | 102 | return value; |
af1f506f NR |
103 | } |
104 | ||
105 | @Override | |
106 | protected String getDesc() { | |
af1f506f | 107 | Element doc = getSourceNode(); |
12443642 NR |
108 | Element tabEls = doc.getElementsByClass("presentation-projet").first(); |
109 | if (tabEls != null) { | |
110 | String[] tab = tabEls.outerHtml().split("<br>"); | |
111 | return getVal(tab, 4); | |
af1f506f NR |
112 | } |
113 | ||
12443642 | 114 | return ""; |
af1f506f NR |
115 | } |
116 | ||
117 | private Image getCover() { | |
118 | Element doc = getSourceNode(); | |
6fc76bae NR |
119 | Element container = doc.getElementsByClass("container").first(); |
120 | ||
121 | if (container != null) { | |
122 | ||
123 | Elements imgs = container.getElementsByTag("img"); | |
124 | Element img = null; | |
125 | if (imgs.size() >= 1) { | |
126 | img = imgs.get(0); | |
127 | if (img.hasClass("banniere-team-projet")) { | |
128 | img = null; | |
129 | if (imgs.size() >= 2) { | |
130 | img = imgs.get(1); | |
131 | } | |
132 | } | |
133 | } | |
af1f506f | 134 | |
6fc76bae NR |
135 | if (img != null) { |
136 | String coverUrl = img.absUrl("src"); | |
af1f506f | 137 | |
af1f506f | 138 | try { |
0a264fbe NR |
139 | InputStream coverIn = Instance.getInstance().getCache() |
140 | .open(new URL(coverUrl), this, true); | |
6fc76bae | 141 | try { |
0a264fbe NR |
142 | Image ii = new Image(coverIn); |
143 | if (ii.getSize() == 0) { | |
144 | ii.close(); | |
145 | throw new IOException("Empty image not accepted"); | |
146 | } | |
147 | ||
148 | return ii; | |
6fc76bae NR |
149 | } finally { |
150 | coverIn.close(); | |
151 | } | |
152 | } catch (IOException e) { | |
d66deb8d | 153 | Instance.getInstance().getTraceHandler().error(e); |
af1f506f | 154 | } |
af1f506f NR |
155 | } |
156 | } | |
157 | ||
158 | return null; | |
159 | } | |
160 | ||
12443642 NR |
161 | private String getVal(String[] tab, int i) { |
162 | String val = ""; | |
163 | ||
164 | if (i < tab.length) { | |
165 | val = StringUtils.unhtml(tab[i]); | |
166 | int pos = val.indexOf(":"); | |
167 | if (pos >= 0) { | |
168 | val = val.substring(pos + 1).trim(); | |
169 | } | |
170 | } | |
171 | ||
172 | return val; | |
173 | } | |
174 | ||
af1f506f | 175 | @Override |
6fc76bae NR |
176 | protected List<Entry<String, URL>> getChapters(Progress pg) |
177 | throws IOException { | |
af1f506f NR |
178 | List<Entry<String, URL>> urls = new ArrayList<Entry<String, URL>>(); |
179 | ||
af1f506f | 180 | Element doc = getSourceNode(); |
6fc76bae NR |
181 | Element table = doc.getElementsByClass("table").first(); |
182 | if (table != null) { | |
183 | for (Element tr : table.getElementsByTag("tr")) { | |
184 | Element a = tr.getElementsByTag("a").first(); | |
185 | if (a != null) { | |
186 | String name = StringUtils.unhtml(a.text()).trim(); | |
187 | URL url = new URL(a.absUrl("href")); | |
188 | urls.add(new AbstractMap.SimpleEntry<String, URL>(name, url)); | |
189 | } | |
af1f506f NR |
190 | } |
191 | } | |
192 | ||
af1f506f NR |
193 | return urls; |
194 | } | |
195 | ||
196 | @Override | |
197 | protected String getChapterContent(URL chapUrl, int number, Progress pg) | |
198 | throws IOException { | |
199 | if (pg == null) { | |
200 | pg = new Progress(); | |
201 | } | |
202 | ||
203 | StringBuilder builder = new StringBuilder(); | |
204 | ||
d66deb8d | 205 | InputStream in = Instance.getInstance().getCache().open(chapUrl, this, false); |
af1f506f NR |
206 | try { |
207 | Element pageDoc = DataUtil.load(in, "UTF-8", chapUrl.toString()); | |
6fc76bae NR |
208 | Element content = pageDoc.getElementById("content"); |
209 | Elements linkEls = content.getElementsByTag("img"); | |
af1f506f | 210 | for (Element linkEl : linkEls) { |
12443642 | 211 | if (linkEl.absUrl("src").isEmpty()) { |
6fc76bae | 212 | continue; |
af1f506f | 213 | } |
6fc76bae NR |
214 | |
215 | builder.append("["); | |
12443642 | 216 | builder.append(linkEl.absUrl("src")); |
6fc76bae | 217 | builder.append("]<br/>"); |
af1f506f NR |
218 | } |
219 | ||
220 | } finally { | |
221 | in.close(); | |
222 | } | |
223 | ||
224 | return builder.toString(); | |
225 | } | |
226 | ||
af1f506f NR |
227 | @Override |
228 | protected boolean supports(URL url) { | |
6fc76bae NR |
229 | // URL structure (the projectId is the manga key): |
230 | // http://mangas-lecture-en-ligne.fr/index_lel.php?page=presentationProjet&idProjet=999 | |
231 | ||
232 | return "mangas-lecture-en-ligne.fr".equals(url.getHost()); | |
af1f506f NR |
233 | } |
234 | } |