From fdc1868cd4b4989a0cae8f5d7aad9a9211b3962f Mon Sep 17 00:00:00 2001 From: FooIbar <118464521+FooIbar@users.noreply.github.com> Date: Sun, 2 Apr 2023 17:40:54 +0800 Subject: [PATCH] Convert TorrentParser to kotlin --- .../ehviewer/client/parser/TorrentParser.kt | 153 ++++-------------- .../ehviewer/ui/scene/GalleryDetailScene.kt | 14 +- 2 files changed, 36 insertions(+), 131 deletions(-) diff --git a/app/src/main/java/com/hippo/ehviewer/client/parser/TorrentParser.kt b/app/src/main/java/com/hippo/ehviewer/client/parser/TorrentParser.kt index 314eeac67..347063060 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/parser/TorrentParser.kt +++ b/app/src/main/java/com/hippo/ehviewer/client/parser/TorrentParser.kt @@ -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(" ([0-9-]+) [0-9:]+[\\s\\S]+ ([0-9.]+ [KMGT]B)[\\s\\S]+ ([0-9]+)[\\s\\S]+ ([0-9]+)[\\s\\S]+ ([0-9]+)[\\s\\S]+([^<]+)[\\s\\S]+onclick=\"document.location='([^\"]+)'[^<]+>([^<]+)") -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(" ([0-9-]+) [0-9:]+[\\s\\S]+ ([0-9\\.]+ [KMGT]B)[\\s\\S]+ ([0-9]+)[\\s\\S]+ ([0-9]+)[\\s\\S]+ ([0-9]+)[\\s\\S]+([^<]+)[\\s\\S]+onclick=\"document.location='([^\"]+)'[^<]+>([^<]+)"); - - public static List parse(String body) { - var torrentList = new ArrayList(); - 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 { + val torrentList = ArrayList() + 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 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]" } } diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryDetailScene.kt b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryDetailScene.kt index f911bd08a..147fd5c00 100755 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryDetailScene.kt +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryDetailScene.kt @@ -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")))