Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: err handling on url/data requests (#199) #255

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/Javinizer/Private/Invoke-JVWebRequest.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
function Invoke-JVWebRequest {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, Position = 0)]
[String]$Uri,

[Parameter()]
[ValidateSet('Delete', 'Get', 'Head', 'Merge', 'Options', 'Patch', 'Post', 'Put', 'Trace')]
[String]$Method = 'Get',

[Parameter()]
[PSObject]$WebSession,

[Parameter()]
[String]$UserAgent,

[Parameter()]
[String]$Body,

[Parameter()]
[Switch]$RestMethod
)

process {
try {
Write-JVLog -Write:$script:JVLogWrite -LogPath $script:JVLogPath -WriteLevel $script:JVLogWriteLevel -Level Debug -Message "[$($MyInvocation.MyCommand.Name)] Performing [$Method] on URL [$Uri]"
if (-not ($RestMethod)) {
if (-not ($WebSession)) {
$request = Invoke-WebRequest -Uri $Uri -Method:$Method -Body:$Body -MaximumRetryCount 3 -RetryIntervalSec 2 -Verbose:$false
} elseif ($WebSession -and $UserAgent) {
$request = Invoke-WebRequest -Uri $Uri -Method:$Method -Body:$Body -WebSession:$WebSession -UserAgent:$UserAgent -MaximumRetryCount 3 -RetryIntervalSec 2 -Verbose:$false
} else {
$request = Invoke-WebRequest -Uri $Uri -Method:$Method -Body:$Body -WebSession:$WebSession -MaximumRetryCount 3 -RetryIntervalSec 2 -Verbose:$false
}
} else {
if (-not ($WebSession)) {
$request = Invoke-RestMethod -Uri $Uri -Method:$Method -Body:$Body -MaximumRetryCount 3 -RetryIntervalSec 2 -Verbose:$false
} elseif ($WebSession -and $UserAgent) {
$request = Invoke-RestMethod -Uri $Uri -Method:$Method -Body:$Body -WebSession:$WebSession -UserAgent:$UserAgent -MaximumRetryCount 3 -RetryIntervalSec 2 -Verbose:$false
} else {
$request = Invoke-RestMethod -Uri $Uri -Method:$Method -Body:$Body -WebSession:$WebSession -MaximumRetryCount 3 -RetryIntervalSec 2 -Verbose:$false
}
}
} catch {
Write-JVLog -Write:$script:JVLogWrite -LogPath $script:JVLogPath -WriteLevel $script:JVLogWriteLevel -Level Error -Message "[$($MyInvocation.MyCommand.Name)] Error [$Method] on URL [$Uri]" -Action Stop
}

Write-Output $request
}
}
11 changes: 3 additions & 8 deletions src/Javinizer/Public/Get-AVDanyuData.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@ function Get-AVDanyuData {

process {
$searchUrl = "https://avdanyuwiki.com/?s=$contentId"
try {
Write-JVLog -Write:$script:JVLogWrite -LogPath $script:JVLogPath -WriteLevel $script:JVLogWriteLevel -Level Debug -Message "[$ContentId] [$($MyInvocation.MyCommand.Name)] Performing [GET] on URL [$searchUrl]"
$webRequest = Invoke-WebRequest -Uri $searchUrl -Method Get -Verbose:$false
} catch {
Write-JVLog -Write:$script:JVLogWrite -LogPath $script:JVLogPath -WriteLevel $script:JVLogWriteLevel -Level Error -Message "[$ContentId] [$($MyInvocation.MyCommand.Name)] Error occured on [GET] on URL [$searchUrl]: $PSItem" -Action 'Continue'
}
$webRequest = Invoke-JVWebRequest -Uri $searchUrl -Method Get -Verbose:$false

if ($webRequest.Content -notmatch 'NOT FOUND') {
# This will retrieve and split all results displayed on the search page to parse
Expand Down Expand Up @@ -53,10 +48,10 @@ function Get-AVDanyuData {

Write-Output $matchedResult
} else {
Write-JVLog -Write:$script:JVLogWrite -LogPath $script:JVLogPath -WriteLevel $script:JVLogWriteLevel -Level Warning -Message "[$ContentId] [$($MyInvocation.MyCommand.Name)] not matched on avdanyuwiki"
Write-JVLog -Write:$script:JVLogWrite -LogPath $script:JVLogPath -WriteLevel $script:JVLogWriteLevel -Level Warning -Message "[$ContentId] not matched on avdanyuwiki"
}
} else {
Write-JVLog -Write:$script:JVLogWrite -LogPath $script:JVLogPath -WriteLevel $script:JVLogWriteLevel -Level Warning -Message "[$ContentId] [$($MyInvocation.MyCommand.Name)] not matched on avdanyuwiki"
Write-JVLog -Write:$script:JVLogWrite -LogPath $script:JVLogPath -WriteLevel $script:JVLogWriteLevel -Level Warning -Message "[$ContentId] not matched on avdanyuwiki"
}
}
}
8 changes: 1 addition & 7 deletions src/Javinizer/Public/Get-AventertainmentData.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,7 @@ function Get-AventertainmentData {

process {
$movieDataObject = @()

try {
Write-JVLog -Write:$script:JVLogWrite -LogPath $script:JVLogPath -WriteLevel $script:JVLogWriteLevel -Level Debug -Message "[$($MyInvocation.MyCommand.Name)] Performing [GET] on URL [$Url]"
$webRequest = Invoke-WebRequest -Uri $Url -Method Get -WebSession $Session -UserAgent $Session.UserAgent -Verbose:$false
} catch {
Write-JVLog -Write:$script:JVLogWrite -LogPath $script:JVLogPath -WriteLevel $script:JVLogWriteLevel -Level Error -Message "[$($MyInvocation.MyCommand.Name)] Error [GET] on URL [$Url]: $PSItem" -Action 'Continue'
}
$webRequest = Invoke-JVWebRequest -Uri $Url -Method Get -WebSession $Session -UserAgent $Session.UserAgent -Verbose:$false

$movieDataObject = [PSCustomObject]@{
Source = if ($Url -match 'languageID=1') { 'aventertainment' } else { 'aventertainmentja' }
Expand Down
64 changes: 9 additions & 55 deletions src/Javinizer/Public/Get-AventertainmentUrl.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,7 @@ function Get-AventertainmentUrl {

process {
$jaPpvUrl = "https://www.aventertainments.com/ppv/ppv_searchproducts.aspx?languageID=1&vodtypeid=1&keyword=$Id"

try {
Write-JVLog -Write:$script:JVLogWrite -LogPath $script:JVLogPath -WriteLevel $script:JVLogWriteLevel -Level Debug -Message "[$Id] [$($MyInvocation.MyCommand.Name)] Performing [GET] on URL [$jaPpvUrl]"
$webRequest = Invoke-WebRequest -Uri $jaPpvUrl -Method Get -WebSession $session -Verbose:$false
} catch {
Write-JVLog -Write:$script:JVLogWrite -LogPath $script:JVLogPath -WriteLevel $script:JVLogWriteLevel -Level Error -Message "[$Id] [$($MyInvocation.MyCommand.Name)] Error occured on [GET] on URL [$jaPpvUrl]: $PSItem" -Action 'Continue'
}
$webRequest = Invoke-JVWebRequest -Uri $jaPpvUrl -Method Get -WebSession $session -Verbose:$false

$searchResults = $webRequest.links.href | Where-Object { $_ -like '*new_detail*' } | Select-Object -Unique

Expand All @@ -28,13 +22,7 @@ function Get-AventertainmentUrl {

$count = 1
foreach ($result in $searchResults) {
try {
Write-JVLog -Write:$script:JVLogWrite -LogPath $script:JVLogPath -WriteLevel $script:JVLogWriteLevel -Level Debug -Message "[$Id] [$($MyInvocation.MyCommand.Name)] Performing [GET] on URL [$result]"
$webRequest = Invoke-WebRequest -Uri $result -WebSession:$Session -UserAgent:$Session.UserAgent -Method Get -Verbose:$false
} catch {
Write-JVLog -Write:$script:JVLogWrite -LogPath $script:JVLogPath -WriteLevel $script:JVLogWriteLevel -Level Error -Message "[$Id] [$($MyInvocation.MyCommand.Name)] Error occured on [GET] on URL [$result]: $PSItem" -Action 'Continue'
}

$webRequest = Invoke-JVWebRequest -Uri $result -WebSession:$Session -UserAgent:$Session.UserAgent -Method Get -Verbose:$false
$resultId = Get-AventertainmentId -WebRequest $webRequest

try {
Expand All @@ -60,13 +48,7 @@ function Get-AventertainmentUrl {

if ($null -eq $enAventertainmentUrl) {
$enPpvUrl = "https://www.aventertainments.com/ppv/ppv_searchproducts.aspx?languageID=1&vodtypeid=2&keyword=$Id"

try {
Write-JVLog -Write:$script:JVLogWrite -LogPath $script:JVLogPath -WriteLevel $script:JVLogWriteLevel -Level Debug -Message "[$Id] [$($MyInvocation.MyCommand.Name)] Performing [GET] on URL [$enPpvUrl]"
$webRequest = Invoke-WebRequest -Uri $enPpvUrl -Method Get -WebSession $session -Verbose:$false
} catch {
Write-JVLog -Write:$script:JVLogWrite -LogPath $script:JVLogPath -WriteLevel $script:JVLogWriteLevel -Level Error -Message "[$Id] [$($MyInvocation.MyCommand.Name)] Error occured on [GET] on URL [$enPpvUrl]: $PSItem" -Action 'Continue'
}
$webRequest = Invoke-JVWebRequest -Uri $enPpvUrl -Method Get -WebSession $session -Verbose:$false

$searchResults = $webRequest.links.href | Where-Object { $_ -like '*new_detail*' } | Select-Object -Unique

Expand All @@ -80,13 +62,7 @@ function Get-AventertainmentUrl {

$count = 1
foreach ($result in $searchResults) {
try {
Write-JVLog -Write:$script:JVLogWrite -LogPath $script:JVLogPath -WriteLevel $script:JVLogWriteLevel -Level Debug -Message "[$Id] [$($MyInvocation.MyCommand.Name)] Performing [GET] on URL [$result]"
$webRequest = Invoke-WebRequest -Uri $result -WebSession:$Session -UserAgent:$Session.UserAgent -Method Get -Verbose:$false
} catch {
Write-JVLog -Write:$script:JVLogWrite -LogPath $script:JVLogPath -WriteLevel $script:JVLogWriteLevel -Level Error -Message "[$Id] [$($MyInvocation.MyCommand.Name)] Error occured on [GET] on URL [$result]: $PSItem" -Action 'Continue'
}

$webRequest = Invoke-JVWebRequest -Uri $result -WebSession:$Session -UserAgent:$Session.UserAgent -Method Get -Verbose:$false
$resultId = Get-AventertainmentId -WebRequest $webRequest

try {
Expand All @@ -113,13 +89,7 @@ function Get-AventertainmentUrl {

if ($null -eq $enAventertainmentUrl) {
$jaDvdUrl = "https://www.aventertainments.com/search_Products.aspx?languageID=1&dept_id=29&keyword=$Id&searchby=keyword"

try {
Write-JVLog -Write:$script:JVLogWrite -LogPath $script:JVLogPath -WriteLevel $script:JVLogWriteLevel -Level Debug -Message "[$Id] [$($MyInvocation.MyCommand.Name)] Performing [GET] on URL [$jaDvdUrl]"
$webRequest = Invoke-WebRequest -Uri $jaDvdUrl -Method Get -WebSession $session -Verbose:$false
} catch {
Write-JVLog -Write:$script:JVLogWrite -LogPath $script:JVLogPath -WriteLevel $script:JVLogWriteLevel -Level Error -Message "[$Id] [$($MyInvocation.MyCommand.Name)] Error occured on [GET] on URL [$jaDvdUrl]: $PSItem" -Action 'Continue'
}
$webRequest = Invoke-JVWebRequest -Uri $jaDvdUrl -Method Get -WebSession $session -Verbose:$false

$searchResults = $webRequest.links.href | Where-Object { $_ -like '*product_lists*' } | Select-Object -Unique

Expand All @@ -133,12 +103,7 @@ function Get-AventertainmentUrl {

$count = 1
foreach ($result in $searchResults) {
try {
Write-JVLog -Write:$script:JVLogWrite -LogPath $script:JVLogPath -WriteLevel $script:JVLogWriteLevel -Level Debug -Message "[$Id] [$($MyInvocation.MyCommand.Name)] Performing [GET] on URL [$result]"
$webRequest = Invoke-WebRequest -Uri $result -WebSession:$Session -UserAgent:$Session.UserAgent -Method Get -Verbose:$false
} catch {
Write-JVLog -Write:$script:JVLogWrite -LogPath $script:JVLogPath -WriteLevel $script:JVLogWriteLevel -Level Error -Message "[$Id] [$($MyInvocation.MyCommand.Name)] Error occured on [GET] on URL [$result]: $PSItem" -Action 'Continue'
}
$webRequest = Invoke-JVWebRequest -Uri $result -WebSession:$Session -UserAgent:$Session.UserAgent -Method Get -Verbose:$false

$resultId = Get-AventertainmentId -WebRequest $webRequest

Expand Down Expand Up @@ -166,13 +131,7 @@ function Get-AventertainmentUrl {

if ($null -eq $enAventertainmentUrl) {
$enDvdUrl = "https://www.aventertainments.com/search_Products.aspx?languageID=1&dept_id=43&keyword=$Id&searchby=keyword"

try {
Write-JVLog -Write:$script:JVLogWrite -LogPath $script:JVLogPath -WriteLevel $script:JVLogWriteLevel -Level Debug -Message "[$Id] [$($MyInvocation.MyCommand.Name)] Performing [GET] on URL [$enDvdUrl]"
$webRequest = Invoke-WebRequest -Uri $enDvdUrl -Method Get -WebSession $session -Verbose:$false
} catch {
Write-JVLog -Write:$script:JVLogWrite -LogPath $script:JVLogPath -WriteLevel $script:JVLogWriteLevel -Level Error -Message "[$Id] [$($MyInvocation.MyCommand.Name)] Error occured on [GET] on URL [$enDvdUrl]: $PSItem" -Action 'Continue'
}
$webRequest = Invoke-JVWebRequest -Uri $enDvdUrl -Method Get -WebSession $session -Verbose:$false

$searchResults = $webRequest.links.href | Where-Object { $_ -like '*product_lists*' } | Select-Object -Unique

Expand All @@ -186,12 +145,7 @@ function Get-AventertainmentUrl {

$count = 1
foreach ($result in $searchResults) {
try {
Write-JVLog -Write:$script:JVLogWrite -LogPath $script:JVLogPath -WriteLevel $script:JVLogWriteLevel -Level Debug -Message "[$Id] [$($MyInvocation.MyCommand.Name)] Performing [GET] on URL [$result]"
$webRequest = Invoke-WebRequest -Uri $result -WebSession:$Session -UserAgent:$Session.UserAgent -Method Get -Verbose:$false
} catch {
Write-JVLog -Write:$script:JVLogWrite -LogPath $script:JVLogPath -WriteLevel $script:JVLogWriteLevel -Level Error -Message "[$Id] [$($MyInvocation.MyCommand.Name)] Error occured on [GET] on URL [$result]: $PSItem" -Action 'Continue'
}
$webRequest = Invoke-JVWebRequest -Uri $result -WebSession:$Session -UserAgent:$Session.UserAgent -Method Get -Verbose:$false

$resultId = Get-AventertainmentId -WebRequest $webRequest

Expand All @@ -218,7 +172,7 @@ function Get-AventertainmentUrl {
}

if ($null -eq $enAventertainmentUrl) {
Write-JVLog -Write:$script:JVLogWrite -LogPath $script:JVLogPath -WriteLevel $script:JVLogWriteLevel -Level Warning -Message "[$Id] [$($MyInvocation.MyCommand.Name)] not matched on AVEntertainment"
Write-JVLog -Write:$script:JVLogWrite -LogPath $script:JVLogPath -WriteLevel $script:JVLogWriteLevel -Level Warning -Message "[$Id] not matched on AVEntertainment"
return
} else {
$urlObject = [PSCustomObject]@{
Expand Down
2 changes: 1 addition & 1 deletion src/Javinizer/Public/Get-DLgetchuData.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function Get-DLgetchuData {
Write-JVLog -Write:$script:JVLogWrite -LogPath $script:JVLogPath -WriteLevel $script:JVLogWriteLevel -Level Warning -Message "[$($MyInvocation.MyCommand.Name)] Not found on DLgetchu [$Url]"
continue
} catch {
Write-JVLog -Write:$script:JVLogWrite -LogPath $script:JVLogPath -WriteLevel $script:JVLogWriteLevel -Level Error -Message "[$($MyInvocation.MyCommand.Name)] Error [GET] on URL [$Url]: $PSItem" -Action 'Continue'
Write-JVLog -Write:$script:JVLogWrite -LogPath $script:JVLogPath -WriteLevel $script:JVLogWriteLevel -Level Error -Message "[$($MyInvocation.MyCommand.Name)] Error [GET] on URL [$Url]: $PSItem" -Action Stop
}

$movieDataObject = [PSCustomObject]@{
Expand Down
10 changes: 1 addition & 9 deletions src/Javinizer/Public/Get-DmmData.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,7 @@ function Get-DmmData {
$session.Cookies.Add($cookie)
}

try {
Write-JVLog -Write:$script:JVLogWrite -LogPath $script:JVLogPath -WriteLevel $script:JVLogWriteLevel -Level Debug -Message "[$($MyInvocation.MyCommand.Name)] Performing [GET] on URL [$Url]"
$webRequest = Invoke-WebRequest -Uri $Url -WebSession $session -Method Get -Verbose:$false
} catch [Microsoft.PowerShell.Commands.HttpResponseException] {
Write-JVLog -Write:$script:JVLogWrite -LogPath $script:JVLogPath -WriteLevel $script:JVLogWriteLevel -Level Warning -Message "[$($MyInvocation.MyCommand.Name)] Not found on DMM [$Url]"
continue
} catch {
Write-JVLog -Write:$script:JVLogWrite -LogPath $script:JVLogPath -WriteLevel $script:JVLogWriteLevel -Level Error -Message "[$($MyInvocation.MyCommand.Name)] Error [GET] on URL [$Url]: $PSItem" -Action 'Continue'
}
$webRequest = Invoke-JVWebRequest -Uri $Url -WebSession $session -Method Get -Verbose:$false

$movieDataObject = [PSCustomObject]@{
Source = if ($Url -match '/en/') { 'dmm' } else { 'dmmja' }
Expand Down
4 changes: 2 additions & 2 deletions src/Javinizer/Public/Get-DmmUrl.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function Get-DmmUrl {
}
} #>

$r18Results = Get-R18Url -Id $Id -Strict:$Strict -AllResults -WarningAction SilentlyContinue
$r18Results = Get-R18Url -Id $Id -Strict:$Strict -AllResults -WarningAction SilentlyContinue -Verbose:$false
$resultObject = foreach ($entry in $r18Results) {
$cid = (($entry.En -split 'id=')[1] -split '\/')[0]
[PSCustomObject]@{
Expand Down Expand Up @@ -58,7 +58,7 @@ function Get-DmmUrl {

Write-Output $urlObject
} else {
Write-JVLog -Write:$script:JVLogWrite -LogPath $script:JVLogPath -WriteLevel $script:JVLogWriteLevel -Level Warning -Message "[$Id] [$($MyInvocation.MyCommand.Name)] not matched on DMM"
Write-JVLog -Write:$script:JVLogWrite -LogPath $script:JVLogPath -WriteLevel $script:JVLogWriteLevel -Level Warning -Message "[$Id] not matched on DMM"
return
}
}
Expand Down
Loading