fix: simplify qbit action finish detect

This commit is contained in:
master 2024-03-03 02:30:39 +08:00
parent 05273e21ba
commit 01053da8d2

View File

@ -118,9 +118,9 @@ impl QBittorrentDownloader {
Ok(()) Ok(())
} }
pub async fn wait_some_torrents_until<F>( pub async fn wait_torrents_until<F>(
&self, &self,
hashes: Vec<String>, arg: GetTorrentListArg,
stop_wait_fn: F, stop_wait_fn: F,
timeout: Option<Duration>, timeout: Option<Duration>,
) -> eyre::Result<()> ) -> eyre::Result<()>
@ -128,11 +128,7 @@ impl QBittorrentDownloader {
F: FnMut(Vec<QbitTorrent>) -> bool, F: FnMut(Vec<QbitTorrent>) -> bool,
{ {
self.wait_until( self.wait_until(
|| { || arg,
GetTorrentListArg::builder()
.hashes(hashes.join("|"))
.build()
},
async move |client: Arc<Qbit>, async move |client: Arc<Qbit>,
arg: GetTorrentListArg| arg: GetTorrentListArg|
-> eyre::Result<Vec<QbitTorrent>> { -> eyre::Result<Vec<QbitTorrent>> {
@ -251,28 +247,17 @@ impl TorrentDownloader for QBittorrentDownloader {
} }
async fn delete_torrents(&self, hashes: Vec<String>) -> eyre::Result<()> { async fn delete_torrents(&self, hashes: Vec<String>) -> eyre::Result<()> {
let existed_list = self self.client
.client .delete_torrents(hashes.clone(), Some(true))
.get_torrent_list(
GetTorrentListArg::builder()
.hashes(hashes.clone().join("|"))
.build(),
)
.await?; .await?;
if !existed_list.is_empty() { self.wait_torrents_until(
self.client GetTorrentListArg::builder()
.delete_torrents(hashes.clone(), Some(true)) .hashes(hashes.join("|"))
.await?; .build(),
self.wait_sync_until( |torrents| -> bool { torrents.is_empty() },
|sync_data| -> bool { None,
sync_data )
.torrents .await?;
.map_or(true, |t| hashes.iter().all(|h| !t.contains_key(h)))
},
None,
)
.await?;
}
Ok(()) Ok(())
} }
@ -300,16 +285,15 @@ impl TorrentDownloader for QBittorrentDownloader {
self.client self.client
.set_torrent_location(hashes.clone(), new_path) .set_torrent_location(hashes.clone(), new_path)
.await?; .await?;
self.wait_sync_until( self.wait_torrents_until(
|sync_data| -> bool { GetTorrentListArg::builder()
hashes.iter().all(|hash| { .hashes(hashes.join("|"))
sync_data.torrents.as_ref().map_or(false, |t| { .build(),
t.get(hash).map_or(false, |t| { |torrents| -> bool {
t.save_path torrents.iter().all(|t| {
.as_ref() t.save_path
.map_or(false, |p| path_str_equals(p, new_path).unwrap_or(false)) .as_ref()
}) .map_or(false, |p| path_str_equals(p, new_path).unwrap_or(false))
})
}) })
}, },
None, None,
@ -348,15 +332,14 @@ impl TorrentDownloader for QBittorrentDownloader {
} else { } else {
result?; result?;
} }
self.wait_sync_until( self.wait_torrents_until(
|sync_data| { GetTorrentListArg::builder()
sync_data.torrents.map_or(false, |ts| { .hashes(hashes.join("|"))
hashes.iter().all(|h| { .build(),
ts.get(h).map_or(false, |t| { |torrents| {
t.category.as_ref().map_or(false, |c| c == category) torrents
}) .iter()
}) .all(|t| t.category.as_ref().map_or(false, |c| c == category))
})
}, },
None, None,
) )
@ -372,19 +355,18 @@ impl TorrentDownloader for QBittorrentDownloader {
.add_torrent_tags(hashes.clone(), tags.clone()) .add_torrent_tags(hashes.clone(), tags.clone())
.await?; .await?;
let tag_sets = tags.iter().map(|s| s.as_str()).collect::<HashSet<&str>>(); let tag_sets = tags.iter().map(|s| s.as_str()).collect::<HashSet<&str>>();
self.wait_sync_until( self.wait_torrents_until(
|sync_data| { GetTorrentListArg::builder()
sync_data.torrents.map_or(false, |ts| { .hashes(hashes.join("|"))
hashes.iter().all(|h| { .build(),
ts.get(h).map_or(false, |t| { |torrents| {
t.tags.as_ref().map_or(false, |t| { torrents.iter().all(|t| {
t.split(',') t.tags.as_ref().map_or(false, |t| {
.map(|s| s.trim()) t.split(',')
.filter(|s| !s.is_empty()) .map(|s| s.trim())
.collect::<HashSet<&str>>() .filter(|s| !s.is_empty())
.is_superset(&tag_sets) .collect::<HashSet<&str>>()
}) .is_superset(&tag_sets)
})
}) })
}) })
}, },