Add more warnings source to 1.6) and fix warnings
[nikiroo-utils.git] / src / be / nikiroo / fanfix / supported / MangaFox.java
1 package be.nikiroo.fanfix.supported;
2
3 import java.awt.image.BufferedImage;
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
14 import be.nikiroo.fanfix.Instance;
15 import be.nikiroo.fanfix.data.MetaData;
16 import be.nikiroo.utils.ImageUtils;
17 import be.nikiroo.utils.Progress;
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() {
28 return "MangaFox.me";
29 }
30
31 @Override
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());
40 meta.setUrl(source.toString());
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;
51 }
52
53 private List<String> getTags(InputStream in) {
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
70 private String getTitle(InputStream in) {
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
90 private String getAuthor(InputStream in) {
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 }
122
123 StringBuilder builder = new StringBuilder();
124 for (String author : authors) {
125 if (builder.length() > 0) {
126 builder.append(", ");
127 }
128
129 builder.append(author);
130 }
131
132 return builder.toString();
133 }
134
135 private String getDate(InputStream in) {
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
166 private BufferedImage getCover(InputStream in) {
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) {
185 InputStream coverIn;
186 try {
187 coverIn = openEx(cover);
188 try {
189 return ImageUtils.fromStream(coverIn);
190 } finally {
191 coverIn.close();
192 }
193 } catch (IOException e) {
194 }
195 }
196
197 return null;
198 }
199
200 @Override
201 protected List<Entry<String, URL>> getChapters(URL source, InputStream in,
202 Progress pg) {
203 List<Entry<String, URL>> urls = new ArrayList<Entry<String, URL>>();
204
205 String volumeAt = "<h3 class=\"volume\">";
206 String linkAt = "href=\"http://mangafox.me/";
207 String endAt = "<script type=\"text/javascript\">";
208
209 boolean started = false;
210
211 @SuppressWarnings("resource")
212 Scanner scan = new Scanner(in, "UTF-8");
213 scan.useDelimiter("\\n");
214 while (scan.hasNext()) {
215 String line = scan.next();
216
217 if (started && line.contains(endAt)) {
218 break;
219 } else if (!started && line.contains(volumeAt)) {
220 started = true;
221 }
222
223 if (started && line.contains(linkAt)) {
224 // Chapter content url
225 String url = null;
226 int pos = line.indexOf("href=\"");
227 if (pos >= 0) {
228 line = line.substring(pos + "href=\"".length());
229 pos = line.indexOf('\"');
230 if (pos >= 0) {
231 url = line.substring(0, pos);
232 }
233 }
234
235 // Chapter name
236 String name = null;
237 if (scan.hasNext()) {
238 name = StringUtils.unhtml(scan.next()).trim();
239 // Remove the "new" tag if present
240 if (name.endsWith("new")) {
241 name = name.substring(0, name.length() - 3).trim();
242 }
243 }
244
245 try {
246 final String key = name;
247 final URL value = new URL(url);
248 urls.add(new Entry<String, URL>() {
249 @Override
250 public URL setValue(URL value) {
251 return null;
252 }
253
254 @Override
255 public String getKey() {
256 return key;
257 }
258
259 @Override
260 public URL getValue() {
261 return value;
262 }
263 });
264 } catch (MalformedURLException e) {
265 Instance.syserr(e);
266 }
267 }
268 }
269
270 if (pg == null) {
271 pg = new Progress(0, urls.size());
272 } else {
273 pg.setMinMax(0, urls.size());
274 }
275
276 int i = 1;
277 for (Entry<String, URL> entry : urls) {
278 // to help with the retry and the originalUrl
279 refresh(entry.getValue().toString());
280 pg.setProgress(i++);
281 }
282
283 // the chapters are in reversed order
284 Collections.reverse(urls);
285
286 return urls;
287 }
288
289 @Override
290 protected String getChapterContent(URL source, InputStream in, int number,
291 Progress pg) {
292 if (pg == null) {
293 pg = new Progress();
294 } else {
295 // Since we have no idea how many images we have, we cycle from 0
296 // to max, then again, then again...
297 pg.setMinMax(0, 20);
298 }
299
300 StringBuilder builder = new StringBuilder();
301 String base = getCurrentReferer().toString();
302 int pos = base.lastIndexOf('/');
303 base = base.substring(0, pos + 1); // including the '/' at the end
304
305 int i = 1;
306 boolean close = false;
307 while (in != null) {
308 String linkNextLine = getLine(in, "return enlarge()", 0);
309 try {
310 in.reset();
311 } catch (IOException e) {
312 Instance.syserr(e);
313 }
314
315 String linkImageLine = getLine(in, "return enlarge()", 1);
316 String linkNext = null;
317 String linkImage = null;
318 pos = linkNextLine.indexOf("href=\"");
319 if (pos >= 0) {
320 linkNextLine = linkNextLine.substring(pos + "href=\"".length());
321 pos = linkNextLine.indexOf('\"');
322 if (pos >= 0) {
323 linkNext = linkNextLine.substring(0, pos);
324 }
325 }
326 pos = linkImageLine.indexOf("src=\"");
327 if (pos >= 0) {
328 linkImageLine = linkImageLine
329 .substring(pos + "src=\"".length());
330 pos = linkImageLine.indexOf('\"');
331 if (pos >= 0) {
332 linkImage = linkImageLine.substring(0, pos);
333 }
334 }
335
336 if (linkImage != null) {
337 builder.append("[");
338 // to help with the retry and the originalUrl, part 1
339 builder.append(withoutQuery(linkImage));
340 builder.append("]<br/>");
341 }
342
343 // to help with the retry and the originalUrl, part 2
344 refresh(linkImage);
345 pg.setProgress((i++) % pg.getMax());
346
347 if (close) {
348 try {
349 in.close();
350 } catch (IOException e) {
351 Instance.syserr(e);
352 }
353 }
354
355 in = null;
356 if (linkNext != null && !"javascript:void(0);".equals(linkNext)) {
357 URL url;
358 try {
359 url = new URL(base + linkNext);
360 in = openEx(base + linkNext);
361 setCurrentReferer(url);
362 pg.setProgress((i++) % pg.getMax());
363 } catch (IOException e) {
364 Instance.syserr(new IOException(
365 "Cannot get the next manga page which is: "
366 + linkNext, e));
367 }
368 }
369
370 close = true;
371 }
372
373 setCurrentReferer(source);
374 return builder.toString();
375 }
376
377 @Override
378 protected boolean supports(URL url) {
379 return "mangafox.me".equals(url.getHost())
380 || "www.mangafox.me".equals(url.getHost());
381 }
382
383 /**
384 * Refresh the {@link URL} by calling {@link MangaFox#openEx(String)}.
385 *
386 * @param url
387 * the URL to refresh
388 *
389 * @return TRUE if it was refreshed
390 */
391 private boolean refresh(String url) {
392 try {
393 openEx(url).close();
394 return true;
395 } catch (Exception e) {
396 return false;
397 }
398 }
399
400 /**
401 * Open the URL through the cache, but: retry a second time after 100ms if
402 * it fails, remove the query part of the {@link URL} before saving it to
403 * the cache (so it can be recalled later).
404 *
405 * @param url
406 * the {@link URL}
407 *
408 * @return the resource
409 *
410 * @throws IOException
411 * in case of I/O error
412 */
413 private InputStream openEx(String url) throws IOException {
414 try {
415 return Instance.getCache().open(new URL(url), this, true,
416 withoutQuery(url));
417 } catch (Exception e) {
418 // second chance
419 try {
420 Thread.sleep(100);
421 } catch (InterruptedException ee) {
422 }
423
424 return Instance.getCache().open(new URL(url), this, true,
425 withoutQuery(url));
426 }
427 }
428
429 /**
430 * Return the same input {@link URL} but without the query part.
431 *
432 * @param url
433 * the inpiut {@link URL} as a {@link String}
434 *
435 * @return the input {@link URL} without query
436 */
437 private URL withoutQuery(String url) {
438 URL o = null;
439 try {
440 // Remove the query from o (originalUrl), so it can be cached
441 // correctly
442 o = new URL(url);
443 o = new URL(o.getProtocol() + "://" + o.getHost() + o.getPath());
444
445 return o;
446 } catch (MalformedURLException e) {
447 return null;
448 }
449 }
450 }