Refactor qBittorrent script for improved authentication and torrent management
This commit is contained in:
+33
-18
@@ -1,35 +1,45 @@
|
|||||||
# Configuration
|
# Configuration
|
||||||
$qbittorrentUrl = 'http://localhost:8080' # Change if necessary
|
$qbtHost = 'ktr.h4ck.me'
|
||||||
$username = 'your_username'
|
$qbtPort = '7878'
|
||||||
$password = 'your_password'
|
$qbtUser = 'kelly'
|
||||||
$excludedTrackers = @('tracker1.com', 'tracker2.com')
|
$qbtPassword = 'KjBWnJC;cabA^gFxcN]5'
|
||||||
|
$excludedTrackers = @(
|
||||||
|
'digitalcore.club',
|
||||||
|
'please.passthepopcorn.me:2710',
|
||||||
|
'please.passthepopcorn.me'
|
||||||
|
)
|
||||||
|
|
||||||
# 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 Delete-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")
|
# $session.UploadString("$qbittorrentUrl/api/v2/torrents/delete", "hashes=$hash")
|
||||||
}
|
}
|
||||||
|
|
||||||
# Main script
|
# Main script
|
||||||
@@ -37,15 +47,20 @@ $session = Get-QBittorrentSession
|
|||||||
$torrents = Get-Torrents -session $session
|
$torrents = Get-Torrents -session $session
|
||||||
|
|
||||||
foreach ($torrent in $torrents) {
|
foreach ($torrent in $torrents) {
|
||||||
if ($torrent.state -eq 'completed') {
|
if ($torrent.state -eq 'uploading' -or $torrent.state -like "*UP") {
|
||||||
# Check if any of the excluded trackers are in the torrent's tracker list
|
# Check if any of the excluded trackers are in the torrent's tracker list
|
||||||
$trackers = $torrent.trackers | ForEach-Object { $_.url }
|
if ($torrent.tracker -match "https?://([^/]+)") {
|
||||||
|
$trackers = $matches[1]
|
||||||
|
} else {
|
||||||
|
$trackers = @()
|
||||||
|
}
|
||||||
if (-not ($trackers | Where-Object { $excludedTrackers -contains $_ })) {
|
if (-not ($trackers | Where-Object { $excludedTrackers -contains $_ })) {
|
||||||
# Delete the torrent
|
# Delete the torrent
|
||||||
Delete-Torrent -session $session -hash $torrent.hash
|
Write-Host "Would delete: $($torrent.name)"
|
||||||
|
# Delete-Torrent -session $session -hash $torrent.hash
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# Logout (optional, but good practice)
|
# Logout (optional, but good practice)
|
||||||
$session.UploadString("$qbittorrentUrl/api/v2/auth/logout", "")
|
# $session.UploadString("$qbittorrentUrl/api/v2/auth/logout", "")
|
||||||
|
|||||||
Reference in New Issue
Block a user