From 2ec229c8ef280c5fe0546b21dcc98fa2525aa68f Mon Sep 17 00:00:00 2001 From: seven332 Date: Tue, 19 Mar 2019 21:22:43 +0800 Subject: [PATCH] Fix parsing popular --- .../com/hippo/ehviewer/client/EhClient.java | 3 - .../com/hippo/ehviewer/client/EhEngine.java | 41 ------------ .../java/com/hippo/ehviewer/client/EhUrl.java | 14 ++++ .../ehviewer/client/data/ListUrlBuilder.java | 2 + .../client/parser/GalleryListParser.java | 8 ++- .../client/parser/WhatsHotParser.java | 61 ----------------- .../ehviewer/ui/scene/GalleryListScene.java | 66 ++----------------- 7 files changed, 28 insertions(+), 167 deletions(-) delete mode 100644 app/src/main/java/com/hippo/ehviewer/client/parser/WhatsHotParser.java diff --git a/app/src/main/java/com/hippo/ehviewer/client/EhClient.java b/app/src/main/java/com/hippo/ehviewer/client/EhClient.java index f15d5d664..045facf19 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/EhClient.java +++ b/app/src/main/java/com/hippo/ehviewer/client/EhClient.java @@ -46,7 +46,6 @@ public class EhClient { public static final int METHOD_ADD_FAVORITES_RANGE = 10; public static final int METHOD_MODIFY_FAVORITES = 11; public static final int METHOD_GET_TORRENT_LIST = 12; - public static final int METHOD_GET_WHATS_HOT = 13; public static final int METHOD_GET_PROFILE = 14; public static final int METHOD_VOTE_COMMENT = 15; public static final int METHOD_IMAGE_SEARCH = 16; @@ -162,8 +161,6 @@ public class EhClient { return EhEngine.modifyFavorites(this, mOkHttpClient, (String) params[0], (long[]) params[1], (Integer) params[2], (Boolean) params[3]); case METHOD_GET_TORRENT_LIST: return EhEngine.getTorrentList(this, mOkHttpClient, (String) params[0], (Long) params[1], (String) params[2]); - case METHOD_GET_WHATS_HOT: - return EhEngine.getWhatsHot(this, mOkHttpClient); case METHOD_GET_PROFILE: return EhEngine.getProfile(this, mOkHttpClient); case METHOD_VOTE_COMMENT: diff --git a/app/src/main/java/com/hippo/ehviewer/client/EhEngine.java b/app/src/main/java/com/hippo/ehviewer/client/EhEngine.java index f6ec47f45..97fae574e 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/EhEngine.java +++ b/app/src/main/java/com/hippo/ehviewer/client/EhEngine.java @@ -46,7 +46,6 @@ import com.hippo.ehviewer.client.parser.RateGalleryParser; import com.hippo.ehviewer.client.parser.SignInParser; import com.hippo.ehviewer.client.parser.TorrentParser; import com.hippo.ehviewer.client.parser.VoteCommentParser; -import com.hippo.ehviewer.client.parser.WhatsHotParser; import com.hippo.network.StatusCodeException; import com.hippo.util.ExceptionUtils; import com.hippo.yorozuya.AssertUtils; @@ -750,46 +749,6 @@ public class EhEngine { return null; } - public static List getWhatsHot(@Nullable EhClient.Task task, - OkHttpClient okHttpClient) throws Throwable { - String url = EhUrl.HOST_E; - Log.d(TAG, url); - Request request = new EhRequestBuilder(url, null).build(); - Call call = okHttpClient.newCall(request); - - // Put call - if (null != task) { - task.setCall(call); - } - - String body = null; - Headers headers = null; - List list; - int code = -1; - try { - Response response = call.execute(); - code = response.code(); - headers = response.headers(); - body = response.body().string(); - list = WhatsHotParser.parse(body); - } catch (Throwable e) { - ExceptionUtils.throwIfFatal(e); - throwException(call, code, headers, body, e); - throw e; - } - - if (list.size() > 0) { - // Fill by api - fillGalleryListByApi(task, okHttpClient, list, url); - } - - for (GalleryInfo info : list) { - info.thumb = EhUrl.getFixedPreviewThumbUrl(info.thumb); - } - - return list; - } - private static ProfileParser.Result getProfileInternal(@Nullable EhClient.Task task, OkHttpClient okHttpClient, String url, String referer) throws Throwable { Log.d(TAG, url); diff --git a/app/src/main/java/com/hippo/ehviewer/client/EhUrl.java b/app/src/main/java/com/hippo/ehviewer/client/EhUrl.java index 1e26ca77d..e03c416f4 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/EhUrl.java +++ b/app/src/main/java/com/hippo/ehviewer/client/EhUrl.java @@ -40,6 +40,9 @@ public class EhUrl { public static final String API_E = HOST_E + "api.php"; public static final String API_EX = HOST_EX + "api.php"; + public static final String URL_POPULAR_E = "https://e-hentai.org/popular"; + public static final String URL_POPULAR_EX = "https://exhentai.org/popular"; + public static final String URL_IMAGE_SEARCH_E = "https://upload.e-hentai.org/image_lookup.php"; public static final String URL_IMAGE_SEARCH_EX = "https://exhentai.org/upload/image_lookup.php"; @@ -152,6 +155,17 @@ public class EhUrl { return "https://ehwiki.org/wiki/" + tag.replace(' ', '_'); } + @NonNull + public static String getPopularUrl() { + switch (Settings.getGallerySite()) { + default: + case SITE_E: + return URL_POPULAR_E; + case SITE_EX: + return URL_POPULAR_EX; + } + } + @NonNull public static String getImageSearchUrl() { switch (Settings.getGallerySite()) { diff --git a/app/src/main/java/com/hippo/ehviewer/client/data/ListUrlBuilder.java b/app/src/main/java/com/hippo/ehviewer/client/data/ListUrlBuilder.java index 848b0c6a7..54860228a 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/data/ListUrlBuilder.java +++ b/app/src/main/java/com/hippo/ehviewer/client/data/ListUrlBuilder.java @@ -462,6 +462,8 @@ public class ListUrlBuilder implements Cloneable, Parcelable { } return sb.toString(); } + case MODE_WHATS_HOT: + return EhUrl.getPopularUrl(); case MODE_IMAGE_SEARCH: return EhUrl.getImageSearchUrl(); } diff --git a/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryListParser.java b/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryListParser.java index 001d96329..6a32b3d55 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryListParser.java +++ b/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryListParser.java @@ -118,7 +118,7 @@ public class GalleryListParser { return -2; } - private static GalleryInfo parseGalleryInfoMinimal(Element e) { + private static GalleryInfo parseGalleryInfo(Element e) { GalleryInfo gi = new GalleryInfo(); // Title, gid, token (required), tags @@ -267,8 +267,10 @@ public class GalleryListParser { //noinspection unchecked result.galleryInfoList = Collections.EMPTY_LIST; return result; + } else if (body.contains("Currently Popular Recent Galleries")) { + result.pages = 1; } else { - throw e; + result.pages = Integer.MAX_VALUE; } } @@ -283,7 +285,7 @@ public class GalleryListParser { List list = new ArrayList<>(es.size()); // First one is table header, skip it for (int i = 0; i < es.size(); i++) { - GalleryInfo gi = parseGalleryInfoMinimal(es.get(i)); + GalleryInfo gi = parseGalleryInfo(es.get(i)); if (null != gi) { list.add(gi); } diff --git a/app/src/main/java/com/hippo/ehviewer/client/parser/WhatsHotParser.java b/app/src/main/java/com/hippo/ehviewer/client/parser/WhatsHotParser.java deleted file mode 100644 index dbc27717b..000000000 --- a/app/src/main/java/com/hippo/ehviewer/client/parser/WhatsHotParser.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * 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 com.hippo.ehviewer.client.EhUtils; -import com.hippo.ehviewer.client.data.GalleryInfo; -import com.hippo.ehviewer.client.exception.ParseException; -import com.hippo.util.ExceptionUtils; -import com.hippo.util.JsoupUtils; -import java.util.ArrayList; -import java.util.List; -import org.jsoup.Jsoup; -import org.jsoup.nodes.Document; -import org.jsoup.nodes.Element; -import org.jsoup.select.Elements; - -public class WhatsHotParser { - - @SuppressWarnings("ConstantConditions") - public static List parse(String body) throws ParseException { - try { - List galleryInfoList = new ArrayList<>(15); - Document d = Jsoup.parse(body); - Element pp = d.getElementById("pp"); - Elements id1List = pp.getElementsByClass("id1"); - for (int i = 0, n = id1List.size(); i < n; i++) { - GalleryInfo galleryInfo = new GalleryInfo(); - Element id1 = id1List.get(i); - Element id3 = JsoupUtils.getElementByClass(id1, "id3"); - Element temp = JsoupUtils.getElementByTag(id3, "a"); - String url = temp.attr("href"); - GalleryDetailUrlParser.Result result = GalleryDetailUrlParser.parse(url); - galleryInfo.gid = result.gid; - galleryInfo.token = result.token; - temp = JsoupUtils.getElementByTag(temp, "img"); - galleryInfo.thumb = EhUtils.handleThumbUrlResolution(temp.attr("src")); - galleryInfo.title = temp.attr("title"); - galleryInfo.generateSLang(); - galleryInfoList.add(galleryInfo); - } - return galleryInfoList; - } catch (Throwable e) { - ExceptionUtils.throwIfFatal(e); - throw new ParseException("Parse whats hot error", body); - } - } -} diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryListScene.java b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryListScene.java index 75d90d944..0425cd79c 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryListScene.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryListScene.java @@ -1364,15 +1364,7 @@ public final class GalleryListScene extends BaseScene } mUrlBuilder.setPageIndex(page); - if (ListUrlBuilder.MODE_WHATS_HOT == mUrlBuilder.getMode()) { - EhRequest request = new EhRequest(); - request.setMethod(EhClient.METHOD_GET_WHATS_HOT); - request.setCallback(new GetWhatsHotListener(getContext(), - activity.getStageId(), getTag(), taskId)); - request.setArgs(); - mClient.execute(request); - Analytics.viewWhatsHot(); - } else if (ListUrlBuilder.MODE_IMAGE_SEARCH == mUrlBuilder.getMode()) { + if (ListUrlBuilder.MODE_IMAGE_SEARCH == mUrlBuilder.getMode()) { EhRequest request = new EhRequest(); request.setMethod(EhClient.METHOD_IMAGE_SEARCH); request.setCallback(new GetGalleryListListener(getContext(), @@ -1390,7 +1382,12 @@ public final class GalleryListScene extends BaseScene activity.getStageId(), getTag(), taskId)); request.setArgs(url); mClient.execute(request); - Analytics.viewGalleryList(); + + if (ListUrlBuilder.MODE_WHATS_HOT == mUrlBuilder.getMode()) { + Analytics.viewWhatsHot(); + } else { + Analytics.viewGalleryList(); + } } } @@ -1454,55 +1451,6 @@ public final class GalleryListScene extends BaseScene } } - private void onGetWhatsHotSuccess(List result, int taskId) { - if (mHelper != null && mSearchBarMover != null && - mHelper.isCurrentTask(taskId)) { - mHelper.setPages(taskId, 1); - mHelper.onGetPageData(taskId, result); - } - } - - private void onGetWhatsHotFailure(Exception e, int taskId) { - if (mHelper != null && mSearchBarMover != null && - mHelper.isCurrentTask(taskId)) { - mHelper.onGetException(taskId, e); - } - } - - private static class GetWhatsHotListener extends EhCallback> { - - private final int mTaskId; - - public GetWhatsHotListener(Context context, int stageId, String sceneTag, int taskId) { - super(context, stageId, sceneTag); - mTaskId = taskId; - } - - @Override - public void onSuccess(List result) { - GalleryListScene scene = getScene(); - if (scene != null) { - scene.onGetWhatsHotSuccess(result, mTaskId); - } - } - - @Override - public void onFailure(Exception e) { - GalleryListScene scene = getScene(); - if (scene != null) { - scene.onGetWhatsHotFailure(e, mTaskId); - } - } - - @Override - public void onCancel() {} - - @Override - public boolean isInstance(SceneFragment scene) { - return scene instanceof GalleryListScene; - } - } - private static class GetGalleryListListener extends EhCallback { private final int mTaskId;