Add size and cost for archive download
This commit is contained in:
1 parent
e49f5c95f4
commit
21c8d24d57
3 files changed
+127
-25
No files matched your search
@@ -684,8 +684,8 @@ public class EhEngine {
|
||||
return result;
|
||||
}
|
||||
|
||||
public static Pair<String, Pair<String, String>[]> getArchiveList(@Nullable EhClient.Task task, OkHttpClient okHttpClient,
|
||||
String url, long gid, String token) throws Throwable {
|
||||
public static ArchiveParser.Result getArchiveList(@Nullable EhClient.Task task, OkHttpClient okHttpClient,
|
||||
String url, long gid, String token) throws Throwable {
|
||||
String referer = EhUrl.getGalleryDetailUrl(gid, token);
|
||||
Log.d(TAG, url);
|
||||
Request request = new EhRequestBuilder(url, referer).build();
|
||||
@@ -698,7 +698,7 @@ public class EhEngine {
|
||||
|
||||
String body = null;
|
||||
Headers headers = null;
|
||||
Pair<String, Pair<String, String>[]> result;
|
||||
ArchiveParser.Result result;
|
||||
int code = -1;
|
||||
try (Response response = call.execute()) {
|
||||
code = response.code();
|
||||
|
||||
@@ -16,33 +16,137 @@
|
||||
|
||||
package com.hippo.ehviewer.client.parser;
|
||||
|
||||
import android.util.Pair;
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class ArchiveParser {
|
||||
|
||||
private static final Pattern PATTERN_FORM = Pattern.compile("<form id=\"hathdl_form\" action=\"[^\"]*?or=([^=\"]*?)\" method=\"post\">");
|
||||
private static final Pattern PATTERN_ARCHIVE = Pattern.compile("<a href=\"[^\"]*\" onclick=\"return do_hathdl\\('([0-9]+|org)'\\)\">([^<]+)</a>");
|
||||
private static final Pattern PATTERN_ARCHIVE = Pattern.compile("<p><a href=\"[^\"]*\" onclick=\"return do_hathdl\\('([0-9]+|org)'\\)\">([^<]+)</a></p>\\s*<p>([\\w. ]+)</p>\\s*<p>([\\w. ]+)</p>");
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static Pair<String, Pair<String, String>[]> parse(String body) {
|
||||
public static Result parse(String body) {
|
||||
Matcher m = PATTERN_FORM.matcher(body);
|
||||
if (!m.find()) {
|
||||
return new Pair<String, Pair<String, String>[]>("", new Pair[0]);
|
||||
return new Result(null, null);
|
||||
}
|
||||
String paramOr = m.group(1);
|
||||
List<Pair<String, String>> archiveList = new ArrayList<>();
|
||||
var archiveList = new ArrayList<Archive>();
|
||||
m = PATTERN_ARCHIVE.matcher(body);
|
||||
while (m.find()) {
|
||||
String res = ParserUtils.trim(m.group(1));
|
||||
String name = ParserUtils.trim(m.group(2));
|
||||
Pair<String, String> item = new Pair<>(res, name);
|
||||
String size = ParserUtils.trim(m.group(3));
|
||||
String cost = ParserUtils.trim(m.group(4));
|
||||
var item = new Archive(res, name, size, cost);
|
||||
archiveList.add(item);
|
||||
}
|
||||
return new Pair<>(paramOr, archiveList.toArray(new Pair[0]));
|
||||
return new Result(paramOr, archiveList);
|
||||
}
|
||||
|
||||
public static final class Archive {
|
||||
private final String res;
|
||||
private final String name;
|
||||
private final String size;
|
||||
private final String cost;
|
||||
|
||||
public Archive(String res, String name, String size, String cost) {
|
||||
this.res = res;
|
||||
this.name = name;
|
||||
this.size = size;
|
||||
this.cost = cost;
|
||||
}
|
||||
|
||||
public String format() {
|
||||
return String.format("%s [%s, %s]", name, size, cost);
|
||||
}
|
||||
|
||||
public String res() {
|
||||
return res;
|
||||
}
|
||||
|
||||
public String name() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public String cost() {
|
||||
return cost;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == this) return true;
|
||||
if (obj == null || obj.getClass() != this.getClass()) return false;
|
||||
var that = (Archive) obj;
|
||||
return Objects.equals(this.res, that.res) &&
|
||||
Objects.equals(this.name, that.name) &&
|
||||
Objects.equals(this.size, that.size) &&
|
||||
Objects.equals(this.cost, that.cost);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(res, name, size, cost);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Archive[" +
|
||||
"res=" + res + ", " +
|
||||
"name=" + name + ", " +
|
||||
"size=" + size + ", " +
|
||||
"cost=" + cost + ']';
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static final class Result {
|
||||
private final String paramOr;
|
||||
private final List<Archive> archiveList;
|
||||
|
||||
public Result(String paramOr, List<Archive> archiveList) {
|
||||
this.paramOr = paramOr;
|
||||
this.archiveList = archiveList;
|
||||
}
|
||||
|
||||
public String paramOr() {
|
||||
return paramOr;
|
||||
}
|
||||
|
||||
public List<Archive> archiveList() {
|
||||
return archiveList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == this) return true;
|
||||
if (obj == null || obj.getClass() != this.getClass()) return false;
|
||||
var that = (Result) obj;
|
||||
return Objects.equals(this.paramOr, that.paramOr) &&
|
||||
Objects.equals(this.archiveList, that.archiveList);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(paramOr, archiveList);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Result[" +
|
||||
"paramOr=" + paramOr + ", " +
|
||||
"archiveList=" + archiveList + ']';
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -84,6 +84,7 @@ import com.hippo.ehviewer.client.data.GalleryTagGroup;
|
||||
import com.hippo.ehviewer.client.data.ListUrlBuilder;
|
||||
import com.hippo.ehviewer.client.data.PreviewSet;
|
||||
import com.hippo.ehviewer.client.exception.NoHAtHClientException;
|
||||
import com.hippo.ehviewer.client.parser.ArchiveParser;
|
||||
import com.hippo.ehviewer.client.parser.RateGalleryParser;
|
||||
import com.hippo.ehviewer.client.parser.VoteTagParser;
|
||||
import com.hippo.ehviewer.dao.DownloadInfo;
|
||||
@@ -257,7 +258,7 @@ public class GalleryDetailScene extends CollapsingToolbarScene implements View.O
|
||||
}
|
||||
});
|
||||
private String mArchiveFormParamOr;
|
||||
private Pair<String, String>[] mArchiveList;
|
||||
private List<ArchiveParser.Archive> mArchiveList;
|
||||
@State
|
||||
private int mState = STATE_INIT;
|
||||
private boolean mModifyingFavorites;
|
||||
@@ -1754,7 +1755,7 @@ public class GalleryDetailScene extends CollapsingToolbarScene implements View.O
|
||||
}
|
||||
|
||||
private class ArchiveListDialogHelper implements AdapterView.OnItemClickListener,
|
||||
DialogInterface.OnDismissListener, EhClient.Callback<Pair<String, Pair<String, String>[]>> {
|
||||
DialogInterface.OnDismissListener, EhClient.Callback<ArchiveParser.Result> {
|
||||
|
||||
@Nullable
|
||||
private CircularProgressIndicator mProgressView;
|
||||
@@ -1789,21 +1790,18 @@ public class GalleryDetailScene extends CollapsingToolbarScene implements View.O
|
||||
}
|
||||
}
|
||||
|
||||
private void bind(Pair<String, String>[] data) {
|
||||
private void bind(List<ArchiveParser.Archive> data) {
|
||||
if (null == mDialog || null == mProgressView || null == mErrorText || null == mListView) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (0 == data.length) {
|
||||
if (null == data || 0 == data.size()) {
|
||||
mProgressView.setVisibility(View.GONE);
|
||||
mErrorText.setVisibility(View.VISIBLE);
|
||||
mListView.setVisibility(View.GONE);
|
||||
mErrorText.setText(R.string.no_archives);
|
||||
} else {
|
||||
String[] nameArray = new String[data.length];
|
||||
for (int i = 0, n = data.length; i < n; i++) {
|
||||
nameArray[i] = data[i].second;
|
||||
}
|
||||
var nameArray = data.stream().map(ArchiveParser.Archive::format).toArray(String[]::new);
|
||||
mProgressView.setVisibility(View.GONE);
|
||||
mErrorText.setVisibility(View.GONE);
|
||||
mListView.setVisibility(View.VISIBLE);
|
||||
@@ -1815,8 +1813,8 @@ public class GalleryDetailScene extends CollapsingToolbarScene implements View.O
|
||||
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
||||
Context context = getContext();
|
||||
MainActivity activity = getMainActivity();
|
||||
if (null != context && null != activity && null != mArchiveList && position < mArchiveList.length) {
|
||||
String res = mArchiveList[position].first;
|
||||
if (null != context && null != activity && null != mArchiveList && position < mArchiveList.size()) {
|
||||
String res = mArchiveList.get(position).res();
|
||||
EhRequest request = new EhRequest();
|
||||
request.setMethod(EhClient.METHOD_DOWNLOAD_ARCHIVE);
|
||||
request.setArgs(mGalleryDetail.gid, mGalleryDetail.token, mArchiveFormParamOr, res);
|
||||
@@ -1843,12 +1841,12 @@ public class GalleryDetailScene extends CollapsingToolbarScene implements View.O
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSuccess(Pair<String, Pair<String, String>[]> result) {
|
||||
public void onSuccess(ArchiveParser.Result result) {
|
||||
if (mRequest != null) {
|
||||
mRequest = null;
|
||||
mArchiveFormParamOr = result.first;
|
||||
mArchiveList = result.second;
|
||||
bind(result.second);
|
||||
mArchiveFormParamOr = result.paramOr();
|
||||
mArchiveList = result.archiveList();
|
||||
bind(result.archiveList());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user