Move torrent parser to rust

This commit is contained in:
tarsin committed 2023-06-14 16:20:18 +08:00
1 parent 84d13fe9c5
commit cbebdb542d
3 files changed
+34 -34

No files matched your search

@@ -15,42 +15,16 @@
*/
package com.hippo.ehviewer.client.parser
import org.jsoup.Jsoup
import java.util.regex.Pattern
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>")
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()) {
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
val list = mutableListOf<Array<String>>()
parseTorrent(body, list)
return list.map { Result(it[0], it[1], it[2].toInt(), it[3].toInt(), it[4].toInt(), it[5], it[6]) }
}
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,
) {
class Result(val posted: String, val size: String, val seeds: Int, val peers: Int, val downloads: Int, val url: String, val name: String) {
fun format() = "[$posted] $name [$size] [↑$seeds ↓$peers ✓$downloads]"
}
}
private external fun parseTorrent(body: String, list: List<Array<String>>)