From 292697231cb85653ba18848ca464ed0c5398da70 Mon Sep 17 00:00:00 2001 From: jvlflame Date: Mon, 31 Aug 2020 00:30:56 -0700 Subject: [PATCH] Update find and sort functionality --- src/Javinizer/Public/Javinizer.ps1 | 640 ++++++++++------------------- 1 file changed, 211 insertions(+), 429 deletions(-) diff --git a/src/Javinizer/Public/Javinizer.ps1 b/src/Javinizer/Public/Javinizer.ps1 index 21f27121..de9b3f1d 100644 --- a/src/Javinizer/Public/Javinizer.ps1 +++ b/src/Javinizer/Public/Javinizer.ps1 @@ -218,326 +218,245 @@ function Javinizer { [CmdletBinding(DefaultParameterSetName = 'Path')] param ( [Parameter(ParameterSetName = 'Info', Mandatory = $true, Position = 0)] - [Alias('f')] - [string]$Find, - [Parameter(ParameterSetNAme = 'Info', Mandatory = $false)] - [switch]$Aggregated, - [Parameter(ParameterSetName = 'Path', Mandatory = $false, Position = 0)] + [Alias ('f')] + [PSObject]$Find, + [Parameter(ParameterSetNAme = 'Info')] + [Switch]$Aggregated, + [Parameter(ParameterSetNAme = 'Info')] + [Switch]$ShowNfo, + [Parameter(ParameterSetName = 'Info')] + [Switch]$R18, + [Parameter(ParameterSetName = 'Info')] + [Switch]$R18Zh, + [Parameter(ParameterSetName = 'Info')] + [Switch]$Dmm, + [Parameter(ParameterSetName = 'Info')] + [Switch]$Javlibrary, + [Parameter(ParameterSetName = 'Info')] + [Switch]$JavlibraryZh, + [Parameter(ParameterSetName = 'Info')] + [Switch]$JavlibraryJa, + [Parameter(ParameterSetName = 'Info')] + [Switch]$Javbus, + [Parameter(ParameterSetName = 'Info')] + [Switch]$JavbusJa, + [Parameter(ParameterSetName = 'Info')] + [Switch]$JavbusZh, + [Parameter(ParameterSetName = 'Info')] + [Switch]$Jav321, + [Parameter(ParameterSetName = 'Path', Position = 0)] [Alias('p')] - [string]$Path, - [Parameter(ParameterSetName = 'Path', Mandatory = $false, Position = 1)] + [System.IO.DirectoryInfo]$Path, + [Parameter(ParameterSetName = 'Path', Position = 1)] [Alias('d')] - [string]$DestinationPath, - [Parameter(ParameterSetName = 'Path', Mandatory = $false)] + [System.IO.DirectoryInfo]$DestinationPath, + [Parameter(ParameterSetName = 'Path')] [Alias('u')] - [string]$Url, - [Parameter(ParameterSetName = 'Path', Mandatory = $false)] - [Alias('a')] - [switch]$Apply, - [Parameter(ParameterSetName = 'Path', Mandatory = $false)] + [Array]$Url, + [Parameter(ParameterSetName = 'Path')] [Alias('m')] - [switch]$Multi, - [Parameter(ParameterSetName = 'Path', Mandatory = $false)] + [Switch]$Multi, + [Parameter(ParameterSetName = 'Path')] [Parameter(ParameterSetName = 'JavLibrary')] - [switch]$Recurse, - [Parameter(ParameterSetName = 'Path', Mandatory = $false)] - [Parameter(ParameterSetName = 'JavLibrary')] - [switch]$Strict, - [Parameter(ParameterSetName = 'Path', Mandatory = $false)] - [switch]$Force, - [Parameter(ParameterSetName = 'Path', Mandatory = $false)] - [string]$ImportSettings, - [Parameter(ParameterSetName = 'Path', Mandatory = $false)] - [bool]$MoveToFolder, - [Parameter(ParameterSetName = 'Path', Mandatory = $false)] - [bool]$RenameFile, + [Switch]$Recurse, + [Parameter(ParameterSetName = 'Path')] + [Switch]$Force, + [Parameter(ParameterSetName = 'Path')] + [PSObject]$Settings, + [Parameter(ParameterSetName = 'Path')] + [String]$ImportSettings, [Parameter(ParameterSetName = 'Help')] [Alias('h')] - [switch]$Help, + [Switch]$Help, [Parameter(ParameterSetName = 'Version')] [Alias('v')] - [switch]$Version, + [Switch]$Version, [Parameter(ParameterSetName = 'Settings')] - [switch]$OpenSettings, + [Switch]$OpenSettings, [Parameter(ParameterSetName = 'Settings')] - [string]$BackupSettings, + [String]$BackupSettings, [Parameter(ParameterSetName = 'Settings')] - [string]$RestoreSettings, - [Parameter(ParameterSetName = 'Log')] - [switch]$OpenLog, - [Parameter(ParameterSetName = 'Log')] - [ValidateSet('List', 'Grid', 'Table', 'Object')] - [string]$ViewLog, - [Parameter(ParameterSetName = 'Log')] - [ValidateSet('INFO', 'WARN', 'ERROR', 'DEBUG')] - [string]$LogLevel, + [String]$RestoreSettings, [Parameter(ParameterSetName = 'Log')] - [int]$Entries, - [Parameter(ParameterSetName = 'Log')] - [ValidateSet('Asc', 'Desc')] - [AllowEmptyString()] - [AllowNull()] - [string]$Order, + [Switch]$OpenLog, [Parameter(ParameterSetName = 'JavLibrary')] - [string]$SetJavlibraryOwned, + [String]$SetJavlibraryOwned, [Parameter(ParameterSetName = 'Thumbs')] - [switch]$GetThumbs, + [Switch]$GetThumbs, [Parameter(ParameterSetName = 'Thumbs')] - [int]$UpdateThumbs, + [Int]$UpdateThumbs, [Parameter(ParameterSetName = 'Thumbs')] - [switch]$OpenThumbs, + [Switch]$OpenThumbs, [Parameter(ParameterSetName = 'Thumbs')] - [switch]$SetEmbyActorThumbs, - [Parameter(ParameterSetName = 'Path', Mandatory = $false)] - [Parameter(ParameterSetName = 'Info', Mandatory = $false)] - [switch]$R18, - [Parameter(ParameterSetName = 'Path', Mandatory = $false)] - [Parameter(ParameterSetName = 'Info', Mandatory = $false)] - [switch]$R18Zh, - [Parameter(ParameterSetName = 'Path', Mandatory = $false)] - [Parameter(ParameterSetName = 'Info', Mandatory = $false)] - [switch]$Dmm, - [Parameter(ParameterSetName = 'Path', Mandatory = $false)] - [Parameter(ParameterSetName = 'Info', Mandatory = $false)] - [switch]$Javlibrary, - [Parameter(ParameterSetName = 'Path', Mandatory = $false)] - [Parameter(ParameterSetName = 'Info', Mandatory = $false)] - [switch]$JavlibraryZh, - [Parameter(ParameterSetName = 'Path', Mandatory = $false)] - [Parameter(ParameterSetName = 'Info', Mandatory = $false)] - [switch]$JavlibraryJa, - [Parameter(ParameterSetName = 'Path', Mandatory = $false)] - [Parameter(ParameterSetName = 'Info', Mandatory = $false)] - [switch]$Javbus, - [Parameter(ParameterSetName = 'Path', Mandatory = $false)] - [Parameter(ParameterSetName = 'Info', Mandatory = $false)] - [switch]$JavbusJa, - [Parameter(ParameterSetName = 'Path', Mandatory = $false)] - [Parameter(ParameterSetName = 'Info', Mandatory = $false)] - [switch]$Jav321, - [string]$ScriptRoot = (Get-Item $PSScriptRoot).Parent + [Switch]$SetEmbyActorThumbs ) begin { + $urlLocation = @() if (!($Multi.IsPresent -or $GetThumbs.IsPresent -or $UpdateThumbs.IsPresent)) { $ProgressPreference = 'SilentlyContinue' } - $urlLocation = @() - $urlList = @() - try { - # Load the settings file from either commandline path or default - if ($PSBoundParameters.ContainsKey('ImportSettings')) { - $settingsPath = Get-Item -LiteralPath $ImportSettings - } else { - $settingsPath = Join-Path -Path $ScriptRoot -ChildPath 'settings.ini' + + <# try { + # Allow user to update settings via commandline using a hashtable + # I.e $updateSettings = @{ "priority.actress" = @('r18', 'javlibrary', dmm'); "moveToFolder" = "true"} + # Javinizer -Path . -Settings $updateSettings + foreach ($setting in $Settings.GetEnumerator()) { + $nest = $setting.Key -split '\.' + if ($nest.Count -eq 1) { + $root = $nest[0] + $script:gSettings.$root = $setting.Value + } elseif ($nest.Count -eq 2) { + $root = $nest[0] + $key = $nest[1] + $script:gSettings.$root.$key = $setting.Value + } elseif ($nest.Count -eq 3) { + $root = $nest[0] + $nestOne = $nest[1] + $key = $nest[2] + $script:gSettings.$root.$nestOne.$key = $setting.Value + } elseif ($nest.Count -eq 4) { + $root = $nest[0] + $nestOne = $nest[1] + $nestTwo = $nest[2] + $key = $nest[3] + $script:gSettings.$root.$nestOne.$nestTwo.$key = $setting.Value + } } - $settings = Import-IniSettings -Path $settingsPath } catch { - throw "[$(Get-TimeStamp)][$($MyInvocation.MyCommand.Name)] Unable to load settings from path: $settingsPath" - } + Write-JLog -Level Error -Message $_ + } #> + } - Test-Settings -Path $settingsPath -ErrorAction Stop + process { + $settingsPath = Join-Path -Path ((Get-Item $PSScriptRoot).Parent) -ChildPath 'jvSettings.json' + $Settings = Get-Content -LiteralPath $settingsPath | ConvertFrom-Json -Depth 32 - if (($settings.Other.'log-path' -eq '') -or ($null -eq $settings.Other.'log-path')) { - $global:javinizerLogPath = Join-Path -Path $ScriptRoot -ChildPath javinizer.log + if ($Settings.'admin.log.path' -eq '') { + $logPath = Join-Path -Path ((Get-Item $PSScriptRoot).Parent) -ChildPath 'jvLog.log' } else { - $global:javinizerLogPath = $settings.Other.'log-path' -replace '"', '' + $logPath = $Settings.'admin.log.path' } - if ($settings.Other.'check-updates' -eq 'True') { - if (-not ($javinizerUpdateCheck)) { - Update-Javinizer - $global:javinizerUpdateCheck = $true + # The user will be able import an external settings file via commandline + # by using the ImportSettings parameter + <# try { + # TODO Change this path + $gSettingsPath = 'X:\git\Projects\JAV-Organizer\src\Javinizer\javinizerSettings.json' + if ($PSBoundParameters.ContainsKey('ImportSettings')) { + $script:gSettings = Get-Content -LiteralPath $ImportSettings | ConvertFrom-Json + } else { + $script:gSettings = Get-Content -LiteralPath $gSettingsPath | ConvertFrom-Json } - } else { - $global:javinizerUpdateCheck = $false + } catch { + throw $_ + } #> + + Add-LoggingTarget -Name File -Configuration @{ + Path = $logPath + Append = $true + Encoding = 'utf8' + Level = $Settings.'admin.log.level' + Format = '[%{timestamp}] [%{level:-7}] %{message}' } - if ($PSBoundParameters.ContainsKey('MoveToFolder')) { - if ($MoveToFolder -eq $true) { - $Settings.General.'move-to-folder' = 'True' - } elseif ($MoveToFolder -eq $false) { - $Settings.General.'move-to-folder' = 'False' - } - } + switch ($PsCmdlet.ParameterSetName) { + 'Info' { + if ($Find -match 'https?:\/\/') { + $urlObject = Get-JVUrlLocation -Url $Find + $data = foreach ($item in $urlObject) { + if ($item.Source -eq 'dmm') { + $item.Url | Get-DmmData + } - if ($PSBoundParameters.ContainsKey('RenameFile')) { - if ($RenameFile -eq $true) { - $Settings.General.'rename-file' = 'True' - } elseif ($RenameFile -eq $false) { - $Settings.General.'rename-file' = 'False' - } - } + if ($item.Source -eq 'jav321') { + $item.Url | Get-Jav321Data + } - if (($settings.Other.'verbose-shell-output' -eq 'True') -or ($PSBoundParameters.ContainsKey('Verbose'))) { - $VerbosePreference = 'Continue' - } else { - $VerbosePreference = 'SilentlyContinue' - } + if ($item.Source -eq 'javbus') { + $item.Url | Get-JavbusData + } - if ($settings.Other.'debug-shell-output' -eq 'True' -or ($DebugPreference -eq 'Continue')) { - $DebugPreference = 'Continue' - } elseif ($settings.Other.'debug-shell-output' -eq 'False') { - $DebugPreference = 'SilentlyContinue' - } else { - $DebugPreference = 'SilentlyContinue' - } + if ($item.Source -eq 'javbusja') { + $item.Url | Get-JavbusData + } - #Write-Debug "[$(Get-TimeStamp)][$($MyInvocation.MyCommand.Name)] Parameter set: [$($PSCmdlet.ParameterSetName)]" - #Write-Debug "[$(Get-TimeStamp)][$($MyInvocation.MyCommand.Name)] Bound parameters: [$($PSBoundParameters.Keys)]" - #$settings.Main.GetEnumerator() | Sort-Object Key | Out-String | Write-Debug -ErrorAction 'SilentlyContinue' - #$settings.General.GetEnumerator() | Sort-Object Key | Out-String | Write-Debug -ErrorAction 'SilentlyContinue' - #$settings.Metadata.GetEnumerator() | Sort-Object Key | Out-String | Write-Debug -ErrorAction 'SilentlyContinue' - #$settings.Locations.GetEnumerator() | Sort-Object Key | Out-String | Write-Debug -ErrorAction 'SilentlyContinue' - #$settings.'Emby/Jellyfin'.GetEnumerator() | Sort-Object Key | Out-String | Write-Debug -ErrorAction 'SilentlyContinue' - #$settings.Other.GetEnumerator() | Sort-Object Key | Out-String | Write-Debug -ErrorAction 'SilentlyContinue' - - if (-not ($PSBoundParameters.ContainsKey('r18')) -and ` - (-not ($PSBoundParameters.ContainsKey('dmm')) -and ` - (-not ($PSBoundParameters.ContainsKey('javlibrary')) -and ` - (-not ($PSBoundParameters.ContainsKey('javlibraryzh')) -and ` - (-not ($PSBoundParameters.ContainsKey('javlibraryja')) -and ` - (-not ($PSBoundParameters.ContainsKey('r18zh')) -and ` - (-not ($PSBoundParameters.ContainsKey('javbus')) -and ` - (-not ($PSBoundParameters.ContainsKey('javbusja')) -and ` - (-not ($PSBoundParameters.ContainsKey('jav321'))))))))))) { - if ($settings.Main.'scrape-r18' -eq 'true') { - $R18 = $true - } - if ($settings.Main.'scrape-dmm' -eq 'true') { - $Dmm = $true - } - if ($settings.Main.'scrape-javlibrary' -eq 'true') { - $Javlibrary = $true - } - if ($settings.Main.'scrape-javlibraryzh' -eq 'true') { - $JavlibraryZh = $true - } - if ($settings.Main.'scrape-javlibraryja' -eq 'true') { - $JavlibraryJa = $true - } - if ($settings.Main.'scrape-r18zh' -eq 'true') { - $R18Zh = $true - } - if ($settings.Main.'scrape-javbus' -eq 'true') { - $Javbus = $true - } - if ($settings.Main.'scrape-javbusja' -eq 'true') { - $javbusJa = $true - } + if ($item.Source -eq 'javbuszh') { + $item.Url | Get-JavbusData + } - if ($settings.Main.'scrape-jav321' -eq 'true') { - $jav321 = $true - } - } - } + if ($item.Source -eq 'javlibrary') { + $item.Url | Get-JavlibraryData + } - process { - # Write-Host "[$(Get-TimeStamp)][$($MyInvocation.MyCommand.Name)] Function started" - Write-Debug "[$(Get-TimeStamp)][$($MyInvocation.MyCommand.Name)] R18: [$R18]; R18Zh: [$R18Zh] Dmm: [$Dmm]; Javlibrary: [$Javlibrary]; JavlibraryZh: [$JavlibraryZh]; JavlibraryJa: [$JavlibraryJa]; Javbus: [$Javbus]; JavbusJa: [$JavbusJa]; Jav321: [$Jav321]" - switch ($PsCmdlet.ParameterSetName) { - 'Info' { - $dataObject = Get-FindDataObject -Find $Find -Settings $settings -Aggregated:$Aggregated -Dmm:$Dmm -R18:$R18 -R18Zh:$R18Zh -Javlibrary:$Javlibrary -JavlibraryZh:$JavlibraryZh -JavlibraryJa:$JavlibraryJa -Javbus:$Javbus -JavbusJa:$JavbusJa -Jav321:$Jav321 - Write-Output $dataObject - } + if ($item.Source -eq 'javlibraryja') { + $item.Url | Get-JavlibraryData + } - 'Log' { - if ($OpenLog.IsPresent) { - if ([System.Environment]::OSVersion.Platform -eq 'Win32NT') { - try { - Write-Host "[$(Get-TimeStamp)][$($MyInvocation.MyCommand.Name)] Opening javinizer.log file from [$javinizerLogPath]" - Invoke-Item -Path $javinizerLogPath - } catch { - Write-Warning "[$(Get-TimeStamp)][$($MyInvocation.MyCommand.Name)] Error opening javinizer.log file from [$javinizerLogPath]" - throw $_ + if ($item.Source -eq 'javlibraryzh') { + $item.Url | Get-JavlibraryData } - } elseif ([System.Environment]::OSVersion.Platform -eq 'Unix') { - try { - Write-Host "[$(Get-TimeStamp)][$($MyInvocation.MyCommand.Name)] Opening javinizer.log file from [$javinizerLogPath]" - nano $javinizerLogPath - } catch { - Write-Warning "[$(Get-TimeStamp)][$($MyInvocation.MyCommand.Name)] Error opening javinizer.log file from [$javinizerLogPath]" - throw $_ + + if ($item.Source -eq 'r18') { + $item.Url | Get-R18Data } + + if ($item.Source -eq 'r18zh') { + $item.Url | Get-R18Data + } + } + + $data = [PSCustomObject]@{ + Data = $data } + } else { + $data = Get-JVData -Id $Find -R18:$R18 -R18Zh:$R18Zh -Javlibrary:$Javlibrary -JavlibraryJa:$JavlibraryJa -JavlibraryZh:$JavlibraryZh -Dmm:$Dmm ` + -Javbus:$Javbus -JavbusJa:$JavbusJa -JavbusZh:$JavbusZh -Jav321:$Jav321 } - if ($ViewLog) { - try { - if (!($PSBoundParameters.ContainsKey('Entries'))) { - $Entries = 10 - } + if ($Aggregated) { + $data = $data | Get-JVAggregatedData -Settings $Settings + } - if ($PSBoundParameters.ContainsKey('LogLevel')) { - Get-Log -Path $javinizerLogPath -LogView $ViewLog -LogLevel $LogLevel -Entries $Entries -Order $Order - } else { - Get-Log -Path $javinizerLogPath -LogView $ViewLog -Entries $Entries -Order $Order - } + if ($ShowNfo) { + $nfo = $data.Data | Get-JVNfo -ActressLanguageJa:$Settings.'sort.metadata.nfo.actresslanguageja' -NameOrder:$Settings.'sort.metadata.nfo.firstnameorder' -AddTag:$Settings.'sort.metadata.nfo.seriesastag' + Write-Output $nfo + } else { + Write-Output $data.Data + } + } + + 'Log' { + if ($OpenLog) { + try { + Invoke-Item -LiteralPath $logPath } catch { - Write-Warning "[$(Get-TimeStamp)][$($MyInvocation.MyCommand.Name)] Error displaying javinizer.log from [$javinizerLogPath]: $($PSItem.ToString())" + Write-JLog -Level Error -Message "Error occurred when opening log file [$logPath]: $PSItem" } } } 'Settings' { if ($OpenSettings.IsPresent) { - if ([System.Environment]::OSVersion.Platform -eq 'Win32NT') { - try { - Write-Host "[$(Get-TimeStamp)][$($MyInvocation.MyCommand.Name)] Opening settings.ini file from [$settingsPath]" - Invoke-Item -Path $settingsPath - } catch { - Write-Warning "[$(Get-TimeStamp)][$($MyInvocation.MyCommand.Name)] Error opening settings.ini file from [$settingsPath]" - throw $_ - } - } elseif ([System.Environment]::OSVersion.Platform -eq 'Unix') { - try { - Write-Host "[$(Get-TimeStamp)][$($MyInvocation.MyCommand.Name)] Opening settings.ini file from [$settingsPath]" - nano $settingsPath - } catch { - Write-Warning "[$(Get-TimeStamp)][$($MyInvocation.MyCommand.Name)] Error opening settings.ini file from [$settingsPath]" - throw $_ - } - } - } elseif ($PSBoundParameters.ContainsKey('BackupSettings')) { - $backupSettingsParams = @{ - LiteralPath = (Join-Path -Path $ScriptRoot -ChildPath 'settings.ini'), (Join-Path -Path $ScriptRoot -ChildPath 'r18-thumbs.csv') - CompressionLevel = 'Fastest' - DestinationPath = $BackupSettings - } try { - Write-Host "[$(Get-TimeStamp)][$($MyInvocation.MyCommand.Name)] Writing settings backup archive to [$BackupSettings]" - Compress-Archive @backupSettingsParams + Invoke-Item -Path $settingsPath } catch { - Write-Warning "[$(Get-TimeStamp)][$($MyInvocation.MyCommand.Name)] Error writing settings backup archive to [$BackupSettings]" - throw $_ - } - } elseif ($PSBoundParameters.ContainsKey('RestoreSettings')) { - $restoreSettingsParams = @{ - LiteralPath = $RestoreSettings - DestinationPath = $ScriptRoot - Force = $true - } - try { - Write-Host "[$(Get-TimeStamp)][$($MyInvocation.MyCommand.Name)] Restoring settings backup archive from [$RestoreSettings] to [$ScriptRoot]" - Expand-Archive @restoreSettingsParams - } catch { - Write-Warning "[$(Get-TimeStamp)][$($MyInvocation.MyCommand.Name)] Error restoring settings backup archive to [$ScriptRoot] from [$RestoreSettings]" - throw $_ + Write-JLog -Level Error -Message "Error occurred when opening settings file [$settingsPath]: $PSItem" } } } 'JavLibrary' { - + # TODO Re-check without creating a cloudflare session if (!($Session)) { New-CloudflareSession -ScriptRoot $ScriptRoot } try { - Write-Debug "[$(Get-TimeStamp)][$($MyInvocation.MyCommand.Name)] Getting owned movies on JAVLibrary" + Write-JLog -Level Debug -Message "Getting owned movies on JAVLibrary" $request = Invoke-WebRequest -Uri "https://www.javlibrary.com/en/mv_owned_print.php" -Verbose:$false -Headers @{ "method" = "GET" "authority" = "www.javlibrary.com" @@ -558,15 +477,14 @@ function Javinizer { $javlibraryOwnedMovies = ($request.content -split '' | ForEach-Object { (($_ -split '<\/td>')[0] -split ' ')[0] }) $global:javlibraryOwnedMovies = $javlibraryOwnedMovies[2..($javlibraryOwnedMovies.Length - 1)] } catch { - Write-Error "[$(Get-TimeStamp)][$($MyInvocation.MyCommand.Name)] Error getting existing owned movies on JAVLibrary: $PSItem" + Write-JLog -Level Error -Message "Error getting existing owned movies on JAVLibrary: $PSItem" return } if ($null -ne $global:javlibraryOwnedMovies) { if ($global:javlibraryOwnedMovies.Count -gt 1) { if ($javlibraryOwnedMovies[0].Length -le 1) { - Write-Error "[$(Get-TimeStamp)][$($MyInvocation.MyCommand.Name)] Error authenticating to JAVLibrary to set owned movies, check that your username and session-cookie are valid" - return + Write-JLog -Level Error -Message "Error authenticating to JAVLibrary to set owned movies, check that your username and sessionCookie are valid" } } } @@ -578,10 +496,11 @@ function Javinizer { $movieList = (Convert-JavTitle -Path $SetJavLibraryOwned -Recurse:$Recurse -Settings $Settings -Strict:$Strict).Id } } catch { - Write-Error "[$(Get-TimeStamp)][$($MyInvocation.MyCommand.Name)] Error getting movies [$SetJavLibraryOwned]: $PSItem" - return + Write-JLog -Level Error -Message "Error getting movies from [$SetJavLibraryOwned]: $PSItem" } + # Validate which movies in the current path are unowned + # This will avoid trying to re-add currently existing movies on JAVLibrary $unowned = @() foreach ($movie in $movieList) { if (!($javlibraryOwnedMovies -match $movie)) { @@ -589,24 +508,25 @@ function Javinizer { } } + Write-JLog -Level Info -Message "[$($unowned.Count)] movies to add" + if ($unowned.Count -ge 1) { $index = 1 foreach ($movie in $unowned) { - Write-Host "[$(Get-TimeStamp)][$($MyInvocation.MyCommand.Name)] ($index of $($unowned.Count)) Setting [$movie] to owned on JAVLibrary" - $javlibObject = Get-JavLibraryDataObject -Name $movie + Write-JLog -Level Info -Message "($index of $($unowned.Count)) Setting [$movie] as owned on JAVLibrary" + $javlibObject = Get-JavlibraryData -Name $movie if ($null -ne $javlibObject) { $ajaxId = $javlibObject.AjaxId $url = $javlibObject.Url Set-JavlibraryOwned -AjaxId $ajaxId -JavlibraryUrl $url -Settings $settings Start-Sleep -Seconds $Settings.JavLibrary.'request-interval-sec' } else { - Write-Warning "[$(Get-TimeStamp)][$($MyInvocation.MyCommand.Name)] Movie [$movie] not matched on JAVLibrary, skipping..." + Write-JLog -Level Warning -Message "Skipping [$movie] -- not matched on JAVLibrary" } $index++ - } } else { - Write-Warning "[$(Get-TimeStamp)][$($MyInvocation.MyCommand.Name)] No new movies were detected in [$SetJavLibraryOwned], exiting..." + Write-JLog -Level Warning -Message "Exiting -- no new movies detected in [$SetJavLibraryOwned]" } } @@ -622,18 +542,10 @@ function Javinizer { if ($GetThumbs.IsPresent) { Get-R18ThumbCsv -ScriptRoot $ScriptRoot -Settings $settings -Force:$Force } elseif ($OpenThumbs.IsPresent) { - if ([System.Environment]::OSVersion.Platform -eq 'Win32NT') { - try { - Invoke-Item -Path (Join-Path $ScriptRoot -ChildPath 'r18-thumbs.csv') - } catch { - throw $_ - } - } elseif ([System.Environment]::OSVersion.Platform -eq 'Unix') { - try { - nano (Join-Path $ScriptRoot -ChildPath 'r18-thumbs.csv') - } catch { - throw $_ - } + try { + Invoke-Item -Path (Join-Path $ScriptRoot -ChildPath 'r18-thumbs.csv') + } catch { + Write-JLog -Level Error -Message "Error opening thumb csv: $PSItem" } } elseif ($PSBoundParameters.ContainsKey('UpdateThumbs')) { Get-R18ThumbCsv -ScriptRoot $ScriptRoot -NewPages $UpdateThumbs -Force:$Force @@ -642,177 +554,47 @@ function Javinizer { } } - 'Path' { - if (-not ($PSBoundParameters.ContainsKey('Path'))) { - if (-not ($Apply.IsPresent)) { - Write-Warning "[$(Get-TimeStamp)][$($MyInvocation.MyCommand.Name)] Neither [Path] nor [Apply] parameters are specified; Exiting..." - return - } - $Path = ($settings.Locations.'input-path') -replace '"', '' - $DestinationPath = ($settings.Locations.'output-path') -replace '"', '' + # Default path to location.input in settings if not specified + if (!($Path)) { + $Path = $Settings.'location.input' } - try { - $getPath = Get-Item -LiteralPath ($Path).replace('`[', '[').replace('`]', ']') -ErrorAction Stop - } catch { - Write-Warning "[$(Get-TimeStamp)][$($MyInvocation.MyCommand.Name)] Path: [$Path] does not exist; Exiting..." - return + # This will check that the Path is valid + if (!(Test-Path -LiteralPath $Path)) { + Write-JLog -Level Error -Message "[$($MyInvocation.MyCommand.Name)] Path [$Path] is not a valid path" } - if (-not ($PSBoundParameters.ContainsKey('DestinationPath')) -and (-not ($Apply.IsPresent))) { - if (Test-Path -LiteralPath $getPath.FullName -PathType Leaf) { - $DestinationPath = $getPath.DirectoryName - } else { - $DestinationPath = $Path - } + # Default destination path to location.output in settings if not specified + if (!($DestinationPath)) { + $DestinationPath = $Settings.'location.output' } - try { - $getDestinationPath = Get-Item -LiteralPath $DestinationPath -ErrorAction 'SilentlyContinue' - } catch [System.Management.Automation.SessionStateException] { - Write-Warning "[$(Get-TimeStamp)][$($MyInvocation.MyCommand.Name)] Destination Path: [$DestinationPath] does not exist; Attempting to create the directory..." - New-Item -ItemType Directory -LiteralPath $DestinationPath -Confirm | Out-Null - $getDestinationPath = Get-Item -LiteralPath $DestinationPath -ErrorAction Stop - } catch { - throw $_ - } + if ($Url) { - try { - Write-Debug "[$(Get-TimeStamp)][$($MyInvocation.MyCommand.Name)] Attempting to read file(s) from path: [$($getPath.FullName)]" - $fixedPath = ($getPath.FullName).replace('[', '`[').replace(']', '`]') - $fileDetails = Convert-JavTitle -Path $fixedPath -Recurse:$Recurse -Settings $settings -Strict:$Strict - } catch { - Write-Warning "[$(Get-TimeStamp)][$($MyInvocation.MyCommand.Name)] Path: [$Path] does not contain any video files or does not exist; Exiting..." - return } - if ($Javlibrary) { - if ($null -eq $session) { - New-CloudflareSession -ScriptRoot $ScriptRoot - } + # This will check that the DestinationPath is a valid directory + if (Test-Path -LiteralPath $DestinationPath -PathType Leaf) { + Write-JLog -Level Error -Message "[$($MyInvocation.MyCommand.Name)] DestinationPath [$DestinationPath] is not a valid directory path" } - #Write-Debug "[$(Get-TimeStamp)][$($MyInvocation.MyCommand.Name)] Converted file details: [$($fileDetails)]" + try { + $javMovies = $Settings | Get-JVItem -Path $Path -Recurse:$Recurse -Strict:$Strict + Write-Host "[$($MyInvocation.MyCommand.Name)] [Path - $Path] [DestinationPath - $DestinationPath] [Count - $($javMovies.Count)]" + } catch { + Write-JLog -Level Error -Message "[$($MyInvocation.MyCommand.Name)] Exiting -- no movies detected in [$Path]" + } - # Match a single file and perform actions on it - if ((Test-Path -LiteralPath $getPath.FullName -PathType Leaf) -and (Test-Path -LiteralPath $getDestinationPath.FullName -PathType Container)) { - Write-Debug "[$(Get-TimeStamp)][$($MyInvocation.MyCommand.Name)] Detected path: [$($getPath.FullName)] as single item" - if ($PSBoundParameters.ContainsKey('Url')) { - if ($Url -match ',') { - $urlList = $Url -split ',' - $urlLocation = Test-UrlLocation -Url $urlList - } else { - $urlLocation = Test-UrlLocation -Url $Url - } - $dataObject = Get-AggregatedDataObject -UrlLocation $urlLocation -Settings $settings -ErrorAction 'SilentlyContinue' - Set-JavMovie -DataObject $dataObject -Settings $settings -Path $getPath.FullName -DestinationPath $getDestinationPath.FullName -ScriptRoot $ScriptRoot - } else { - $dataObject = Get-AggregatedDataObject -FileDetails $fileDetails -Settings $settings -R18:$R18 -R18Zh:$R18Zh -Dmm:$Dmm -Javlibrary:$Javlibrary -JavlibraryZh:$JavlibraryZh -JavlibraryJa:$JavlibraryJa -Javbus:$Javbus -JavbusJa:$JavbusJa -Jav321:$Jav321 -ErrorAction 'SilentlyContinue' -ScriptRoot $ScriptRoot - Set-JavMovie -DataObject $dataObject -Settings $settings -Path $getPath.FullName -DestinationPath $getDestinationPath.FullName -ScriptRoot $ScriptRoot - } - # Match a directory/multiple files and perform actions on them - } elseif (((Test-Path -LiteralPath $getPath.FullName -PathType Container) -and (Test-Path -LiteralPath $getDestinationPath.FullName -PathType Container)) -or $Apply.IsPresent) { - Write-Debug "[$(Get-TimeStamp)][$($MyInvocation.MyCommand.Name)] Detected path: [$($getPath.FullName)] as directory and destinationpath: [$($getDestinationPath.FullName)] as directory" - Write-Host "[$(Get-TimeStamp)][$($MyInvocation.MyCommand.Name)] Sort path: [$($getPath.FullName)]" - Write-Host "[$(Get-TimeStamp)][$($MyInvocation.MyCommand.Name)] Destination path: [$($getDestinationPath.FullName)]" - Write-Host "[$(Get-TimeStamp)][$($MyInvocation.MyCommand.Name)] Included file ext: [$($Settings.General.'included-file-extensions')]" - Write-Host "[$(Get-TimeStamp)][$($MyInvocation.MyCommand.Name)] Excluded file strings: [$($Settings.General.'excluded-file-strings')]" - - if ($Multi.IsPresent) { - $throttleCount = $Settings.General.'multi-sort-throttle-limit' - try { - if ($Javlibrary) { - if ($null -eq $session) { - New-CloudflareSession -ScriptRoot $ScriptRoot - } - } else { - $global:session = $null - } - - if ($Settings.General.'move-to-folder' -eq 'True') { - $movePreference = $true - } else { - $movePreference = $false - } - - if ($Settings.General.'rename-file' -eq 'True') { - $renamePreference = $true - } else { - $renamePreference = $false - } - - $files = Get-VideoFile -Path $Path -Recurse:$Recurse -Settings $Settings - foreach ($file in $files) { - $filePath = $file.FullName - Start-ThreadJob -Name $file.BaseName -ThrottleLimit $Settings.General.'multi-sort-throttle-limit' -ScriptBlock { - $Settings = $using:Settings - - # This will import the module in the separate Javinizer threads if the module isn't installed using PowerShellGet - if ($Settings.Other.'module-psm1-path') { - Import-Module $Settings.Other.'module-psm1-path' - } - - $global:javinizerUpdatecheck = $using:javinizerUpdateCheck - - if ($using:Javlibrary) { - $global:Session = $using:Session - } - - Javinizer -Path $using:filePath -DestinationPath:($using:DestinationPath) -ScriptRoot $using:ScriptRoot -Strict:($using:Strict) -MoveToFolder:($using:movePreference) -RenameFile:($using:renamePreference) -Force:($using:Force) - } | Out-Null - } - - $waitJobs = Get-Job -IncludeChildJob | Where-Object { $_.PSJobTypeName -eq 'ThreadJob' } - $totalJobs = $waitJobs.Count - $completed = 0 - while ($waitJobs.Count -ne 0) { - $runningJobs = @() - $completedJobs = @() - $otherJobs = @() - - foreach ($job in $waitJobs) { - if ($job.State -eq 'Completed') { - $completedJobs += $job - } elseif ($job.State -eq 'Running') { - $runningJobs += $job - } else { - $otherJobs += $job - } - } - - Write-Progress -Id 1 -Activity 'Javinizer' -Status "Remaining Jobs: $($Waitjobs.Count)" -PercentComplete (($completed / $totalJobs) * 100) - Write-Progress -ParentId 1 -Id 2 -Activity "Max threads: [$($Settings.General.'multi-sort-throttle-limit')]" -Status "Sorting: $($runningJobs.Name -join ', ')" - - $waitJobs = $runningJobs + $otherJobs - $completed += $completedJobs.Count - } - - } catch { - Write-Error "[$(Get-TimeStamp)][$($MyInvocation.MyCommand.Name)] There was an error starting multi sort for path: [$($getPath.FullName)] with destinationpath: [$DestinationPath] and threads: [$throttleCount]: $PSItem" - } finally { - # Stop all running jobs if script is stopped by user input - Get-Job | Receive-Job - Write-Host "[$(Get-TimeStamp)][$($MyInvocation.MyCommand.Name)] Sort has completed or has been stopped prematurely; Stopping all running jobs..." - Get-Job | Remove-Job -Force - } - } else { - $index = 1 - foreach ($video in $fileDetails) { - Write-Host "[$(Get-TimeStamp)][$($MyInvocation.MyCommand.Name)] ($index of $($fileDetails.Count)) Sorting [$($video.OriginalFileName)]" - $dataObject = Get-AggregatedDataObject -FileDetails $video -Settings $settings -R18:$R18 -R18Zh:$R18Zh -Dmm:$Dmm -Javlibrary:$Javlibrary -JavlibraryZh:$JavlibraryZh -JavlibraryJa:$JavlibraryJa -Javbus:$javbus -JavbusJa:$JavbusJa -Jav321:$Jav321 -ScriptRoot $ScriptRoot -ErrorAction 'SilentlyContinue' - Set-JavMovie -DataObject $dataObject -Settings $settings -Path $video.OriginalFullName -DestinationPath $getDestinationPath.FullName -Force:$Force -ScriptRoot $ScriptRoot - $index++ - } - } - } else { - throw "[$(Get-TimeStamp)][$($MyInvocation.MyCommand.Name)] Specified Path: [$Path] and/or DestinationPath: [$DestinationPath] did not match allowed types" + $index = 1 + foreach ($movie in $javMovies) { + Write-Host "[$index of $($javMovies.Count)] Sorting [$($movie.FileName)] as [$($movie.Id)]" + $javData = Get-JVData -Settings $Settings -Id $movie.Id + $javAggregatedData = $javData | Get-JVAggregatedData -Settings $Settings + $javAggregatedData | Set-JVMovie -Path $movie.FullName -DestinationPath $DestinationPath -Settings $Settings -PartNumber $movie.Partnumber + $index++ } } } } - - end { - # Write-Host "[$(Get-TimeStamp)][$($MyInvocation.MyCommand.Name)] Function ended" - } }