Skip to content

Commit

Permalink
Change code Layout
Browse files Browse the repository at this point in the history
  • Loading branch information
Rouzax committed May 25, 2023
1 parent 9f18906 commit fb8216b
Show file tree
Hide file tree
Showing 17 changed files with 271 additions and 462 deletions.
134 changes: 47 additions & 87 deletions TorrentScript.ps1
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
[CmdletBinding()]
param(
[Parameter(
mandatory = $false
)]
[string] $DownloadPath,
[string]$DownloadPath,

[Parameter(
mandatory = $false
)]
[string] $DownloadLabel,
[string]$DownloadLabel,

[Parameter(
mandatory = $false
)]
[string] $TorrentHash,
[string]$TorrentHash,

[Parameter(
Mandatory = $false
)]
[switch] $NoCleanUp
[switch]$NoCleanUp
)

# User Variables
try
{
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 @@ -91,15 +90,12 @@ $SubtitleNamesToRemove = $Config.SubtitleNamesToRemove
# Get function definition files.
$Functions = @( Get-ChildItem -Path $PSScriptRoot\functions\*.ps1 -ErrorAction SilentlyContinue )
# Dot source the files
ForEach ($import in @($Functions))
{
Try
{
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 @@ -115,8 +111,7 @@ 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 ( ($Null -eq $DownloadPath) -or ($DownloadPath -eq '') ) {
$DownloadPath = Get-Input -Message 'Download Name' -Required
$DownloadPath = Join-Path -Path $DownloadRootPath -ChildPath $DownloadPath
}
Expand All @@ -125,29 +120,25 @@ if ( ($Null -eq $DownloadPath) -or ($DownloadPath -eq '') )
$DownloadName = Split-Path -Path $DownloadPath -Leaf

# Download Label
if ( ($Null -eq $DownloadLabel) -or ($DownloadLabel -eq '') )
{
if ( ($Null -eq $DownloadLabel) -or ($DownloadLabel -eq '') ) {
$DownloadLabel = Get-Input -Message 'Download Label'
}
# Torrent Hash (only needed for Radarr)
if ( ($Null -eq $TorrentHash) -or ($TorrentHash -eq '') )
{
if ( ($Null -eq $TorrentHash) -or ($TorrentHash -eq '') ) {
$TorrentHash = Get-Input -Message 'Torrent Hash'
}

# Uppercase TorrentHash
$TorrentHash = $TorrentHash.ToUpper()

# Handle NoProcess Torrent Label
if ($DownloadLabel -eq 'NoProcess')
{
if ($DownloadLabel -eq 'NoProcess') {
Write-Host 'Do nothing'
Exit
}

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

Expand All @@ -164,12 +155,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 @@ -180,8 +169,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 @@ -193,80 +181,66 @@ $SingleFile = (Get-Item -LiteralPath $DownloadPath) -is [System.IO.FileInfo]
$Folder = (Get-Item -LiteralPath $DownloadPath) -is [System.IO.DirectoryInfo]

# Set Source and Destination paths and get Rar paths
if ($Folder)
{
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')
{
if ([IO.Path]::GetExtension($DownloadPath) -eq '.rar') {
$RarFilePaths = (Get-Item -LiteralPath $DownloadPath).FullName
}
}

# Find rar files
$RarCount = $RarFilePaths.Count
if ($RarCount -gt 0)
{
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
}

if ($RarFile)
{
if ($RarFile) {
Write-HTMLLog -Column1 '*** Unrar Download ***' -Header
Write-HTMLLog -Column1 'Starting:' -Column2 'Unpacking files'
$TotalSize = (Get-ChildItem -LiteralPath $DownloadPath -Recurse | Measure-Object -Property Length -sum).Sum
$TotalSize = (Get-ChildItem -LiteralPath $DownloadPath -Recurse | Measure-Object -Property Length -Sum).Sum
$UnRarStopWatch = [system.diagnostics.stopwatch]::startNew()
foreach ($Rar in $RarFilePaths)
{
foreach ($Rar in $RarFilePaths) {
Start-UnRar -UnRarSourcePath $Rar -UnRarTargetPath $ProcessPathFull
}
# Stop the Stopwatch
$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 '*.*'
}


# Starting Post Processing for Movies and TV Shows
if ($DownloadLabel -eq $TVLabel)
{
if ($DownloadLabel -eq $TVLabel) {
$MKVFiles = Get-ChildItem -LiteralPath $ProcessPathFull -Recurse -Filter '*.mkv'
$MKVCount = $MKVFiles.Count
if ($MKVCount -gt 0)
{
if ($MKVCount -gt 0) {
$MKVFile = $true
}
else
{
else {
$MKVFile = $false
}
if ($MKVFile)
{
if ($MKVFile) {
# Download any missing subs
Start-Subliminal -Source $ProcessPathFull

Expand All @@ -277,26 +251,21 @@ if ($DownloadLabel -eq $TVLabel)
Start-SubEdit -File '*.srt' -Source $ProcessPathFull

Write-HTMLLog -Column1 '*** MKV Files ***' -Header
foreach ($Mkv in $MKVFiles)
{
foreach ($Mkv in $MKVFiles) {
Write-HTMLLog -Column1 ' ' -Column2 $Mkv.name
}
$SrtFiles = Get-ChildItem -LiteralPath $ProcessPathFull -Recurse -Filter '*.srt'
if ($SrtFiles.Count -gt 0)
{
if ($SrtFiles.Count -gt 0) {
Write-HTMLLog -Column1 '*** Subtitle Files ***' -Header
foreach ($Srt in $SrtFiles)
{
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)
{
foreach ($File in $Files) {
Write-HTMLLog -Column1 'File:' -Column2 $File.name
}
}
Expand All @@ -306,20 +275,16 @@ if ($DownloadLabel -eq $TVLabel)
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)
{
if ($MKVCount -gt 0) {
$MKVFile = $true
}
else
{
else {
$MKVFile = $false
}
if ($MKVFile)
{
if ($MKVFile) {
# Download any missing subs
Start-Subliminal -Source $ProcessPathFull

Expand All @@ -330,23 +295,19 @@ elseif ($DownloadLabel -eq $MovieLabel)
Start-SubEdit -File '*.srt' -Source $ProcessPathFull

Write-HTMLLog -Column1 '*** MKV Files ***' -Header
foreach ($Mkv in $MKVFiles)
{
foreach ($Mkv in $MKVFiles) {
Write-HTMLLog -Column1 ' ' -Column2 $Mkv.name
}
Write-HTMLLog -Column1 '*** Subtitle Files ***' -Header
$SrtFiles = Get-ChildItem -LiteralPath $ProcessPathFull -Recurse -Filter '*.srt'
foreach ($Srt in $SrtFiles)
{
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)
{
foreach ($File in $Files) {
Write-HTMLLog -Column1 'File:' -Column2 $File.name
}
}
Expand All @@ -360,8 +321,7 @@ elseif ($DownloadLabel -eq $MovieLabel)
# Reached the end of script
Write-HTMLLog -Column1 '*** Post Process General Download ***' -Header
$Files = Get-ChildItem -LiteralPath $ProcessPathFull -Recurse -Filter '*.*'
foreach ($File in $Files)
{
foreach ($File in $Files) {
Write-HTMLLog -Column1 'File:' -Column2 $File.name
}
Stop-Script -ExitReason "$DownloadLabel - $DownloadName"
35 changes: 13 additions & 22 deletions functions/CleanProcessPath.ps1
Original file line number Diff line number Diff line change
@@ -1,52 +1,43 @@
function CleanProcessPath
{
function CleanProcessPath {
[CmdletBinding()]
param (
[Parameter(
Mandatory = $true
)]
[string] $Path,
[string]$Path,

[Parameter(
Mandatory = $false
)]
[bool] $NoCleanUp
[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
{
foreach ($commandName in $commands) {
if (!($command = Get-Command $commandName -ErrorAction SilentlyContinue)) {
Try {
. $PSScriptRoot\$commandName.ps1
Write-Host "$commandName Function loaded." -ForegroundColor Green
}
Catch
{
Catch {
Write-Error -Message "Failed to import $commandName function: $_"
exit 1
}
}
}
# Start

if ($NoCleanUp)
{
if ($NoCleanUp) {
Write-HTMLLog -Column1 'Cleanup' -Column2 'NoCleanUp switch was given at command line, leaving files'
}
else
{
try
{
If (Test-Path -LiteralPath $ProcessPathFull)
{
else {
try {
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
Loading

0 comments on commit fb8216b

Please sign in to comment.