-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmozaic-dl.ps1
More file actions
80 lines (63 loc) · 2.59 KB
/
Copy pathmozaic-dl.ps1
File metadata and controls
80 lines (63 loc) · 2.59 KB
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
# outputDir is where the downloaded files will be saved.
$outputDir = ".\patches"
# stateFile is used to track already downloaded patches to avoid needless repetition.
$stateFile = "downloadedPatches.txt"
# this endpoint is scheduled to leave alpha on 2020-03-01
# at that time tne new endpoint is v1
# $apiEndpoint = "https://patchstorage.com/api/v1"
$apiEndpoint = "https://patchstorage.com/api/alpha"
# this is the id for the mozaic platform.
# it's a magic integer for the sake of keeping this script simple,
# but can be found with this API request or opening the link:
# irm https://patchstorage.com/api/alpha/platforms?search=mozaic
$mozaicId = 3341
# The API default value is 10, and has a limit of 100.
# Anything higher than 100 throws a 400 error.
$patchesPerRequest = 25
# This is a failsafe to avoid runaway request loops.
# This may not be needed but belts and suspenders...
$maxPages = 30
# start at the first page. This will be used to iterate the requests.
$page = 1
# setup the working files
if (-not (Test-Path -Path $outputDir)) {
New-Item -ItemType Directory -Path $outputDir -ErrorAction Stop
}
if (Test-Path -Path $stateFile -PathType Leaf) {
$priorDownloads = Get-Content $stateFile
}
else {
New-Item -ItemType File -Path $stateFile -ErrorAction Stop
$priorDownloads = @()
}
# initialize the state
$allPatchesFound = $false
$patchesFound = @()
while (-not ($allPatchesFound) -and ($page -le $maxPages)) {
$uri = "{0}/patches/?platforms={1}&per_page={2}&page={3}" -f $apiEndpoint, $mozaicId, $patchesPerRequest, $page
Write-Output "[Query #$page] Performing API query $uri"
$req = Invoke-RestMethod -uri $uri -ErrorAction Stop
# append the patches found in the current request
$patchesFound += $req
# once the query returns fewer results than the patchesPerRequest limit
if ($req.length -lt $patchesPerRequest) {
Write-Output "[Query End] Last query returned $($req.length) results, which is less than the page max of $patchesPerRequest"
$allPatchesFound = $true
}
# increment the page index
$page++
}
foreach ($patch in $patchesFound) {
$patchInfo = Invoke-RestMethod -uri $patch.self
$uri = $patchInfo.files.url
$name = $patchInfo.files.filename
$outFile = "$outputDir\$name"
if ($name -notin $priorDownloads) {
Write-Output "[$name] downloading from $uri to $outFile"
Invoke-RestMethod -Uri $uri -OutFile $outFile -ErrorAction Stop
$name | Out-File -FilePath $stateFile -Append -Force
}
else {
Write-Output "[$name] found in $stateFile as a prior download. Skipping."
}
}