From acb2c720af55ea81b29556f4338e482b37d65dfd Mon Sep 17 00:00:00 2001 From: FooIbar <118464521+FooIbar@users.noreply.github.com> Date: Thu, 7 Mar 2024 22:57:59 +0800 Subject: [PATCH] Fix torrent parsing --- .../ehviewer/ui/screen/GalleryDetailScreen.kt | 31 ++++++++++--------- app/src/main/rust/src/parser/torrent.rs | 2 +- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/app/src/main/kotlin/com/hippo/ehviewer/ui/screen/GalleryDetailScreen.kt b/app/src/main/kotlin/com/hippo/ehviewer/ui/screen/GalleryDetailScreen.kt index fd9f80c3e..693a0de0b 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/ui/screen/GalleryDetailScreen.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/ui/screen/GalleryDetailScreen.kt @@ -601,18 +601,23 @@ fun GalleryDetailScreen(args: GalleryDetailScreenArgs, navigator: DestinationsNa } } } - val items = mTorrentList!!.map { it.format() } - val selected = showSelectItem(items, R.string.torrents, false) - val url = mTorrentList!![selected].url - val name = "${mTorrentList!![selected].name}.torrent" - val r = DownloadManager.Request(Uri.parse(url)) - r.setDestinationInExternalPublicDir( - Environment.DIRECTORY_DOWNLOADS, - AppConfig.APP_DIRNAME + "/" + FileUtils.sanitizeFilename(name), - ) - r.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED) - r.addRequestHeader("Cookie", EhCookieStore.getCookieHeader(url)) - downloadManager.enqueue(r) + if (mTorrentList!!.isEmpty()) { + showSnackbar(noTorrents) + } else { + val items = mTorrentList!!.map { it.format() } + val selected = showSelectItem(items, R.string.torrents, false) + val url = mTorrentList!![selected].url + val name = "${mTorrentList!![selected].name}.torrent" + val r = DownloadManager.Request(Uri.parse(url)) + r.setDestinationInExternalPublicDir( + Environment.DIRECTORY_DOWNLOADS, + AppConfig.APP_DIRNAME + "/" + FileUtils.sanitizeFilename(name), + ) + r.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED) + r.addRequestHeader("Cookie", EhCookieStore.getCookieHeader(url)) + downloadManager.enqueue(r) + showSnackbar(downloadTorrentStarted) + } } EhIconButton( icon = Icons.Default.SwapVerticalCircle, @@ -624,8 +629,6 @@ fun GalleryDetailScreen(args: GalleryDetailScreenArgs, navigator: DestinationsNa if (granted) { runSuspendCatching { showTorrentDialog() - }.onSuccess { - showSnackbar(downloadTorrentStarted) }.onFailure { logcat(it) showSnackbar(downloadTorrentFailed) diff --git a/app/src/main/rust/src/parser/torrent.rs b/app/src/main/rust/src/parser/torrent.rs index f0822f2b9..c1be41530 100644 --- a/app/src/main/rust/src/parser/torrent.rs +++ b/app/src/main/rust/src/parser/torrent.rs @@ -25,7 +25,7 @@ fn parse_torrent_list(dom: &VDom, parser: &Parser) -> Result> { if html.contains("Expunged") { None } else { - let grp = regex!(" ([0-9-]+) [0-9:]+[\\s\\S]+ ([0-9.]+ [KMGT]iB)[\\s\\S]+ ([0-9]+)[\\s\\S]+ ([0-9]+)[\\s\\S]+ ([0-9]+)[\\s\\S]+([^<]+)[\\s\\S]+onclick=\"document.location='([^\"]+)'[^<]+>([^<]+)").captures(&html)?; + let grp = regex!("([0-9-]+) [0-9:]+[\\s\\S]+ ([0-9.]+ [KMGT]iB)[\\s\\S]+ ([0-9]+)[\\s\\S]+ ([0-9]+)[\\s\\S]+ ([0-9]+)[\\s\\S]+ ([^<]+)[\\s\\S]+onclick=\"document.location='([^\"]+)'[^<]+>([^<]+)").captures(&html)?; let name = unescape(&grp[8]).ok()?; Some(Torrent { posted: grp[1].to_string(),