Add session validation for qBittorrent API and fix typo in delete message

This commit is contained in:
Kelly Thomas Reardon
2024-12-28 09:10:13 -06:00
parent 59bed82939
commit b554c5a774
+17 -1
View File
@@ -22,6 +22,19 @@ function Get-QBittorrentSession {
return $qbtSession return $qbtSession
} }
# Function to check if the session is valid
function Test-QBittorrentSession {
param (
[Microsoft.PowerShell.Commands.WebRequestSession]$session
)
try {
$qbtResponse = Invoke-WebRequest -Uri "https://$($qbtHost):$($qbtPort)/api/v2/app/version" -WebSession $session -Method GET -ErrorAction Stop
return $true
} catch {
return $false
}
}
# Function to get all torrents # Function to get all torrents
function Get-Torrents { function Get-Torrents {
param ( param (
@@ -64,6 +77,9 @@ function Remove-Torrent {
$session = Get-QBittorrentSession $session = Get-QBittorrentSession
while ($true) while ($true)
{ {
if (-not (Test-QBittorrentSession -session $session)) {
$session = Get-QBittorrentSession
}
$torrents = Get-Torrents -session $session $torrents = Get-Torrents -session $session
foreach ($torrent in $torrents) { foreach ($torrent in $torrents) {
@@ -76,7 +92,7 @@ while ($true)
} }
if (-not ($trackers | Where-Object { $excludedTrackers -contains $_ })) { if (-not ($trackers | Where-Object { $excludedTrackers -contains $_ })) {
# Delete the torrent # Delete the torrent
Write-Host "Delete: $($torrent.name) $($toreent.hash)" Write-Host "Delete: $($torrent.name) $($torrent.hash)"
Remove-Torrent -session $session -hash $torrent.hash -name $torrent.name Remove-Torrent -session $session -hash $torrent.hash -name $torrent.name
} }
} }