-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqbtMagnetlinkTemplate.ps1
126 lines (103 loc) · 4.26 KB
/
qbtMagnetlinkTemplate.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
$QBT_TORRENT_NAME=NULL_NAME
$QBT_INFOHASH=NULL_HASH
# Define qBittorrent API credentials and URL
$qBittorrentURL = "http://localhost:8080"
$qBittorrentUser = "admin"
# Get secure-password that was saved on disk
function Get-SecurePassword {
# Path to the encrypted password file
$passwordFilePath = "$env:LOCALAPPDATA\qBittorrentPassword.txt"
# Check if the password file exists
if (Test-Path $passwordFilePath) {
# Read the encrypted password and convert it to a SecureString
$encryptedPassword = Get-Content -Path $passwordFilePath
$securePassword = $encryptedPassword | ConvertTo-SecureString
return $securePassword
}
else {
Write-Error "Password file not found at $passwordFilePath"
return $null
}
}
$qBittorrentPassword = Get-SecurePassword
function Get-qBittorrentSession {
param (
[String]$qBittorrentUrl,
[String]$username,
[SecureString]$password
)
$passwordPlain = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto(
[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($password)
)
$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
try {
Invoke-RestMethod -Uri "$qBittorrentUrl/api/v2/auth/login" -Method Post -WebSession $session -Body @{
username = $username
password = $passwordPlain
} | Out-Null
return $session
}
catch {
Write-Error "Failed to authenticate to qBittorrent. Please check your credentials and URL."
return $null
}
}
$session = Get-qBittorrentSession -qBittorrentUrl $qBittorrentUrl -username $qBittorrentUser -password $qBittorrentPassword
if (-not $session) { return }
# Set current directory
$currentDIR = $PSScriptRoot
# Check if torrent is already in client
$foundSTRING = Invoke-RestMethod -Uri "$qBittorrentUrl/api/v2/torrents/info" -Method Post -WebSession $session -Body @{
"hashes" = $QBT_INFOHASH
}
# Add torrents to array object
$torrentFiles = (Get-ChildItem -Filter *.torrent).FullName
# Main loop for adding torrent or changing location
if ( $foundSTRING -ne "") {
Write-Host "$QBT_TORRENT_NAME is already in client"
Invoke-RestMethod -Uri "$qBittorrentUrl/api/v2/torrents/setLocation" -Method Post -ContentType "application/x-www-form-urlencoded" -WebSession $session -Body @{
"hashes" = $QBT_INFOHASH
"location" = $currentDIR
}
# Recheck torrent
Invoke-RestMethod -Uri "$qBittorrentUrl/api/v2/torrents/recheck" -Method Post -ContentType "application/x-www-form-urlencoded" -WebSession $session -Body @{
"hashes" = $QBT_INFOHASH
}
# Resume torrent
Invoke-RestMethod -Uri "$qBittorrentUrl/api/v2/torrents/start" -Method Post -WebSession $session -Body @{
"hashes" = $QBT_INFOHASH
}
}
else {
Write-Host "$QBT_TORRENT_NAME is NOT in client"
foreach ($torrent in $torrentFiles) {
# Get just the file name (no path)
$FileName = Split-Path $torrent -leaf
# Get a GUID that is used to indicate the start and end of the file in the request
$boundary = "$([System.Guid]::NewGuid().ToString())"
# Read the contents of the file
$FileBytes = [System.IO.File]::ReadAllBytes($torrent)
$FileContent = [System.Text.Encoding]::GetEncoding('iso-8859-1').GetString($FileBytes)
# Build a Body to submit the request
$bodyLines = @"
--$boundary
Content-Disposition: form-data; name="savepath"
$currentDir
--$boundary
Content-Disposition: form-data; name=`"`"; filename=`"$FileName`"
Content-Type: application/x-bittorrent
$FileContent
$($boundary)--
"@
# Add torrent via .torrent files
Invoke-RestMethod -Uri "$qBittorrentUrl/api/v2/torrents/add" -Method Post -WebSession $session -ContentType "multipart/form-data; boundary=`"$boundary`"" -Body $bodyLines
# Recheck torrent
Invoke-RestMethod -Uri "$qBittorrentUrl/api/v2/torrents/recheck" -Method Post -ContentType "application/x-www-form-urlencoded" -WebSession $session -Body @{
"hashes" = $QBT_INFOHASH
}
# Resume torrent
Invoke-RestMethod -Uri "$qBittorrentUrl/api/v2/torrents/start" -Method Post -WebSession $session -Body @{
"hashes" = $QBT_INFOHASH
}
}
}