-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMonitorMediaServer.ps1
1284 lines (1074 loc) · 55.2 KB
/
MonitorMediaServer.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<#
.SYNOPSIS
Monitor and manage a Plex Media Server
.DESCRIPTION
Monitor services and managed Plex video content
.NOTES
.LINK
https://github.com/PowerShellCrack/PSPMSMAM
#>
#*=============================================
##* Runtime Function - REQUIRED
##*=============================================
#Requires -Modules Posh-SSH,WinSCP
#region FUNCTION: Check if running in ISE
Function Test-IsISE {
# try...catch accounts for:
# Set-StrictMode -Version latest
try {
return ($null -ne $psISE);
}
catch {
return $false;
}
}
#endregion
#region FUNCTION: Check if running in Visual Studio Code
Function Test-VSCode{
if($env:TERM_PROGRAM -eq 'vscode') {
return $true;
}
Else{
return $false;
}
}
#endregion
#region FUNCTION: Find script path for either ISE or console
Function Get-ScriptPath {
<#
.SYNOPSIS
Finds the current script path even in ISE or VSC
.LINK
Test-VSCode
Test-IsISE
#>
param(
[switch]$Parent
)
Begin{}
Process{
if ($PSScriptRoot -eq "")
{
if (Test-IsISE)
{
$ScriptPath = $psISE.CurrentFile.FullPath
}
elseif(Test-VSCode){
$context = $psEditor.GetEditorContext()
$ScriptPath = $context.CurrentFile.Path
}Else{
$ScriptPath = (Get-location).Path
}
}
else
{
$ScriptPath = $PSCommandPath
}
}
End{
If($Parent){
Split-Path $ScriptPath -Parent
}Else{
$ScriptPath
}
}
}
#endregion
Function Resolve-ActualPath{
[CmdletBinding()]
param(
[string]$FileName,
[string]$WorkingPath,
[Switch]$Parent
)
## Get the name of this function
[string]${CmdletName} = $MyInvocation.MyCommand
Write-Verbose ("Attempting to resolve filename: {0}" -f $FileName)
If(Resolve-Path $FileName -ErrorAction SilentlyContinue){
$FullPath = Resolve-Path $FileName
}
#If unable to resolve the file path try building path from workign path location
Else{
$FullPath = Join-Path -Path $WorkingPath -ChildPath $FileName
}
Write-Verbose ("Attempting to resolve with full path: {0}" -f $FullPath)
#Try to resolve the path one more time using the fullpath set
Try{
$ResolvedPath = Resolve-Path $FullPath -ErrorAction $ErrorActionPreference
}
Catch{
Write-Verbose ("Unable to resolve path: {0}: {1}" -f $FullPath,$_.Exception.Message)
Throw ("{0}" -f $_.Exception.Message)
}
Finally{
If($Parent){
$Return = Split-Path $ResolvedPath -Parent
}Else{
$Return = $ResolvedPath
}
$Return
}
}
##*=============================================
##* VARIABLE DECLARATION
##*=============================================
#region VARIABLES: Building paths & values
# Use function to get paths because Powershell ISE & other editors have differnt results
[string]$scriptPath = Get-ScriptPath
[string]$scriptName = [IO.Path]::GetFileNameWithoutExtension($scriptPath)
[string]$scriptRoot = Split-Path -Path $scriptPath -Parent
#Get required folder and File paths
[string]$ExtensionPath = Join-Path -Path $scriptRoot -ChildPath 'Extensions'
[string]$HelpersPath = Join-Path -Path $scriptRoot -ChildPath 'Helpers'
[string]$ConfigPath = Join-Path -Path $scriptRoot -ChildPath 'Configs'
[string]$AssemblyPath = Join-Path -Path $scriptRoot -ChildPath 'Assembly'
[string]$LogDir = Join-Path $scriptRoot -ChildPath 'Logs'
[string]$StoredDataDir = Join-Path $scriptRoot -ChildPath 'StoredData'
##*===============================================
##* EXTENSIONS
##*===============================================
#Import Script extensions
. "$ExtensionPath\Logging.ps1"
. "$ExtensionPath\SupportFunctions.ps1"
. "$ExtensionPath\VideoParser.ps1"
. "$ExtensionPath\HttpAPI.ps1"
. "$ExtensionPath\INIAPI.ps1"
. "$ExtensionPath\ImdbMovieAPI.ps1"
. "$ExtensionPath\TmdbAPI.ps1"
. "$ExtensionPath\TauTulliAPI.ps1"
. "$ExtensionPath\RadarrAPI.ps1"
. "$ExtensionPath\PlexAPI.ps1"
. "$ExtensionPath\XmlRpc.ps1"
#import helpers
. "$HelpersPath\CleanFolders.ps1"
. "$HelpersPath\MovieSearch.ps1"
$LogfileName = "$($scriptName)_$(Get-Date -Format 'yyyy-MM-dd_Thh-mm-ss-tt').log"
Try{Start-transcript "$LogDir\$LogfileName" -ErrorAction Stop}catch{Start-Transcript "$PSScriptRoot\$LogfileName"}
# PARSE MAIN CONFIG FILE
#=================================================
[string]$AutomationXMLContent = (Get-Content "$ConfigPath\MediaServer.xml" -ReadCount 0) -replace "&","&"
[Xml.XmlDocument]$AutomationConfigFile = $AutomationXMLContent
[Xml.XmlElement]$GlobalSettings = $AutomationConfigFile.AutomationConfig
[Boolean]$SendCriticalReport = [Boolean]::Parse($AutomationConfigFile.AutomationConfig.SendCriticalReport)
$ServicesToCheck = $GlobalSettings.ServiceCheck.service
$WebsToCheck = $GlobalSettings.WebCheck.Web
$ProcessesToCheck = $GlobalSettings.ProcessCheck.process
[Boolean]$IgnoreResillioSync = [Boolean]::Parse($GlobalSettings.Resillio_IgnoreSync)
[Boolean]$EnableResillioCheck = [Boolean]::Parse($GlobalSettings.Resillio_CheckForFiles)
$ResillioSyncPath = $GlobalSettings.Resillio_FolderLocation
# PARSE PLEX CONFIG FILE
#=================================================
[string]$PlexConfigFile = $GlobalSettings.SourceConfigs.Config | Where Id -eq 'Plex' | Select -ExpandProperty ConfigFile
[Xml.XmlDocument]$PlexXMLContent = (Get-Content "$ConfigPath\$PlexConfigFile" -ReadCount 0) -replace "&","&"
## Variables: Toolkit Name
[string]$PlexScriptName = $PlexXMLContent.PlexConfigs.PlexScriptConfigs.Name
[string]$PlexScriptFriendlyName = $PlexXMLContent.PlexConfigs.PlexScriptConfigs.FriendlyName
## Variables: Script Info
[version]$PlexScriptVersion = [version]$PlexXMLContent.PlexConfigs.PlexScriptConfigs.Version
#generate new guid if version change: new-guid
[guid]$PlexScriptGUID = $PlexXMLContent.PlexConfigs.PlexScriptConfigs.GUID
[string]$plexScriptDate = $PlexXMLContent.PlexConfigs.PlexScriptConfigs.Date
[hashtable]$plexScriptParameters = $PSBoundParameters
[string]$PlexURLType = $PlexXMLContent.PlexConfigs.UseURLType
switch($PlexURLType){
"External" {$PlexURL = $PlexXMLContent.PlexConfigs.ExternalURL}
"Internal" {$PlexURL = $PlexXMLContent.PlexConfigs.InternalURL}
}
$PlexCredsFile = Resolve-ActualPath -FileName $PlexXMLContent.PlexConfigs.UserCredentials -WorkingPath $ConfigPath -ErrorAction SilentlyContinue
$PlexCreds = Import-Clixml $PlexCredsFile.Path
$PlexUser = $PlexCreds.UserName
$PlexPassword = $PlexCreds.GetNetworkCredential().Password
$PlexAuthToken = Get-PlexAuthToken -PlexUsername $PlexUser -PlexPassword $PlexPassword
# PARSE RADARR CONFIG FILE
#=================================================
[string]$RadarrConfigFile = $GlobalSettings.SourceConfigs.Config | Where Id -eq 'Radarr' | Select -ExpandProperty ConfigFile
[Xml.XmlDocument]$RadarrXMLContent = (Get-Content "$ConfigPath\$RadarrConfigFile" -ReadCount 0) -replace "&","&"
[Xml.XmlElement]$RadarrConfigs = $RadarrXMLContent.RadarrAutomation.RadarrConfigs
[Xml.XmlElement]$RadarrSettings = $RadarrXMLContent.RadarrAutomation.GlobalSettings
[Xml.XmlElement]$NewMovieConfigs = $RadarrXMLContent.RadarrAutomation.MovieConfigs
[string]$Global:RadarrURL = $RadarrConfigs.InternalURL
[string]$Global:RadarrPort = $RadarrConfigs.Port
[string]$Global:RadarrAPIkey = $RadarrConfigs.API
[string]$OMDBAPI = $RadarrSettings.OMDBAPI
[string]$TMDBAPI = $RadarrSettings.TMDBAPI
[string[]]$VideoExtensions = $RadarrSettings.VideoExtensions.ext -split ','
[string[]]$VideoSupportFiles = $RadarrSettings.VideoSupportFiles.ext -split ','
[string]$SupportedLanguages = ($RadarrSettings.VideoSupportFiles.languages).split(',') -join '|'
[string]$MoviesDir = $RadarrSettings.MoviesRootPath
[Boolean]$SendChangeReport = [Boolean]::Parse($RadarrSettings.SendChangeReport)
[string]$FilterSeriesFolders = ($RadarrSettings.MovieSeriesFolders.Folder) -join "|"
[string]$FilterOtherFolders = ($RadarrSettings.FilterMovieSubFolders.Folder) -join "|"
[string]$FilterFileNames = ($RadarrSettings.VideoIgnoreFileNames.name).split(',') -join '|'
# Update Data Configs
[boolean]$ProcessMovies = -Not([boolean]::Parse($RadarrSettings.CheckStatusOnly))
[Boolean]$ProcessRequestedMovies = [Boolean]::Parse($NewMovieConfigs.MovetoGenreFolder)
[string]$MovieRequestsPath = $NewMovieConfigs.MovieRequestedMovePath
[string]$DownloadedMoviePath = $NewMovieConfigs.DownloadedMoviePath
# PARSE TAUTULLI CONFIG FILE
#=================================================
[string]$TautulliConfigFile = $GlobalSettings.SourceConfigs.Config | Where Id -eq 'Tautulli' | Select -ExpandProperty ConfigFile
[Xml.XmlDocument]$TautulliXMLContent = (Get-Content "$ConfigPath\$TautulliConfigFile" -ReadCount 0) -replace "&","&"
[Xml.XmlElement]$TautulliConfigs = $TautulliXMLContent.TautulliConfigs
[Xml.XmlElement]$TautulliScriptConfigs = $TautulliXMLContent.TautulliConfigs.TautulliScriptConfigs
[string]$Global:TautulliAPIKey = $TautulliConfigs.TautulliAPI
#Get-TautulliInfo -apiKey $TautulliAPI -command get_activity
# PARSE RTORRENT CONFIG FILE
#=================================================
[string]$RtorrentConfigFile = $GlobalSettings.SourceConfigs.Config | Where Id -eq 'rTorrent' | Select -ExpandProperty ConfigFile
[Xml.XmlDocument]$RtorrentXMLContent = (Get-Content "$ConfigPath\$RtorrentConfigFile" -ReadCount 0) -replace "&","&"
[Xml.XmlElement]$RtorrentConfigs = $RtorrentXMLContent.rTorrentConfigs
[Xml.XmlElement]$RtorrentSshConsole = $RtorrentConfigs.SshConsole
#grab gmail authentication
If(Test-Path "$ConfigPath\$($RtorrentSshConsole.HostCreds)" ){
$SshCreds = (Import-Clixml "$ConfigPath\$($RtorrentSshConsole.HostCreds)")
}
#grab gmail authentication
If(Test-Path "$ConfigPath\GmailAuth.xml"){
$GmailCreds = (Import-Clixml "$ConfigPath\GmailAuth.xml")
}
#Start EMAIL body generation
$PMShtmlbody = "<hr/><h1>PMS Status:</h1><br/>"
$PMShtmlbody += "<table style=`"width:100%`">"
$PMShtmlbody += "<tr><td><h3>Services:</h3></td><td><ul>"
## CHECK SERVICES
#=================================================
#TEST $service = $ServicesToCheck | Where Name -eq Tomcat9
#TEST $service = $ServicesToCheck | Where Name -eq PlexService
$ReportCritical = 0
Foreach($service in $ServicesToCheck)
{
Write-Host ("Checking service [{0}] status..." -f $service.FriendlyName) -NoNewline
$SystemService = Get-Service -Name $service.Name -ErrorAction SilentlyContinue
If($SystemService)
{
If($SystemService.Status -eq $service.state){
Write-Host ("exists and is currently {0}" -f $service.Name) -ForegroundColor Green
$ServiceState = $service.state
$ServiceColor = 'Green'
}
Else{
Write-Host ("exists but is not in state. [{0}]" -f $service.state) -ForegroundColor Yellow
Try{
Write-Host (" Attempting to start service [{0}]..." -f $service.Name) -NoNewline
Start-Service $SystemService -ErrorAction Stop
$ServiceColor = 'Green'
}
Catch{
Write-Host ("failed: [{0}]" -f $_.Exception.Message) -ForegroundColor Red
$ServiceColor = 'Red'
}
Finally{
$ServiceState = Get-Service -Name $service.Name
If( ($Service.Critical -eq 'High') -and ($ServiceState.Status -ne $service.state) ){
$ReportCritical ++
}
}
}
}
Else{
Write-Host ("does not exist. Ignoring service check for [{0}]" -f $service.Name,$service.FriendlyName) -ForegroundColor Yellow
$ServiceState = 'None'
$ServiceColor = 'Black'
}
$PMShtmlbody += "<li><b>$($service.Name)</b>:<font color='$ServiceColor'><i> $ServiceState</i></font></li>"
}
$PMShtmlbody += "</ul></td></tr>"
$PMShtmlbody += "</table><br/><br/>"
## CHECK URLS
#=================================================
$PMShtmlbody += "<table style=`"width:100%`">"
$PMShtmlbody += "<tr><td><h3>Web Services:</h3></td><td><ul>"
#TEST $Web = $WebsToCheck | Where Name -eq "Local Resilio"
#TEST $Web = $WebsToCheck | Where Name -eq "Remote Resilio"
#TEST $Web = $WebsToCheck | Where Name -eq Organizr
#TEST $Web = $WebsToCheck | Where Name -eq rTorrent
#TEST $Web = $WebsToCheck | Where Name -eq Tautulli
Foreach($Web in $WebsToCheck)
{
switch($Web.ConnectType)
{
'xmlRpc' {
#$responselist = Invoke-RPCMethod -Uri "$SeedboxUrl/rutorrent/plugins/httprpc/action.php" -RequestBody $request -Credential $credentials
$Cmdlet = 'Invoke-RPCMethod'
If($Web.Config){
If($WebConfig = Resolve-ActualPath -FileName $Web.config -WorkingPath $scriptRoot -ErrorAction SilentlyContinue){
[Xml.XmlDocument]$WebXMLConfigs = (Get-Content $WebConfig.Path -ReadCount 0) -replace "&","&"
[Xml.XmlElement]$WebConfigs = $WebXMLConfigs.($Web.Name + 'Configs')
$WebParams = @{Uri = ($WebConfigs.ExternalURL + '/' + $WebConfigs.RPCPath)}
#Get request body
$bytes = [System.Text.Encoding]::Unicode.GetBytes($WebConfigs.RequestBody.'#cdata-section')
$request = [System.Text.Encoding]::ASCII.GetString($bytes)
$WebParams['Body'] = $request
If($WebConfigs.Credentials){
If($CredFile = Resolve-ActualPath -FileName $WebConfigs.Credentials -WorkingPath $scriptRoot -ErrorAction SilentlyContinue){
[System.Management.Automation.PSCredential]$Creds = Import-Clixml $CredFile.path
if ($Creds -ne [System.Management.Automation.PSCredential]::Empty) {
$WebParams['Credential'] = $Creds
}
}
}
}
}
}
'API' {
$Cmdlet = 'Invoke-WebRequest'
If($Web.Config){
If($WebConfig = Resolve-ActualPath -FileName $Web.config -WorkingPath $scriptRoot -ErrorAction SilentlyContinue){
[Xml.XmlDocument]$WebXMLConfigs = (Get-Content $WebConfig.Path -ReadCount 0) -replace "&","&"
[Xml.XmlElement]$WebConfigs = $WebXMLConfigs.($Web.Name + 'Configs')
$WebParams = @{Uri = $WebConfigs.ExternalURL}
If($WebConfigs.Credentials){
If($CredFile = Resolve-ActualPath -FileName $WebConfigs.Credentials -WorkingPath $scriptRoot -ErrorAction SilentlyContinue){
[System.Management.Automation.PSCredential]$Creds = Import-Clixml $CredFile.Path
if ($Creds -ne [System.Management.Automation.PSCredential]::Empty) {
$WebParams['Credential'] = $Creds
}
}
}
}
}
}
default {
$Cmdlet = 'Invoke-WebRequest'
$WebParams = @{Uri = $Web.uri}
If($Web.Credentials){
If($CredFile = Resolve-ActualPath -FileName $Web.Credentials -WorkingPath $scriptRoot -ErrorAction SilentlyContinue){
[System.Management.Automation.PSCredential]$Creds = Import-Clixml $CredFile.Path
if ($Creds -ne [System.Management.Automation.PSCredential]::Empty) {
$WebParams['Credential'] = $Creds
}
}
}
}
}
$ignoredCodes = @()
If($Web.IgnoreCodes){
$ignoredCodes += ($Web.IgnoreCodes).split(',')
}
$Response = $False
Try
{
Write-Host ("Testing [{0}] web url [{1}]..." -f $Web.Name, $WebParams.URI) -NoNewline
If($Cmdlet -eq 'Invoke-RPCMethod'){
$Response = Invoke-RPCMethod @WebParams -ErrorAction Stop -TimeoutSec 30
}
Else{
$Response = Invoke-WebRequest @WebParams -ErrorAction Stop -TimeoutSec 30
}
}
Catch [System.Net.WebException]
{
#Write-Host ("{0}" -f $_.Exception.Message) -ForegroundColor Red
}
Finally
{
If($Response.StatusCode -eq '200'){
$ouputmsg = "is currently available."
$ServiceColor = 'Green'
}
ElseIf($Response.StatusCode -in $ignoredCodes){
$ouputmsg = "is currently available."
$ServiceColor = 'Yellow'
}
Else{
$ouputmsg = "isn't currently available."
$ServiceColor = 'Red'
}
Write-Host $ouputmsg -ForegroundColor $ServiceColor
$PMShtmlbody += "<li><b>$($Web.Name)</b> [$($WebParams.URI)]:<font color='$ServiceColor'><i> $ouputmsg</i></font></li>"
#clear incase differnet attributes are used in next iteration
$WebParams = $null
}
}
$PMShtmlbody += "</ul></td></tr>"
$PMShtmlbody += "</table><br/><br/>"
[int]$hour = get-date -format HH
#only send email if a critical report is needed
If($GmailCreds -and ($ReportCritical -gt 0) -and $SendCriticalReport)
{
$subject = ("Plex Media Server status - {0}" -f (Get-Date -Format 'MMM d'))
$EmailParams = @{
From=$GmailCreds.UserName
to=$GmailCreds.UserName
SmtpServer='smtp.gmail.com'
Port=587
UseSsl=$true
Credential=$GmailCreds
Subject=$subject
Body=$PMShtmlbody
BodyAsHtml=$true
}
Send-MailMessage @EmailParams
}
## CHECK SYNC STATUS
##---------------------
If(-Not$IgnoreResillioSync){
If(Test-Path "$DownloadedMoviePath\.sync"){
$Syncing = Get-ChildItem -Path "$DownloadedMoviePath\.sync" | Where {$_.Extension -eq '.!sync'}
If($Syncing.count -gt 0){
Write-Host ("Syncing is still in progress...unable to process downloaded movies folder: {0}" -f $DownloadedMoviePath ) -ForegroundColor Yellow
Exit
}
}
}
## BUILD GENRE MAPPINGS
##---------------------
#add folder paths to each genre tag
$MoviesGenreMappings = @{}
[PSCustomObject]$MoviesGenreMappings = $NewMovieConfigs.GenreMappings.Map
Foreach($genre in $MoviesGenreMappings){
#$FolderPath = Get-ChildItem $MoviesDir | Where Name -eq $genre.BindingFolder | Select -First 1
$FolderPath = Join-Path -Path $MoviesDir -ChildPath $genre.BindingFolder
If($FolderPath){
$MoviesGenreMappings | Where Tag -eq $genre.Tag | Add-Member -MemberType NoteProperty -Name 'FolderPath' -Value $FolderPath -Force
}
}
## CHECK DOWNLOAD FOLDER FOR VIDEOS
##---------------------------------
#TEST $DownloadedMoviePath = 'D:\Data\Downloads\sync'
#get all download files that are videos
$DownloadedFiles = Get-ChildItem -LiteralPath $DownloadedMoviePath -Recurse | Where {$_.PSIsContainer -eq $false -and $_.Extension -in $VideoExtensions -and $_.BaseName -notmatch $FilterFileNames}
#$DownloadedFiles.Fullname
#determine which ones are movies (regex example: movie.name.1999....)
$DownloadedMovies = $DownloadedFiles | Where {$_.name -match "([ .\w']+?)(\W\d{4}\W?.*)" -and $_.name -notmatch "^.*S\d\dE\d\d"}
#$DownloadedMovies.Fullname
#determine which ones are tv shows
$DownloadedTvShows = $DownloadedFiles | Where {$_.name -match "^.*S\d\dE\d\d"}
#$DownloadedTvShows.Fullname
#$RequestedMovies = Get-ChildItem -Path $MovieRequestsPath -Directory
$RequestedMovies = Get-ChildItem -LiteralPath $MovieRequestsPath -Recurse | Where {$_.PSIsContainer -eq $false -and $_.Extension -in $VideoExtensions}
#$RequestedMovies.Fullname
If($DownloadedMovies.Count -gt 0 -or $RequestedMovies.Count -gt 0){
# Get all movies in Radarr (this is required for additonal details)
$ExistingRadarrMovies = Get-RadarrMovies
}
#set null arrays
$NewMovies = @()
$NewMovieInfoMappings = @()
$NewRadarrMovieActions = @()
#adde movies to new list
$NewMovies += $DownloadedMovies
$NewMovies += $RequestedMovies
## CHECK FOR NEW MOVIES
##---------------------
#$NewMovie = $NewMovies[0]
Foreach($NewMovie in $NewMovies)
{
$ErrorActionPreference = 'Stop'
Try{
$FileInfo = "" | Select SourcePath,Size,SourceFileName,SourceFilePath
$FileInfo.SourcePath = (Split-Path $NewMovie.FullName -Parent)
$FileInfo.SourceFilePath = $NewMovie.FullName
$FileInfo.SourceFileName = $NewMovie.Name
$FileInfo.Size = $NewMovie.Length
Write-Host ("---------------------------------------------------------") -ForegroundColor Cyan
Write-Host ("Parsing movie from file name [{0}]... " -f $NewMovie.name) -ForegroundColor Cyan
$MovieInfo = ConvertTo-MovieData -Value $NewMovie.Name -FormatTitleCase
#TEST $MovieInfo = ConvertTo-MovieData -Value ($NewMovies | Where name -like "*die.another.day*").Name
#TEST $MovieInfo = ConvertTo-MovieData -Value ($NewMovies | Where name -like "*Venom.Let.There.Be.Carnage*").Name
If($VerbosePreference -eq 'Continue'){$MovieInfo}
#Search online for more accruate information
$OnlineMovie = Search-MovieTitle -Title $MovieInfo.Title -Year $MovieInfo.Year -IMDBApiKey $OMDBAPI -TMDBApiKey $TMDBAPI
If($VerbosePreference -eq 'Continue'){$OnlineMovie}
#start object merging collection
$MultipleObjectsParams = @{}
If($OnlineMovie){
$MovieDetails = "" | Select OnlineimdbID,OnlinetmdbID,OnlinePoster
$MovieDetails.OnlineimdbID = $OnlineMovie.imdbID
$MovieDetails.OnlinetmdbID = $OnlineMovie.tmdbID
$MovieDetails.OnlinePoster = $OnlineMovie.Poster
#Add another object to merge
$MultipleObjectsParams += @{Object3 = $MovieDetails}
#Update movie title and year based on online find (its more accurate)
$MovieInfo.Title = $OnlineMovie.Title
$MovieInfo.Year = $OnlineMovie.Year
}
#Add the Movie info and file into to collection of mergable objects
$MultipleObjectsParams += @{
Object1 = $MovieInfo
Object2 = $FileInfo
}
[array]$genres = ($OnlineMovie.Genres -split ',').Trim() | Select -Unique
$MatchedMapping = $null
#$Map = $MoviesGenreMappings[0]
Foreach($Map in $MoviesGenreMappings)
{
#check to see if Mactchmapping has a value
# if it doesn't attempt to find it
If($null -eq $MatchedMapping)
{
$Property = $Map.Property
$CompareValue = $Map.Tag
$PropertyValue = $null
If($Property -eq 'name')
{
$UseObject = $NewMovie
If($UseObject.movieFile.path){
$PropertyValue = (Split-Path $UseObject.movieFile.path -leaf)
}
If($UseObject.Name){
$PropertyValue = $UseObject.Name
}
}
ElseIf($Property -eq 'genre')
{
$Property = 'genres'
If($NewMovie.genres){
$UseObject = $NewMovie
}
Else{
$UseObject = $OnlineMovie
}
$PropertyValue = $UseObject.genres -join '|'
}
Else{
If($NewMovie.$Property){
$UseObject = $NewMovie
}
If($OnlineMovie.$Property){
$UseObject = $OnlineMovie
}
$PropertyValue = $UseObject.$Property
}
If($null -eq $PropertyValue){
Write-Host (" Movie property [{0}] does not exist, question skipped... " -f $Property) -ForegroundColor Yellow
}
ElseIf( [string]::IsNullOrEmpty($UseObject.$Property) ){
Write-Host (" Movie property [{0}] does not have a value, question skipped... " -f $Property) -ForegroundColor Yellow
}
ElseIf($PropertyValue -match '\|'){
Write-Host (" Does movie property [{0}] with value of [{1}] match [{2}]? " -f $Property,($PropertyValue | Trim-Length 125 -Traildots), $CompareValue) -NoNewline
If($Map.Tag -match $PropertyValue){
Write-Host 'Yes' -ForegroundColor Green
$MatchedMapping = $Map
$MultipleObjectsParams += @{Object4 = $Map}
}
Else{
Write-Host 'No' -ForegroundColor Red
}
}
ElseIf($Map.Tag -match '\*'){
Write-Host (" Does movie property [{0}] with value of [{1}] like [{2}]? " -f $Property,($PropertyValue | Trim-Length 125 -Traildots), $CompareValue) -NoNewline
If($PropertyValue -like $CompareValue){
Write-Host 'Yes' -ForegroundColor Green
$MatchedMapping = $Map
$MultipleObjectsParams += @{Object4 = $Map}
}
Else{
Write-Host 'No' -ForegroundColor Red
}
}
Else{
Write-Host (" Does movie property [{0}] with value of [{1}] equal [{2}]? " -f $Property,($PropertyValue | Trim-Length 125 -Traildots), $CompareValue) -NoNewline
If($PropertyValue -eq $CompareValue){
Write-Host 'Yes' -ForegroundColor Green
$MatchedMapping = $Map
$MultipleObjectsParams += @{Object4 = $Map}
}
Else{
Write-Host 'No' -ForegroundColor Red
}
}
}
}#end genre mapping loop
#If Mapping does not exist, build default mapping to request folder.
If($MatchedMapping)
{
Write-Host ("[{0}] will be mapped to [{1}]" -f $UseObject.title,$MatchedMapping.FolderPath) -ForegroundColor Green
}
Else{
Write-Host ("Unable to map [{0}] to a folder location" -f $UseObject.title) -ForegroundColor White -BackgroundColor Red
$MatchedMapping = "" | Select FolderPath
$MatchedMapping.FolderPath = $MatchedMapping
}
#$MultipleObjectsParams += @{Object4 = $MatchedMapping}
If($VerbosePreference -eq 'Continue'){$MultipleObjectsParams}
$NewMovieInfoMappings += Merge-MultipleObjects @MultipleObjectsParams
}
Catch{
Write-host ("Unable to create info for movie {0}. {1} {2}" -f $NewMovie.Name,$_.Exception.Message,$_.ScriptStackTrace) -ForegroundColor Red
}
Finally{
$ErrorActionPreference = "SilentlyContinue"
}
}#end movie loop
#$NewMovieInfoMappings
## GET RADARR ACTIONS
##---------------------
If($NewMovieInfoMappings.Count -gt 0)
{
$NewRadarrMovieActions = @()
#TEST $NewRadarrMovie = $NewMovieInfoMappings[0]
#TEST $NewRadarrMovie = $NewMovieInfoMappings[1]
#TEST $NewRadarrMovie = $NewMovieInfoMappings[2]
#TEST $NewRadarrMovie = $NewMovieInfoMappings[-1]
Foreach($NewRadarrMovie in $NewMovieInfoMappings)
{
$ErrorActionPreference = 'Stop'
Try{
#create radarr object
$MovieJob = "" | Select RadarrMovieStatus,FileAction,RadarrAction,RadarrFilePath,RadarrFileStatus,RadarrNewFolderPath,SupportFileAction,SupportFilesPath
Write-Host ("----------------------------------------------------------") -ForegroundColor Cyan
Write-Host ("Determining actions for movie [{0}]... " -f $NewRadarrMovie.Title) -ForegroundColor Cyan
#Assume if on srt file exists; its engligh unless it named otherwise
$SupportFiles = @()
$SupportFiles = Get-ChildItem -LiteralPath $NewRadarrMovie.SourcePath | Where-Object {$_.PSIsContainer -eq $false -and $_.Extension -in $VideoSupportFiles}
If($SupportFiles.count -gt 1){
$SupportFiles = $SupportFiles | Where-Object $_.BaseName -match $SupportedLanguages
$MovieJob.SupportFileAction = 'Move'
}
$MovieJob.SupportFilesPath = $SupportFiles.FullName
#Remove any other files that are not video related but only if it exits in its own folder
If( ($NewRadarrMovie.SourcePath -ne $DownloadedMoviePath) -and ($NewRadarrMovie.SourcePath -ne $MovieRequestsPath) ){
Get-childitem -LiteralPath $NewRadarrMovie.SourcePath -Recurse |
Where-Object {$_.PSIsContainer -eq $false -and $_.BaseName -notin $SupportFiles.BaseName -and $_.Extension -notin $VideoExtensions -and `
($_.Extension -notin $VideoSupportFiles -or $_.BaseName -notmatch $SupportedLanguages)} |
Remove-Item -Force -ErrorAction SilentlyContinue
}
#TEST $ExistingRadarrMovies | Where {($_.title -like '*Shang*')}
Write-Host (" Does movie entry [{0}] already exist in Radarr? " -f $NewRadarrMovie.Title) -NoNewline
$MovieInRadarr = $ExistingRadarrMovies | Where {($_.title -eq $NewRadarrMovie.Title -or $_.sortTitle -eq $NewRadarrMovie.Title) -and $_.year -eq $NewRadarrMovie.Year}
If($MovieInRadarr){
Write-Host 'Yes' -ForegroundColor Green
$MovieJob.RadarrMovieStatus = 'Exist'
}
Else{
Write-Host 'No' -ForegroundColor Red
$MovieJob.RadarrMovieStatus = 'NotExist'
}
If($MovieJob.RadarrMovieStatus -eq 'Exist')
{
Write-Host (" Is there a movie file that exist with Radarr entry [{0}]? " -f $NewRadarrMovie.Title) -NoNewline
If($MovieInRadarr.movieFile.Path){
$MovieJob.RadarrFilePath = $MovieInRadarr.movieFile.Path
If(Test-Path $MovieInRadarr.movieFile.Path){
Write-Host 'Yes' -ForegroundColor Green
}
Else{
Write-Host 'Missing' -ForegroundColor Red
$MovieJob.FileAction = 'Move'
$MovieJob.RadarrAction = 'Update'
$MovieJob.RadarrFileStatus = 'Missing'
}
}
Else{
Write-Host 'No' -ForegroundColor Red
$MovieJob.FileAction = 'Move'
$MovieJob.RadarrAction = 'Update'
$MovieJob.RadarrFileStatus = 'Missing'
}
If($MovieJob.RadarrFileStatus -ne 'Missing')
{
Write-Host (" Is the movie file path in the right location? [{0}]? " -f $NewRadarrMovie.FolderPath) -NoNewline
If( (Split-Path $MovieInRadarr.movieFile.Path -Parent) -eq $NewRadarrMovie.FolderPath ){
Write-Host 'Yes' -ForegroundColor Green
}
Else{
Write-Host 'No' -ForegroundColor Red
$MovieJob.FileAction = 'Move'
$MovieJob.RadarrAction = 'Update'
$MovieJob.RadarrFilePath = (Join-path $NewRadarrMovie.FolderPath -ChildPath $NewRadarrMovie.FileName)
}
Write-Host (" Is there a language file that exist with Radarr entry [{0}]? " -f $NewRadarrMovie.Title) -NoNewline
$RadarrSupportFile = Get-childitem -LiteralPath $MovieInRadarr.movieFile.Path | Where-Object {$_.PSIsContainer -eq $false -and $_.Extension -in $VideoSupportFiles}
If($RadarrSupportFile.count -gt 0){
Write-Host 'Yes' -ForegroundColor Green
$MovieJob.SupportFilesPath = $RadarrSupportFile.FullName
$MovieJob.SupportFileAction = 'Move'
}
Else{
Write-Host 'No' -ForegroundColor Red
$MovieJob.SupportFileAction = 'Move'
}
Write-Host (" Is the movie file [{0}] in Radarr the same file? " -f $MovieInRadarr.movieFile.relativePath) -NoNewline
If($MovieInRadarr.movieFile.size -eq $NewRadarrMovie.FileSize){
Write-Host ('size is the same [{0}]. Will delete new movie file' -f $MovieInRadarr.movieFile.size) -ForegroundColor Red
$MovieJob.FileAction = 'Delete'
$MovieJob.RadarrAction = 'None'
}
ElseIf($MovieInRadarr.movieFile.size -gt $NewRadarrMovie.FileSize){
Write-Host ('size is larger [{0}]. needs review' -f $MovieInRadarr.movieFile.size) -ForegroundColor Yellow
$MovieJob.FileAction = 'None'
$MovieJob.RadarrAction = 'None'
}
Else{
Write-Host ('size is smaller. Updating Radarr movie with with new file [{0}]' -f $NewRadarrMovie.FileName) -ForegroundColor Yellow
$MovieJob.FileAction = 'Move'
$MovieJob.RadarrAction = 'Update'
$MovieJob.RadarrNewFolderPath = Join-path $NewRadarrMovie.FolderPath -ChildPath $NewRadarrMovie.SimpleTitle
}
}
Else{
Write-Host (" No movie FILE exists. Updating Radarr movie with with new file [{0}]" -f $NewRadarrMovie.FileName) -ForegroundColor Yellow
$MovieJob.FileAction = 'Move'
$MovieJob.RadarrAction = 'Update'
$MovieJob.RadarrNewFolderPath = Join-path $NewRadarrMovie.FolderPath -ChildPath $NewRadarrMovie.SimpleTitle
}
}
Else{
Write-Host ('No MOVIE exists in Radarr. Adding new movie to Radarr [{0}]' -f $NewRadarrMovie.FileName) -ForegroundColor Green
$MovieJob.FileAction = 'Move'
$MovieJob.RadarrAction = 'New'
$MovieJob.RadarrNewFolderPath = Join-path $NewRadarrMovie.FolderPath -ChildPath $NewRadarrMovie.SimpleTitle
}
#build new object that combines mappings and radarr actions
$NewRadarrMovieActions += Merge-MultipleObjects $NewRadarrMovie $MovieJob
}
Catch{
Write-host ("Unable to build radarr actions for movie {0}. {1} {2}" -f $NewMovie.Name,$_.Exception.Message,$_.ScriptStackTrace) -ForegroundColor Red
}
Finally{
$ErrorActionPreference = "SilentlyContinue"
}
} #End loop
}
#$NewRadarrMovieActions
## DO ACTION - NEW MOVIE FOR RADARR
##----------------------------------
If($ProcessRequestedMovies -and $NewRadarrMovieActions.Count -gt 0)
{
$moviehtmlbody = "<hr/><h1>Movies Changes:</h1><br/>"
$moviehtmlbody += "<table style=`"width:100%`">"
$PlexActivity = Get-PlexActivity -PlexToken $PlexAuthToken -Details
If($PlexActivity.title -in $NewRadarrMovieActions.title){
Write-Host ("Movie in Plex is currently streaming...unable to continue with updating movies folder") -ForegroundColor Yellow
Break
}
#$MovieToProcess = $NewRadarrMovieActions[0]
#$MovieToProcess = $NewRadarrMovieActions[1]
#$MovieToProcess = $NewRadarrMovieActions[-1]
Foreach($MovieToProcess in $NewRadarrMovieActions)
{
$MovieIsReady = $false
$SourceFolder = Split-Path $MovieToProcess.SourceFilePath -Parent
Write-Host ("----------------------------------------------------------") -ForegroundColor Cyan
Write-Host ("Processing new movie [{0}]... " -f $MovieToProcess.Title) -ForegroundColor Cyan
$DestinationPath = (Join-Path $MovieToProcess.RadarrNewFolderPath -ChildPath $MovieToProcess.FileName)
$MovieInRadarr = $ExistingRadarrMovies | Where {($_.title -eq $MovieToProcess.Title -or $_.sortTitle -eq $MovieToProcess.Title) -and $_.year -eq $MovieToProcess.Year}
If($MovieToProcess.RadarrAction -eq 'Update' -and $MovieToProcess.RadarrFileStatus -ne 'Missing'){
New-Item $MovieToProcess.RadarrFilePath -ItemType Directory -ErrorAction SilentlyContinue | Out-Null
}
If($MovieToProcess.RadarrFilePath){
If( (Split-Path $MovieToProcess.RadarrFilePath -Parent) -ne $MovieToProcess.RadarrNewFolderPath){
#delete the entire folder?
If(Test-Path $MovieToProcess.RadarrFilePath){
Get-Item (Split-Path $MovieToProcess.RadarrFilePath -Parent) | Remove-Item -Recurse -Force
}
}
}
Switch($MovieToProcess.FileAction){
'Move' {
Write-Host ("Moving movie file [{0}] to [{1}]..." -f $MovieToProcess.SourceFileName, $MovieToProcess.RadarrNewFolderPath) -NoNewline
Try{
New-Item $MovieToProcess.RadarrNewFolderPath -ItemType Directory -ErrorAction SilentlyContinue | Out-Null
Move-Item -LiteralPath $MovieToProcess.SourceFilePath -Destination $DestinationPath -Force | Out-Null
Write-Host ('Done. Renamed file to [{0}]' -f $MovieToProcess.FileName) -ForegroundColor Green
}
Catch{
write-host ("Failed: {0}" -f $_.Exception.Message) -ForegroundColor Red
Continue
}
}
'Delete' {
Get-Item -LiteralPath $MovieToProcess.SourcePath | Remove-Item -Recurse -Force
}
'none'{}
} #end file actions
Switch($MovieToProcess.SupportFilesAction){
'New' {
#TODO: Develop API to pull language file
}
'Move' {
#$SupportFile = $MovieToProcess.SupportFilesPath[0]
If($MovieToProcess.SupportFilesPath.count -eq 1){
$FileName = Split-Path $MovieToProcess.SupportFilesPath -Leaf
Write-Host ("Moving support file [{0}] to [{1}]..." -f $FileName, $MovieToProcess.RadarrNewFolderPath) -NoNewline
$ext = [System.IO.Path]::GetExtension($FileName)
If($FileName -match $SupportedLanguages){
$SupportDestinationPath = $DestinationPath.replace($MovieToProcess.FileExtension,'') + '.' + $matches[0] + $ext
}Else{
$SupportDestinationPath = $DestinationPath.replace($MovieToProcess.FileExtension,$ext)
}
$NewFilename = Split-Path $SupportDestinationPath -Leaf
Try{
Move-Item -LiteralPath $MovieToProcess.SupportFilesPath -Destination $SupportDestinationPath -Force | Out-Null
Write-Host ('Done. Renamed file to [{0}]' -f $NewFilename) -ForegroundColor Green
}
Catch{
write-host ("Failed: {0}" -f $_.Exception.Message) -ForegroundColor Red
Continue
}
}
Else{
Foreach($SupportFile in $MovieToProcess.SupportFilesPath){
$FileName = Split-Path $SupportFile -Leaf
Write-Host ("Moving support file [{0}] to [{1}]..." -f $FileName, $MovieToProcess.RadarrNewFolderPath) -NoNewline
$ext = [System.IO.Path]::GetExtension($FileName)
If($FileName -match $SupportedLanguages){
$SupportDestinationPath = $DestinationPath.replace($MovieToProcess.FileExtension,'') + '.' + $matches[0] + $ext
}Else{
$SupportDestinationPath = $DestinationPath.replace($MovieToProcess.FileExtension,$ext)
}
$NewFilename = Split-Path $SupportDestinationPath -Leaf
Try{
Move-Item -LiteralPath $MovieToProcess.SupportFilesPath -Destination $SupportDestinationPath -Force | Out-Null
Write-Host ('Done. Renamed file to [{0}]' -f $NewFilename) -ForegroundColor Green
}
Catch{
write-host ("Failed: {0}" -f $_.Exception.Message) -ForegroundColor Red
Continue
}
}
}
}
} #end support file actions
Switch($MovieToProcess.RadarrAction){
'New' {
Try{
Write-host ("Adding new Radarr's Movie [{1}] with path [{0}]..." -f $MovieToProcess.title,$MovieToProcess.RadarrNewFolderPath) -NoNewline
#$MovieToProcess = Get-RadarrMovie -MovieTitle $MovieToProcess.Title -Year $MovieToProcess.Year -AsObject
New-RadarrMovie -Title $MovieToProcess.Title -Year $MovieToProcess.Year `
-imdbID $MovieToProcess.OnlineImdbID -tmdbID $MovieToProcess.OnlineTmdbID `
-Path $DestinationPath -PosterImage $MovieToProcess.OnlinePoster `
-SearchAfterImport
#Refresh-RadarrMovie -Title $MovieToProcess.Title
Write-host ("Done") -ForegroundColor Green
$MovieIsReady = $true
}
Catch{
write-host ("Failed: {0}" -f $_.Exception.Message) -ForegroundColor Red
Continue
}
}
'Update' {
Try{
Write-host ("Updating Radarr's movie path for [{1}] to [{0}]..." -f $MovieToProcess.RadarrNewFolderPath,$MovieToProcess.title) -NoNewline
$Null = Update-RadarrMoviePath -InputObject $MovieInRadarr -DestinationPath $MovieToProcess.RadarrNewFolderPath -ErrorAction Stop
$Result = Refresh-RadarrMovie -Id $MovieInRadarr.id -ErrorAction Stop
Write-host ("Done") -ForegroundColor Green
$MovieIsReady = $true
}
Catch{