From f7362f84bb787d33cfd8548aca0e1cd8341a99e5 Mon Sep 17 00:00:00 2001
From: seven332
Date: Sun, 19 Feb 2017 14:58:45 +0800
Subject: [PATCH] Add GalleryListConverter parsing favourite slot
---
.../converter/GalleryListConverter.java | 47 ++++++++++++++++++-
1 file changed, 45 insertions(+), 2 deletions(-)
diff --git a/app/src/main/java/com/hippo/ehviewer/client/converter/GalleryListConverter.java b/app/src/main/java/com/hippo/ehviewer/client/converter/GalleryListConverter.java
index 7f4f74bf..1fd84383 100644
--- a/app/src/main/java/com/hippo/ehviewer/client/converter/GalleryListConverter.java
+++ b/app/src/main/java/com/hippo/ehviewer/client/converter/GalleryListConverter.java
@@ -32,6 +32,7 @@ import com.hippo.ehviewer.client.data.GalleryInfo;
import com.hippo.ehviewer.client.exception.InnerParseException;
import com.hippo.ehviewer.client.exception.ParseException;
import com.hippo.ehviewer.client.result.GalleryListResult;
+import com.hippo.yorozuya.MathUtils;
import com.hippo.yorozuya.StringUtils;
import java.util.ArrayList;
import java.util.Collections;
@@ -51,7 +52,7 @@ public class GalleryListConverter extends EhConverter {
private static final String LOG_TAG = GalleryListConverter.class.getSimpleName();
private static final String EMPTY_KEYWORD = "No hits found
";
private static final Pattern PATTERN_COVER_SIZE = Pattern.compile("height:(\\d+)px; width:(\\d+)px");
- private static final Pattern PATTERN_RATING = Pattern.compile("(\\d+)px");
+ private static final Pattern PATTERN_PX = Pattern.compile("(\\d+)px");
@NonNull
@Override
@@ -183,6 +184,15 @@ public class GalleryListConverter extends EhConverter {
gi.token = pair.second;
gi.title = StringUtils.strip(a.text());
+ // Favourite slot
+ Element it3 = e.getElementsByClass("it3").first();
+ if (it3 != null) {
+ Element f = it3.getElementById("favicon_" + gi.gid);
+ if (f != null) {
+ gi.favouriteSlot = parseFavouriteSlot(f.attr("style"));
+ }
+ }
+
// Rating
Element it4r = e.getElementsByClass("it4r").first();
if (it4r != null) {
@@ -231,7 +241,7 @@ public class GalleryListConverter extends EhConverter {
// The first one is x offset, 16px for a star.
// The second one is y offset, row1 is -1px, row2 is -21px
private static float parseRating(String style) {
- Matcher m = PATTERN_RATING.matcher(style);
+ Matcher m = PATTERN_PX.matcher(style);
int num1;
int num2;
float rate = 5.0f;
@@ -255,6 +265,39 @@ public class GalleryListConverter extends EhConverter {
return rate;
}
+ // ehentai website uses a picture to show favourite slot.
+ // Looks like:
+ // ------------------
+ // ◇
+ // ◇
+ // ◇
+ // ◇
+ // ◇
+ // ◇
+ // ◇
+ // ◇
+ // ◇
+ // ◇
+ //
+ // ------------------
+ // The style looks like:
+ // background-position:0px -2px
+ // The first one is x offset, always 0
+ // The second one is y offset, starts from -2, step -19
+ private static int parseFavouriteSlot(String style) {
+ Matcher m = PATTERN_PX.matcher(style);
+ if (!m.find() || !m.find()) {
+ return -1;
+ }
+ int num = ConverterUtils.parseInt(m.group(1), -1);
+ if (num == -1) {
+ return -1;
+ }
+ int slot = (num - 2) / 19;
+ slot = MathUtils.clamp(slot, 0, 9);
+ return slot;
+ }
+
////////////////
// Pain part