Fix ptoken error with multi-page viewer enabled

This commit is contained in:
NekoInverter committed 2020-12-27 19:14:19 +08:00
1 parent e75ad9fc6e
commit 3c07e24319
3 files changed
+74

No files matched your search

@@ -158,6 +158,11 @@ public class EhUrl {
return builder.build();
}
public static String getGalleryMultiPageViewerUrl(long gid, String token) {
UrlBuilder builder = new UrlBuilder(getHost() + "mpv/" + gid + '/' + token + '/');
return builder.build();
}
public static String getPageUrl(long gid, int index, String pToken) {
return getHost() + "s/" + pToken + '/' + gid + '-' + (index + 1);
}
@@ -0,0 +1,27 @@
package com.hippo.ehviewer.client.parser;
import com.hippo.ehviewer.client.exception.EhException;
import com.hippo.util.ExceptionUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
public class GalleryMultiPageViewerPTokenParser {
public static ArrayList<String> parse(String body) throws Exception {
ArrayList<String> list = new ArrayList<>();
String imagelist = body.substring(body.indexOf("var imagelist = ") + 16, body.indexOf(";", body.indexOf("var imagelist = ")));
try {
JSONArray ja = new JSONArray(imagelist);
for (int i = 0; i < ja.length(); i++) {
JSONObject jo = ja.getJSONObject(i);
list.add(jo.getString("k"));
}
} catch (Throwable e) {
ExceptionUtils.throwIfFatal(e);
throw new EhException(body);
}
return list;
}
}
@@ -43,6 +43,7 @@ import com.hippo.ehviewer.client.data.PreviewSet;
import com.hippo.ehviewer.client.exception.Image509Exception;
import com.hippo.ehviewer.client.exception.ParseException;
import com.hippo.ehviewer.client.parser.GalleryDetailParser;
import com.hippo.ehviewer.client.parser.GalleryMultiPageViewerPTokenParser;
import com.hippo.ehviewer.client.parser.GalleryPageApiParser;
import com.hippo.ehviewer.client.parser.GalleryPageParser;
import com.hippo.ehviewer.client.parser.GalleryPageUrlParser;
@@ -768,6 +769,42 @@ public final class SpiderQueen implements Runnable {
}
}
private String getPTokenFromMultiPageViewer(int index) {
SpiderInfo spiderInfo = mSpiderInfo.get();
if (spiderInfo == null) {
return null;
}
try {
String url = EhUrl.getGalleryMultiPageViewerUrl(
mGalleryInfo.gid, mGalleryInfo.token);
if (DEBUG_PTOKEN) {
Log.d(TAG, "getPTokenFromMultiPageViewer index " + index + ", url " + url);
}
String referer = EhUrl.getReferer();
Request request = new EhRequestBuilder(url, referer).build();
Response response = mHttpClient.newCall(request).execute();
String body = response.body().string();
ArrayList<String> list = GalleryMultiPageViewerPTokenParser.parse(body);
for (int i = 0; i < list.size(); i++) {
synchronized (mPTokenLock) {
spiderInfo.pTokenMap.put(i, list.get(i));
}
}
String pToken;
synchronized (mPTokenLock) {
pToken = spiderInfo.pTokenMap.get(index);
}
return pToken;
} catch (Throwable e) {
ExceptionUtils.throwIfFatal(e);
return null;
}
}
private String getPTokenFromInternet(int index) {
SpiderInfo spiderInfo = mSpiderInfo.get();
if (spiderInfo == null) {
@@ -926,6 +963,11 @@ public final class SpiderQueen implements Runnable {
pToken = getPTokenFromInternet(index);
}
if (null == pToken) {
// Multi-page viewer enabled maybe
pToken = getPTokenFromMultiPageViewer(index);
}
if (null == pToken) {
// If failed, set the pToken "failed"
synchronized (mPTokenLock) {