From f674e2331e9f3c2ae72f68c6773b41a73c6cde70 Mon Sep 17 00:00:00 2001 From: seven332 Date: Tue, 8 Mar 2016 01:42:25 +0800 Subject: [PATCH] Add whats hot --- .../com/hippo/ehviewer/client/EhClient.java | 3 + .../com/hippo/ehviewer/client/EhEngine.java | 28 +++++++ .../java/com/hippo/ehviewer/client/EhUrl.java | 1 + .../client/parser/GalleryApiParser.java | 2 +- .../ehviewer/client/parser/ParserUtils.java | 11 +++ .../client/parser/WhatsHotParser.java | 60 +++++++++++++++ .../com/hippo/ehviewer/ui/MainActivity.java | 5 +- .../ehviewer/ui/scene/GalleryListScene.java | 74 +++++++++++++++++-- 8 files changed, 175 insertions(+), 9 deletions(-) create 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 02fabe56b..560229e33 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/EhClient.java +++ b/app/src/main/java/com/hippo/ehviewer/client/EhClient.java @@ -54,6 +54,7 @@ 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; private final ThreadPoolExecutor mRequestThreadPool; private final OkHttpClient mOkHttpClient; @@ -171,6 +172,8 @@ 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]); + case METHOD_GET_WHATS_HOT: + return EhEngine.getWhatsHot(this, mOkHttpClient); default: return new IllegalStateException("Can't detect method " + mMethod); } 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 bf775bf59..8604e774f 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/EhEngine.java +++ b/app/src/main/java/com/hippo/ehviewer/client/EhEngine.java @@ -37,6 +37,7 @@ import com.hippo.ehviewer.client.parser.GalleryTokenApiParser; 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.WhatsHotParser; import com.hippo.network.StatusCodeException; import com.hippo.yorozuya.AssertUtils; @@ -518,4 +519,31 @@ public class EhEngine { return result; } + + public static List getWhatsHot(EhClient.Task task, + OkHttpClient okHttpClient) throws Exception { + String url = EhUrl.HOST_G; + Log.d(TAG, url); + Request request = new EhRequestBuilder(url, task.getEhConfig()).build(); + Call call = okHttpClient.newCall(request); + + // Put call + task.setCall(call); + + String body = null; + Headers headers = null; + int code = -1; + try { + Response response = call.execute(); + code = response.code(); + headers = response.headers(); + body = response.body().string(); + List list = WhatsHotParser.parse(body); + fillGalleryListByApi(task, okHttpClient, list); + return list; + } catch (Exception e) { + throwException(call, code, headers, body, e); + throw e; + } + } } 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 2edc9cf76..398c86313 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/EhUrl.java +++ b/app/src/main/java/com/hippo/ehviewer/client/EhUrl.java @@ -26,6 +26,7 @@ public class EhUrl { public static final String DOMAIN_LOFI = "lofi.e-hentai.org"; public static final String HOST_EX = "http://" + DOMAIN_EX + "/"; + public static final String HOST_G = "http://" + DOMAIN_G + "/"; public static final String API_SIGN_IN = "http://forums.e-hentai.org/index.php?act=Login&CODE=01"; public static final String API_REGISTER = "http://forums.e-hentai.org/index.php?act=Reg&CODE=00"; diff --git a/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryApiParser.java b/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryApiParser.java index 8f5249a24..ab8110d0e 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryApiParser.java +++ b/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryApiParser.java @@ -43,7 +43,7 @@ public class GalleryApiParser { gi.category = EhUtils.getCategory(g.getString("category")); gi.thumb = g.getString("thumb"); gi.uploader = g.getString("uploader"); - gi.posted = g.getString("posted"); // TODO long to date + gi.posted = ParserUtils.formatDate(ParserUtils.parseLong(g.getString("posted")) * 1000); gi.rating = Float.parseFloat(g.getString("rating")); // tags JSONArray tagJa = g.getJSONArray("tags"); diff --git a/app/src/main/java/com/hippo/ehviewer/client/parser/ParserUtils.java b/app/src/main/java/com/hippo/ehviewer/client/parser/ParserUtils.java index ac79210b6..adcdb9a8f 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/parser/ParserUtils.java +++ b/app/src/main/java/com/hippo/ehviewer/client/parser/ParserUtils.java @@ -18,8 +18,19 @@ package com.hippo.ehviewer.client.parser; import com.hippo.yorozuya.StringUtils; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Locale; + public class ParserUtils { + public static final DateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.US); + + public static synchronized String formatDate(long time) { + return sDateFormat.format(new Date(time)); + } + public static String trim(String str) { // Avoid null if (str == null) { 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 new file mode 100644 index 000000000..2ae0b0a95 --- /dev/null +++ b/app/src/main/java/com/hippo/ehviewer/client/parser/WhatsHotParser.java @@ -0,0 +1,60 @@ +/* + * 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.data.GalleryInfo; +import com.hippo.ehviewer.client.exception.ParseException; +import com.hippo.util.JsoupUtils; + +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; +import org.jsoup.nodes.Element; +import org.jsoup.select.Elements; + +import java.util.ArrayList; +import java.util.List; + +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 = temp.attr("src"); + galleryInfo.title = temp.attr("title"); + galleryInfo.generateSLang(); + galleryInfoList.add(galleryInfo); + } + return galleryInfoList; + } catch (Exception e) { + throw new ParseException("Parse whats hot error", body); + } + } +} diff --git a/app/src/main/java/com/hippo/ehviewer/ui/MainActivity.java b/app/src/main/java/com/hippo/ehviewer/ui/MainActivity.java index 8ceda8838..4d4cfddd9 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/MainActivity.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/MainActivity.java @@ -374,7 +374,10 @@ public final class MainActivity extends StageActivity startSceneFirstly(new Announcer(GalleryListScene.class) .setArgs(args)); } else if (id == R.id.nav_whats_hot) { - + Bundle args = new Bundle(); + args.putString(GalleryListScene.KEY_ACTION, GalleryListScene.ACTION_WHATS_HOT); + startSceneFirstly(new Announcer(GalleryListScene.class) + .setArgs(args)); } else if (id == R.id.nav_favourite) { startScene(new Announcer(FavoritesScene.class)); } else if (id == R.id.nav_history) { 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 f8ed586c0..18dde77a9 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 @@ -82,6 +82,7 @@ import com.hippo.yorozuya.SimpleHandler; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; +import java.util.List; public final class GalleryListScene extends BaseScene implements EasyRecyclerView.OnItemClickListener, EasyRecyclerView.OnItemLongClickListener, @@ -798,13 +799,22 @@ public final class GalleryListScene extends BaseScene protected void getPageData(int taskId, int type, int page) { mUrlBuilder.setPageIndex(page); - String url = mUrlBuilder.build(); - EhRequest request = new EhRequest(); - request.setMethod(EhClient.METHOD_GET_GALLERY_LIST); - request.setCallback(new GetGalleryListListener(getContext(), - ((StageActivity) getActivity()).getStageId(), getTag(), taskId)); - request.setArgs(url, Settings.getShowJpnTitle()); // TODO check tag filter - mClient.execute(request); + if (ListUrlBuilder.MODE_WHATS_HOT == mUrlBuilder.getMode()) { + EhRequest request = new EhRequest(); + request.setMethod(EhClient.METHOD_GET_WHATS_HOT); + request.setCallback(new GetWhatsHotListener(getContext(), + ((StageActivity) getActivity()).getStageId(), getTag(), taskId)); + request.setArgs(); + mClient.execute(request); + } else { + String url = mUrlBuilder.build(); + EhRequest request = new EhRequest(); + request.setMethod(EhClient.METHOD_GET_GALLERY_LIST); + request.setCallback(new GetGalleryListListener(getContext(), + ((StageActivity) getActivity()).getStageId(), getTag(), taskId)); + request.setArgs(url, Settings.getShowJpnTitle()); + mClient.execute(request); + } } @Override @@ -852,6 +862,56 @@ 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); + SimpleHandler.getInstance().post(mReturnSearchBarRunnable); + } + } + + private void onGetWhatsHotFailure(Exception e, int taskId) { + if (mHelper != null && mSearchBarMover != null && + mHelper.isCurrentTask(taskId) && isViewCreated()) { + mHelper.onGetExpection(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;