Skip to content

Commit a433179

Browse files
Error handling
Throw a proper exception when API key is missing. Also fixed edge case bug where multiple TMDB IDs returned from same query.
1 parent 0083bf5 commit a433179

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

scripts/MatroskaTagGenerator.ps1

+19-3
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ function Get-ID {
173173
else { $queryID = $query.results[0].id }
174174

175175
if ($queryID) {
176+
if ($queryID.Count -gt 1) {
177+
Write-Host "More than 1 ID returned. Selecting the first option"
178+
$queryID = $queryID[0]
179+
}
176180
Write-Host "ID successfully retrieved! ID: " -NoNewline
177181
Write-Host $queryID @progressColors
178182
if ('TMDbID' -in $SkipProperties) {
@@ -210,10 +214,12 @@ function Get-Metadata {
210214
}
211215
else { $obj = @{} }
212216

213-
# Pull general info including IMDb ID
217+
# Pull general info to collect other props
218+
$genQuery = Invoke-RestMethod -Uri "https://api.themoviedb.org/3/movie/$($id)?api_key=$($APIKey)" -Method GET
219+
220+
# Pull IMDb ID
214221
if ('IMDbID' -notin $SkipProperties) {
215222
Write-Host "Requesting IMDB ID..."
216-
$genQuery = Invoke-RestMethod -Uri "https://api.themoviedb.org/3/movie/$($id)?api_key=$($APIKey)" -Method GET
217223
if ($imdbID = $genQuery | Select-Object -ExpandProperty imdb_id) {
218224
Write-Host "IMDb ID successfully retrieved! ID: " -NoNewline
219225
Write-Host $imdbID @progressColors
@@ -224,7 +230,7 @@ function Get-Metadata {
224230
Write-Warning "Failed to retrieve IMDb ID. Property will be skipped"
225231
}
226232
}
227-
else { Write-Host "Skipping IMDb ID..." @warnColors }
233+
else { Write-Host "Skipping IMDb ID..." @warnColors }
228234

229235
Write-Host "---------------------------------------------" @dividerColor
230236

@@ -366,6 +372,16 @@ function New-XMLTagFile {
366372
# Main Script Logic #
367373
#########################################################
368374

375+
if (!$APIKey) {
376+
$params = @{
377+
CategoryActivity = 'MKV Tag Generator'
378+
Category = 'InvalidArgument'
379+
Message = "MatroskaTagGenerator: An API key is required"
380+
ErrorId = 77
381+
}
382+
Write-Error "MatroskaTagGenerator: An API key is required" -ErrorAction Stop
383+
}
384+
369385
if ($Path.EndsWith('.xml')) {
370386
$outXML = $Path
371387
}

0 commit comments

Comments
 (0)