From ea18b8c090cacb5e77686877fa0e742b2b1a33f8 Mon Sep 17 00:00:00 2001 From: seven332 Date: Thu, 29 May 2014 20:39:03 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=20List=20=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hippo/ehviewer/ehclient/DetailParser.java | 4 +- src/com/hippo/ehviewer/ehclient/EhClient.java | 130 ++---------------- .../hippo/ehviewer/ehclient/ListParser.java | 110 ++++++++++++++- .../ehviewer/exception/SadPandaException.java | 21 +++ .../hippo/ehviewer/network/HttpHelper.java | 19 +++ 5 files changed, 159 insertions(+), 125 deletions(-) create mode 100644 src/com/hippo/ehviewer/exception/SadPandaException.java diff --git a/src/com/hippo/ehviewer/ehclient/DetailParser.java b/src/com/hippo/ehviewer/ehclient/DetailParser.java index 8aa101e97..1a80a9631 100644 --- a/src/com/hippo/ehviewer/ehclient/DetailParser.java +++ b/src/com/hippo/ehviewer/ehclient/DetailParser.java @@ -106,7 +106,7 @@ public class DetailParser { + "]*>Visible:]*>([^<>]+)" // visible + "]*>Language:]*>([^<>]+)" // language + ".+" - + "\\(([\\d]+)\\)" // people + + "\\(([\\d|,]+)\\)" // people + "" + "]*>([^<>]+)" // rating + ".+" @@ -127,7 +127,7 @@ public class DetailParser { parent = m.group(10); visible = m.group(11); language = m.group(12); - people = Integer.parseInt(m.group(13)); + people = Integer.parseInt(m.group(13).replace(",", "")); Pattern pattern = Pattern.compile("([\\d|\\.]+)"); Matcher matcher = pattern.matcher(m.group(14)); diff --git a/src/com/hippo/ehviewer/ehclient/EhClient.java b/src/com/hippo/ehviewer/ehclient/EhClient.java index c39ae0b25..c4b532ea8 100644 --- a/src/com/hippo/ehviewer/ehclient/EhClient.java +++ b/src/com/hippo/ehviewer/ehclient/EhClient.java @@ -580,134 +580,22 @@ public class EhClient { String pageContent = hp.get(url); if (pageContent != null) { - boolean getPageCount = false; - - // Get indexPerPage and maxPage - Pattern p = Pattern - .compile("

\"]+\">Showing ([\\d|,]+)-([\\d|,]+) of ([\\d|,]+)

"); - Matcher m = p.matcher(pageContent); - if (m.find() && m.groupCount() == 3) { - int startIndex = Integer.parseInt(m.group(1).replace(",", - "")); - int endIndex = Integer - .parseInt(m.group(2).replace(",", "")); - int maxIndex = Integer - .parseInt(m.group(3).replace(",", "")); - if (endIndex != maxIndex) - indexPerPage = endIndex - startIndex + 1; - maxPage = (maxIndex + indexPerPage - 1) / indexPerPage; - // To continue get list - getPageCount = true; - } else if (pageContent.contains("No hits found

")) { - lmdArray = new ArrayList(); + ListParser parser = new ListParser(); + switch (parser.parser(pageContent)) { + case ListParser.ALL: + maxPage = parser.maxPage; + indexPerPage = parser.indexPerPage; + lmdArray = parser.giList; + break; + case ListParser.NOT_FOUND: maxPage = 0; - } else if (pageContent.contains("JFIF")) { // sad panda - lmdArray = new ArrayList(); - maxPage = -1; - } else - eMsg = mAppContext.getString(R.string.em_parser_error); - - // Get list - if (getPageCount) { - p = Pattern - .compile("alt=\"([\\w|\\-]+)\"[^<>]*/>]*>([\\w|\\-|\\s|:]+)]*>]*>]+>(?:]*src=\"([^<>\"]+)\"[^<>]*alt=\"([^<>]+)\" style[^<>]*/>|init~([^<>\"~]+~[^<>\"~]+)~([^<>]+))(?:]*>(?:]*>]*/>)?(?:]*>(?:]*>)?]*/>(?:)?)?)?]*>\"]*href=\"([^<>\"]*)\"[^<>]*>([^<>]+)]*>]*style=\"([^<>\"]+)\">\\s*]*>
]*>([^<>]+)
"); - m = p.matcher(pageContent); - - while (m.find()) { - if (lmdArray == null) - lmdArray = new ArrayList(); - GalleryInfo lmd = new GalleryInfo(); - - lmd.category = getType(m.group(1)); - lmd.posted = m.group(2); - if (m.group(3) == null) { - lmd.thumb = "http://" - + m.group(5).replace('~', '/'); - lmd.title = StringEscapeUtils.unescapeHtml4(m - .group(6)); - } else { - lmd.thumb = m.group(3); - lmd.title = StringEscapeUtils.unescapeHtml4(m - .group(4)); - } - // http://g.e-hentai.org/g/671107/b726a0b986/ - - // Get gid and token - Pattern pattern = Pattern - .compile("/(\\d+)/(\\w+)"); - Matcher matcher = pattern.matcher(m.group(7)); - if (matcher.find()) { - lmd.gid = Integer.parseInt(matcher.group(1)); - lmd.token = matcher.group(2); - } else - continue; - - String temp = StringEscapeUtils.unescapeHtml4(m - .group(8)); - if (!lmd.title.equals(temp)) { - Log.w(TAG, - "Maybe parser error, !lmd.name.equals(temp)"); - Log.w(TAG, "first is " + lmd.title); - Log.w(TAG, "second is " + temp); - lmd.title = temp; - } - lmd.rating = Float.parseFloat(getRate(m.group(9))); - lmd.uploader = m.group(10); - lmdArray.add(lmd); - } - if (lmdArray == null) - eMsg = mAppContext.getString(R.string.em_parser_error); + break; } } else eMsg = hp.getEMsg(); } } - /* - public void vote(final int gid, final String token, - final String group, final String tag, - final boolean isUp, final OnVoteListener listener) { - final JSONObject json = new JSONObject(); - try { - json.put("method", "taggallery"); - json.put("apiuid", APIUID); - json.put("apikey", APIKEY); - json.put("gid", gid); - json.put("token", token); - json.put("tags", group + ":" + tag); - if (isUp) - json.put("token", 1); - else - json.put("token", -1); - } catch (JSONException e) { - listener.onFailure(e.getMessage()); - return; - } - - new Thread(new Runnable() { - @Override - public void run() { - HttpHelper hp = new HttpHelper(mAppContext); - hp.setOnRespondListener(new HttpHelper.OnRespondListener() { - @Override - public void onSuccess(String pageContext) { - VoteParser parser = new VoteParser(); - if (parser.parser(pageContext)) { - listener.onSuccess(parser.mTagPane); - } else { - listener.onFailure("parser error"); // TODO - } - } - @Override - public void onFailure(String eMsg) { - listener.onFailure(eMsg); - } - }); - hp.postJson(E_API, json); - } - }).start(); - } - */ public void getMangaList(String url, final Object checkFlag, final OnGetMangaListListener listener) { diff --git a/src/com/hippo/ehviewer/ehclient/ListParser.java b/src/com/hippo/ehviewer/ehclient/ListParser.java index 9051345f3..236a2af9b 100644 --- a/src/com/hippo/ehviewer/ehclient/ListParser.java +++ b/src/com/hippo/ehviewer/ehclient/ListParser.java @@ -16,12 +16,118 @@ package com.hippo.ehviewer.ehclient; +import java.util.ArrayList; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.apache.commons.lang3.StringEscapeUtils; + +import com.hippo.ehviewer.data.GalleryInfo; + public class ListParser { + public static final int ALL = 0x1; + public static final int NOT_FOUND = 0x2; + public static final int SAD_PANDA = 0x3; + public static final int PARSER_ERROR = -1; + public int indexPerPage; + public int maxPage; - boolean parser(String pageContext) { + public ArrayList giList; + + public int parser(String pageContext) { - return false; + Pattern p; + Matcher m; + + p = Pattern.compile("

