Add GalleryPageApiParser

This commit is contained in:
seven332 committed 2019-02-16 15:02:40 +08:00
1 parent cca6e2a72e
commit a3aa4ba17f
3 files changed
+121

No files matched your search

@@ -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("<img[^>]*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("<a href=\"([^\"]+)fullimg.php([^\"]+)\">");
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;
}
}
@@ -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);
}
}
@@ -0,0 +1 @@
{"p":3,"s":"s\/7e7a4305a4\/1366222-3","n":"<div class=\"sn\"><a onclick=\"return load_image(1, '3913869966')\" href=\"https:\/\/e-hentai.org\/s\/3913869966\/1366222-1\"><img src=\"https:\/\/ehgt.org\/g\/f.png\" \/><\/a><a id=\"prev\" onclick=\"return load_image(2, '34972180ed')\" href=\"https:\/\/e-hentai.org\/s\/34972180ed\/1366222-2\"><img src=\"https:\/\/ehgt.org\/g\/p.png\" \/><\/a><div><span>3<\/span> \/ <span>13<\/span><\/div><a id=\"next\" onclick=\"return load_image(4, 'd2b5dac191')\" href=\"https:\/\/e-hentai.org\/s\/d2b5dac191\/1366222-4\"><img src=\"https:\/\/ehgt.org\/g\/n.png\" \/><\/a><a onclick=\"return load_image(13, 'f264699329')\" href=\"https:\/\/e-hentai.org\/s\/f264699329\/1366222-13\"><img src=\"https:\/\/ehgt.org\/g\/l.png\" \/><\/a><\/div>","i":"<div>Valentines_2019_002.jpg :: 1280 x 960 :: 178.8 KB<\/div>","k":"7e7a4305a4","i3":"<a onclick=\"return load_image(4, 'd2b5dac191')\" href=\"https:\/\/e-hentai.org\/s\/d2b5dac191\/1366222-4\"><img id=\"img\" src=\"http:\/\/69.30.203.46:60111\/h\/6047fa2f194742f6fa541ec1f631ec3ab438f960-183117-1280-960-jpg\/keystamp=1550291100-c4438f48c8;fileindex=67379169;xres=1280\/Valentines_2019_002.jpg\" style=\"height:960px;width:1280px\" onerror=\"this.onerror=null; nl('15151-430636')\" \/><\/a>","i5":"<div class=\"sb\"><a href=\"https:\/\/e-hentai.org\/g\/1366222\/c0bf6f0a8e\/\"><img src=\"https:\/\/ehgt.org\/g\/b.png\" referrerpolicy=\"no-referrer\" \/><\/a><\/div>","i6":" &nbsp; <img src=\"https:\/\/ehgt.org\/g\/mr.gif\" class=\"mr\" \/> <a href=\"https:\/\/e-hentai.org\/?f_shash=7e7a4305a47ebd25a11454003f042f1adc1015b1&amp;fs_from=Valentines_2019_002.jpg+from+%5BMr.+Phoenyxx%5D+Valentines+2019\">Show all galleries with this file<\/a> &nbsp; <img src=\"https:\/\/ehgt.org\/g\/mr.gif\" class=\"mr\" \/> <a href=\"#\" onclick=\"prompt('Copy the URL below.', 'https:\/\/e-hentai.org\/r\/6047fa2f194742f6fa541ec1f631ec3ab438f960-183117-1280-960-jpg\/forumtoken\/1366222-3\/Valentines_2019_002.jpg'); return false\">Generate a static forum image link<\/a> &nbsp; <img src=\"https:\/\/ehgt.org\/g\/mr.gif\" class=\"mr\" \/> <a href=\"#\" id=\"loadfail\" onclick=\"return nl('15151-430636')\">Click here if the image fails loading<\/a> ","i7":" &nbsp; <img src=\"https:\/\/ehgt.org\/g\/mr.gif\" class=\"mr\" \/> <a href=\"https:\/\/e-hentai.org\/fullimg.php?gid=1366222&amp;page=3&amp;key=puxxvyg98a4\">Download original 1600 x 1200 335.9 KB source<\/a>","si":15151,"x":"1280","y":"960"}