diff --git a/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryPageParser.java b/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryPageParser.java index 8efc3861c..f612d5a03 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryPageParser.java +++ b/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryPageParser.java @@ -17,10 +17,8 @@ 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; @@ -29,6 +27,8 @@ public class GalleryPageParser { 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(""); + // TODO Not sure about the size of show keys + private static final Pattern PATTERN_SHOW_KEY = Pattern.compile("var showkey=\"([0-9a-z]+)\";"); public static Result parse(String body) throws ParseException { Matcher m; @@ -45,11 +45,15 @@ public class GalleryPageParser { if (m.find()) { result.originImageUrl = StringUtils.unescapeXml(m.group(1)) + "fullimg.php" + StringUtils.unescapeXml(m.group(2)); } + m = PATTERN_SHOW_KEY.matcher(body); + if (m.find()) { + result.showKey = m.group(1); + } - if (!TextUtils.isEmpty(result.imageUrl)) { + if (!TextUtils.isEmpty(result.imageUrl) && !TextUtils.isEmpty(result.showKey)) { return result; } else { - throw new ParseException("Parse image url and skip hath key error", body); + throw new ParseException("Parse image url and show error", body); } } @@ -57,5 +61,6 @@ public class GalleryPageParser { public String imageUrl; public String skipHathKey; public String originImageUrl; + public String showKey; } } diff --git a/app/src/test/java/com/hippo/ehviewer/client/parser/GalleryPageParserTest.java b/app/src/test/java/com/hippo/ehviewer/client/parser/GalleryPageParserTest.java new file mode 100644 index 000000000..ab34f0f46 --- /dev/null +++ b/app/src/test/java/com/hippo/ehviewer/client/parser/GalleryPageParserTest.java @@ -0,0 +1,47 @@ +/* + * 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 GalleryPageParserTest { + + @Test + public void testParse() throws IOException, ParseException { + InputStream resource = GalleryPageParserTest.class.getResourceAsStream("GalleryPageParserTest.html"); + BufferedSource source = Okio.buffer(Okio.source(resource)); + String body = source.readUtf8(); + + GalleryPageParser.Result result = GalleryPageParser.parse(body); + assertEquals("http://108.6.41.160:2688/h/5c63e9a5810d8d9c873d9e0dfaadc4a0d70a13bf-188862-1280-879-jpg/keystamp=1550291700-145ecbbb10;fileindex=67290651;xres=1280/10.jpg", result.imageUrl); + assertEquals("26664-430636", result.skipHathKey); + assertEquals("https://e-hentai.org/fullimg.php?gid=1363978&page=10&key=qt2hwrx98a4", result.originImageUrl); + assertEquals("ghz0e5m98a4", result.showKey); + } +} diff --git a/app/src/test/resources/com/hippo/ehviewer/client/parser/GalleryPageParserTest.html b/app/src/test/resources/com/hippo/ehviewer/client/parser/GalleryPageParserTest.html new file mode 100644 index 000000000..e7da42e7c --- /dev/null +++ b/app/src/test/resources/com/hippo/ehviewer/client/parser/GalleryPageParserTest.html @@ -0,0 +1,17 @@ + + + +(C95) [rurudot (rurudo)] Innocence + + + + + +(C95) [rurudot (rurudo)] Innocence10 / 1110.jpg :: 1280 x 879 :: 184.4 KB10.jpg :: 1280 x 879 :: 184.4 KB10 / 11 Show all galleries with this file Generate a static forum image link Click here if the image fails loading Download original 4893 x 3360 15.05 MB source[Front Page] + +
[Front Page]