Merge pull request #1 from blinkfink182/v0.1

V0.1
This commit was merged in pull request #1.
This commit is contained in:
Kelly Thomas Reardon
2024-12-27 22:22:53 -06:00
committed by GitHub
3 changed files with 89 additions and 29 deletions
+10
View File
@@ -0,0 +1,10 @@
FROM mcr.microsoft.com/powershell
ENV SCHEDULE=60
VOLUME /tmp
WORKDIR /data
ADD ["Invoke-QBTCleanarr.ps1", "/data/"]
ENTRYPOINT ["pwsh", "-Command", "/data/Invoke-QBTCleanarr.ps1"]
+66 -29
View File
@@ -1,51 +1,88 @@
# Configuration # Configuration
$qbittorrentUrl = 'http://localhost:8080' # Change if necessary [int] $schedule = $ENV:SCHEDULE
$username = 'your_username' [string] $qbtHost = $ENV:QBTHOST
$password = 'your_password' [string] $qbtPort = $ENV:QBTPORT
$excludedTrackers = @('tracker1.com', 'tracker2.com') [string] $qbtUser = $ENV:QBTUSER
[string] $qbtPassword = $ENV:QBTPASSWORD
[string] $notificationWebhookURL = $ENV:NOTIFICATIONWEBHOOKURL
# Function to authenticate and get a session # Function to authenticate and get a session
function Get-QBittorrentSession { function Get-QBittorrentSession {
$session = New-Object System.Net.WebClient $qbtHeaders = @{
$session.Headers.Add("Content-Type", "application/x-www-form-urlencoded") Referer = "https://$($qbtHost):$($qbtPort)"
}
$qbtBody = @{
username = $qbtUser
password = $qbtPassword
}
$qbtSession = [Microsoft.PowerShell.Commands.WebRequestSession]::new()
$qbtResponse = Invoke-WebRequest -Uri "https://$($qbtHost):$($qbtPort)/api/v2/auth/login" -WebSession $qbtSession -Method POST -Headers $qbtHeaders -Body $qbtBody
# Authenticate return $qbtSession
$session.UploadString("$qbittorrentUrl/api/v2/auth/login", "username=$username&password=$password")
return $session
} }
# Function to get all torrents # Function to get all torrents
function Get-Torrents { function Get-Torrents {
param ( param (
[System.Net.WebClient]$session [Microsoft.PowerShell.Commands.WebRequestSession]$session
) )
$response = $session.DownloadString("$qbittorrentUrl/api/v2/torrents/info") $qbtResponse = Invoke-WebRequest -Uri "https://$($qbtHost):$($qbtPort)/api/v2/torrents/info" -WebSession $session -Method GET
return $response | ConvertFrom-Json return $qbtResponse.Content | ConvertFrom-Json
} }
# Function to delete a torrent # Function to delete a torrent
function Delete-Torrent { function Remove-Torrent {
param ( param (
[System.Net.WebClient]$session, [Microsoft.PowerShell.Commands.WebRequestSession]$session,
[string]$hash [string]$hash
) )
$session.UploadString("$qbittorrentUrl/api/v2/torrents/delete", "hashes=$hash") $qbtHeaders = @{
} Referer = "https://$($qbtHost):$($qbtPort)"
}
$qbtBody = @{
hashes = $hash
deleteFiles = 'true'
}
$qbtResponse = Invoke-WebRequest -Uri "https://$($qbtHost):$($qbtPort)/api/v2/torrents/delete" -WebSession $session -Method POST -Headers $qbtHeaders -Body $qbtBody
# Main script if ($notificationWebhookURL)
$session = Get-QBittorrentSession {
$torrents = Get-Torrents -session $session $headers = @{
"Content-Type"="application/json"
foreach ($torrent in $torrents) {
if ($torrent.state -eq 'completed') {
# Check if any of the excluded trackers are in the torrent's tracker list
$trackers = $torrent.trackers | ForEach-Object { $_.url }
if (-not ($trackers | Where-Object { $excludedTrackers -contains $_ })) {
# Delete the torrent
Delete-Torrent -session $session -hash $torrent.hash
} }
$body = @{
content="Removed torrent with hash $hash"
}
$json = $body | ConvertTo-Json
Invoke-WebRequest -URI $notificationWebhookURL -Method post -Body $json -Headers $headers
} }
} }
# Logout (optional, but good practice) while ($true)
$session.UploadString("$qbittorrentUrl/api/v2/auth/logout", "") {
# Main script
$session = Get-QBittorrentSession
$torrents = Get-Torrents -session $session
foreach ($torrent in $torrents) {
if ($torrent.state -eq 'uploading' -or $torrent.state -like "*UP") {
# Check if any of the excluded trackers are in the torrent's tracker list
if ($torrent.tracker -match "https?://([^/]+)") {
$trackers = $matches[1]
} else {
$trackers = @()
}
if (-not ($trackers | Where-Object { $excludedTrackers -contains $_ })) {
# Delete the torrent
Write-Host "Would delete: $($torrent.name)"
Remove-Torrent -session $session -hash $torrent.hash
}
}
}
# Logout (optional, but good practice)
# $session.UploadString("$qbittorrentUrl/api/v2/auth/logout", "")
Write-Host "Sleeping for $schedule minutes..."
Start-Sleep ($schedule * 60)
}
+13
View File
@@ -0,0 +1,13 @@
# SAMPLE DOCKER COMPOSE
services:
qbt-cleanarr:
container_name: qbt-cleanarr
image: docker.io/blinkfink182/qbt-cleanarr
environment:
- SCHEDULE=15 # OPTIONAL: time in minutes to wait between check, 60 is default
# BELOW ARE ALL REQUIRED
- QBTHOST=host.docker.internal # qbittorrent host
- QBTPORT=8787 # qbittorrent port
- QBTUSER=admin # qbittorrent webUI username
- QBTPASSWORD=adminpass # qbittorrent webUI password
- NOTIFICATIONWEBHOOKURL=https://discord.com/api/webhooks/<webhookURL> # discord notification wehook URL