From 23acdb649209d251dba1bb4064deb786cd3802c7 Mon Sep 17 00:00:00 2001 From: FooIbar <118464521+FooIbar@users.noreply.github.com> Date: Mon, 2 Jan 2023 01:30:52 +0800 Subject: [PATCH] Support non-H@H archive download --- .../com/hippo/ehviewer/client/EhClient.java | 2 +- .../com/hippo/ehviewer/client/EhEngine.java | 44 ++++++++--- .../ehviewer/client/parser/ArchiveParser.java | 78 ++++++++++++++++--- .../ehviewer/ui/scene/GalleryDetailScene.java | 35 +++++++-- app/src/main/res/values-zh-rCN/strings.xml | 3 + app/src/main/res/values/strings.xml | 3 + 6 files changed, 134 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/com/hippo/ehviewer/client/EhClient.java b/app/src/main/java/com/hippo/ehviewer/client/EhClient.java index f77e6564b..e50be9369 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/EhClient.java +++ b/app/src/main/java/com/hippo/ehviewer/client/EhClient.java @@ -167,7 +167,7 @@ public class EhClient { case METHOD_ARCHIVE_LIST -> EhEngine.getArchiveList(this, mOkHttpClient, (String) params[0], (Long) params[1], (String) params[2]); case METHOD_DOWNLOAD_ARCHIVE -> - EhEngine.downloadArchive(this, mOkHttpClient, (Long) params[0], (String) params[1], (String) params[2], (String) params[3]); + EhEngine.downloadArchive(this, mOkHttpClient, (Long) params[0], (String) params[1], (String) params[2], (String) params[3], (Boolean) params[4]); case METHOD_VOTE_TAG -> EhEngine.voteTag(this, mOkHttpClient, (Long) params[0], (String) params[1], (Long) params[2], (String) params[3], (String) params[4], (Integer) params[5]); case METHOD_GET_UCONFIG -> EhEngine.getUConfig(this, mOkHttpClient); 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 e8f961e2d..5760de2be 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/EhEngine.java +++ b/app/src/main/java/com/hippo/ehviewer/client/EhEngine.java @@ -33,7 +33,6 @@ import com.hippo.ehviewer.client.data.GalleryInfo; import com.hippo.ehviewer.client.data.PreviewSet; import com.hippo.ehviewer.client.exception.CancelledException; import com.hippo.ehviewer.client.exception.EhException; -import com.hippo.ehviewer.client.exception.NoHAtHClientException; import com.hippo.ehviewer.client.exception.ParseException; import com.hippo.ehviewer.client.parser.ArchiveParser; import com.hippo.ehviewer.client.parser.EventPaneParser; @@ -65,8 +64,6 @@ import org.jsoup.select.Elements; import java.io.File; import java.util.ArrayList; import java.util.List; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import okhttp3.Call; import okhttp3.FormBody; @@ -88,8 +85,6 @@ public class EhEngine { private static final String KOKOMADE_URL = "https://exhentai.org/img/kokomade.jpg"; private static final MediaType MEDIA_TYPE_JPEG = MediaType.parse("image/jpeg"); - private static final Pattern PATTERN_NEED_HATH_CLIENT = Pattern.compile("(You must have a H@H client assigned to your account to use this feature\\.)"); - public static EhFilter sEhFilter; public static void initialize() { @@ -714,8 +709,8 @@ public class EhEngine { return result; } - public static Void downloadArchive(@Nullable EhClient.Task task, OkHttpClient okHttpClient, - long gid, String token, String or, String res) throws Throwable { + public static String downloadArchive(@Nullable EhClient.Task task, OkHttpClient okHttpClient, + long gid, String token, String or, String res, boolean isHAtH) throws Throwable { if (or == null || or.length() == 0) { throw new EhException("Invalid form param or: " + or); } @@ -723,7 +718,16 @@ public class EhEngine { throw new EhException("Invalid res: " + res); } FormBody.Builder builder = new FormBody.Builder(); - builder.add("hathdl_xres", res); + if (isHAtH) { + builder.add("hathdl_xres", res); + } else { + builder.add("dltype", res); + if (res.equals("org")) { + builder.add("dlcheck", "Download Original Archive"); + } else { + builder.add("dlcheck", "Download Resample Archive"); + } + } String url = EhUrl.getDownloadArchive(gid, token, or); String referer = EhUrl.getGalleryDetailUrl(gid, token); String origin = EhUrl.getOrigin(); @@ -752,11 +756,27 @@ public class EhEngine { throw e; } - Matcher m = PATTERN_NEED_HATH_CLIENT.matcher(body); - if (m.find()) { - throw new NoHAtHClientException("No H@H client"); + var result = ArchiveParser.parseArchiveUrl(body); + if (!isHAtH) { + if (result == null) { + Thread.sleep(1000); + try (var response = call.clone().execute()) { + code = response.code(); + headers = response.headers(); + body = response.body().string(); + throwException(call, code, headers, body, null); + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); + throwException(call, code, headers, body, e); + throw e; + } + result = ArchiveParser.parseArchiveUrl(body); + if (result == null) { + throw new EhException("Archive unavailable"); + } + } + return result; } - return null; } diff --git a/app/src/main/java/com/hippo/ehviewer/client/parser/ArchiveParser.java b/app/src/main/java/com/hippo/ehviewer/client/parser/ArchiveParser.java index 38e0c600e..9af7a1618 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/parser/ArchiveParser.java +++ b/app/src/main/java/com/hippo/ehviewer/client/parser/ArchiveParser.java @@ -18,51 +18,101 @@ package com.hippo.ehviewer.client.parser; import androidx.annotation.NonNull; +import com.hippo.ehviewer.R; +import com.hippo.ehviewer.client.exception.NoHAtHClientException; + +import org.jsoup.Jsoup; + import java.util.ArrayList; import java.util.List; import java.util.Objects; +import java.util.function.IntFunction; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ArchiveParser { - private static final Pattern PATTERN_FORM = Pattern.compile("