From 5b0c302530999f44186fa4c481bd19e6a38650b8 Mon Sep 17 00:00:00 2001 From: FooIbar <118464521+FooIbar@users.noreply.github.com> Date: Sun, 2 Apr 2023 16:42:51 +0800 Subject: [PATCH] Convert GalleryPageApiParser to kotlin --- .../client/parser/GalleryPageApiParser.kt | 96 +++++++++---------- 1 file changed, 45 insertions(+), 51 deletions(-) diff --git a/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryPageApiParser.kt b/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryPageApiParser.kt index c8b76cb0f..4f65431b0 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryPageApiParser.kt +++ b/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryPageApiParser.kt @@ -13,65 +13,59 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +package com.hippo.ehviewer.client.parser -package com.hippo.ehviewer.client.parser; +import com.hippo.ehviewer.client.exception.ParseException +import com.hippo.yorozuya.StringUtils +import org.json.JSONException +import org.json.JSONObject +import java.util.regex.Matcher +import java.util.regex.Pattern -import android.text.TextUtils; +object GalleryPageApiParser { + private val PATTERN_IMAGE_URL = Pattern.compile("]*src=\"([^\"]+)\" style") + private val PATTERN_SKIP_HATH_KEY = Pattern.compile("onclick=\"return nl\\('([^)]+)'\\)") + private val PATTERN_ORIGIN_IMAGE_URL = + Pattern.compile("") -import com.hippo.ehviewer.client.exception.ParseException; -import com.hippo.yorozuya.StringUtils; - -import org.json.JSONException; -import org.json.JSONObject; - -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -public class GalleryPageApiParser { - - private static final Pattern PATTERN_IMAGE_URL = Pattern.compile("]*src=\"([^\"]+)\" style"); - private static final Pattern PATTERN_SKIP_HATH_KEY = Pattern.compile("onclick=\"return nl\\('([^\\)]+)'\\)"); - private static final Pattern PATTERN_ORIGIN_IMAGE_URL = Pattern.compile(""); - - public static Result parse(String body) throws ParseException { - try { - Matcher m; - Result result = new Result(); - - JSONObject jo = new JSONObject(body); + fun parse(body: String): Result { + return try { + var m: Matcher + val jo = JSONObject(body) if (jo.has("error")) { - throw new ParseException(jo.getString("error"), body); + throw ParseException(jo.getString("error"), body) } - - String i3 = jo.getString("i3"); - m = PATTERN_IMAGE_URL.matcher(i3); - if (m.find()) { - result.imageUrl = StringUtils.unescapeXml(StringUtils.trim(m.group(1))); - } - String i6 = jo.getString("i6"); - m = PATTERN_SKIP_HATH_KEY.matcher(i6); - if (m.find()) { - result.skipHathKey = StringUtils.unescapeXml(StringUtils.trim(m.group(1))); - } - String i7 = jo.getString("i7"); - m = PATTERN_ORIGIN_IMAGE_URL.matcher(i7); - if (m.find()) { - result.originImageUrl = StringUtils.unescapeXml(m.group(1)) + "fullimg.php" + StringUtils.unescapeXml(m.group(2)); - } - - if (!TextUtils.isEmpty(result.imageUrl)) { - return result; + val i3 = jo.getString("i3") + m = PATTERN_IMAGE_URL.matcher(i3) + val imageUrl = if (m.find()) { + StringUtils.unescapeXml(StringUtils.trim(m.group(1))) } else { - throw new ParseException("Parse image url and skip hath key error", body); + null } - } catch (JSONException e) { - throw new ParseException("Can't parse json", body, e); + val i6 = jo.getString("i6") + m = PATTERN_SKIP_HATH_KEY.matcher(i6) + val skipHathKey = if (m.find()) { + StringUtils.unescapeXml(StringUtils.trim(m.group(1))) + } else { + null + } + val i7 = jo.getString("i7") + m = PATTERN_ORIGIN_IMAGE_URL.matcher(i7) + val originImageUrl = if (m.find()) { + StringUtils.unescapeXml(m.group(1)) + "fullimg.php" + + StringUtils.unescapeXml(m.group(2)) + } else { + null + } + if (!imageUrl.isNullOrEmpty()) { + Result(imageUrl, skipHathKey, originImageUrl) + } else { + throw ParseException("Parse image url and skip hath key error", body) + } + } catch (e: JSONException) { + throw ParseException("Can't parse json", body, e) } } - public static class Result { - public String imageUrl; - public String skipHathKey; - public String originImageUrl; - } + class Result(val imageUrl: String, val skipHathKey: String?, val originImageUrl: String?) }