-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSABNZBD.PSM1
168 lines (144 loc) · 5.39 KB
/
SABNZBD.PSM1
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
Function Get-SABRawQueue () {
<#
.Description
The Get-SABRawQueue pulls the raw queue data from the SABNZBD rest interface.
You can call this function directly but it is used by other functions to pull data.
#>
Param(
[parameter(Mandatory = $true)]
[string]$hostname,
[parameter(Mandatory = $true)]
[string]$apikey
)
$curError = $null
$URI = "http://$hostName/api?mode=queue&output=json&apikey=$apiKey"
(Invoke-RestMethod -URI $URI -ErrorVariable $curError -ErrorAction Stop).queue
} # End Function Get-SABRawQueue
function Get-SABQueueList () {
<#
.Description
The Get-SABQueueList returns a formatted list of slots currently in the queue. Use the ExcludeCompleted switch to only show active slots.
#>
Param(
[parameter(Mandatory = $true)]
[string]$hostname,
[parameter(Mandatory = $true)]
[string]$apikey,
[switch]$ExcludeCompleted
)
$Queue = @()
$rawQueue = Get-SABRawQueue $hostname $apikey
if ($rawQueue.Error) {Write-Output "The following error occured : $($rawQueue.Error)"; return} # End If
Foreach ($job in ($rawQueue).slots) {
$properties = [ordered]@{
"Size (MB)" = [math]::Round($job.mb);
"Remaining (MB)" = [math]::Round($job.mbleft);
"Completed (%)" = [math]::Round(100 - (($job.mbleft / $job.mb) * 100));
"Time Left" = $job.timeleft;
"FileName" = $job.filename;
}
$Queue += New-Object -TypeName psobject -Property $properties
}
if ($ExcludeCompleted) {
$Queue | Where-Object -Property 'Completed (%)' -NE -Value 100
}
else {
$Queue
}
} # End Function Get-SABQueueList
function Get-SABQueueTotals () {
<#
.Description
The Get-SABQueueTotals returns statistics for the entire queue.
#>
Param(
[parameter(Mandatory = $true)]
[string]$hostname,
[parameter(Mandatory = $true)]
[string]$apikey
)
$rawQueue = Get-SABRawQueue $hostname $apikey
if ($rawQueue.Error) {Write-Output "The following error occured : $($rawQueue.Error)"; return} # End If
$properties = [ordered]@{
"No. Downloads" = [math]::Round(($rawQueue).noofslots_total);
"Speed (KB/s)" = [math]::Round(($rawQueue).kbpersec);
"Total (MB)" = [math]::Round(($rawQueue).mb);
"Total Remaining (MB)" = [math]::Round(($rawQueue).mbleft);
"Completed (%)" = [math]::Round(100 - ((($rawQueue).mbleft / ($rawQueue).mb) * 100))
} # End Properties
$totals = New-Object -TypeName psobject -Property $properties
$totals
} # End Function Get-SABQueueTotals
function Set-SABQueueStatus () {
<#
.Description
The Get-SABQueueStatus function is used to control the status of the queue as a whole.
Use this function to pause or resume the queue. If Pausing, you can set a time (in minutes) that the queue should be paused for.
#>
Param(
[parameter(Mandatory = $true)]
[string]$hostname,
[parameter(Mandatory = $true)]
[string]$apikey,
[parameter(Mandatory = $true)]
[ValidateSet("Pause", "Resume")]
[string]$status,
[int]$minutes
)
switch ($status) {
"Pause" {
If ($minutes) {
$URI = "http://$hostName/api?mode=config&name=set_pause&value=$minutes&apikey=$apiKey"
Invoke-RestMethod -URI $URI -ErrorVariable $curError -ErrorAction Stop
}
else {
$URI = "http://$hostName/api?mode=pause&apikey=$apiKey"
Invoke-RestMethod -URI $URI -ErrorVariable $curError -ErrorAction Stop
} # End If
}
"Resume" {
$URI = "http://$hostName/api?mode=resume&apikey=$apiKey"
Invoke-RestMethod -URI $URI -ErrorVariable $curError -ErrorAction Stop
}
} # End Switch
} # End Function Set-SABQueueStatus
function Set-SABSlotStatus () {
Param(
[parameter(Mandatory = $true)]
[string]$hostname,
[parameter(Mandatory = $true)]
[string]$apikey
)
# Not Implemented Yet
}
function Set-SABSpeedLimit () {
<#
.Description
The Get-SABSpeedLimit sets teh current queue speed using either a set speed (M or K) or a percentage.
To use this function you need to have set a global speed limit in the SABNZBD instance config.
#>
Param(
[parameter(Mandatory = $true)]
[string]$hostname,
[parameter(Mandatory = $true)]
[string]$apikey,
[parameter(Mandatory = $true,
ParameterSetName = "ByPercentage")]
[int]$percentage,
[parameter(Mandatory = $true,
ParameterSetName = "ByValue")]
[ValidatePattern("^[1-9][0-9]*[mkMK]$")]
[string]$value
)
switch ($psCmdlet.ParameterSetName) {
"ByPercentage" {
$URI = "http://$hostName/api?mode=config&name=speedlimit&value=$percentage&apikey=$apiKey"
Invoke-RestMethod -URI $URI -ErrorVariable $curError -ErrorAction Stop
}
"ByValue" {
$URI = "http://$hostName/api?mode=config&name=speedlimit&value=$value&apikey=$apiKey"
Invoke-RestMethod -URI $URI -ErrorVariable $curError -ErrorAction Stop
}
}
}
Export-ModuleMember -Function *