/*
 * Copyright 2016 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;

public class GalleryPageParser {

    private static final Pattern PATTERN_IMAGE_URL = Pattern.compile("<div id=\"i3\"><a[^>]+><img id=\"img\" src=\"([^\"]+)\"");
    private static final Pattern PATTERN_SKIP_HATH_KEY = Pattern.compile("onclick=\"return nl\\('([^\\)]+)'\\)");
    private static final Pattern PATTERN_ORIGIN_IMAGE_URL = Pattern.compile("<div id=\"i7\" class=\"if\"> &nbsp; <img[^>]+/> <a href=\"([^\"]+)\">");

    public static Result parse(String body) throws ParseException {
        Matcher m;
        Result result = new Result();
        m = PATTERN_IMAGE_URL.matcher(body);
        if (m.find()) {
            result.imageUrl = StringUtils.unescapeXml(StringUtils.trim(m.group(1)));
        }
        m = PATTERN_SKIP_HATH_KEY.matcher(body);
        if (m.find()) {
            result.skipHathKey = StringUtils.unescapeXml(StringUtils.trim(m.group(1)));
        }
        m = PATTERN_ORIGIN_IMAGE_URL.matcher(body);
        if (m.find()) {
            result.originImageUrl = StringUtils.unescapeXml(StringUtils.trim(m.group(1)));
        }
        if (!TextUtils.isEmpty(result.imageUrl) && !TextUtils.isEmpty(result.skipHathKey)) {
            return result;
        } else {
            throw new ParseException("Parse image url and skip hath key error", body);
        }
    }

    public static class Result {
        public String imageUrl;
        public String skipHathKey;
        public String originImageUrl;
    }
}

@@ -925,32 +925,43 @@
 ttern.compile(%22%3C
+(?:/iframe%7C
 div id=%5C%22i3%5C%22%3E%3Ca
@@ -957,16 +957,22 @@
 d=%5C%22i3%5C%22
+)%5B%5E%3E%5D*
 %3E%3Ca%5B%5E%3E%5D+
@@ -980,33 +980,23 @@
 %3Cimg
- id=%5C%22img%5C%22 
+%5B%5E%3E%5D*
 src=%5C%22(
-%5B%5E%5C%22%5D+
+.+?
 )%5C%22%22
@@ -1194,65 +1194,36 @@
 e(%22%3C
-div id=%5C%22i7%5C%22 class=%5C%22if%5C%22%3E &nbsp; %3Cimg%5B%5E%3E%5D+/%3E %3Ca href=%5C%22
+a href=%5C%22(%5B%5E%5C%22%5D+)fullimg.php
 (%5B%5E%5C
@@ -1822,32 +1822,62 @@
 ils.unescapeXml(
+m.group(1)) + %22fullimg.php%22 + 
 StringUtils.trim
@@ -1864,36 +1864,43 @@
 %22 + StringUtils.
-trim
+unescapeXml
 (m.group(1)));%0A 
@@ -1884,36 +1884,35 @@
 capeXml(m.group(
-1
+2
 ))
-)
 ;%0A        %7D%0A    
@@ -1899,32 +1899,33 @@
 (2));%0A        %7D%0A
+%0A
         if (!Tex
@@ -1959,50 +1959,8 @@
 Url)
- && !TextUtils.isEmpty(result.skipHathKey)
 ) %7B%0A

Fix parse gallery page error in e-hentai