Convert GalleryPageApiParser to kotlin
This commit is contained in:
1 parent
47c29d9eb5
commit
5b0c302530
1 file changed
+45
-51
@@ -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("<img[^>]*src=\"([^\"]+)\" style")
|
||||
private val PATTERN_SKIP_HATH_KEY = Pattern.compile("onclick=\"return nl\\('([^)]+)'\\)")
|
||||
private val PATTERN_ORIGIN_IMAGE_URL =
|
||||
Pattern.compile("<a href=\"([^\"]+)fullimg.php([^\"]+)\">")
|
||||
|
||||
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("<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);
|
||||
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?)
|
||||
}
|
||||
Reference in new issue
Block a user