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