Skip to content

Commit

Permalink
Update logging to remove dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
jvlflame committed Sep 2, 2020
1 parent 45c455d commit 686faa9
Show file tree
Hide file tree
Showing 24 changed files with 297 additions and 237 deletions.
1 change: 0 additions & 1 deletion src/Javinizer/Private/Convert-HTMLCharacter.ps1
Expand Up @@ -19,7 +19,6 @@ function Convert-HTMLCharacter {
-replace '&#039', ''

$newString = $String.Trim()
# Write-JVLog -Level Debug -Message "Begin String: [$String]; End string: [$newString]"
Write-Output $newString
}
}
4 changes: 2 additions & 2 deletions src/Javinizer/Private/Convert-JVTitle.ps1
Expand Up @@ -86,7 +86,7 @@ function Convert-JVTitle {
$id = ($file | Select-String $RegexString).Matches.Groups[$RegexIdMatch].Value
$partNum = ($file | Select-String $RegexString).Matches.Groups[$RegexPtMatch].Value
} catch {
Write-JVLog -Level Debug -Message "File [$file] not matched by regex"
Write-JVLog -Write:$script:JVLogWrite -LogPath $script:JVLogPath -WriteLevel $script:JVLogWriteLevel -Level Debug -Message "File [$file] not matched by regex"
break
}
if ($fileBaseNameUpper -eq 1) {
Expand Down Expand Up @@ -198,7 +198,7 @@ function Convert-JVTitle {
<#
#Match ID-###-A, ID-###-B, etc.
elseif ($fileBaseNameUpper[$x] -match "[-][0-9]{1,6}[-][a-iA-I]$") {
Write-JVLog -Level Debug -Message "Match 3"
Write-JVLog -Write:$script:JVLogWrite -LogPath $script:JVLogPath -WriteLevel $script:JVLogWriteLevel -Level Debug -Message "Match 3"
$fileP1, $fileP2, $fileP3, $fileP4 = $fileBaseNameUpper[$x] -split "([-][0-9]{1,6})[-]([a-zA-Z])"
$fileBaseNameUpperCleaned += $fileP1 + $fileP2 + $fileP3
}
Expand Down
2 changes: 1 addition & 1 deletion src/Javinizer/Private/Get-JVUrlLocation.ps1
Expand Up @@ -70,7 +70,7 @@ function Get-JVUrlLocation {
Source = 'jav321'
}
} else {
Write-JVLog -Level Warning -Message "[$($MyInvocation.MyCommand.Name)] [Url - $Url] not matched"
Write-JVLog -Write:$script:JVLogWrite -LogPath $script:JVLogPath -WriteLevel $script:JVLogWriteLevel -Level Warning -Message "[$($MyInvocation.MyCommand.Name)] [Url - $Url] not matched"
}
}
Write-Output $testUrlObject
Expand Down
4 changes: 2 additions & 2 deletions src/Javinizer/Private/Scraper.R18.ps1
Expand Up @@ -223,10 +223,10 @@ function Get-R18Series {

if ($series -like '*...') {
try {
Write-JVLog -Level Debug -Message "Performing [GET] on URL [$seriesUrl]"
Write-JVLog -Write:$script:JVLogWrite -LogPath $script:JVLogPath -WriteLevel $script:JVLogWriteLevel -Level Debug -Message "Performing [GET] on URL [$seriesUrl]"
$seriesSearch = Invoke-WebRequest -Uri $seriesUrl -Method Get -Verbose:$false
} catch {
Write-JVLog -Level ERROR -Message "Error [GET] on URL [$seriesUrl]: $PSItem"
Write-JVLog -Write:$script:JVLogWrite -LogPath $script:JVLogPath -WriteLevel $script:JVLogWriteLevel -Level ERROR -Message "Error [GET] on URL [$seriesUrl]: $PSItem"
}
$series = (Convert-HtmlCharacter -String ((((($seriesSearch.Content -split '<div class="breadcrumbs">')[1]) -split '<\/span>')[0]) -split '<span>')[1]) -replace "`t", ''
}
Expand Down
78 changes: 36 additions & 42 deletions src/Javinizer/Private/Set-JavlibraryOwned.ps1
@@ -1,54 +1,48 @@
function Set-JavlibraryOwned {
[CmdletBinding()]
param (
[object]$AjaxId,
[object]$JavlibraryUrl,
[object]$Settings
)

Write-Debug "[$(Get-TimeStamp)][$($MyInvocation.MyCommand.Name)] Function started"
Write-Debug "[$(Get-TimeStamp)][$($MyInvocation.MyCommand.Name)] AjaxId: $AjaxId"
Write-Debug "[$(Get-TimeStamp)][$($MyInvocation.MyCommand.Name)] Url: $JavlibraryUrl"

try {
$timeout = New-TimeSpan -Seconds $Settings.JavLibrary.'request-timeout-sec'
$stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
while ($check.content -notmatch '"ERROR":1' -and $stopwatch.elapsed -lt $timeout) {
if ($check.Content -match '"ERROR":-3') {
Start-Sleep -Seconds 3
process {
try {
$timeout = New-TimeSpan -Seconds $Settings.JavLibrary.'request-timeout-sec'
$stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
while ($check.content -notmatch '"ERROR":1' -and $stopwatch.elapsed -lt $timeout) {
if ($check.Content -match '"ERROR":-3') {
Start-Sleep -Seconds 3
}
$check = Invoke-WebRequest -Uri "https://www.javlibrary.com/ajax/ajax_cv_favoriteadd.php" `
-Method "POST" `
-Headers @{
"method" = "POST"
"authority" = "www.javlibrary.com"
"scheme" = "https"
"path" = "/ajax/ajax_cv_favoriteadd.php"
"accept" = "application/json, text/javascript, */*; q=0.01"
"x-requested-with" = "XMLHttpRequest"
"user-agent" = $session.UserAgent
"origin" = "https://www.javlibrary.com"
"sec-fetch-site" = "same-origin"
"sec-fetch-mode" = "cors"
"sec-fetch-dest" = "empty"
"referer" = $JavlibraryUrl
"accept-encoding" = "gzip, deflate, br"
"accept-language" = "en-US,en;q=0.9"
"cookie" = "timezone=420; over18=18; __cfduid=$SessionCFDUID; userid=$($Settings.JavLibrary.username); session=$($Settings.JavLibrary.'session-cookie')"
} `
-ContentType "application/x-www-form-urlencoded; charset=UTF-8" `
-Body "type=2&targetid=$AjaxId" `
-Verbose:$false
}
$check = Invoke-WebRequest -Uri "https://www.javlibrary.com/ajax/ajax_cv_favoriteadd.php" `
-Method "POST" `
-Headers @{
"method" = "POST"
"authority" = "www.javlibrary.com"
"scheme" = "https"
"path" = "/ajax/ajax_cv_favoriteadd.php"
"accept" = "application/json, text/javascript, */*; q=0.01"
"x-requested-with" = "XMLHttpRequest"
"user-agent" = $session.UserAgent
"origin" = "https://www.javlibrary.com"
"sec-fetch-site" = "same-origin"
"sec-fetch-mode" = "cors"
"sec-fetch-dest" = "empty"
"referer" = $JavlibraryUrl
"accept-encoding" = "gzip, deflate, br"
"accept-language" = "en-US,en;q=0.9"
"cookie" = "timezone=420; over18=18; __cfduid=$SessionCFDUID; userid=$($Settings.JavLibrary.username); session=$($Settings.JavLibrary.'session-cookie')"
} `
-ContentType "application/x-www-form-urlencoded; charset=UTF-8" `
-Body "type=2&targetid=$AjaxId" `
-Verbose:$false
}

if ($stopwatch.elapsed -gt $timeout) {
Write-Error "[$(Get-TimeStamp)][$($MyInvocation.MyCommand.Name)] Movie [$JavlibraryUrl] timed out while setting owned status"
Write-Log -Log $javinizerLogPath -Level ERROR -Text "Movie [$JavlibraryUrl] timed out while setting owned status"
if ($stopwatch.elapsed -gt $timeout) {
Write-JVLog -Level Warning -Message "Failed setting owned on JAVLibrary [$JavlibraryUrl]: Timed out"
}
} catch {
Write-JVLog -Level Error -Message "Failed setting owned on JAVLibrary [$JavlibraryUrl]: $PSItem"
}
} catch {
Write-Error "[$(Get-TimeStamp)][$($MyInvocation.MyCommand.Name)] Error setting owned status for [$JavlibraryUrl]: $PSItem"
Write-Log -Log $javinizerLogPath -Level ERROR -Text "Error setting owned status for [$JavlibraryUrl]: $PSItem"
}
}



56 changes: 27 additions & 29 deletions src/Javinizer/Private/Test-JVSettings.ps1
Expand Up @@ -20,41 +20,39 @@ function Test-JVSettings {
[String]$Type
)

process {
foreach ($setting in $SettingsGroup) {
$settingValue = $Settings.($setting)

if ($Type -eq 'Path') {
if ($settingValue -ne '') {
if (!(Test-Path -Path $settingValue -IsValid)) {
Write-JVLog -Level Error -Message "Error occurred when validating setting [$setting] with value [$settingValue] as a Path"
}
}
}
foreach ($setting in $SettingsGroup) {
$settingValue = $Settings.($setting)

if ($Type -eq 'Boolean') {
if ($settingValue -ne 0 -and $settingValue -ne 1 -and $settingValue.GetType() -ne 'System.ValueType') {
Write-JVLog -Level Error -Message "Error occurred when validating setting [$setting] with value [$settingValue] as a Boolean"
if ($Type -eq 'Path') {
if ($settingValue -ne '') {
if (!(Test-Path -Path $settingValue)) {
Write-JVLog -Level Error -Message "Error occurred when validating setting [$setting] with value [$settingValue] as a path"
}
}
}

if ($Type -eq 'Integer') {
if ($settingValue.GetType().BaseType -ne 'System.ValueType' -and $settingValue.GetType().Name -notlike 'int*') {
Write-JVLog -Level Error -Message "Error occurred when validating setting [$setting] with value [$settingValue] as a Integer"
}
if ($Type -eq 'Boolean') {
if ($settingValue -ne 0 -and $settingValue -ne 1) {
Write-JVLog -Level Error -Message "Error occurred when validating setting [$setting] with value [$settingValue] as a boolean"
}
}

if ($Type -eq 'Integer') {
if ($settingValue.GetType().BaseType -ne 'System.ValueType' -and $settingValue.GetType().Name -notlike 'int*') {
Write-JVLog -Level Error -Message "Error occurred when validating setting [$setting] with value [$settingValue] as an integer"
}

if ($Type -eq 'String') {
if ($settingValue.GetType().BaseType -ne 'System.Object' -and $settingValue.GetType().Name -ne 'String') {
Write-JVLog -Level Error -Message "Error occurred when validating setting [$setting] with value [$settingValue] as a String"
}
}

if ($Type -eq 'String') {
if ($settingValue.GetType().BaseType -ne 'System.Object' -and $settingValue.GetType().Name -ne 'String') {
Write-JVLog -Level Error -Message "Error occurred when validating setting [$setting] with value [$settingValue] as a string"
}
}

if ($Type -eq 'Array') {
if ($settingValue.GetType().Name -notlike '*Object*') {
Write-JVLog -Level Error -Message "Error occurred when validating setting [$setting] with value [$settingValue] as a Array"
}
if ($Type -eq 'Array') {
if ($settingValue.GetType().BaseType -notlike '*array*' -and $settingValue.GetType().BaseType -notlike '*object*') {
Write-JVLog -Level Error -Message "Error occurred when validating setting [$setting] with value [$settingValue] as an array"
}
}
}
Expand All @@ -70,6 +68,8 @@ function Test-JVSettings {

$booleanSettings = @(
'admin.log',
'javlibrary.request.interval',
'javlibrary.request.timeout',
'match.regex',
'scraper.movie.dmm',
'scraper.movie.jav321',
Expand Down Expand Up @@ -100,8 +100,6 @@ function Test-JVSettings {
) | Test-JVSettingsGroup -Settings $Settings -Type Boolean

$integerSettings = @(
'javlibrary.request.interval',
'javlibrary.request.timeout',
'match.minimumfilesize',
'match.regex.idmatch',
'match.regex.ptmatch',
Expand Down Expand Up @@ -147,7 +145,7 @@ function Test-JVSettings {
'sort.metadata.priority.screenshoturl',
'sort.metadata.priority.series',
'sort.metadata.priority.title',
'sort.metadata.priority.trailerurl'
'sort.metadata.priority.trailerurl',
'sort.metadata.requiredfield'
) | Test-JVSettingsGroup -Settings $Settings -Type Array

Expand Down
53 changes: 43 additions & 10 deletions src/Javinizer/Private/Write-JVLog.ps1
@@ -1,35 +1,68 @@
function Write-JVLog {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string]$Message,
[Parameter(Mandatory = $true, Position = 0)]
[String]$Message,

[Parameter(Mandatory = $true)]
[Parameter(Mandatory = $true, Position = 1)]
[ValidateSet('Debug', 'Info', 'Warning', 'Error')]
[string]$Level,
[String]$Level,

[Parameter()]
[AllowEmptyString()]
[String]$Write,

[Parameter()]
[AllowEmptyString()]
[String]$WriteLevel,

[Parameter()]
[AllowEmptyString()]
[String]$LogPath,

[Parameter()]
[ValidateSet('Break', 'Continue', 'Ignore', 'Inquire', 'SilentlyContinue', 'Stop', 'Suspend')]
[string]$Action = 'Stop'
[String]$Action = 'Stop'
)

$timeStamp = Get-Date -Format s

if ($Level -eq 'Debug') {
Write-Debug -Message $Message
Write-Log -Level $Level -Message $Message | Wait-Logging
if ($WriteLevel -eq 'Debug') {
$formattedMessage = "[$timeStamp][DEBUG] $Message"
}
Write-Debug -Message "$Message"
}

if ($Level -eq 'Info') {
if ($WriteLevel -eq 'Debug' -or $WriteLevel -eq 'Info') {
$formattedMessage = "[$timeStamp][INFO ] $Message"
}
Write-Verbose -Message $Message
Write-Log -Level $Level -Message $Message | Wait-Logging
}

if ($Level -eq 'Warning') {
if ($WriteLevel -eq 'Debug' -or $WriteLevel -eq 'Info' -or $WriteLevel -eq 'Warning') {
$formattedMessage = "[$timeStamp][WARN ] $Message"
}
Write-Warning -Message $Message
Write-Log -Level $Level -Message $Message | Wait-Logging
}

if ($Level -eq 'Error') {
Write-Log -Level $Level -Message $Message | Wait-Logging
if ($writeLevel -eq 'Debug' -or $WriteLevel -eq 'Info' -or $WriteLevel -eq 'Warning' -or $WriteLevel -eq 'Error') {
$formattedMessage = "[$timeStamp][ERROR] $Message"
}
Write-Error -Message $Message -ErrorAction $Action
}

if ($LogPath -ne '' -and $null -ne $LogPath) {
if ($formattedMessage -ne '' -and $null -ne $formattedMessage) {
if ($Write -eq 1) {
$LogMutex = New-Object System.Threading.Mutex($false, "LogMutex")
$LogMutex.WaitOne() | Out-Null
$formattedMessage | Out-File -FilePath $LogPath -Append
$LogMutex.ReleaseMutex() | Out-Null
}
}
}
}
6 changes: 3 additions & 3 deletions src/Javinizer/Public/Get-DmmData.ps1
Expand Up @@ -13,10 +13,10 @@ function Get-DmmData {
$dmmUrl = $Url

try {
Write-JVLog -Level Debug -Message "Performing [GET] on URL [$dmmUrl]"
Write-JVLog -Write:$script:JVLogWrite -LogPath $script:JVLogPath -WriteLevel $script:JVLogWriteLevel -Level Debug -Message "Performing [GET] on URL [$dmmUrl]"
$webRequest = Invoke-WebRequest -Uri $dmmUrl -Method Get -Verbose:$false
} catch {
Write-JVLog -Level Error -Message "Error [GET] on URL [$dmmUrl]: $PSItem"
Write-JVLog -Write:$script:JVLogWrite -LogPath $script:JVLogPath -WriteLevel $script:JVLogWriteLevel -Level Error -Message "Error [GET] on URL [$dmmUrl]: $PSItem"
}

$movieDataObject = [PSCustomObject]@{
Expand All @@ -41,7 +41,7 @@ function Get-DmmData {
#TrailerUrl = Get-DmmTrailerUrl -WebRequest $webRequest
}

Write-JVLog -Level Debug -Message "DMM data object: $($movieDataObject | ConvertTo-Json -Depth 32 -Compress)"
Write-JVLog -Write:$script:JVLogWrite -LogPath $script:JVLogPath -WriteLevel $script:JVLogWriteLevel -Level Debug -Message "DMM data object: $($movieDataObject | ConvertTo-Json -Depth 32 -Compress)"
Write-Output $movieDataObject
}
}

0 comments on commit 686faa9

Please sign in to comment.