Convert TorrentParser to kotlin

This commit is contained in:
FooIbar authored and tarsin committed 2023-04-03 00:06:40 +08:00
1 parent 917cb107ec
commit fdc1868cd4
2 files changed
+36 -131

No files matched your search

@@ -13,135 +13,44 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hippo.ehviewer.client.parser
package com.hippo.ehviewer.client.parser;
import org.jsoup.Jsoup
import java.util.regex.Pattern
import androidx.annotation.NonNull;
object TorrentParser {
private val 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>")
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("</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>");
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());
fun parse(body: String): List<Result> {
val torrentList = ArrayList<Result>()
val d = Jsoup.parse(body)
val es = d.select("form>div>table")
for (e in es) {
val 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);
val posted = m.group(1)!!
val size = m.group(2)!!
val seeds = m.group(3)!!.toInt()
val peers = m.group(4)!!.toInt()
val downloads = m.group(5)!!.toInt()
val url = ParserUtils.trim(m.group(7))
val name = ParserUtils.trim(m.group(8))
torrentList.add(Result(posted, size, seeds, peers, downloads, url, name))
}
}
return torrentList;
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 + ']';
}
class Result(
private val posted: String,
private val size: String,
private val seeds: Int,
private val peers: Int,
private val downloads: Int,
val url: String,
val name: String,
) {
fun format() = "[$posted] $name [$size] [↑$seeds ↓$peers ✓$downloads]"
}
}
@@ -1692,7 +1692,7 @@ class GalleryDetailScene : BaseScene(), DownloadInfoListener {
mListView!!.visibility = View.GONE
mErrorText!!.setText(R.string.no_archives)
} else {
val nameArray = data.stream().map {
val nameArray = data.map {
it.run {
if (isHAtH) {
val costStr =
@@ -1706,7 +1706,7 @@ class GalleryDetailScene : BaseScene(), DownloadInfoListener {
"$nameStr [$size] [$costStr]"
}
}
}.toArray()
}.toTypedArray()
mProgressView!!.visibility = View.GONE
mErrorText!!.visibility = View.GONE
mListView!!.visibility = View.VISIBLE
@@ -1837,11 +1837,7 @@ class GalleryDetailScene : BaseScene(), DownloadInfoListener {
mListView!!.visibility = View.GONE
mErrorText!!.setText(R.string.no_torrents)
} else {
val nameArray = data.stream().map { torrent: TorrentParser.Result ->
torrent.format { id: Int ->
resources.getString(id)
}
}.toArray()
val nameArray = data.map { it.format() }.toTypedArray()
mProgressView!!.visibility = View.GONE
mErrorText!!.visibility = View.GONE
mListView!!.visibility = View.VISIBLE
@@ -1853,8 +1849,8 @@ class GalleryDetailScene : BaseScene(), DownloadInfoListener {
override fun onItemClick(parent: AdapterView<*>?, view: View, position: Int, id: Long) {
val context = context
if (null != context && null != mTorrentList && position < mTorrentList!!.size) {
val url = mTorrentList!![position].url()
val name = mTorrentList!![position].name()
val url = mTorrentList!![position].url
val name = mTorrentList!![position].name
// TODO: Don't use buggy system download service
val r =
DownloadManager.Request(Uri.parse(url.replace("exhentai.org", "ehtracker.org")))