Skip to content

Commit

Permalink
Error handling
Browse files Browse the repository at this point in the history
Throw a proper exception when API key is missing. Also fixed edge case bug where multiple TMDB IDs returned from same query.
  • Loading branch information
patrickenfuego committed May 9, 2023
1 parent 0083bf5 commit a433179
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions scripts/MatroskaTagGenerator.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ function Get-ID {
else { $queryID = $query.results[0].id }

if ($queryID) {
if ($queryID.Count -gt 1) {
Write-Host "More than 1 ID returned. Selecting the first option"
$queryID = $queryID[0]
}
Write-Host "ID successfully retrieved! ID: " -NoNewline
Write-Host $queryID @progressColors
if ('TMDbID' -in $SkipProperties) {
Expand Down Expand Up @@ -210,10 +214,12 @@ function Get-Metadata {
}
else { $obj = @{} }

# Pull general info including IMDb ID
# Pull general info to collect other props
$genQuery = Invoke-RestMethod -Uri "https://api.themoviedb.org/3/movie/$($id)?api_key=$($APIKey)" -Method GET

# Pull IMDb ID
if ('IMDbID' -notin $SkipProperties) {
Write-Host "Requesting IMDB ID..."
$genQuery = Invoke-RestMethod -Uri "https://api.themoviedb.org/3/movie/$($id)?api_key=$($APIKey)" -Method GET
if ($imdbID = $genQuery | Select-Object -ExpandProperty imdb_id) {
Write-Host "IMDb ID successfully retrieved! ID: " -NoNewline
Write-Host $imdbID @progressColors
Expand All @@ -224,7 +230,7 @@ function Get-Metadata {
Write-Warning "Failed to retrieve IMDb ID. Property will be skipped"
}
}
else { Write-Host "Skipping IMDb ID..." @warnColors }
else { Write-Host "Skipping IMDb ID..." @warnColors }

Write-Host "---------------------------------------------" @dividerColor

Expand Down Expand Up @@ -366,6 +372,16 @@ function New-XMLTagFile {
# Main Script Logic #
#########################################################

if (!$APIKey) {
$params = @{
CategoryActivity = 'MKV Tag Generator'
Category = 'InvalidArgument'
Message = "MatroskaTagGenerator: An API key is required"
ErrorId = 77
}
Write-Error "MatroskaTagGenerator: An API key is required" -ErrorAction Stop
}

if ($Path.EndsWith('.xml')) {
$outXML = $Path
}
Expand Down

0 comments on commit a433179

Please sign in to comment.