-
-
Save Taloth/e02e76bcb29942f791cc2e8595192846 to your computer and use it in GitHub Desktop.
DownloadedEpisodesScan example PowerShell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param($FilePath, $InfoHash, $Label) | |
############ | |
# Settings | |
# Url to Sonarr api, should include UrlBase is specified. | |
$SonarrApiRootUrl = "http://testsonarr:8989/api" | |
# ApiKey as specified in System->General->Api Key | |
$SonarrApiKey = "4e6e7bff2e2431907b15e26e6b7ee792" | |
# Whether to wait for completion of the command | |
$WaitForCompletion = $true | |
# Whether to log the Responses | |
$Verbose = $true | |
############ | |
$ErrorActionPreference = 'Stop' | |
# TODO: Filter on Label? | |
$url = "$SonarrApiRootUrl/command"; | |
$command = @{ | |
'name' = "DownloadedEpisodesScan" | |
'path' = $FilePath | |
'importMode' = 'Move' | |
'downloadClientId' = $InfoHash | |
} | ConvertTo-Json | |
if ($SonarrApiRootUrl -match 'testsonarr' -or $SonarrApiKey -eq '4e6e7bff2e2431907b15e26e6b7ee792') { | |
Write-Error "Change the Settings at the top of the script..." | |
} | |
Write-Host "Sending Request to Sonarr: Url=$url Content=$command" | |
$response = Invoke-RestMethod -Uri $url -Method Post -Body $command -Headers @{"X-Api-Key"=$SonarrApiKey} -MaximumRedirection 0 -Verbose:$Verbose | |
if (-not $response) { | |
Write-Error "Failed to submit command" | |
} | |
$response | Format-List | Out-String | Write-Verbose -Verbose:$Verbose | |
# We need to the command ID to be able to query the status | |
$commandId = $response.id | |
# A bit of advanced logic to see if the command has already been completed, we're only looping if $WaitForCompletion is set to $true | |
while ($response -and $response.status) | |
{ | |
Write-Host "Checking status..." | |
$response = Invoke-RestMethod -Uri "$url/$commandId" -Method Get -Headers @{"X-Api-Key"=$SonarrApiKey} -MaximumRedirection 0 -Verbose:$Verbose | |
if (-not $response) { | |
Write-Error "Failed to get status from command" | |
break | |
} | |
$response | Format-List | Out-String | Write-Verbose -Verbose:$Verbose | |
if (@('queued', 'started', 'completed') -notcontains $response.status) { | |
Write-Error "Command $($response.status): $($response.exception)" | |
break | |
} | |
if ($response.status -eq 'completed') { | |
Write-Host "Command finished" | |
break | |
} | |
if (-not $WaitForCompletion) { | |
break | |
} | |
Start-Sleep -Milliseconds 1000 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment