From a3aa4ba17ff1d4720c9bdfc2dd21c8e04782bad9 Mon Sep 17 00:00:00 2001 From: seven332 Date: Sat, 16 Feb 2019 15:02:40 +0800 Subject: [PATCH] Add GalleryPageApiParser --- .../client/parser/GalleryPageApiParser.java | 74 +++++++++++++++++++ .../parser/GalleryPageApiParserTest.java | 46 ++++++++++++ .../parser/GalleryPageApiParserTest.json | 1 + 3 files changed, 121 insertions(+) create mode 100644 app/src/main/java/com/hippo/ehviewer/client/parser/GalleryPageApiParser.java create mode 100644 app/src/test/java/com/hippo/ehviewer/client/parser/GalleryPageApiParserTest.java create mode 100644 app/src/test/resources/com/hippo/ehviewer/client/parser/GalleryPageApiParserTest.json diff --git a/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryPageApiParser.java b/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryPageApiParser.java new file mode 100644 index 000000000..6180f2267 --- /dev/null +++ b/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryPageApiParser.java @@ -0,0 +1,74 @@ +/* + * Copyright 2019 Hippo Seven + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.hippo.ehviewer.client.parser; + +import android.text.TextUtils; +import com.hippo.ehviewer.client.exception.ParseException; +import com.hippo.yorozuya.StringUtils; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.json.JSONException; +import org.json.JSONObject; + +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); + if (jo.has("error")) { + throw new 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; + } else { + throw new ParseException("Parse image url and skip hath key error", body); + } + } catch (JSONException e) { + throw new ParseException("Can't parse json", body, e); + } + } + + public static class Result { + public String imageUrl; + public String skipHathKey; + public String originImageUrl; + } +} diff --git a/app/src/test/java/com/hippo/ehviewer/client/parser/GalleryPageApiParserTest.java b/app/src/test/java/com/hippo/ehviewer/client/parser/GalleryPageApiParserTest.java new file mode 100644 index 000000000..8526ea391 --- /dev/null +++ b/app/src/test/java/com/hippo/ehviewer/client/parser/GalleryPageApiParserTest.java @@ -0,0 +1,46 @@ +/* + * Copyright 2019 Hippo Seven + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.hippo.ehviewer.client.parser; + +import static org.junit.Assert.assertEquals; + +import com.hippo.ehviewer.client.exception.ParseException; +import java.io.IOException; +import java.io.InputStream; +import okio.BufferedSource; +import okio.Okio; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; + +@Config(manifest = Config.NONE) +@RunWith(RobolectricTestRunner.class) +public class GalleryPageApiParserTest { + + @Test + public void testParse() throws IOException, ParseException { + InputStream resource = GalleryPageApiParserTest.class.getResourceAsStream("GalleryPageApiParserTest.json"); + BufferedSource source = Okio.buffer(Okio.source(resource)); + String body = source.readUtf8(); + + GalleryPageApiParser.Result result = GalleryPageApiParser.parse(body); + assertEquals("http://69.30.203.46:60111/h/6047fa2f194742f6fa541ec1f631ec3ab438f960-183117-1280-960-jpg/keystamp=1550291100-c4438f48c8;fileindex=67379169;xres=1280/Valentines_2019_002.jpg", result.imageUrl); + assertEquals("15151-430636", result.skipHathKey); + assertEquals("https://e-hentai.org/fullimg.php?gid=1366222&page=3&key=puxxvyg98a4", result.originImageUrl); + } +} diff --git a/app/src/test/resources/com/hippo/ehviewer/client/parser/GalleryPageApiParserTest.json b/app/src/test/resources/com/hippo/ehviewer/client/parser/GalleryPageApiParserTest.json new file mode 100644 index 000000000..2d7414c7b --- /dev/null +++ b/app/src/test/resources/com/hippo/ehviewer/client/parser/GalleryPageApiParserTest.json @@ -0,0 +1 @@ +{"p":3,"s":"s\/7e7a4305a4\/1366222-3","n":"