Src/Public/New-BricksetCatalogue.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
function New-BricksetCatalogue {
    <#
    .SYNOPSIS
        Creates an inventory catalogue of a Brickset collection in HTML, Text & Word formats using the Brickset API.
    .DESCRIPTION
        Creates an inventory catalogue of a Brickset collection in HTML, Text & Word formats using the Brickset API - https://brickset.com/article/52664/api-version-3-documentation.
    .PARAMETER Format
        Specifies the output format of the catalogue.
        The supported output formats are HTML, Text & WORD.
        Multiple output formats may be specified, separated by a comma.
    .PARAMETER Credential
        Specifies the stored credential for the Brickset API.
    .PARAMETER Username
        Specifies the username for the Brickset API.
    .PARAMETER Password
        Specifies the password for the Brickset API.
    .PARAMETER ApiKey
        Specifies an API key to authenticate to the Brickset API.
    .PARAMETER Timestamp
        Specifies whether to append a timestamp string to the catalogue filename.
        By default, the timestamp string is not added to the catalogue filename.
    .PARAMETER OutputFolder
        Specifies the folder path to save the catalogue file.
    .PARAMETER Filename
        Specifies a filename for the catalogue.
    .PARAMETER OrderBy
        Specifies the sort order for the sets.
        The supported sort orders are 'Name', 'Theme', 'Number', 'Pieces', 'QtyOwned', 'Rating'
    .PARAMETER ExcludeWantedSets
        Excludes wanted sets from the catalogue.
    .PARAMETER ExcludeOwnedSets
        Excludes owned sets from the catalogue.
    .PARAMETER ExcludeWantedMinifigs
        Excludes wanted minifigs from the catalogue.
    .PARAMETER ExcludeOwnedMinifigs
        Excludes owned minifigs from the catalogue.
    .PARAMETER ExcludeToC
        Excludes the Table of Contents from the catalogue.
    .EXAMPLE
        PS C:\>New-BricksetCatalogue -Username 'tim@lego.com' -Password 'LEGO!' -ApiKey 'cgY-67-tYUip' -OutputFolder 'C:\MyDocs'
        Creates a Brickset catalogue in HTML format using the specified username, password and API key.
    .EXAMPLE
        PS C:\>New-BricksetCatalogue -Format Word -Credential (Get-Credential) -ApiKey 'cgY-67-tYUip' -OutputFolder 'C:\MyDocs'
        Creates a Brickset catalogue in Word format using a PSCredential and API key.
    .EXAMPLE
        PS C:\>New-BricksetCatalogue -Format HTML,Text,Word -Username 'tim@lego.com' -Password 'LEGO!' -ApiKey 'cgY-67-tYUip' -OutputFolder 'C:\MyDocs'
        Creates a Brickset catalogue in HTML, Text and Word formats using the specified username, password and API key.
    .NOTES
        Version: 0.1.3
        Author: Tim Carman
        Twitter: @tpcarman
        Github: tpcarman
        Credits: Iain Brighton (@iainbrighton) - PScribo module
                        Jonathan Medd (@jonathanmedd) - Brickset module
    .LINK
        https://github.com/tpcarman/BricksetCatalogue
    #>


    [CmdletBinding()]
    param (
        [Parameter(
            Position = 0,
            Mandatory = $false,
            HelpMessage = 'Please provide the Brickset Catalogue output format'
        )]
        [ValidateNotNullOrEmpty()]
        [ValidateSet('Word', 'HTML', 'Text')]
        [Array] $Format = 'HTML',

        [Parameter(
            Position = 1,
            Mandatory = $true,
            HelpMessage = 'Please provide credentials to connect to the Brickset API',
            ParameterSetName = 'Credential'
        )]
        [ValidateNotNullOrEmpty()]
        [PSCredential] $Credential,

        [Parameter(
            Position = 1,
            Mandatory = $true,
            HelpMessage = 'Please provide the username to connect to the Brickset API',
            ParameterSetName = 'UsernameAndPassword'
        )]
        [ValidateNotNullOrEmpty()]
        [String] $Username,

        [Parameter(
            Position = 2,
            Mandatory = $true,
            HelpMessage = 'Please provide the password to connect to the Brickset API',
            ParameterSetName = 'UsernameAndPassword'
        )]
        [ValidateNotNullOrEmpty()]
        [String] $Password,

        [Parameter(
            Position = 3,
            Mandatory = $true,
            HelpMessage = 'Please provide Brickset API key'
        )]
        [ValidateNotNullOrEmpty()]
        [String] $ApiKey,

        [Parameter(
            Position = 4,
            Mandatory = $false,
            HelpMessage = 'Specify the Brickset Catalogue filename'
        )]
        [ValidateNotNullOrEmpty()]
        [String] $Filename = 'BricksetCatalogue',

        [Parameter(
            Position = 5,
            Mandatory = $true,
            HelpMessage = 'Please provide the folder path to save the Brickset Catalogue file'
        )]
        [ValidateNotNullOrEmpty()]
        [String] $OutputFolder,

        [Parameter(
            Mandatory = $false,
            HelpMessage = 'Specify whether to append a timestamp to the document filename'
        )]
        [Switch] $Timestamp = $false,

        [Parameter(
            Mandatory = $false,
            HelpMessage = 'Specify whether to append a timestamp to the document filename'
        )]
        [ValidateSet('Name', 'Theme', 'Number', 'Pieces', 'QtyOwned', 'Rating')]
        [String] $OrderBy = 'Theme',

        [Parameter(
            Mandatory = $false,
            HelpMessage = 'Specify whether to exclude wanted sets from the catalogue'
        )]
        [Switch] $ExcludeWantedSets = $false,

        [Parameter(
            Mandatory = $false,
            HelpMessage = 'Specify whether to exclude wanted minifigs from the catalogue'
        )]
        [Switch] $ExcludeWantedMinifigs = $false,

        [Parameter(
            Mandatory = $false,
            HelpMessage = 'Specify whether to exclude owned sets from the catalogue'
        )]
        [Switch] $ExcludeOwnedSets = $false,

        [Parameter(
            Mandatory = $false,
            HelpMessage = 'Specify whether to exclude owned minifigs from the catalogue'
        )]
        [Switch] $ExcludeOwnedMinifigs = $false,

        [Parameter(
            Mandatory = $false,
            HelpMessage = 'Specify whether to exclude the Table of Contents from the catalogue'
        )]
        [Switch] $ExcludeToC = $false
    )

    # If Username & Password are used, convert to PSCredential
    if (($Username -and $Password)) {
        $SecurePassword = ConvertTo-SecureString $Password -AsPlainText -Force
        $Credential = New-Object System.Management.Automation.PSCredential ($Username, $SecurePassword)
    }

    # If Timestamp parameter is specified, add the timestamp to the catalogue filename
    if ($Timestamp) {
        $FileName = $Filename + " - " + (Get-Date -Format 'yyyy-MM-dd_HH.mm.ss')
    }

    # Check for output folder
    if (!(Test-Path -Path $OutputFolder)) {
        Write-Error -Message "Output folder not found [$($OutputFolder)]" -ErrorAction Stop
    }

    # Connect to Brickset API
    Try {
        $BrickSet = Connect-Brickset -apiKey $ApiKey -credential $Credential -ErrorAction Stop
    } Catch {
        Write-Error $_
    }

    if ($BrickSet) {
        # Brickset Collection Sets
        if (!($ExcludeOwnedSets)) {
            $BrickSetSetOwned = Get-BricksetSetOwned -orderBy $OrderBy
            $TotalSetsOwned = ($BrickSetSetOwned.collection.qtyowned | Measure-Object -Sum).Sum
            $UniqueSetsOwned = ($BrickSetSetOwned.collection.qtyowned | Measure-Object -Sum).Count
            $TotalSetPieceCount = ($BrickSetSetOwned.pieces | Measure-Object -Sum).Sum
            $BrickSetSetOwnedThemes = $BrickSetSetOwned.theme | Select-Object -Unique | Sort-Object
        }

        if (!($ExcludeWantedSets)) {
            $BrickSetSetWanted = Get-BricksetSetWanted -orderBy $OrderBy
            $BrickSetSetWantedThemes = $BrickSetSetWanted.theme | Select-Object -Unique | Sort-Object
        }

        # Brickset Collection Minifigs
        if (!($ExcludeOwnedMinifigs)) {
            $BricksetMinifigOwned = Get-BricksetMinifigCollectionOwned | Sort-Object name
        }
        if (!($ExcludeWantedMinifigs)) {
            $BricksetMinifigWanted = Get-BricksetMinifigCollectionWanted | Sort-Object name
        }

        # Create Brickset Catalogue
        $BrickSetCatalogue = Document -Name $FileName {
            # Set Document Style
            Set-DocStyle

            # Set Header & Footer
            Header -Default {
                Paragraph -Style Header "Brickset Collection - $($Filename)"
            }

            Footer -Default {
                Paragraph -Style Footer 'Page <!# PageNumber #!> of <!# TotalPages #!>'
            }

            # Cover Page
            Set-CoverPage

            # Table of Contents
            if (!($ExcludeToC)) {
                TOC
                PageBreak
            }

            # Summary Sections
            if ($BrickSetSetOwned) {
                Section -Style Heading1 "Summary" {
                    $BrickSetCollection = [PSCustomObject]@{
                        'Total Sets Owned' = $TotalSetsOwned
                        'Unique Sets Owned' = $UniqueSetsOwned
                        'Total Set Piece Count' = $TotalSetPieceCount
                    }
                    $TableParams = @{
                        Name = 'Brickset Collection Summary'
                        List = $false
                        ColumnWidths = 33, 34, 33
                        Caption = '- Brickset Collection Summary'
                    }
                    $BrickSetCollection | Table @TableParams
                }
            }

            # Sets Sections
            if (($BrickSetSetOwned) -or ($BrickSetSetWanted)) {
                Section -Style Heading1 "Sets" {
                    if (($BrickSetSetOwned) -and (!($ExcludeOwnedSets))) {
                        Section -Style Heading2 "Owned" {
                            foreach ($SetTheme in $BrickSetSetOwnedThemes) {
                                Section -Style Heading3 $($SetTheme) {
                                    $BrickSetSetOwnedByTheme = $BrickSetSetOwned | Where-Object {$_.theme -eq $SetTheme}
                                    foreach ($SetOwned in $BrickSetSetOwnedByTheme) {
                                        Section -Style Heading4 -ExcludeFromTOC "$($SetOwned.Number): $($SetOwned.Name)" {
                                            $Instructions = Get-BricksetSetInstructions -setId $SetOwned.setId
                                            Image -Uri $SetOwned.image.thumbnailURL -Align Center
                                            Blankline
                                            $SetOwnedInfo = [PSCustomObject] @{
                                                'Set Number' = $SetOwned.Number
                                                'Name' = $SetOwned.Name
                                                'Set Type' = $SetOwned.category
                                                'Theme Group' = $SetOwned.themeGroup
                                                'Theme' = $SetOwned.Theme
                                                'Year Released' = $SetOwned.Year
                                                'Pieces' = $SetOwned.Pieces
                                                'Minifigs' = Switch ($SetOwned.Minifigs) {
                                                    $null { '0' }
                                                    default { $SetOwned.Minifigs }
                                                }
                                                'Age Range' = Switch ($SetOwned.ageRange.max) {
                                                    $null { "$($SetOwned.ageRange.min)+" }
                                                    default { "$($SetOwned.ageRange.min) - $($SetOwned.ageRange.max)" }
                                                }
                                                'Packaging' = $SetOwned.PackagingType
                                                'Availability' = $SetOwned.Availability
                                                'Qty Owned' = $SetOwned.collection.qtyOwned
                                                'Instructions' = $Instructions.URL -join [Environment]::NewLine
                                                'Rating' = $SetOwned.Rating
                                                'Notes' = Switch ($SetOwned.collection.notes) {
                                                    $null { '' }
                                                    default { $SetOwned.collection.notes }
                                                }
                                                'BrickSet URL' = $SetOwned.bricksetURL
                                            }
                                            $TableParams = @{
                                                Name = "$($SetOwned.Number): $($SetOwned.Name)"
                                                List = $true
                                                ColumnWidths = 50, 50
                                                Caption = "- $($SetOwned.Number): $($SetOwned.Name)"
                                            }
                                            $SetOwnedInfo | Table @TableParams
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if (($BrickSetSetWanted) -and (!($ExcludeWantedSets))) {
                        Section -Style Heading2 "Wanted" {
                            foreach ($SetTheme in $BrickSetSetWantedThemes) {
                                Section -Style Heading3 $($SetTheme) {
                                    $BrickSetSetWantedByTheme = $BrickSetSetWanted | Where-Object {$_.theme -eq $SetTheme}
                                    foreach ($SetWanted in $BrickSetSetWantedByTheme) {
                                        Section -Style Heading4 -ExcludeFromTOC "$($SetWanted.Number): $($SetWanted.Name)" {
                                            $Instructions = Get-BricksetSetInstructions -setId $SetWanted.setId
                                            Image -Uri $SetWanted.image.thumbnailURL -Align Center
                                            Blankline
                                            $SetWantedInfo = [PSCustomObject] @{
                                                'Set Number' = $SetWanted.Number
                                                'Name' = $SetWanted.Name
                                                'Set Type' = $SetWanted.category
                                                'Theme Group' = $SetWanted.themeGroup
                                                'Theme' = $SetWanted.Theme
                                                'Year Released' = $SetWanted.Year
                                                'Pieces' = $SetWanted.Pieces
                                                'Minifigs' = Switch ($SetWanted.Minifigs) {
                                                    $null { '0' }
                                                    default { $SetWanted.Minifigs }
                                                }
                                                'Age Range' = Switch ($SetWanted.ageRange.max) {
                                                    $null { "$($SetWanted.ageRange.min)+" }
                                                    default { "$($SetWanted.ageRange.min) - $($SetWanted.ageRange.max)" }
                                                }
                                                'Packaging' = $SetWanted.PackagingType
                                                'Availability' = $SetWanted.Availability
                                                'Qty Owned' = $SetWanted.collection.qtyOwned
                                                'Instructions' = $Instructions.URL -join [Environment]::NewLine
                                                'Rating' = $SetWanted.Rating
                                                'Notes' = Switch ($SetWanted.collection.notes) {
                                                    $null { '' }
                                                    default { $SetWanted.collection.notes }
                                                }
                                                'BrickSet URL' = $SetWanted.bricksetURL
                                            }
                                            $TableParams = @{
                                                Name = "$($SetWanted.Number): $($SetWanted.Name)"
                                                List = $true
                                                ColumnWidths = 50, 50
                                                Caption = "- $($SetWanted.Number): $($SetWanted.Name)"
                                            }
                                            $SetWantedInfo | Table @TableParams
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            # Minifigs Sections
            if (($BricksetMinifigOwned) -or ($BricksetMinifigWanted)) {
                Section -Style Heading1 "Minfigs" {
                    if (($BricksetMinifigOwned) -and (!($ExcludeOwnedMinifigs))) {
                        Section -Style Heading2 "Owned" {
                            foreach ($MinfigOwned in $BricksetMinifigOwned) {
                                Section -Style Heading3 -ExcludeFromTOC "$($MinfigOwned.minifigNumber): $($MinfigOwned.Name)" {
                                    Image -Uri "https://img.bricklink.com/ItemImage/MN/0/$($MinfigOwned.minifigNumber).png" -Align Center -Percent 50 -Text $($MinfigOwned.minifigNumber)
                                    Blankline
                                    $MinifigOwnedInfo = [PSCustomObject]@{
                                        'Name' = $MinfigOwned.Name
                                        'Number' = $MinfigOwned.minifigNumber
                                        'Category' = $MinfigOwned.Category
                                        'Owned in Sets' = $MinfigOwned.OwnedInSets
                                        'Owned Loose' = $MinfigOwned.OwnedLoose
                                    }
                                    $TableParams = @{
                                        Name = "$($MinfigOwned.minifigNumber): $($MinfigOwned.Name)"
                                        List = $true
                                        ColumnWidths = 50, 50
                                        Caption = "- $($MinfigOwned.minifigNumber): $($MinfigOwned.Name)"
                                    }
                                    $MinifigOwnedInfo | Table @TableParams
                                }
                            }
                        }
                    }
                    if (($BricksetMinifigWanted) -and (!($ExcludeWantedMinifigs))) {
                        Section -Style Heading2 "Wanted" {
                            foreach ($MinifigWanted in $BricksetMinifigWanted) {
                                Section -Style Heading3 -ExcludeFromTOC "$($MinifigWanted.minifigNumber): $($MinifigWanted.Name)" {
                                    Image -Uri "https://img.bricklink.com/ItemImage/MN/0/$($MinifigWanted.minifigNumber).png" -Align Center -Percent 50 -Text $($MinifigWanted.minifigNumber)
                                    Blankline
                                    $MinifigOwnedInfo = [PSCustomObject]@{
                                        'Name' = $MinifigWanted.Name
                                        'Number' = $MinifigWanted.minifigNumber
                                        'Category' = $MinifigWanted.Category
                                        'Owned in Sets' = $MinifigWanted.OwnedInSets
                                        'Owned Loose' = $MinifigWanted.OwnedLoose
                                    }
                                    $TableParams = @{
                                        Name = "$($MinifigWanted.minifigNumber): $($MinifigWanted.Name)"
                                        List = $true
                                        ColumnWidths = 50, 50
                                        Caption = "- $($MinifigWanted.minifigNumber): $($MinifigWanted.Name)"
                                    }
                                    $MinifigOwnedInfo | Table @TableParams
                                }
                            }
                        }
                    }
                }
            }
        }
        Export-Document -Document $BrickSetCatalogue -Format $Format -Path $OutputFolder
        Disconnect-Brickset -Confirm:$false
    }
}