Skip to content

Commit

Permalink
Merge pull request #2 from Rouzax/Create_Separate_Fuctions
Browse files Browse the repository at this point in the history
Create separate fuctions
  • Loading branch information
Rouzax authored Feb 3, 2023
2 parents e017960 + 14a4d95 commit 3f23602
Show file tree
Hide file tree
Showing 17 changed files with 1,776 additions and 1,004 deletions.
1,169 changes: 165 additions & 1,004 deletions TorrentScript.ps1

Large diffs are not rendered by default.

54 changes: 54 additions & 0 deletions functions/CleanProcessPath.ps1
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'
}
}
}
58 changes: 58 additions & 0 deletions functions/Format-Size.ps1
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"
}
}
}
50 changes: 50 additions & 0 deletions functions/Get-Input.ps1
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
}
127 changes: 127 additions & 0 deletions functions/Import-Medusa.ps1
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"
}
}
Loading

0 comments on commit 3f23602

Please sign in to comment.