-
Notifications
You must be signed in to change notification settings - Fork 61
/
Get-DmmUrl.ps1
79 lines (67 loc) · 2.92 KB
/
Get-DmmUrl.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
function Get-DmmUrl {
[CmdletBinding()]
param (
[Parameter(Position = 0, ValueFromPipeline = $true)]
[string]$Id,
[Parameter()]
[string]$r18Url
)
process {
if ($r18Url) {
$r18Id = (($r18Url -split 'id=')[1] -split '\/')[0]
$directUrl = "https://www.dmm.co.jp/digital/videoa/-/detail/=/cid=$r18Id"
Write-JLog -Level Debug -Message "Converting R18 Id to Dmm: [$r18Id] -> [$directUrl]"
} else {
# 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 '-'
$Id = $splitId[0] + $splitId[1].PadLeft(5, '0')
}
$searchUrl = "https://www.dmm.co.jp/search/?redirect=1&enc=UTF-8&category=&searchstr=$Id"
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]"
}
$retryCount = 3
$searchResults = ($webrequest.links.href | Where-Object { $_ -like '*digital/videoa/*' })
$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-DmmContentId -WebRequest $webRequest
Write-JLog -Level Debug -Message "Result [$count] is [$resultId]"
if ($resultId -match $Id) {
$directUrl = $result
break
}
if ($count -eq $retryCount) {
break
}
$count++
}
}
}
if ($null -eq $directUrl) {
Write-JLog -Level Warning -Message "Search [$Id] not matched on DMM"
return
} else {
$urlObject = [PSCustomObject]@{
Url = $directUrl
Language = 'ja'
}
Write-Output $urlObject
}
}
}