\"]+\">Showing ([\\d|,]+)-([\\d|,]+) of ([\\d|,]+)

"); + m = p.matcher(pageContext); + if (m.find()) { + int startIndex = Integer.parseInt(m.group(1).replace(",", "")); + int endIndex = Integer.parseInt(m.group(2).replace(",", "")); + int maxIndex = Integer.parseInt(m.group(3).replace(",", "")); + if (endIndex != maxIndex || startIndex == 1) + indexPerPage = endIndex - startIndex + 1; + else + indexPerPage = 25; // default + maxPage = (maxIndex + indexPerPage - 1) / indexPerPage; + } else if (pageContext.contains("No hits found

")) { + maxPage = 0; + return NOT_FOUND; + } else { + return PARSER_ERROR; + } + + giList = new ArrayList(indexPerPage); + p = Pattern.compile("" // category + + "(.+?)" // posted + + "" + + "(?:" + + "|init~([^<>\"~]+~[^<>\"~]+)~([^<>]+))" // thumb and title + + "" + + ".+?" + + "" // url and title + + ".+?" + + "
\"]+)\">" // rating + + ".+?" + + "
(.+?)"); // uploader + m = p.matcher(pageContext); + while (m.find()) { + GalleryInfo gi = new GalleryInfo(); + + gi.category = EhClient.getType(m.group(1)); + gi.posted = m.group(2); + if (m.group(3) == null) { + gi.thumb = "http://" + + m.group(5).replace('~', '/'); + gi.title = StringEscapeUtils.unescapeHtml4(m + .group(6)); + } else { + gi.thumb = m.group(3); + gi.title = StringEscapeUtils.unescapeHtml4(m + .group(4)); + } + + Pattern pattern = Pattern + .compile("/(\\d+)/(\\w+)"); + Matcher matcher = pattern.matcher(m.group(7)); + if (matcher.find()) { + gi.gid = Integer.parseInt(matcher.group(1)); + gi.token = matcher.group(2); + } else + continue; + + gi.rating = Float.parseFloat(getRate(m.group(9))); + gi.uploader = m.group(10); + giList.add(gi); + } + + return ALL; + } + + private String getRate(String rawRate) { + Pattern p = Pattern.compile("\\d+px"); + Matcher m = p.matcher(rawRate); + int num1; + int num2; + int rate = 5; + String re; + if (m.find()) + num1 = Integer.parseInt(m.group().replace("px", "")); + else + return null; + if (m.find()) + num2 = Integer.parseInt(m.group().replace("px", "")); + else + return null; + rate = rate - num1 / 16; + if (num2 == 21) { + rate--; + re = Integer.toString(rate); + re = re + ".5"; + } else + re = Integer.toString(rate); + return re; } } diff --git a/src/com/hippo/ehviewer/exception/SadPandaException.java b/src/com/hippo/ehviewer/exception/SadPandaException.java new file mode 100644 index 000000000..e62383969 --- /dev/null +++ b/src/com/hippo/ehviewer/exception/SadPandaException.java @@ -0,0 +1,21 @@ +/* + * Copyright (C) 2014 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.exception; + +public class SadPandaException extends Exception { + private static final long serialVersionUID = 1L; +} diff --git a/src/com/hippo/ehviewer/network/HttpHelper.java b/src/com/hippo/ehviewer/network/HttpHelper.java index 8d1c3bff3..fddf74e97 100644 --- a/src/com/hippo/ehviewer/network/HttpHelper.java +++ b/src/com/hippo/ehviewer/network/HttpHelper.java @@ -42,6 +42,7 @@ import com.hippo.ehviewer.DiskCache; import com.hippo.ehviewer.R; import com.hippo.ehviewer.exception.RedirectionException; import com.hippo.ehviewer.exception.ResponseCodeException; +import com.hippo.ehviewer.exception.SadPandaException; import com.hippo.ehviewer.util.Constants; import com.hippo.ehviewer.util.Ui; import com.hippo.ehviewer.util.Util; @@ -143,6 +144,9 @@ public class HttpHelper { else if (e instanceof SocketException) return "SocketException : " + e.getMessage(); + else if (e instanceof SadPandaException) + return "Sad Panda"; + else return e.getMessage(); } @@ -214,6 +218,11 @@ public class HttpHelper { switch (responseCode) { case HttpURLConnection.HTTP_OK: case HttpURLConnection.HTTP_PARTIAL: + // Test sad panda + String contentType = conn.getHeaderField("Content-Type"); + if (contentType != null && contentType.equals("image/gif")) + throw new SadPandaException(); + String pageContext = getPageContext(conn); if (msg != null) { Package p = new Package(); @@ -364,6 +373,11 @@ public class HttpHelper { switch (responseCode) { case HttpURLConnection.HTTP_OK: case HttpURLConnection.HTTP_PARTIAL: + // Test sad panda + String contentType = conn.getHeaderField("Content-Type"); + if (contentType != null && contentType.equals("image/gif")) + throw new SadPandaException(); + String pageContext = getPageContext(conn); if (msg != null) { Package p = new Package(); @@ -538,6 +552,11 @@ public class HttpHelper { switch (responseCode) { case HttpURLConnection.HTTP_OK: case HttpURLConnection.HTTP_PARTIAL: + // Test sad panda + String contentType = conn.getHeaderField("Content-Type"); + if (contentType != null && contentType.equals("image/gif")) + throw new SadPandaException(); + String pageContext = getPageContext(conn); if (msg != null) { Package p = new Package();