@Test public void testParse() throws Exception { InputStream resource = GalleryPageApiParserTest.class.getResourceAsStream(file); BufferedSource source = Okio.buffer(Okio.source(resource)); String body = source.readUtf8(); GalleryListParser.Result result = GalleryListParser.parse(body); assertEquals(25, result.galleryInfoList.size()); result.galleryInfoList.forEach(gi -> { assertNotEquals(0, gi.gid); assertNotEquals(0, gi.token); assertNotNull(gi.title); assertNotEquals(0, gi.category); assertNotEquals(EhUtils.UNKNOWN, gi.category); assertNotEquals(0, gi.thumbWidth); assertNotEquals(0, gi.thumbHeight); assertNotNull(gi.thumb); assertNotNull(gi.posted); assertNotEquals(0.0, gi.rating); if (E_MINIMAL.equals(file) || E_MINIMAL_PLUS.equals(file) || E_COMPAT.equals(file) || E_EXTENDED.equals(file) || EX_MINIMAL.equals(file) || EX_MINIMAL_PLUS.equals(file) || EX_COMPAT.equals(file) || EX_EXTENDED.equals(file)) { assertNotNull(gi.uploader); } else { assertNull(gi.uploader); } assertNotEquals(0, gi.pages); }); }
public static Result parse(@NonNull String body) throws Exception { Result result = new Result(); Document d = Jsoup.parse(body); try { Element ptt = d.getElementsByClass("ptt").first(); Elements es = ptt.child(0).child(0).children(); result.pages = Integer.parseInt(es.get(es.size() - 2).text().trim()); Element e = es.get(es.size() - 1); if (e != null) { e = e.children().first(); if (e != null) { String href = e.attr("href"); Matcher matcher = PATTERN_NEXT_PAGE.matcher(href); if (matcher.find()) { result.nextPage = NumberUtils.parseIntSafely(matcher.group(1), 0); } } } } catch (Throwable e) { ExceptionUtils.throwIfFatal(e); result.noWatchedTags = body.contains("<p>You do not have any watched tags"); if (body.contains("No hits found</p>")) { result.pages = 0; result.galleryInfoList = Collections.EMPTY_LIST; return result; } else if (d.getElementsByClass("ptt").isEmpty()) { result.pages = 1; } else { result.pages = Integer.MAX_VALUE; } } try { Element itg = d.getElementsByClass("itg").first(); Elements es; if ("table".equalsIgnoreCase(itg.tagName())) { es = itg.child(0).children(); } else { es = itg.children(); } List<GalleryInfo> list = new ArrayList<>(es.size()); for (int i = 0; i < es.size(); i++) { GalleryInfo gi = parseGalleryInfo(es.get(i)); if (null != gi) { list.add(gi); } } if (list.isEmpty()) { throw new ParseException("No gallery", body); } result.galleryInfoList = list; } catch (Throwable e) { ExceptionUtils.throwIfFatal(e); e.printStackTrace(); throw new ParseException("Can't parse gallery list", body); } return result; }
GalleryListParser { public static Result parse(@NonNull String body) throws Exception { Result result = new Result(); Document d = Jsoup.parse(body); try { Element ptt = d.getElementsByClass("ptt").first(); Elements es = ptt.child(0).child(0).children(); result.pages = Integer.parseInt(es.get(es.size() - 2).text().trim()); Element e = es.get(es.size() - 1); if (e != null) { e = e.children().first(); if (e != null) { String href = e.attr("href"); Matcher matcher = PATTERN_NEXT_PAGE.matcher(href); if (matcher.find()) { result.nextPage = NumberUtils.parseIntSafely(matcher.group(1), 0); } } } } catch (Throwable e) { ExceptionUtils.throwIfFatal(e); result.noWatchedTags = body.contains("<p>You do not have any watched tags"); if (body.contains("No hits found</p>")) { result.pages = 0; result.galleryInfoList = Collections.EMPTY_LIST; return result; } else if (d.getElementsByClass("ptt").isEmpty()) { result.pages = 1; } else { result.pages = Integer.MAX_VALUE; } } try { Element itg = d.getElementsByClass("itg").first(); Elements es; if ("table".equalsIgnoreCase(itg.tagName())) { es = itg.child(0).children(); } else { es = itg.children(); } List<GalleryInfo> list = new ArrayList<>(es.size()); for (int i = 0; i < es.size(); i++) { GalleryInfo gi = parseGalleryInfo(es.get(i)); if (null != gi) { list.add(gi); } } if (list.isEmpty()) { throw new ParseException("No gallery", body); } result.galleryInfoList = list; } catch (Throwable e) { ExceptionUtils.throwIfFatal(e); e.printStackTrace(); throw new ParseException("Can't parse gallery list", body); } return result; } }
GalleryListParser { public static Result parse(@NonNull String body) throws Exception { Result result = new Result(); Document d = Jsoup.parse(body); try { Element ptt = d.getElementsByClass("ptt").first(); Elements es = ptt.child(0).child(0).children(); result.pages = Integer.parseInt(es.get(es.size() - 2).text().trim()); Element e = es.get(es.size() - 1); if (e != null) { e = e.children().first(); if (e != null) { String href = e.attr("href"); Matcher matcher = PATTERN_NEXT_PAGE.matcher(href); if (matcher.find()) { result.nextPage = NumberUtils.parseIntSafely(matcher.group(1), 0); } } } } catch (Throwable e) { ExceptionUtils.throwIfFatal(e); result.noWatchedTags = body.contains("<p>You do not have any watched tags"); if (body.contains("No hits found</p>")) { result.pages = 0; result.galleryInfoList = Collections.EMPTY_LIST; return result; } else if (d.getElementsByClass("ptt").isEmpty()) { result.pages = 1; } else { result.pages = Integer.MAX_VALUE; } } try { Element itg = d.getElementsByClass("itg").first(); Elements es; if ("table".equalsIgnoreCase(itg.tagName())) { es = itg.child(0).children(); } else { es = itg.children(); } List<GalleryInfo> list = new ArrayList<>(es.size()); for (int i = 0; i < es.size(); i++) { GalleryInfo gi = parseGalleryInfo(es.get(i)); if (null != gi) { list.add(gi); } } if (list.isEmpty()) { throw new ParseException("No gallery", body); } result.galleryInfoList = list; } catch (Throwable e) { ExceptionUtils.throwIfFatal(e); e.printStackTrace(); throw new ParseException("Can't parse gallery list", body); } return result; }  }
GalleryListParser { public static Result parse(@NonNull String body) throws Exception { Result result = new Result(); Document d = Jsoup.parse(body); try { Element ptt = d.getElementsByClass("ptt").first(); Elements es = ptt.child(0).child(0).children(); result.pages = Integer.parseInt(es.get(es.size() - 2).text().trim()); Element e = es.get(es.size() - 1); if (e != null) { e = e.children().first(); if (e != null) { String href = e.attr("href"); Matcher matcher = PATTERN_NEXT_PAGE.matcher(href); if (matcher.find()) { result.nextPage = NumberUtils.parseIntSafely(matcher.group(1), 0); } } } } catch (Throwable e) { ExceptionUtils.throwIfFatal(e); result.noWatchedTags = body.contains("<p>You do not have any watched tags"); if (body.contains("No hits found</p>")) { result.pages = 0; result.galleryInfoList = Collections.EMPTY_LIST; return result; } else if (d.getElementsByClass("ptt").isEmpty()) { result.pages = 1; } else { result.pages = Integer.MAX_VALUE; } } try { Element itg = d.getElementsByClass("itg").first(); Elements es; if ("table".equalsIgnoreCase(itg.tagName())) { es = itg.child(0).children(); } else { es = itg.children(); } List<GalleryInfo> list = new ArrayList<>(es.size()); for (int i = 0; i < es.size(); i++) { GalleryInfo gi = parseGalleryInfo(es.get(i)); if (null != gi) { list.add(gi); } } if (list.isEmpty()) { throw new ParseException("No gallery", body); } result.galleryInfoList = list; } catch (Throwable e) { ExceptionUtils.throwIfFatal(e); e.printStackTrace(); throw new ParseException("Can't parse gallery list", body); } return result; }  static Result parse(@NonNull String body); }
GalleryListParser { public static Result parse(@NonNull String body) throws Exception { Result result = new Result(); Document d = Jsoup.parse(body); try { Element ptt = d.getElementsByClass("ptt").first(); Elements es = ptt.child(0).child(0).children(); result.pages = Integer.parseInt(es.get(es.size() - 2).text().trim()); Element e = es.get(es.size() - 1); if (e != null) { e = e.children().first(); if (e != null) { String href = e.attr("href"); Matcher matcher = PATTERN_NEXT_PAGE.matcher(href); if (matcher.find()) { result.nextPage = NumberUtils.parseIntSafely(matcher.group(1), 0); } } } } catch (Throwable e) { ExceptionUtils.throwIfFatal(e); result.noWatchedTags = body.contains("<p>You do not have any watched tags"); if (body.contains("No hits found</p>")) { result.pages = 0; result.galleryInfoList = Collections.EMPTY_LIST; return result; } else if (d.getElementsByClass("ptt").isEmpty()) { result.pages = 1; } else { result.pages = Integer.MAX_VALUE; } } try { Element itg = d.getElementsByClass("itg").first(); Elements es; if ("table".equalsIgnoreCase(itg.tagName())) { es = itg.child(0).children(); } else { es = itg.children(); } List<GalleryInfo> list = new ArrayList<>(es.size()); for (int i = 0; i < es.size(); i++) { GalleryInfo gi = parseGalleryInfo(es.get(i)); if (null != gi) { list.add(gi); } } if (list.isEmpty()) { throw new ParseException("No gallery", body); } result.galleryInfoList = list; } catch (Throwable e) { ExceptionUtils.throwIfFatal(e); e.printStackTrace(); throw new ParseException("Can't parse gallery list", body); } return result; }  static Result parse(@NonNull String body);  }