Add more info for torrent download

This commit is contained in:
小白-白 authored and tarsin committed 2023-01-04 12:26:46 +08:00
1 parent 869da61fd0
commit b9daf608ff
4 files changed
+132 -28

No files matched your search

@@ -649,8 +649,8 @@ public class EhEngine {
return result;
}
public static Pair<String, String>[] getTorrentList(@Nullable EhClient.Task task, OkHttpClient okHttpClient,
String url, long gid, String token) throws Throwable {
public static List<TorrentParser.Result> getTorrentList(@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();
@@ -663,7 +663,7 @@ public class EhEngine {
String body = null;
Headers headers = null;
Pair<String, String>[] result;
List<TorrentParser.Result> result;
int code = -1;
try (Response response = call.execute()) {
code = response.code();
@@ -16,27 +16,132 @@
package com.hippo.ehviewer.client.parser;
import android.util.Pair;
import androidx.annotation.NonNull;
import org.jsoup.Jsoup;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.IntFunction;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TorrentParser {
private static final Pattern PATTERN_TORRENT = Pattern.compile("<td colspan=\"5\"> &nbsp; <a href=\".*\" onclick=\"document.location='([^\"]+)'[^<]+>([^<]+)</a></td>");
private static final Pattern PATTERN_TORRENT = Pattern.compile("</span> ([0-9-]+) [0-9:]+</td>[\\s\\S]+</span> ([0-9\\.]+ [KMGT]B)</td>[\\s\\S]+</span> ([0-9]+)</td>[\\s\\S]+</span> ([0-9]+)</td>[\\s\\S]+</span> ([0-9]+)</td>[\\s\\S]+</span>([^<]+)</td>[\\s\\S]+onclick=\"document.location='([^\"]+)'[^<]+>([^<]+)</a>");
@SuppressWarnings("unchecked")
public static Pair<String, String>[] parse(String body) {
List<Pair<String, String>> torrentList = new ArrayList<>();
Matcher m = PATTERN_TORRENT.matcher(body);
while (m.find()) {
String url = ParserUtils.trim(m.group(1));
String name = ParserUtils.trim(m.group(2));
Pair<String, String> item = new Pair<>(url, name);
torrentList.add(item);
public static List<Result> parse(String body) {
var torrentList = new ArrayList<Result>();
var d = Jsoup.parse(body);
var es = d.select("form>div>table");
for (var e : es) {
Matcher m = PATTERN_TORRENT.matcher(e.html());
if (m.find()) {
String posted = m.group(1);
String size = m.group(2);
int seeds = Integer.parseInt(m.group(3));
int peers = Integer.parseInt(m.group(4));
int downloads = Integer.parseInt(m.group(5));
String uploader = ParserUtils.trim(m.group(6));
String url = ParserUtils.trim(m.group(7));
String name = ParserUtils.trim(m.group(8));
var item = new Result(posted, size, seeds, peers, downloads, uploader, url, name);
torrentList.add(item);
}
}
return torrentList;
}
public static final class Result {
private final String posted;
private final String size;
private final int seeds;
private final int peers;
private final int downloads;
private final String uploader;
private final String url;
private final String name;
public Result(String posted, String size, int seeds, int peers, int downloads, String uploader, String url, String name) {
this.posted = posted;
this.size = size;
this.seeds = seeds;
this.peers = peers;
this.downloads = downloads;
this.uploader = uploader;
this.url = url;
this.name = name;
}
public String format(IntFunction<String> getString) {
return String.format("[%s] %s [%s] [↑%s ↓%s ✓%s]", posted, name, size, seeds, peers, downloads);
}
public String posted() {
return posted;
}
public String size() {
return size;
}
public int seeds() {
return seeds;
}
public int peers() {
return peers;
}
public int downloads() {
return downloads;
}
public String uploader() {
return uploader;
}
public String url() {
return url;
}
public String name() {
return name;
}
@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.posted, that.posted) &&
Objects.equals(this.size, that.size) &&
this.seeds == that.seeds &&
this.peers == that.peers &&
this.downloads == that.downloads &&
Objects.equals(this.uploader, that.uploader) &&
Objects.equals(this.url, that.url) &&
Objects.equals(this.name, that.name);
}
@Override
public int hashCode() {
return Objects.hash(posted, size, seeds, peers, downloads, uploader, url, name);
}
@NonNull
@Override
public String toString() {
return "Result[" +
"posted=" + posted + ", " +
"size=" + size + ", " +
"seeds=" + seeds + ", " +
"peers=" + peers + ", " +
"downloads=" + downloads + ", " +
"uploader=" + uploader + ", " +
"url=" + url + ", " +
"name=" + name + ']';
}
return torrentList.toArray(new Pair[0]);
}
}
@@ -86,6 +86,7 @@ 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.TorrentParser;
import com.hippo.ehviewer.client.parser.VoteTagParser;
import com.hippo.ehviewer.dao.DownloadInfo;
import com.hippo.ehviewer.dao.Filter;
@@ -243,7 +244,7 @@ public class GalleryDetailScene extends CollapsingToolbarScene implements View.O
@Nullable
private GalleryDetail mGalleryDetail;
private int mRequestId = IntIdGenerator.INVALID_ID;
private Pair<String, String>[] mTorrentList;
private List<TorrentParser.Result> mTorrentList;
ActivityResultLauncher<String> requestStoragePermissionLauncher = registerForActivityResult(
new ActivityResultContracts.RequestPermission(),
result -> {
@@ -1879,7 +1880,7 @@ public class GalleryDetailScene extends CollapsingToolbarScene implements View.O
}
private class TorrentListDialogHelper implements AdapterView.OnItemClickListener,
DialogInterface.OnDismissListener, EhClient.Callback<Pair<String, String>[]> {
DialogInterface.OnDismissListener, EhClient.Callback<List<TorrentParser.Result>> {
@Nullable
private CircularProgressIndicator mProgressView;
@@ -1914,21 +1915,18 @@ public class GalleryDetailScene extends CollapsingToolbarScene implements View.O
}
}
private void bind(Pair<String, String>[] data) {
private void bind(List<TorrentParser.Result> 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_torrents);
} 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(torrent -> torrent.format(getResources()::getString)).toArray(String[]::new);
mProgressView.setVisibility(View.GONE);
mErrorText.setVisibility(View.GONE);
mListView.setVisibility(View.VISIBLE);
@@ -1939,9 +1937,9 @@ public class GalleryDetailScene extends CollapsingToolbarScene implements View.O
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Context context = getContext();
if (null != context && null != mTorrentList && position < mTorrentList.length) {
String url = mTorrentList[position].first;
String name = mTorrentList[position].second;
if (null != context && null != mTorrentList && position < mTorrentList.size()) {
String url = mTorrentList.get(position).url();
String name = mTorrentList.get(position).name();
// TODO: Don't use buggy system download service
DownloadManager.Request r = new DownloadManager.Request(Uri.parse(url.replace("exhentai.org", "ehtracker.org")));
r.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,
@@ -1979,7 +1977,7 @@ public class GalleryDetailScene extends CollapsingToolbarScene implements View.O
}
@Override
public void onSuccess(Pair<String, String>[] result) {
public void onSuccess(List<TorrentParser.Result> result) {
if (mRequest != null) {
mRequest = null;
mTorrentList = result;