Skip to content

Commit

Permalink
Fixed issue #3
Browse files Browse the repository at this point in the history
  • Loading branch information
Rouzax committed May 31, 2023
1 parent d52dfc7 commit 8edce73
Show file tree
Hide file tree
Showing 15 changed files with 82 additions and 138 deletions.
66 changes: 30 additions & 36 deletions TorrentScript.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ param(
try {
$configPath = Join-Path $PSScriptRoot 'config.json'
$Config = Get-Content $configPath -ErrorAction Stop | ConvertFrom-Json -ErrorAction Stop
}
catch {
} catch {
Write-Host 'Exception:' $_.Exception.Message -ForegroundColor Red
Write-Host 'Invalid config.json file' -ForegroundColor Red
exit 1
Expand Down Expand Up @@ -88,14 +87,13 @@ $WantedLanguages = $Config.WantedLanguages
$SubtitleNamesToRemove = $Config.SubtitleNamesToRemove

# Get function definition files.
$Functions = @( Get-ChildItem -Path $PSScriptRoot\functions\*.ps1 -ErrorAction SilentlyContinue )
$Functions = @( Get-ChildItem -Path $PSScriptRoot\functions\*.ps1 -ErrorAction SilentlyContinue )
# Dot source the files
ForEach ($import in @($Functions)) {
Try {
# Lightweight alternative to dotsourcing a function script
. ([ScriptBlock]::Create([System.Io.File]::ReadAllText($import)))
}
Catch {
} Catch {
Write-Error -Message "Failed to import function $($import.fullname): $_"
}
}
Expand All @@ -108,10 +106,11 @@ Test-Variable-Path -Path $SubtitleEditPath -Name 'SubtitleEditPath'
Test-Variable-Path -Path $SubliminalPath -Name 'SubliminalPath'
Test-Variable-Path -Path $MailSendPath -Name 'MailSendPath'


# Get input if no parameters defined
# Build the download Location, this is the Download Root Path added with the Download name
if ( ($Null -eq $DownloadPath) -or ($DownloadPath -eq '') ) {
if ($PSBoundParameters.ContainsKey('DownloadPath')) {
Write-Host "Download Path is defined"
} else {
$DownloadPath = Get-Input -Message 'Download Name' -Required
$DownloadPath = Join-Path -Path $DownloadRootPath -ChildPath $DownloadPath
}
Expand All @@ -120,9 +119,18 @@ if ( ($Null -eq $DownloadPath) -or ($DownloadPath -eq '') ) {
$DownloadName = Split-Path -Path $DownloadPath -Leaf

# Download Label
if ( ($Null -eq $DownloadLabel) -or ($DownloadLabel -eq '') ) {
$DownloadLabel = Get-Input -Message 'Download Label'
if ($PSBoundParameters.ContainsKey('DownloadLabel')) {
Write-Host "Download Label is defined"
# Handle empty Torrent Label
} else {
$DownloadLabel = Get-Input -Message 'Download Label'
}

# Handle Empty Download Label
if ($DownloadLabel -eq '') {
$DownloadLabel = 'NoLabel'
}

# Torrent Hash (only needed for Radarr)
if ( ($Null -eq $TorrentHash) -or ($TorrentHash -eq '') ) {
$TorrentHash = Get-Input -Message 'Torrent Hash'
Expand All @@ -137,11 +145,6 @@ if ($DownloadLabel -eq 'NoProcess') {
Exit
}

# Handle empty Torrent Label
if ($DownloadLabel -eq '') {
$DownloadLabel = 'NoLabel'
}

# Create Log file
# Log file of current processing file (will be used to send out the mail)
$LogFilePath = Join-Path -Path $LogArchivePath -ChildPath "$LogFileDateFormat-$DownloadName.html"
Expand All @@ -155,10 +158,10 @@ Write-HTMLLog -Column1 'Name:' -Column2 $DownloadName
Write-HTMLLog -Column1 'Hash:' -Column2 $TorrentHash

# Test File Paths
If (!(Test-Path -LiteralPath $ProcessPath)) {
If (!(Test-Path -LiteralPath $ProcessPath)) {
New-Item -ItemType Directory -Force -Path $ProcessPath | Out-Null
}
If (!(Test-Path -LiteralPath $LogArchivePath)) {
If (!(Test-Path -LiteralPath $LogArchivePath)) {
New-Item -ItemType Directory -Force -Path $LogArchivePath | Out-Null
}

Expand All @@ -169,7 +172,7 @@ $ScriptMutex = New-Mutex -MutexName 'DownloadScript'
$StopWatch = [system.diagnostics.stopwatch]::startNew()

# Check paths from Parameters
If (!(Test-Path -LiteralPath $DownloadPath)) {
If (!(Test-Path -LiteralPath $DownloadPath)) {
Write-Host "$DownloadPath - Not valid location"
Write-HTMLLog -Column1 'Path:' -Column2 "$DownloadPath - Not valid location" -ColorBg 'Error'
Write-HTMLLog -Column1 'Result:' -Column2 'Failed' -ColorBg 'Error'
Expand All @@ -184,8 +187,7 @@ $Folder = (Get-Item -LiteralPath $DownloadPath) -is [System.IO.DirectoryInfo]
if ($Folder) {
$ProcessPathFull = Join-Path -Path $ProcessPath -ChildPath $DownloadLabel | Join-Path -ChildPath $DownloadName
$RarFilePaths = (Get-ChildItem -LiteralPath $DownloadPath -Recurse -Filter '*.rar').FullName
}
elseif ($SingleFile) {
} elseif ($SingleFile) {
$ProcessPathFull = Join-Path -Path $ProcessPath -ChildPath $DownloadLabel | Join-Path -ChildPath $DownloadName.Substring(0, $DownloadName.LastIndexOf('.'))
$DownloadRootPath = Split-Path -Path $DownloadPath
if ([IO.Path]::GetExtension($DownloadPath) -eq '.rar') {
Expand All @@ -197,13 +199,12 @@ elseif ($SingleFile) {
$RarCount = $RarFilePaths.Count
if ($RarCount -gt 0) {
$RarFile = $true
}
else {
} else {
$RarFile = $false
}

# Check is destination folder exists otherwise create it
If (!(Test-Path -LiteralPath $ProcessPathFull)) {
If (!(Test-Path -LiteralPath $ProcessPathFull)) {
New-Item -ItemType Directory -Force -Path $ProcessPathFull | Out-Null
}

Expand All @@ -219,12 +220,10 @@ if ($RarFile) {
$UnRarStopWatch.Stop()
Write-HTMLLog -Column1 'Size:' -Column2 (Format-Size -SizeInBytes $TotalSize)
Write-HTMLLog -Column1 'Throughput:' -Column2 "$(Format-Size -SizeInBytes ($TotalSize/$UnRarStopWatch.Elapsed.TotalSeconds))/s"
}
elseif (-not $RarFile -and $SingleFile) {
} elseif (-not $RarFile -and $SingleFile) {
Write-HTMLLog -Column1 '*** Single File ***' -Header
Start-RoboCopy -Source $DownloadRootPath -Destination $ProcessPathFull -File $DownloadName
}
elseif (-not $RarFile -and $Folder) {
} elseif (-not $RarFile -and $Folder) {
Write-HTMLLog -Column1 '*** Folder ***' -Header
Start-RoboCopy -Source $DownloadPath -Destination $ProcessPathFull -File '*.*'
}
Expand All @@ -236,8 +235,7 @@ if ($DownloadLabel -eq $TVLabel) {
$MKVCount = $MKVFiles.Count
if ($MKVCount -gt 0) {
$MKVFile = $true
}
else {
} else {
$MKVFile = $false
}
if ($MKVFile) {
Expand All @@ -261,8 +259,7 @@ if ($DownloadLabel -eq $TVLabel) {
Write-HTMLLog -Column1 ' ' -Column2 $srt.name
}
}
}
else {
} else {
Write-HTMLLog -Column1 '*** Files ***' -Header
$Files = Get-ChildItem -LiteralPath $ProcessPathFull -Recurse -Filter '*.*'
foreach ($File in $Files) {
Expand All @@ -274,14 +271,12 @@ if ($DownloadLabel -eq $TVLabel) {
Import-Medusa -Source $ProcessPathFull
CleanProcessPath -Path $ProcessPathFull -NoCleanUp $NoCleanUp
Stop-Script -ExitReason "$DownloadLabel - $DownloadName"
}
elseif ($DownloadLabel -eq $MovieLabel) {
} elseif ($DownloadLabel -eq $MovieLabel) {
$MKVFiles = Get-ChildItem -LiteralPath $ProcessPathFull -Recurse -Filter '*.mkv'
$MKVCount = $MKVFiles.Count
if ($MKVCount -gt 0) {
$MKVFile = $true
}
else {
} else {
$MKVFile = $false
}
if ($MKVFile) {
Expand All @@ -303,8 +298,7 @@ elseif ($DownloadLabel -eq $MovieLabel) {
foreach ($Srt in $SrtFiles) {
Write-HTMLLog -Column1 ' ' -Column2 $srt.name
}
}
else {
} else {
Write-HTMLLog -Column1 '*** Files ***' -Header
$Files = Get-ChildItem -LiteralPath $ProcessPathFull -Recurse -Filter '*.*'
foreach ($File in $Files) {
Expand Down
11 changes: 4 additions & 7 deletions functions/CleanProcessPath.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ function CleanProcessPath {
Try {
. $PSScriptRoot\$commandName.ps1
Write-Host "$commandName Function loaded." -ForegroundColor Green
}
Catch {
} Catch {
Write-Error -Message "Failed to import $commandName function: $_"
exit 1
}
Expand All @@ -30,14 +29,12 @@ function CleanProcessPath {

if ($NoCleanUp) {
Write-HTMLLog -Column1 'Cleanup' -Column2 'NoCleanUp switch was given at command line, leaving files'
}
else {
} else {
try {
If (Test-Path -LiteralPath $ProcessPathFull) {
If (Test-Path -LiteralPath $ProcessPathFull) {
Remove-Item -Force -Recurse -LiteralPath $ProcessPathFull
}
}
catch {
} catch {
Write-HTMLLog -Column1 'Exception:' -Column2 $_.Exception.Message -ColorBg 'Error'
Write-HTMLLog -Column1 'Result:' -Column2 'Failed' -ColorBg 'Error'
}
Expand Down
3 changes: 1 addition & 2 deletions functions/Get-Input.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ Function Get-Input {
$Variable = Read-Host -Prompt "$Message"
$Variable = $Variable.Trim()
}
}
else {
} else {
$Variable = Read-Host -Prompt "$Message"
$Variable = $Variable.Trim()
}
Expand Down
18 changes: 6 additions & 12 deletions functions/Import-Medusa.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ function Import-Medusa {
Try {
. $PSScriptRoot\$commandName.ps1
Write-Host "$commandName Function loaded." -ForegroundColor Green
}
Catch {
} Catch {
Write-Error -Message "Failed to import $commandName function: $_"
exit 1
}
Expand All @@ -58,8 +57,7 @@ function Import-Medusa {
Write-HTMLLog -Column1 '*** Medusa Import ***' -Header
try {
$response = Invoke-RestMethod -Uri "http://$MedusaHost`:$MedusaPort/api/v2/postprocess" -Method Post -Body $Body -Headers $headers
}
catch {
} 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"
Expand All @@ -70,8 +68,7 @@ function Import-Medusa {
do {
try {
$status = Invoke-RestMethod -Uri "http://$MedusaHost`:$MedusaPort/api/v2/postprocess/$($response.queueItem.identifier)" -Method Get -Headers $headers
}
catch {
} 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"
Expand All @@ -92,19 +89,16 @@ function Import-Medusa {
}
Write-HTMLLog -Column1 'Result:' -Column2 'Failed' -ColorBg 'Error'
Stop-Script -ExitReason "Medusa Error: $DownloadLabel - $DownloadName"
}
else {
} else {
Write-HTMLLog -Column1 'Result:' -Column2 'Successful' -ColorBg 'Success'
}
}
else {
} 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 {
} 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"
Expand Down
15 changes: 5 additions & 10 deletions functions/Import-Radarr.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ function Import-Radarr {
Try {
. $PSScriptRoot\$commandName.ps1
Write-Host "$commandName Function loaded." -ForegroundColor Green
}
Catch {
} Catch {
Write-Error -Message "Failed to import $commandName function: $_"
exit 1
}
Expand All @@ -53,8 +52,7 @@ function Import-Radarr {
Write-HTMLLog -Column1 '*** Radarr Import ***' -Header
try {
$MoviesScanJob = Invoke-RestMethod -Uri "http://$RadarrHost`:$RadarrPort/api/v3/command" -Method Post -Body $Body -Headers $headers
}
catch {
} catch {
Write-HTMLLog -Column1 'Exception:' -Column2 $_.Exception.Message -ColorBg 'Error'
Write-HTMLLog -Column1 'Result:' -Column2 'Failed' -ColorBg 'Error'
Stop-Script -ExitReason "Radarr Error: $DownloadLabel - $DownloadName"
Expand All @@ -65,8 +63,7 @@ function Import-Radarr {
do {
try {
$MoviesScanResult = Invoke-RestMethod -Uri "http://$RadarrHost`:$RadarrPort/api/v3/command/$($MoviesScanJob.id)" -Method Get -Headers $headers
}
catch {
} catch {
Write-HTMLLog -Column1 'Exception:' -Column2 $_.Exception.Message -ColorBg 'Error'
Write-HTMLLog -Column1 'Result:' -Column2 'Failed' -ColorBg 'Error'
Stop-Script -ExitReason "Radarr Error: $DownloadLabel - $DownloadName"
Expand All @@ -77,8 +74,7 @@ function Import-Radarr {
if ($MoviesScanResult.status -eq 'completed') {
if ($MoviesScanResult.result -eq 'successful') {
Write-HTMLLog -Column1 'Result:' -Column2 'Successful' -ColorBg 'Success'
}
else {
} else {
Write-HTMLLog -Column1 'Result:' -Column2 'Failed' -ColorBg 'Error'
Stop-Script -ExitReason "Radarr Error: $DownloadLabel - $DownloadName"
}
Expand All @@ -95,8 +91,7 @@ function Import-Radarr {
Write-HTMLLog -Column1 'Result:' -Column2 'Failed' -ColorBg 'Error'
Stop-Script -ExitReason "Radarr Error: $DownloadLabel - $DownloadName"
}
}
else {
} else {
Write-HTMLLog -Column1 'Radarr:' -Column2 $MoviesScanJob.status -ColorBg 'Error'
Write-HTMLLog -Column1 'Result:' -Column2 'Failed' -ColorBg 'Error'
Stop-Script -ExitReason "Radarr Error: $DownloadLabel - $DownloadName"
Expand Down
6 changes: 2 additions & 4 deletions functions/New-Mutex.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,14 @@ function New-Mutex {
[void][System.Reflection.Assembly]::LoadWithPartialName('System.Threading')
try {
$Mutex = [System.Threading.Mutex]::OpenExisting($MutexName)
}
catch {
} catch {
$Mutex = New-Object System.Threading.Mutex($true, $MutexName, [ref]$MutexWasCreated)
}
try {
if (!$MutexWasCreated) {
$Mutex.WaitOne() | Out-Null
}
}
catch {
} catch {
}
Write-Host "Lock [$MutexName] acquired. Executing..." -ForegroundColor DarkGray
Write-Output ([PSCustomObject]@{ Name = $MutexName; Mutex = $Mutex })
Expand Down
3 changes: 1 addition & 2 deletions functions/Remove-Mutex.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ function Remove-Mutex {
Write-Host "Releasing lock [$($MutexObject.Name)]..." -ForegroundColor DarkGray
try {
[void]$MutexObject.Mutex.ReleaseMutex()
}
catch {
} catch {
}
} # Remove-Mutex
Loading

0 comments on commit 8edce73

Please sign in to comment.