-
Notifications
You must be signed in to change notification settings - Fork 61
/
Set-JVEmbyThumbs.ps1
133 lines (113 loc) · 7.28 KB
/
Set-JVEmbyThumbs.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
#Requires -PSEdition Core
function Set-JVEmbyThumbs {
[CmdletBinding(SupportsShouldProcess = $true)]
param (
[Parameter(Mandatory = $true, Position = 0, ValueFromPipelineByPropertyName = $true)]
[Alias('emby.url')]
[String]$Url,
[Parameter(Mandatory = $true, Position = 1, ValueFromPipelineByPropertyName = $true)]
[Alias('emby.apikey')]
[String]$ApiKey,
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
[Alias('sort.metadata.nfo.firstnameorder')]
[Boolean]$FirstNameOrder,
[Parameter()]
[System.IO.FileInfo]$ThumbCsvPath = (Join-Path -Path ((Get-Item $PSScriptRoot).Parent) -ChildPath 'jvThumbs.csv'),
[Parameter()]
[Switch]$ReplaceAll
)
begin {
if (-not $PSBoundParameters.ContainsKey('Confirm')) {
$ConfirmPreference = $PSCmdlet.SessionState.PSVariable.GetValue('ConfirmPreference')
}
if (-not $PSBoundParameters.ContainsKey('WhatIf')) {
$WhatIfPreference = $PSCmdlet.SessionState.PSVariable.GetValue('WhatIfPreference')
}
}
process {
if ($Url[-1] -eq '/') {
# Remove the trailing slash if it is included to create the valid searchUrl
$Url = $BaseUrl[0..($BaseUrl.Length - 1)] -join ''
}
try {
$actressUrl = "$Url/emby/Persons/?api_key=$ApiKey"
Write-JVLog -Write:$script:JVLogWrite -LogPath $script:JVLogPath -WriteLevel $script:JVLogWriteLevel -Level Debug -Message "[$($MyInvocation.MyCommand.Name)] Performing [GET] on URL [$actressUrl]"
$embyActress = (Invoke-RestMethod -Method Get -Uri $actressUrl -ErrorAction Stop -Verbose:$false).Items | Select-Object Name, Id, ImageTags
} catch {
Write-JVLog -Write:$script:JVLogWrite -LogPath $script:JVLogPath -WriteLevel $script:JVLogWriteLevel -Level Error -Message "[$($MyInvocation.MyCommand.Name)] Error occurred when getting actresses from Emby: $PSItem"
}
try {
Write-JVLog -Write:$script:JVLogWrite -LogPath $script:JVLogPath -WriteLevel $script:JVLogWriteLevel -Level Debug -Message "[$($MyInvocation.MyCommand.Name)] [ActressCsv - $ThumbCsvPath] imported"
$actressCsv = Import-Csv -LiteralPath $ThumbCsvPath -ErrorAction Stop
} catch {
Write-JVLog -Write:$script:JVLogWrite -LogPath $script:JVLogPath -WriteLevel $script:JVLogWriteLevel -Level Error -Message "[$($MyInvocation.MyCommand.Name)] Error occurred when importing thumbnail csv [$ThumbCsvPath]: $PSItem"
}
if ($ReplaceAll) {
$toDoActress = $embyActress
} else {
$toDoActress = ($embyActress | Where-Object { $null -eq $_.ImageTags.Thumb -and $null -eq $_.ImageTags.Primary })
}
Write-Host "[$($MyInvocation.MyCommand.Name)] [Url - $Url] [Actress without thumbs - $($toDoActress.Count)]"
foreach ($actress in $toDoActress) {
$matched = $null
$thumbUrl = $null
if ($actress.Name -match '[\u3040-\u309f]|[\u30a0-\u30ff]|[\uff66-\uff9f]|[\u4e00-\u9faf]') {
$actressObject = [PSCustomObject]@{
JapaneseName = $actress.Name
}
if ($matched = Compare-Object -ReferenceObject $actressCsv -DifferenceObject $actressObject -IncludeEqual -ExcludeDifferent -PassThru -Property @('JapaneseName')) {
if ($matched.Count -eq 1) {
$thumbUrl = $matched.ThumbUrl
} elseif ($matched.Count -gt 1) {
$thumbUrl = $matched[0].ThumbUrl
}
}
} else {
if ($FirstNameOrder) {
$actressObject = [PSCustomObject]@{
LastName = ($actress.Name -split ' ')[1]
FirstName = ($actress.name -split ' ')[0]
}
if ($matched = Compare-Object -ReferenceObject $actressCsv -DifferenceObject $actressObject -IncludeEqual -ExcludeDifferent -PassThru -Property @('FirstName', 'LastName')) {
if ($matched.Count -eq 1) {
$thumbUrl = $matched.ThumbUrl
} elseif ($matched.Count -gt 1) {
$thumbUrl = $matched[0].ThumbUrl
}
}
} else {
$actressObject = [PSCustomObject]@{
LastName = ($actress.Name -split ' ')[0]
FirstName = ($actress.name -split ' ')[1]
}
if ($matched = Compare-Object -ReferenceObject $actressCsv -DifferenceObject $actressObject -IncludeEqual -ExcludeDifferent -PassThru -Property @('FirstName', 'LastName')) {
if ($matched.Count -eq 1) {
$thumbUrl = $matched.ThumbUrl
} elseif ($matched.Count -gt 1) {
$thumbUrl = $matched[0].ThumbUrl
}
}
}
}
if ($null -ne $thumbUrl) {
if ($PSCmdlet.ShouldProcess("[$($actress.Name)] => [$thumbUrl]")) {
$thumbPostUrl = "$Url/emby/Items/$($actress.Id)/RemoteImages/Download?Type=Thumb&ImageUrl=$thumbUrl&api_key=$ApiKey"
try {
Write-JVLog -Write:$script:JVLogWrite -LogPath $script:JVLogPath -WriteLevel $script:JVLogWriteLevel -Level Debug -Message "Performing [POST] on URL [$thumbPostUrl]"
Invoke-RestMethod -Method Post -Uri "$Url/emby/Items/$($actress.Id)/RemoteImages/Download?Type=Thumb&ImageUrl=$thumbUrl&api_key=$ApiKey" -ErrorAction Continue -Verbose:$false | Out-Null
} catch {
Write-JVLog -Write:$script:JVLogWrite -LogPath $script:JVLogPath -WriteLevel $script:JVLogWriteLevel -Level Error -Message "[$($MyInvocation.MyCommand.Name)] Error occurred on [POST] on URL [$thumbPostUrl]: $PSItem" -Action 'Continue'
}
$primaryPostUrl = "$Url/emby/Items/$($actress.Id)/RemoteImages/Download?Type=Primary&ImageUrl=$thumbUrl&api_key=$ApiKey"
try {
Write-JVLog -Write:$script:JVLogWrite -LogPath $script:JVLogPath -WriteLevel $script:JVLogWriteLevel -Level Debug -Message "Performing [POST] on URL [$primaryPostUrl]"
Invoke-RestMethod -Method Post -Uri "$Url/emby/Items/$($actress.Id)/RemoteImages/Download?Type=Primary&ImageUrl=$thumbUrl&api_key=$ApiKey" -ErrorAction Continue -Verbose:$false | Out-Null
} catch {
Write-JVLog -Write:$script:JVLogWrite -LogPath $script:JVLogPath -WriteLevel $script:JVLogWriteLevel -Level Error -Message "[$($MyInvocation.MyCommand.Name)] Error occurred on [POST] on URL [$primaryPostUrl]: $PSItem" -Action 'Continue'
}
Write-JVLog -Write:$script:JVLogWrite -LogPath $script:JVLogPath -WriteLevel $script:JVLogWriteLevel -Level Info -Message "Set [$($actress.Name)] => [$thumbUrl]"
}
}
}
}
}