From 7744ed0cac78c68951e2d42d0c2a4b8c79581eed Mon Sep 17 00:00:00 2001 From: NekoInverter <7741672-NekoInverter@users.noreply.gitlab.com> Date: Sun, 3 Jan 2021 12:19:35 +0800 Subject: [PATCH] Parse gallery not available --- .../com/hippo/ehviewer/client/EhEngine.java | 14 ++++++++++ .../parser/GalleryNotAvailableParser.java | 26 +++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 app/src/main/java/com/hippo/ehviewer/client/parser/GalleryNotAvailableParser.java diff --git a/app/src/main/java/com/hippo/ehviewer/client/EhEngine.java b/app/src/main/java/com/hippo/ehviewer/client/EhEngine.java index cca704094..4400ce2f4 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/EhEngine.java +++ b/app/src/main/java/com/hippo/ehviewer/client/EhEngine.java @@ -40,6 +40,7 @@ import com.hippo.ehviewer.client.parser.ForumsParser; import com.hippo.ehviewer.client.parser.GalleryApiParser; import com.hippo.ehviewer.client.parser.GalleryDetailParser; import com.hippo.ehviewer.client.parser.GalleryListParser; +import com.hippo.ehviewer.client.parser.GalleryNotAvailableParser; import com.hippo.ehviewer.client.parser.GalleryPageApiParser; import com.hippo.ehviewer.client.parser.GalleryPageParser; import com.hippo.ehviewer.client.parser.GalleryTokenApiParser; @@ -106,11 +107,24 @@ public class EhEngine { throw new EhException("Sad Panda"); } + // Check sad panda(without panda) + if (headers != null && "text/html; charset=UTF-8".equals(headers.get("Content-Type")) && + "0".equals(headers.get("Content-Length"))) { + throw new EhException("Sad Panda\n(without panda)"); + } + // Check kokomade if (body != null && body.contains(KOKOMADE_URL)) { throw new EhException("今回はここまで\n\n" + GetText.getString(R.string.kokomade_tip)); } + if (body != null && body.contains("Gallery Not Available - ")) { + String error = GalleryNotAvailableParser.parse(body); + if (!TextUtils.isEmpty(error)) { + throw new EhException(error); + } + } + if (e instanceof ParseException) { if (body != null && !body.contains("<")) { throw new EhException(body); diff --git a/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryNotAvailableParser.java b/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryNotAvailableParser.java new file mode 100644 index 000000000..cf90c1a05 --- /dev/null +++ b/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryNotAvailableParser.java @@ -0,0 +1,26 @@ +package com.hippo.ehviewer.client.parser; + +import com.hippo.util.ExceptionUtils; +import com.hippo.util.JsoupUtils; + +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; +import org.jsoup.nodes.Element; + +public class GalleryNotAvailableParser { + + public static String parse(String body) { + String error = null; + try { + Document document = Jsoup.parse(body); + Element d = JsoupUtils.getElementByClass(document, "d"); + //noinspection ConstantConditions + error = d.child(0).html(); + error = error.replace("
", "\n"); + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); + e.printStackTrace(); + } + return error; + } +}