-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathleech.ps1
103 lines (95 loc) · 3.23 KB
/
leech.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
#Requires –Version 7
<#PSScriptInfo
.VERSION 1.0
.AUTHOR [email protected]
.TAGS qbittorrent
.RELEASENOTES
$computer = '192.168.1.4'
.\leech.ps1 -Username "admin" -Password "password" -Token "7YBGs5ZtfV2bPW6ANFea" -Computer $computer -Plex $computer
#>
<#
.DESCRIPTION
if you're the kinda person who likes to leech torrents this ones for you.
#>
param(
$Username = 'admin', # password for qbittorrent
$Password, # API password for qbittorrent
$Token, # Token for plex API
$Computer, # where qbittorrent is running
$Port = '6969', # port for qbittorrent
$Plex, # address of plex server
[int]$minutes = '1' # how often to check on downloading file(s)
)
Function Connect-qBtorrent {
[CmdletBinding()]
#[OutputType([Microsoft.PowerShell.Commands.WebRequestSession])]
Param (
$Username,
$Password,
$Computer,
$Port
)
Invoke-RestMethod -Uri "http://$($computer):$($Port)/api/v2/auth/login" -Body "username=$Username&password=$Password" -Method Post -SessionVariable webSession
$Global:Session = $websession
}
Function Remove-qBtorrent {
#start session
[CmdletBinding(SupportsShouldProcess = $True)]
Param (
$Hash,
$DeleteFiles = 'false',
$Computer,
$Port,
$WebSession
)
#if torrent is completely downloaded delete it
if ($PSCmdlet.ShouldProcess("Removing $Hash")) {
Invoke-RestMethod -Uri "http://$($computer):$($Port)/api/v2/torrents/delete?hashes=$($Hash)&deleteFiles=$($DeleteFiles)" -Method Get -webSession $webSession
}
}
Function Get-qBtorrentInfo {
#start session
[CmdletBinding()]
Param (
$Filter,
$WebSession,
$Computer,
$Port
)
$uri = "http://$($computer):$($Port)/api/v2/torrents/info"
if ($Filter) {
$uri = $uri + "?filter=$($Filter)"
}
Invoke-RestMethod -Uri $uri -Method Get -webSession $webSession
}
Connect-qBtorrent -Username $Username -Password $Password -Computer $Computer -Port $Port
while ($results = Get-qBtorrentInfo -WebSession $session -Computer $Computer -Port $port) {
foreach ($result in $results) {
switch ($result.state) {
{ $_ -in 'uploading', 'stalledUP' } {
Write-output "Removing $($result.name)"
Remove-qBtorrent -WebSession $session -Hash $result.hash -Computer $Computer -Port $port
# refresh plex if a movie is downloaded
if ($Plex) {
$return = Invoke-restmethod -uri "http://$($Plex):32400/library/sections?X-Plex-Token=$($token)"
foreach ($path in ($results.save_path | Select-Object -Unique)) {
if ($id = $return.MediaContainer.Directory.Location | Where-Object { ($_.path + "\") -eq $path }) {
Invoke-restmethod -uri "http://$($Plex):32400/library/sections/$($id.id)/refresh??force=1&X-Plex-Token=$($token)"
}
else {
Write-warning ("Could not find folder {0} to refresh in Plex" -f ($($id.path) ? $($id.path) : "Null"))
}
}
}
}
default {
Write-Warning ("Still leeching: {0} - {1}%" -f $($result.name), [math]::Round($($($result.progress) * 100), 2))
}
}
if ((Get-qBtorrentInfo -WebSession $session -Computer $Computer -Port $port).Count -eq 0) {
Write-output "You're all leeched up... Ending"
return
}
}
Start-Sleep ($minutes * 60)
}