-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSABnzbdStatus.ps1
186 lines (167 loc) · 6.23 KB
/
SABnzbdStatus.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
Clear-Host
# Enter the path to the config file for Tautulli and Discord
[string]$strPathToConfig = "$PSScriptRoot\config\config.json"
# Script name MUST match what is in config.json under "ScriptSettings"
[string]$strScriptName = 'SABnzbdStatus'
# Log file path
[string]$strLogFilePath = "$PSScriptRoot\config\log\SABLog.txt"
<############################################################
Do NOT edit lines below unless you know what you are doing!
############################################################>
# Define the functions to be used
function Push-ObjectToDiscord {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$strDiscordWebhook,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[object]$objPayload
)
try {
$null = Invoke-RestMethod -Method Post -Uri $strDiscordWebhook -Body $objPayload -ContentType 'Application/JSON'
Start-Sleep -Seconds 1
}
catch {
Write-Host "Unable to send to Discord. $($_)" -ForegroundColor Red
Write-Host $objPayload
}
}
# Parse the config file and assign variables
[object]$objConfig = Get-Content -Path $strPathToConfig -Raw | ConvertFrom-Json
[string]$strDiscordWebhook = $objConfig.ScriptSettings.$strScriptName.Webhook
[string]$strSABnzbdURL = $objConfig.SABnzbd.URL
[string]$strSABnzbdAPIKey = $objConfig.SABnzbd.APIKey
[object]$objSABnzbdQueue = (Invoke-RestMethod -Method Get -Uri "$strSABnzbdURL/api?apikey=$strSABnzbdAPIKey&output=json&mode=queue").queue
[string]$strSummary = "Downloading **$(($objSABnzbdQueue.slots).Count)** item(s) at **$($objSABnzbdQueue.speed)**Bs/second. Time remaining: **$($objSABnzbdQueue.timeleft)**"
# Ensure the log file exists. If not, create it.
if (-not(Test-Path $strLogFilePath)) {
'' | Out-File -FilePath $strLogFilePath -Force
}
[System.Collections.ArrayList]$arrSlotEmbed = @()
if($objSABnzbdQueue.paused) {
'-1' | Out-File -FilePath $strLogFilePath -Force
if ($objSABnzbdQueue.pause_int -gt 0) {
[string]$strTimeRemaining = $($objSABnzbdQueue.pause_int)
}
else {
[string]$strTimeRemaining = 'Unknown'
}
[hashtable]$htbEmbedProperties = @{
color = '15197440'
title = 'Downloads Paused'
description = 'Downloads are currently paused. Either this was manually done by the server admin, or automatically if there is no free space remaining.'
fields = @{
name = 'Time Left'
value = $strTimeRemaining
inline = $false
}, @{
name = 'Free Space'
value = "$($objSABnzbdQueue.diskspace1)GB"
inline = $true
}, @{
name = 'Total Space'
value = "$($objSABnzbdQueue.diskspacetotal1)GB"
inline = $true
}
footer = @{
text = 'Updated'
}
timestamp = ((Get-Date).AddHours(5)).ToString('yyyy-MM-ddTHH:mm:ss.Mss')
}
# Add the hashtable to the Embed Array
$null = $arrSlotEmbed.Add($htbEmbedProperties)
# Send to Discord
[object]$objPayload = @{
username = 'Downloads Paused'
content = $strSummary
embeds = $arrSlotEmbed
} | ConvertTo-Json -Depth 4
Push-ObjectToDiscord -strDiscordWebhook $strDiscordWebhook -objPayload $objPayload
}
elseif (($objSABnzbdQueue.slots).Count -gt 0) {
# Update the log file with current number of downloads
($objSABnzbdQueue.slots).Count | Out-File -FilePath $strLogFilePath -Force
foreach ($slot in ($objSABnzbdQueue.slots | Select-Object -First 10)) {
$htbEmbedProperties = @{
color = '15197440'
title = 'Filename'
description = $slot.filename
fields = @{
name = 'Completed'
value = "$($slot.percentage)%"
inline = $false
}, @{
name = 'Time Left'
value = $slot.timeleft
inline = $true
}, @{
name = 'Category'
value = $slot.cat
inline = $true
}, @{
name = 'File Size'
value = $slot.size
inline = $true
}
footer = @{
text = 'Updated'
}
timestamp = ((Get-Date).AddHours(5)).ToString('yyyy-MM-ddTHH:mm:ss.Mss')
}
# Add the current hashtable to the Embed Array
$null = $arrSlotEmbed.Add($htbEmbedProperties) | Out-Null
}
# Send to Discord
[object]$objPayload = @{
username = 'First 10 Downloads'
content = $strSummary
embeds = $arrSlotEmbed
} | ConvertTo-Json -Depth 4
Push-ObjectToDiscord -strDiscordWebhook $strDiscordWebhook -objPayload $objPayload
}
# Nothing is being downloaded and SAB is not paused
else {
[hashtable]$htbEmbedProperties = @{
color = '15197440'
title = 'No downloads'
description = 'There is nothing currently in the download queue.'
fields = @{
name = 'Time Left'
value = 'NA'
inline = $false
}, @{
name = 'Free Space'
value = "$($objSABnzbdQueue.diskspace1)GB"
inline = $true
}, @{
name = 'Total Space'
value = "$($objSABnzbdQueue.diskspacetotal1)GB"
inline = $true
}
footer = @{
text = 'Updated'
}
timestamp = ((Get-Date).AddHours(5)).ToString('yyyy-MM-ddTHH:mm:ss.Mss')
}
# Add the hashtable to the Embed Array
$null = $arrSlotEmbed.Add($htbEmbedProperties)
# Get the last logged value
[int]$intLastLogValue = Get-Content $strLogFilePath
# The last logged value is the same as ($objSABnzbdQueue.slots).Count
if ($intLastLogValue -eq 0) {
Write-Host 'Nothing to update.'
}
# The last logged value is NOT the same as ($objSABnzbdQueue.slots).Count. Update the log file and send to Discord
else {
'0' | Out-File -FilePath $strLogFilePath -Force
# Send to Discord
[object]$objPayload = @{
username = 'No Downloads'
content = 'Nothing currently in the download queue.'
embeds = $arrSlotEmbed
} | ConvertTo-Json -Depth 4
Push-ObjectToDiscord -strDiscordWebhook $strDiscordWebhook -objPayload $objPayload
}
}