-
Notifications
You must be signed in to change notification settings - Fork 0
/
PopularOnPlex.ps1
304 lines (280 loc) · 11 KB
/
PopularOnPlex.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
Clear-Host
# Enter the path to the config file for Tautulli and Discord
[string]$strPathToConfig = "$PSScriptRoot\config\config.json"
# Script name MUST match what is in config.json under "ScriptSettings"
[string]$strScriptName = 'PopularOnPlex'
<############################################################
Do NOT edit lines below unless you know what you are doing!
############################################################>
# Define the functions to be used
function Get-TMDBInfo {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$strAPIKey,
[Parameter(Mandatory)]
[ValidateSet('tv', 'movie')]
[string]$strMediaType,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$strTitle,
[Parameter()]
[string]$strYear
)
[string]$strTMDB_URL = "https://api.themoviedb.org/3/search/$($strMediaType)?api_key=$($strAPIKey)&language=en-US&page=1&include_adult=false&query=$strTitle"
if (($null -ne $strYear) -and ($strYear -ne '')) {
$strTMDB_URL = "$($strTMDB_URL)&year=$($strYear)"
}
# Highly inaccurate method that relies on TMDB's ability to match the search title and year, but what other choice do I have?
[string]$strMediaID = (Invoke-RestMethod -Method Get -Uri $strTMDB_URL).results[0].id
[object]$objResults = Invoke-RestMethod -Method Get -Uri "https://api.themoviedb.org/3/$($strMediaType)/$($strMediaID)?api_key=$($strAPIKey)&language=en-US" -ErrorAction SilentlyContinue
return $objResults
}
function Get-SanitizedString {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$strInputString
)
# Credit to FS.Corrupt for the initial version of this function. https://github.com/FSCorrupt
[regex]$regAppendedYear = ' \(([0-9]{4})\)' # This will match any titles with the year appended. I ran into issues with 'Yellowstone (2018)'
[hashtable]$htbReplaceValues = @{
'ß' = 'ss'
'à' = 'a'
'á' = 'a'
'â' = 'a'
'ã' = 'a'
'ä' = 'a'
'å' = 'a'
'æ' = 'ae'
'ç' = 'c'
'è' = 'e'
'é' = 'e'
'ê' = 'e'
'ë' = 'e'
'ì' = 'i'
'í' = 'i'
'î' = 'i'
'ï' = 'i'
'ð' = 'd'
'ñ' = 'n'
'ò' = 'o'
'ó' = 'o'
'ô' = 'o'
'õ' = 'o'
'ö' = 'o'
'ø' = 'o'
'ù' = 'u'
'ú' = 'u'
'û' = 'u'
'ü' = 'u'
'ý' = 'y'
'þ' = 'p'
'ÿ' = 'y'
'“' = '"'
'”' = '"'
'·' = '-'
':' = ''
$regAppendedYear = ''
}
foreach($key in $htbReplaceValues.Keys){
$strInputString = $strInputString -Replace($key, $htbReplaceValues.$key)
}
return $strInputString
}
function Push-ObjectToDiscord {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$strDiscordWebhook,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[object]$objPayload
)
try {
$null = Invoke-RestMethod -Method Post -Uri $strDiscordWebhook -Body $objPayload -ContentType 'Application/Json'
Start-Sleep -Seconds 1
}
catch {
Write-Host "Unable to send to Discord. $($_)" -ForegroundColor Red
Write-Host $objPayload
}
}
# Parse the config file and assign variables
[object]$objConfig = Get-Content -Path $strPathToConfig -Raw | ConvertFrom-Json
[string]$strDiscordWebhook = $objConfig.ScriptSettings.$strScriptName.Webhook
[string]$strCount = $objConfig.ScriptSettings.$strScriptName.Count
[string]$strDays = $objConfig.ScriptSettings.$strScriptName.Days
[string]$strTautulliURL = $objConfig.Tautulli.URL
[string]$strTautulliAPIKey = $objConfig.Tautulli.APIKey
[string]$strTMDB_APIKey = $objConfig.TMDB.APIKey
# Get and store data from Tautulli
[object]$objPlexServerIdentifier = Invoke-RestMethod -Method Get -Uri "$strTautulliURL/api/v2?apikey=$strTautulliAPIKey&cmd=get_server_info"
[string]$strPlexServerIdentifier = ($objPlexServerIdentifier.response.data | Select-Object -ExpandProperty pms_identifier)
[object]$objDataResult = Invoke-RestMethod -Method Get -Uri "$strTautulliURL/api/v2?apikey=$strTautulliAPIKey&cmd=get_home_stats&grouping=1&time_range=$strDays&stats_count=$strCount"
[array]$arrTopMovies = ($objDataResult.response.data | Where-Object -Property stat_id -eq "popular_movies").rows
[array]$arrTopTVShows = ($objDataResult.response.data | Where-Object -Property stat_id -eq "popular_tv").rows
# Clear variables that will be populated
[System.Collections.ArrayList]$arrTopMoviesEmbed = @()
[System.Collections.ArrayList]$arrTopTVShowsEmbed = @()
# Collect Movie information
foreach ($movie in $arrTopMovies) {
[string]$strSanitizedMovieTitle = Get-SanitizedString $($movie.title)
[object]$objTMDBMovieResults = Get-TMDBInfo -strAPIKey $strTMDB_APIKey -strMediaType 'movie' -strTitle $strSanitizedMovieTitle -strYear $movie.year
if($objTMDBMovieResults.id -eq '') {
[hashtable]$htbMovieEmbedParameters = @{
color = '13400320'
title = $strSanitizedMovieTitle
url = 'https://www.themoviedb.org/movie/'
author = @{
name = "Open on Plex"
url = "https://app.plex.tv/desktop/#!/server/$strPlexServerIdentifier/details?key=%2Flibrary%2Fmetadata%2F$($movie.rating_key)"
icon_url = "https://i.imgur.com/FNoiYXP.png"
}
description = "Unknown"
thumbnail = @{url = "https://www.programmableweb.com/sites/default/files/TMDb.jpg"}
fields = @{
name = 'Rating'
value = "??? :star:'s"
inline = $false
},@{
name = 'Users Watched'
value = $movie.users_watched
inline = $false
},@{
name = 'Released'
value = $movie.year
inline = $true
}
footer = @{
text = 'Updated'
}
timestamp = ((Get-Date).AddHours(5)).ToString("yyyy-MM-ddTHH:mm:ss.Mss")
}
}
else {
[hashtable]$htbMovieEmbedParameters = @{
color = '13400320'
title = $strSanitizedMovieTitle
url = "https://www.themoviedb.org/movie/$($objTMDBMovieResults.id)"
author = @{
name = "Open on Plex"
url = "https://app.plex.tv/desktop/#!/server/$strPlexServerIdentifier/details?key=%2Flibrary%2Fmetadata%2F$($movie.rating_key)"
icon_url = "https://i.imgur.com/FNoiYXP.png"
}
description = Get-SanitizedString -strInputString $($objTMDBMovieResults.overview) -ErrorAction SilentlyContinue
thumbnail = @{url = "https://image.tmdb.org/t/p/w500$($objTMDBMovieResults.poster_path)"}
fields = @{
name = 'Rating'
value = "$($objTMDBMovieResults.vote_average) :star:'s"
inline = $false
},@{
name = 'Users Watched'
value = $movie.users_watched
inline = $true
},@{
name = 'Released'
value = $movie.year
inline = $true
}
footer = @{
text = 'Updated'
}
timestamp = ((Get-Date).AddHours(5)).ToString("yyyy-MM-ddTHH:mm:ss.Mss")
}
}
$null = $arrTopMoviesEmbed.Add($htbMovieEmbedParameters)
}
# Collect TV Show information
foreach ($show in $arrTopTVShows) {
[string]$strSanitizedShowTitle = Get-SanitizedString $($show.title)
[object]$objTMDBTVResults = Get-TMDBInfo -strAPIKey $strTMDB_APIKey -strMediaType 'tv' -strTitle $strSanitizedShowTitle -strYear $show.year
if($objTMDBTVResults.id -eq '') { # This is likely due to RatingKey being changed
[hashtable]$htbEmbedParameters = @{
color = '40635'
title = $strSanitizedShowTitle
#url = "https://www.themoviedb.org/movie/$($objTMDBTVResults.id)"
author = @{
name = "Open on Plex"
url = "https://app.plex.tv/desktop/#!/server/$strPlexServerIdentifier/details?key=%2Flibrary%2Fmetadata%2F$($show.rating_key)"
icon_url = 'https://i.imgur.com/FNoiYXP.png'
}
description = "Unknown"
#thumbnail = @{url = "https://image.tmdb.org/t/p/w500$($objTMDBTVResults.poster_path)"}
fields = @{
name = 'Rating'
value = "??? :star:'s"
inline = $false
},@{
name = 'Users Watched'
value = $show.users_watched
inline = $true
},@{
name = 'Seasons'
value = "??? Seasons "
inline = $true
},@{
name = 'Runtime'
value = "??? Minutes"
inline = $true
}
footer = @{
text = 'Updated'
}
timestamp = ((Get-Date).AddHours(5)).ToString("yyyy-MM-ddTHH:mm:ss.Mss")
}
}
else {
[hashtable]$htbEmbedParameters = @{
color = '40635'
title = $strSanitizedShowTitle
url = "https://www.themoviedb.org/tv/$($objTMDBTVResults.id)"
author = @{
name = "Open on Plex"
url = "https://app.plex.tv/desktop/#!/server/$strPlexServerIdentifier/details?key=%2Flibrary%2Fmetadata%2F$($show.rating_key)"
icon_url = "https://i.imgur.com/FNoiYXP.png"
}
description = Get-SanitizedString -strInputString $objTMDBTVResults.overview
thumbnail = @{url = "https://image.tmdb.org/t/p/w500$($objTMDBTVResults.poster_path)"}
fields = @{
name = 'Rating'
value = "$($objTMDBTVResults.vote_average) :star:'s"
inline = $false
},@{
name = 'Users Watched'
value = $show.users_watched
inline = $true
},@{
name = 'Seasons'
value = "$($objTMDBTVResults.number_of_seasons) Seasons "
inline = $true
},@{
name = 'Runtime'
value = "$($objTMDBTVResults.episode_run_time | Select-Object -First 1) Minutes"
inline = $true
}
footer = @{
text = 'Updated'
}
timestamp = ((Get-Date).AddHours(5)).ToString("yyyy-MM-ddTHH:mm:ss.Mss")
}
}
$null = $arrTopTVShowsEmbed.Add($htbEmbedParameters)
}
# Create and send the Movie Payload
[object]$objMoviesPayload = @{
username = "Popular on Plex"
content = "**Popular Movies on Plex:**"
embeds = $arrTopMoviesEmbed
} | ConvertTo-Json -Depth 4
Push-ObjectToDiscord -strDiscordWebhook $strDiscordWebhook -objPayload $objMoviesPayload
# Create and send the TV Show Payload
[object]$objShowsPayload = @{
username = "Popular on Plex"
content = "**Popular TV Shows on Plex:**"
embeds = $arrTopTVShowsEmbed
} | ConvertTo-Json -Depth 4
Push-ObjectToDiscord -strDiscordWebhook $strDiscordWebhook -objPayload $objShowsPayload