Skip to content

Instantly share code, notes, and snippets.

@hl2guide
Created April 4, 2020 12:41
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save hl2guide/1456c50761e389aed5986c8ec3f47550 to your computer and use it in GitHub Desktop.
Gets the full queue of SABnzbd as a snapshot text file using the official API.
# Name: SABqueueSnapShot for PowerShell
# Decription: Gets the full queue of SABnzbd as a snapshot text file using the official API
# Version: 1.0B
# Required Edits: 1-4
# To troubleshoot issues ensure that all firewalls are configured properly :D
# Start Edits:
# Edit 1) Edit this to your API key in SABnzbd's General > Security settings section
$apiKey = 'YOURAPIKEYGOESHERE'
# Edit 2) Edit this to the host IP of the device running SABnzbd
$hostSAB = '127.0.0.1'
# Edit 3) Edit this to 9090 (secured connection or 8080 for insecured) or your custom set
$portSAB = 9090
# Edit 4) Edit this to the folder you want save text files to (must exist and must end in a backslash, \)
$outputPath = 'C:\Users\Dean\Documents\MEGAsync\SAB Queue Snapshots\'
$outputFilename = 'queue_snapshot_' + [DateTime]::Now.ToString("yyyy-MM-dd@HH.mm.ss") + '.txt'
# Example filename output: ...\queue_snapshot_2017-12-08@19.02.42.txt
# Stop Edits.
add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@
$AllProtocols = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12'
[System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
$outputTextFile = $outputPath + $outputFilename
$requestURL = "https://$hostSAB`:$portSAB/sabnzbd/api?output=xml&apikey=$apiKey&mode=queue"
#$requestURL
#exit
$resultQueue = ''
# Makes the API request
try {
Write-Host 'Trying to connect to SABnzbd API...' -ForegroundColor Cyan
$resultQueue = Invoke-RestMethod -Uri $requestURL -Method Get
}
catch {
Write-Host 'Failed to connect to SAB, please check it is running.' `
-ForegroundColor Red
Write-Host "StatusCode:" $_.Exception.Response.StatusCode.value__
Write-Host "StatusDescription:" $_.Exception.Response.StatusDescription
Write-Host "Message:" $_.Exception.Message
exit
}
# Get error code
$errorCode = $resultQueue.result.error
# Check error codes
if ($errorCode -eq 'API Key Required') {
Write-Host 'Please check your API key is entered.' `
-ForegroundColor Yellow
exit
}
if ($errorCode -eq 'API Key Incorrect') {
Write-Host 'Please check your API key is correct.' `
-ForegroundColor Yellow
exit
}
# No errors happened
# Loops through the XML result
Write-Host 'Getting files list from request...' -ForegroundColor Magenta
$output = ''
foreach ($queueItem in $resultQueue.queue.slots.ChildNodes) {
$output += $queueItem.filename + "`r`n"
}
if (Test-Path $outputPath) {
# Formats output and then saves to text file
$output = $output.Trim()
$output -creplace '(?m)^\s*\r?\n', '' | Out-File -FilePath $outputTextFile `
-Encoding utf8 -Force
Write-Host 'Saved queue to text file snapshot:'$outputTextFile `
-ForegroundColor Green
exit
}
else {
Write-Host 'Failed to write output list.' -ForegroundColor Red
Write-Host 'Could not find the set output path:'$outputPath `
-ForegroundColor Red
exit
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment