-
Notifications
You must be signed in to change notification settings - Fork 61
/
Get-R18Url.ps1
156 lines (130 loc) · 5.93 KB
/
Get-R18Url.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
function Get-R18Url {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
[String]$Id,
[Parameter(Mandatory = $true, Position = 1)]
[ValidateSet('en', 'zh')]
[String]$Language
)
process {
$searchResults = @()
$altSearchResults = @()
$searchUrl = "https://www.r18.com/common/search/searchword=$Id/"
# If contentId is given, convert it back to standard movie ID to validate
if ($Id -match '(?:\d{1,5})?([a-zA-Z]{2,10}|[tT]28|[rR]18)(\d{1,5})') {
Write-JLog -Level Debug -Message "Content ID [$Id] detected"
$splitId = $Id | Select-String -Pattern '([a-zA-Z|tT28|rR18]{1,10})(\d{1,5})'
$studioName = $splitId.Matches.Groups[1].Value
$rawStudioId = $splitId.Matches.Groups[2].Value
$studioIdIndex = ($rawStudioId | Select-String -Pattern '[1-9]').Matches.Index
$studioId = ($rawStudioId[$studioIdIndex..($rawStudioId.Length - 1)] -join '').PadLeft(3, '0')
$Id = "$($studioName.ToUpper())-$studioId"
}
# Convert the movie Id (ID-###) to content Id (ID00###) to match dmm naming standards
if ($Id -match '([a-zA-Z|tT28|rR18]+-\d+z{0,1}Z{0,1}e{0,1}E{0,1})') {
$splitId = $Id -split '-'
$contentId = $splitId[0] + $splitId[1].PadLeft(5, '0')
}
# Try matching the video with Video ID
try {
Write-JLog -Level Debug -Message "Performing [GET] on Uri [$searchUrl]"
$webRequest = Invoke-WebRequest -Uri $searchUrl -Method Get -Verbose:$false
} catch {
Write-JLog -Level Error -Message "Error [GET] on URL [$searchUrl]: $PSItem"
}
$retryCount = 3
$searchResults = (($webRequest.Links | Where-Object { $_.href -like "*/videos/vod/*/detail/-/id=*" }).href)
$numResults = $searchResults.count
if ($retryCount -gt $numResults) {
$retryCount = $numResults
}
if ($numResults -ge 1) {
Write-JLog -Level Debug -Message "Searching [$retryCount] of [$numResults] results for [$Id]"
$count = 1
foreach ($result in $searchResults) {
try {
Write-JLog -Level Debug -Message "Performing [GET] on URL [$result]"
$webRequest = Invoke-WebRequest -Uri $result -Method Get -Verbose:$false
} catch {
Write-JLog -Level Error -Message "Error [GET] on URL [$result]: $PSItem"
}
$resultId = Get-R18Id -WebRequest $webRequest
Write-JLog -Level Debug -Message "Result [$count] is [$resultId]"
if ($resultId -eq $Id) {
$directUrl = $result
break
}
if ($count -eq $retryCount) {
break
}
$count++
}
}
# If not matched by Video ID, try matching the video with Content ID
if ($null -eq $directUrl) {
$searchUrl = "https://www.r18.com/common/search/searchword=$contentId/"
try {
Write-JLog -Level Debug -Message "Performing [GET] on URL [$searchUrl]"
$webRequest = Invoke-WebRequest -Uri $searchUrl -Method Get -Verbose:$false
} catch {
Write-JLog -Level Error -Message "Error [GET] on URL [$searchUrl]: $PSItem"
}
$retryCount = 5
$altSearchResults = (($webRequest.Links | Where-Object { $_.href -like "*/videos/vod/movies/detail/-/id=*" }).href)
$numResults = $altSearchResults.count
if ($retryCount -gt $numResults) {
$retryCount = $numResults
}
$count = 1
foreach ($result in $altSearchResults) {
try {
Write-JLog -Level Debug -Message "Performing [GET] on URL [$result]"
$webRequest = Invoke-WebRequest -Uri $result -Method Get -Verbose:$false
} catch {
Write-JLog -Level Error -Message "Error [GET] on URL [$result]: $PSItem"
}
$resultId = Get-R18Id -WebRequest $webRequest
Write-JLog -Level Debug -Message "Result [$count] is [$resultId]"
if ($resultId -eq $Id) {
$directUrl = $result
break
}
if ($count -eq $retryCount) {
break
}
$count++
}
}
# If not matched by Video ID or Content ID, try matching the video with generic R18 URL
if ($null -eq $directUrl) {
$testUrl = "https://www.r18.com/videos/vod/movies/detail/-/id=$contentId/"
try {
Write-JLog -Level Debug -Message "Performing [GET] on Uri [$testUrl]"
$webRequest = Invoke-WebRequest -Uri $testUrl -Method Get -Verbose:$false
} catch {
$webRequest = $null
}
if ($null -ne $webRequest) {
$resultId = Get-R18Id -WebRequest $webRequest
Write-JLog -Level Debug -Message "Result is [$resultId]"
if ($resultId -eq $Id) {
$directUrl = $testUrl
}
}
}
if ($null -eq $directUrl) {
Write-JLog -Level Warning -Message "Search [$Id] not matched on R18"
return
} else {
if ($Language -eq 'zh') {
$directUrl = $directUrl + '&lg=zh'
}
$urlObject = [PSCustomObject]@{
Url = $directUrl
Language = $Language
}
Write-Output $urlObject
}
}
}