-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from Rouzax/Create_Separate_Fuctions
Create separate fuctions
- Loading branch information
Showing
17 changed files
with
1,776 additions
and
1,004 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
function CleanProcessPath | ||
{ | ||
param ( | ||
[Parameter( | ||
Mandatory = $true | ||
)] | ||
[string] $Path, | ||
|
||
[Parameter( | ||
Mandatory = $false | ||
)] | ||
[bool] $NoCleanUp | ||
) | ||
|
||
# Make sure needed functions are available otherwise try to load them. | ||
$commands = 'Write-HTMLLog' | ||
foreach ($commandName in $commands) | ||
{ | ||
if (!($command = Get-Command $commandName -ErrorAction SilentlyContinue)) | ||
{ | ||
Try | ||
{ | ||
. $PSScriptRoot\$commandName.ps1 | ||
Write-Host "$commandName Function loaded." -ForegroundColor Green | ||
} | ||
Catch | ||
{ | ||
Write-Error -Message "Failed to import $commandName function: $_" | ||
exit 1 | ||
} | ||
} | ||
} | ||
# Start | ||
|
||
if ($NoCleanUp) | ||
{ | ||
Write-HTMLLog -Column1 'Cleanup' -Column2 'NoCleanUp switch was given at command line, leaving files' | ||
} | ||
else | ||
{ | ||
try | ||
{ | ||
If (Test-Path -LiteralPath $ProcessPathFull) | ||
{ | ||
Remove-Item -Force -Recurse -LiteralPath $ProcessPathFull | ||
} | ||
} | ||
catch | ||
{ | ||
Write-HTMLLog -Column1 'Exception:' -Column2 $_.Exception.Message -ColorBg 'Error' | ||
Write-HTMLLog -Column1 'Result:' -Column2 'Failed' -ColorBg 'Error' | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
function Format-Size() | ||
{ | ||
<# | ||
.SYNOPSIS | ||
Takes bytes and converts it to KB.MB,GB,TB,PB | ||
.DESCRIPTION | ||
Takes bytes and converts it to KB.MB,GB,TB,PB | ||
.PARAMETER SizeInBytes | ||
Input bytes | ||
.EXAMPLE | ||
Format-Size -SizeInBytes 864132 | ||
843,88 KB | ||
Format-Size -SizeInBytes 8641320 | ||
8,24 MB | ||
.NOTES | ||
General notes | ||
#> | ||
[CmdletBinding()] | ||
param( | ||
[Parameter( | ||
Mandatory = $true, | ||
ValueFromPipeline = $true | ||
)] | ||
[double] $SizeInBytes | ||
) | ||
switch ([math]::Max($SizeInBytes, 0)) | ||
{ | ||
{ $_ -ge 1PB } | ||
{ | ||
'{0:N2} PB' -f ($SizeInBytes / 1PB); break | ||
} | ||
{ $_ -ge 1TB } | ||
{ | ||
'{0:N2} TB' -f ($SizeInBytes / 1TB); break | ||
} | ||
{ $_ -ge 1GB } | ||
{ | ||
'{0:N2} GB' -f ($SizeInBytes / 1GB); break | ||
} | ||
{ $_ -ge 1MB } | ||
{ | ||
'{0:N2} MB' -f ($SizeInBytes / 1MB); break | ||
} | ||
{ $_ -ge 1KB } | ||
{ | ||
'{0:N2} KB' -f ($SizeInBytes / 1KB); break | ||
} | ||
default | ||
{ | ||
"$SizeInBytes Bytes" | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
Function Get-Input | ||
{ | ||
<# | ||
.SYNOPSIS | ||
Get input from user | ||
.DESCRIPTION | ||
Stop the script and get input from user and answer will be returned | ||
.PARAMETER Message | ||
The question to show to the user | ||
.PARAMETER Required | ||
If provided will force an answer to be given | ||
.EXAMPLE | ||
$UserName = Get-Input -Message "What is your name" -Required | ||
$UserAge = Get-Input -Message "What is your age" | ||
.NOTES | ||
General notes | ||
#> | ||
[CmdletBinding()] | ||
param( | ||
[Parameter( | ||
Mandatory = $true | ||
)] | ||
[string] $Message, | ||
|
||
[Parameter( | ||
Mandatory = $false | ||
)] | ||
[switch] $Required | ||
|
||
) | ||
if ($Required) | ||
{ | ||
While ( ($Null -eq $Variable) -or ($Variable -eq '') ) | ||
{ | ||
$Variable = Read-Host -Prompt "$Message" | ||
$Variable = $Variable.Trim() | ||
} | ||
} | ||
else | ||
{ | ||
$Variable = Read-Host -Prompt "$Message" | ||
$Variable = $Variable.Trim() | ||
} | ||
Return $Variable | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
|
||
function Import-Medusa | ||
{ | ||
<# | ||
.SYNOPSIS | ||
Start import in Medusa | ||
.DESCRIPTION | ||
Start import to Medusa with timeout and error handeling | ||
.PARAMETER Source | ||
Path to episode to import | ||
.EXAMPLE | ||
Import-Medusa -Source 'C:\Temp\Episode' | ||
.NOTES | ||
General notes | ||
#> | ||
param ( | ||
[Parameter(Mandatory = $true)] | ||
$Source | ||
) | ||
|
||
# Make sure needed functions are available otherwise try to load them. | ||
$commands = 'Write-HTMLLog', 'Stop-Script' | ||
foreach ($commandName in $commands) | ||
{ | ||
if (!($command = Get-Command $commandName -ErrorAction SilentlyContinue)) | ||
{ | ||
Try | ||
{ | ||
. $PSScriptRoot\$commandName.ps1 | ||
Write-Host "$commandName Function loaded." -ForegroundColor Green | ||
} | ||
Catch | ||
{ | ||
Write-Error -Message "Failed to import $commandName function: $_" | ||
exit 1 | ||
} | ||
} | ||
} | ||
# Start | ||
|
||
$body = @{ | ||
'proc_dir' = $Source | ||
'resource' = '' | ||
'process_method' = 'move' | ||
'force' = $true | ||
'is_priority' = $false | ||
'delete_on' = $false | ||
'failed' = $false | ||
'proc_type' = 'manual' | ||
'ignore_subs' = $false | ||
} | ConvertTo-Json | ||
|
||
$headers = @{ | ||
'X-Api-Key' = $MedusaApiKey | ||
} | ||
Write-HTMLLog -Column1 '*** Medusa Import ***' -Header | ||
try | ||
{ | ||
$response = Invoke-RestMethod -Uri "http://$MedusaHost`:$MedusaPort/api/v2/postprocess" -Method Post -Body $Body -Headers $headers | ||
} | ||
catch | ||
{ | ||
Write-HTMLLog -Column1 'Exception:' -Column2 $_.Exception.Message -ColorBg 'Error' | ||
Write-HTMLLog -Column1 'Result:' -Column2 'Failed' -ColorBg 'Error' | ||
Stop-Script -ExitReason "Medusa Error: $DownloadLabel - $DownloadName" | ||
} | ||
if ($response.status -eq 'success') | ||
{ | ||
$timeout = New-TimeSpan -Minutes $MedusaTimeOutMinutes | ||
$endTime = (Get-Date).Add($timeout) | ||
do | ||
{ | ||
try | ||
{ | ||
$status = Invoke-RestMethod -Uri "http://$MedusaHost`:$MedusaPort/api/v2/postprocess/$($response.queueItem.identifier)" -Method Get -Headers $headers | ||
} | ||
catch | ||
{ | ||
Write-HTMLLog -Column1 'Exception:' -Column2 $_.Exception.Message -ColorBg 'Error' | ||
Write-HTMLLog -Column1 'Result:' -Column2 'Failed' -ColorBg 'Error' | ||
Stop-Script -ExitReason "Medusa Error: $DownloadLabel - $DownloadName" | ||
} | ||
Start-Sleep 1 | ||
} | ||
until ($status.success -or ((Get-Date) -gt $endTime)) | ||
if ($status.success) | ||
{ | ||
$ValuesToFind = 'Processing failed', 'aborting post-processing', 'Unable to figure out what folder to process' | ||
$MatchPattern = ($ValuesToFind | ForEach-Object { [regex]::Escape($_) }) -join '|' | ||
if ($status.output -match $MatchPattern) | ||
{ | ||
$ValuesToFind = 'Retrieving episode object for', 'Current quality', 'New quality', 'Old size', 'New size', 'Processing failed', 'aborting post-processing', 'Unable to figure out what folder to process' | ||
$MatchPattern = ($ValuesToFind | ForEach-Object { [regex]::Escape($_) }) -join '|' | ||
foreach ($line in $status.output ) | ||
{ | ||
if ($line -match $MatchPattern) | ||
{ | ||
Write-HTMLLog -Column1 'Medusa:' -Column2 $line -ColorBg 'Error' | ||
} | ||
} | ||
Write-HTMLLog -Column1 'Result:' -Column2 'Failed' -ColorBg 'Error' | ||
Stop-Script -ExitReason "Medusa Error: $DownloadLabel - $DownloadName" | ||
} | ||
else | ||
{ | ||
Write-HTMLLog -Column1 'Result:' -Column2 'Successful' -ColorBg 'Success' | ||
} | ||
} | ||
else | ||
{ | ||
Write-HTMLLog -Column1 'Medusa:' -Column2 $status.success -ColorBg 'Error' | ||
Write-HTMLLog -Column1 'Medusa:' -Column2 "Import Timeout: ($MedusaTimeOutMinutes) minutes" -ColorBg 'Error' | ||
Write-HTMLLog -Column1 'Result:' -Column2 'Failed' -ColorBg 'Error' | ||
Stop-Script -ExitReason "Medusa Error: $DownloadLabel - $DownloadName" | ||
} | ||
} | ||
else | ||
{ | ||
Write-HTMLLog -Column1 'Medusa:' -Column2 $response.status -ColorBg 'Error' | ||
Write-HTMLLog -Column1 'Result:' -Column2 'Failed' -ColorBg 'Error' | ||
Stop-Script -ExitReason "Medusa Error: $DownloadLabel - $DownloadName" | ||
} | ||
} |
Oops, something went wrong.