Author Topic: Powershell script to remove completed downloads  (Read 133 times)

dalboz

  • Newbie
  • *
  • Posts: 1
  • Karma: +0/-0
    • View Profile
Powershell script to remove completed downloads
« on: April 27, 2019, 03:14:23 AM »
Just sharing my script to prune completed torrents. I know qBT has this built-in but Sonarr complains if you turn this on, so I use a script. You need to add localhost to bypass authentication.

Code: [Select]
# CONFIG

# Torrents removed if Completed this many minutes ago or older
$PruneAfterMins = 15

# URL of your server including http(s) and port of the WebUI
$ServerURL = 'http://localhost:8080'

# SCRIPT
Clear-Host
$OlderThan = Get-Date (Get-Date).AddMinutes($PruneAfterMins * -1)

$Result = Invoke-RestMethod -Uri "$ServerURL/query/torrents?filter=completed"

foreach ($Torrent in $result) {
    # timestamps are in UTC
    $Fin = [timezone]::CurrentTimeZone.ToLocalTime((Get-Date 01.01.1970) + ([System.TimeSpan]::fromseconds($Torrent.completion_on)))
    $Prune = ($Fin -lt $OlderThan)
    Write-Output $Torrent.name
    Write-Output ('Hash = ' + $Torrent.hash)
    Write-Output ('Finished on ' + $Fin + ' [' + $Torrent.completion_on + ']')
    Write-Output "Prune = $Prune"
    Write-Output ""

    If ($Fin) {
        $Body = ('hashes=' + $Torrent.hash)
        Invoke-RestMethod -Method Post -Uri "$ServerURl/command/delete" -Body $Body
    }
   
}