Commit | Line | Data |
---|---|---|
08fe2e33 NR |
1 | package be.nikiroo.fanfix.supported; |
2 | ||
68686a37 | 3 | import java.awt.image.BufferedImage; |
08fe2e33 NR |
4 | import java.io.IOException; |
5 | import java.io.InputStream; | |
6 | import java.net.MalformedURLException; | |
7 | import java.net.URL; | |
8 | import java.util.ArrayList; | |
9 | import java.util.Collections; | |
10 | import java.util.List; | |
11 | import java.util.Map.Entry; | |
12 | import java.util.Scanner; | |
13 | ||
68686a37 NR |
14 | import javax.imageio.ImageIO; |
15 | ||
08fe2e33 | 16 | import be.nikiroo.fanfix.Instance; |
68686a37 | 17 | import be.nikiroo.fanfix.data.MetaData; |
08fe2e33 NR |
18 | import be.nikiroo.utils.StringUtils; |
19 | ||
20 | class MangaFox extends BasicSupport { | |
21 | @Override | |
22 | protected boolean isHtml() { | |
23 | return true; | |
24 | } | |
25 | ||
26 | @Override | |
27 | public String getSourceName() { | |
d3c15421 | 28 | return "MangaFox.me"; |
08fe2e33 NR |
29 | } |
30 | ||
31 | @Override | |
68686a37 NR |
32 | protected MetaData getMeta(URL source, InputStream in) throws IOException { |
33 | MetaData meta = new MetaData(); | |
34 | ||
35 | meta.setTitle(getTitle(reset(in))); | |
36 | meta.setAuthor(getAuthor(reset(in))); | |
37 | meta.setDate(getDate(reset(in))); | |
38 | meta.setTags(getTags(reset(in))); | |
39 | meta.setSource(getSourceName()); | |
2206ef66 | 40 | meta.setUrl(source.toString()); |
68686a37 NR |
41 | meta.setPublisher(getSourceName()); |
42 | meta.setUuid(source.toString()); | |
43 | meta.setLuid(""); | |
44 | meta.setLang("EN"); | |
45 | meta.setSubject("manga"); | |
46 | meta.setType(getType().toString()); | |
47 | meta.setImageDocument(true); | |
48 | meta.setCover(getCover(reset(in))); | |
49 | ||
50 | return meta; | |
08fe2e33 NR |
51 | } |
52 | ||
68686a37 | 53 | private List<String> getTags(InputStream in) { |
08fe2e33 NR |
54 | List<String> tags = new ArrayList<String>(); |
55 | ||
56 | String line = getLine(in, "/genres/", 0); | |
57 | if (line != null) { | |
58 | line = StringUtils.unhtml(line); | |
59 | String[] tab = line.split(","); | |
60 | if (tab != null) { | |
61 | for (String tag : tab) { | |
62 | tags.add(tag.trim()); | |
63 | } | |
64 | } | |
65 | } | |
66 | ||
67 | return tags; | |
68 | } | |
69 | ||
68686a37 | 70 | private String getTitle(InputStream in) { |
08fe2e33 NR |
71 | String line = getLine(in, " property=\"og:title\"", 0); |
72 | if (line != null) { | |
73 | int pos = -1; | |
74 | for (int i = 0; i < 3; i++) { | |
75 | pos = line.indexOf('"', pos + 1); | |
76 | } | |
77 | ||
78 | if (pos >= 0) { | |
79 | line = line.substring(pos + 1); | |
80 | pos = line.indexOf('"'); | |
81 | if (pos >= 0) { | |
82 | return line.substring(0, pos); | |
83 | } | |
84 | } | |
85 | } | |
86 | ||
87 | return null; | |
88 | } | |
89 | ||
68686a37 | 90 | private String getAuthor(InputStream in) { |
08fe2e33 NR |
91 | List<String> authors = new ArrayList<String>(); |
92 | ||
93 | String line = getLine(in, "/author/", 0, false); | |
94 | if (line != null) { | |
95 | for (String ln : StringUtils.unhtml(line).split(",")) { | |
96 | if (ln != null && !ln.trim().isEmpty() | |
97 | && !authors.contains(ln.trim())) { | |
98 | authors.add(ln.trim()); | |
99 | } | |
100 | } | |
101 | } | |
102 | ||
103 | try { | |
104 | in.reset(); | |
105 | } catch (IOException e) { | |
106 | Instance.syserr(e); | |
107 | } | |
108 | ||
109 | line = getLine(in, "/artist/", 0, false); | |
110 | if (line != null) { | |
111 | for (String ln : StringUtils.unhtml(line).split(",")) { | |
112 | if (ln != null && !ln.trim().isEmpty() | |
113 | && !authors.contains(ln.trim())) { | |
114 | authors.add(ln.trim()); | |
115 | } | |
116 | } | |
117 | } | |
118 | ||
119 | if (authors.isEmpty()) { | |
120 | return null; | |
121 | } else { | |
122 | StringBuilder builder = new StringBuilder(); | |
123 | for (String author : authors) { | |
124 | if (builder.length() > 0) { | |
125 | builder.append(", "); | |
126 | } | |
127 | ||
128 | builder.append(author); | |
129 | } | |
130 | ||
131 | return builder.toString(); | |
132 | } | |
133 | } | |
134 | ||
68686a37 | 135 | private String getDate(InputStream in) { |
08fe2e33 NR |
136 | String line = getLine(in, "/released/", 0); |
137 | if (line != null) { | |
138 | line = StringUtils.unhtml(line); | |
139 | return line.trim(); | |
140 | } | |
141 | ||
142 | return null; | |
143 | } | |
144 | ||
145 | @Override | |
146 | protected String getDesc(URL source, InputStream in) { | |
147 | String line = getLine(in, " property=\"og:description\"", 0); | |
148 | if (line != null) { | |
149 | int pos = -1; | |
150 | for (int i = 0; i < 3; i++) { | |
151 | pos = line.indexOf('"', pos + 1); | |
152 | } | |
153 | ||
154 | if (pos >= 0) { | |
155 | line = line.substring(pos + 1); | |
156 | pos = line.indexOf('"'); | |
157 | if (pos >= 0) { | |
158 | return line.substring(0, pos); | |
159 | } | |
160 | } | |
161 | } | |
162 | ||
163 | return null; | |
164 | } | |
165 | ||
68686a37 | 166 | private BufferedImage getCover(InputStream in) { |
08fe2e33 NR |
167 | String line = getLine(in, " property=\"og:image\"", 0); |
168 | String cover = null; | |
169 | if (line != null) { | |
170 | int pos = -1; | |
171 | for (int i = 0; i < 3; i++) { | |
172 | pos = line.indexOf('"', pos + 1); | |
173 | } | |
174 | ||
175 | if (pos >= 0) { | |
176 | line = line.substring(pos + 1); | |
177 | pos = line.indexOf('"'); | |
178 | if (pos >= 0) { | |
179 | cover = line.substring(0, pos); | |
180 | } | |
181 | } | |
182 | } | |
183 | ||
184 | if (cover != null) { | |
68686a37 | 185 | InputStream coverIn; |
08fe2e33 | 186 | try { |
68686a37 NR |
187 | coverIn = openEx(cover); |
188 | try { | |
189 | return ImageIO.read(coverIn); | |
190 | } finally { | |
191 | coverIn.close(); | |
192 | } | |
193 | } catch (IOException e) { | |
08fe2e33 NR |
194 | } |
195 | } | |
196 | ||
197 | return null; | |
198 | } | |
199 | ||
200 | @Override | |
201 | protected List<Entry<String, URL>> getChapters(URL source, InputStream in) { | |
202 | List<Entry<String, URL>> urls = new ArrayList<Entry<String, URL>>(); | |
203 | ||
204 | String volumeAt = "<h3 class=\"volume\">"; | |
205 | String linkAt = "href=\"http://mangafox.me/"; | |
206 | String endAt = "<script type=\"text/javascript\">"; | |
207 | ||
208 | boolean started = false; | |
209 | ||
210 | @SuppressWarnings("resource") | |
211 | Scanner scan = new Scanner(in, "UTF-8"); | |
212 | scan.useDelimiter("\\n"); | |
213 | while (scan.hasNext()) { | |
214 | String line = scan.next(); | |
215 | ||
216 | if (started && line.contains(endAt)) { | |
217 | break; | |
218 | } else if (!started && line.contains(volumeAt)) { | |
219 | started = true; | |
220 | } | |
221 | ||
222 | if (started && line.contains(linkAt)) { | |
223 | // Chapter content url | |
224 | String url = null; | |
225 | int pos = line.indexOf("href=\""); | |
226 | if (pos >= 0) { | |
227 | line = line.substring(pos + "href=\"".length()); | |
228 | pos = line.indexOf('\"'); | |
229 | if (pos >= 0) { | |
230 | url = line.substring(0, pos); | |
231 | } | |
232 | } | |
233 | ||
234 | // Chapter name | |
235 | String name = null; | |
236 | if (scan.hasNext()) { | |
237 | name = StringUtils.unhtml(scan.next()).trim(); | |
238 | // Remove the "new" tag if present | |
239 | if (name.endsWith("new")) { | |
240 | name = name.substring(0, name.length() - 3).trim(); | |
241 | } | |
242 | } | |
243 | ||
244 | // to help with the retry and the originalUrl | |
245 | refresh(url); | |
246 | ||
247 | try { | |
248 | final String key = name; | |
249 | final URL value = new URL(url); | |
250 | urls.add(new Entry<String, URL>() { | |
251 | public URL setValue(URL value) { | |
252 | return null; | |
253 | } | |
254 | ||
255 | public String getKey() { | |
256 | return key; | |
257 | } | |
258 | ||
259 | public URL getValue() { | |
260 | return value; | |
261 | } | |
262 | }); | |
263 | } catch (MalformedURLException e) { | |
264 | Instance.syserr(e); | |
265 | } | |
266 | } | |
267 | } | |
268 | ||
269 | // the chapters are in reversed order | |
270 | Collections.reverse(urls); | |
271 | ||
272 | return urls; | |
273 | } | |
274 | ||
275 | @Override | |
276 | protected String getChapterContent(URL source, InputStream in, int number) { | |
277 | StringBuilder builder = new StringBuilder(); | |
278 | String base = getCurrentReferer().toString(); | |
279 | int pos = base.lastIndexOf('/'); | |
280 | base = base.substring(0, pos + 1); // including the '/' at the end | |
281 | ||
282 | boolean close = false; | |
283 | while (in != null) { | |
284 | String linkNextLine = getLine(in, "return enlarge()", 0); | |
285 | try { | |
286 | in.reset(); | |
287 | } catch (IOException e) { | |
288 | Instance.syserr(e); | |
289 | } | |
290 | ||
291 | String linkImageLine = getLine(in, "return enlarge()", 1); | |
292 | String linkNext = null; | |
293 | String linkImage = null; | |
294 | pos = linkNextLine.indexOf("href=\""); | |
295 | if (pos >= 0) { | |
296 | linkNextLine = linkNextLine.substring(pos + "href=\"".length()); | |
297 | pos = linkNextLine.indexOf('\"'); | |
298 | if (pos >= 0) { | |
299 | linkNext = linkNextLine.substring(0, pos); | |
300 | } | |
301 | } | |
302 | pos = linkImageLine.indexOf("src=\""); | |
303 | if (pos >= 0) { | |
304 | linkImageLine = linkImageLine | |
305 | .substring(pos + "src=\"".length()); | |
306 | pos = linkImageLine.indexOf('\"'); | |
307 | if (pos >= 0) { | |
308 | linkImage = linkImageLine.substring(0, pos); | |
309 | } | |
310 | } | |
311 | ||
312 | if (linkImage != null) { | |
313 | builder.append("["); | |
314 | // to help with the retry and the originalUrl, part 1 | |
315 | builder.append(withoutQuery(linkImage)); | |
316 | builder.append("]\n"); | |
317 | } | |
318 | ||
319 | // to help with the retry and the originalUrl, part 2 | |
320 | refresh(linkImage); | |
321 | ||
322 | if (close) { | |
323 | try { | |
324 | in.close(); | |
325 | } catch (IOException e) { | |
326 | Instance.syserr(e); | |
327 | } | |
328 | } | |
329 | ||
330 | in = null; | |
331 | if (linkNext != null && !"javascript:void(0);".equals(linkNext)) { | |
332 | URL url; | |
333 | try { | |
334 | url = new URL(base + linkNext); | |
335 | in = openEx(base + linkNext); | |
336 | setCurrentReferer(url); | |
337 | } catch (IOException e) { | |
338 | Instance.syserr(new IOException( | |
339 | "Cannot get the next manga page which is: " | |
340 | + linkNext, e)); | |
341 | } | |
342 | } | |
343 | ||
344 | close = true; | |
345 | } | |
346 | ||
347 | setCurrentReferer(source); | |
348 | return builder.toString(); | |
349 | } | |
350 | ||
351 | @Override | |
352 | protected boolean supports(URL url) { | |
353 | return "mangafox.me".equals(url.getHost()) | |
354 | || "www.mangafox.me".equals(url.getHost()); | |
355 | } | |
356 | ||
357 | /** | |
358 | * Refresh the {@link URL} by calling {@link MangaFox#openEx(String)}. | |
359 | * | |
360 | * @param url | |
361 | * the URL to refresh | |
362 | * | |
363 | * @return TRUE if it was refreshed | |
364 | */ | |
365 | private boolean refresh(String url) { | |
366 | try { | |
367 | openEx(url).close(); | |
368 | return true; | |
369 | } catch (Exception e) { | |
370 | return false; | |
371 | } | |
372 | } | |
373 | ||
374 | /** | |
375 | * Open the URL through the cache, but: retry a second time after 100ms if | |
376 | * it fails, remove the query part of the {@link URL} before saving it to | |
377 | * the cache (so it can be recalled later). | |
378 | * | |
379 | * @param url | |
380 | * the {@link URL} | |
381 | * | |
382 | * @return the resource | |
383 | * | |
384 | * @throws IOException | |
385 | * in case of I/O error | |
386 | */ | |
387 | private InputStream openEx(String url) throws IOException { | |
388 | try { | |
389 | return Instance.getCache().open(new URL(url), this, true, | |
390 | withoutQuery(url)); | |
391 | } catch (Exception e) { | |
392 | // second chance | |
393 | try { | |
394 | Thread.sleep(100); | |
395 | } catch (InterruptedException ee) { | |
396 | } | |
397 | ||
398 | return Instance.getCache().open(new URL(url), this, true, | |
399 | withoutQuery(url)); | |
400 | } | |
401 | } | |
402 | ||
403 | /** | |
404 | * Return the same input {@link URL} but without the query part. | |
405 | * | |
406 | * @param url | |
407 | * the inpiut {@link URL} as a {@link String} | |
408 | * | |
409 | * @return the input {@link URL} without query | |
410 | */ | |
411 | private URL withoutQuery(String url) { | |
412 | URL o = null; | |
413 | try { | |
414 | // Remove the query from o (originalUrl), so it can be cached | |
415 | // correctly | |
416 | o = new URL(url); | |
417 | o = new URL(o.getProtocol() + "://" + o.getHost() + o.getPath()); | |
418 | ||
419 | return o; | |
420 | } catch (MalformedURLException e) { | |
421 | return null; | |
422 | } | |
423 | } | |
424 | } |