-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGet-States.ps1
47 lines (37 loc) · 1.5 KB
/
Get-States.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
<#
.SYNOPSIS
Cache states, cities and districts.
.DESCRIPTION
Script to cache states, cities and districts from API in file states.json.
.EXAMPLE
PS> .\Get-States
#>
if (-not (Test-Path ".\states.json" -PathType Leaf)) {
$unixMillis = ([System.DateTimeOffset]::Now.ToUnixTimeMilliseconds());
$url = "https://vadimklimenko.com/map/statuses.json?t=$unixMillis";
try {
Write-Output "Завантажую інформацію про області, міста та райони..."
$response = Invoke-RestMethod $url -Method GET -ContentType "application/json";
}
catch {
throw "Помилка при завантаженні";
exit 1;
}
$responseArray = $response.states.psobject.properties | Select-Object @{
Name = "name";
Expression = { $_.Name };
},
@{
Name = "districts";
Expression = { $_.Value.districts.psobject.properties | Select-Object @{ Name = "name"; Expression = { $_.Name }; } };
};
$jsonResponseArray = ($responseArray | ConvertTo-Json -Depth 3);
New-Item -ItemType File -Path ".\states.json" | Out-Null;
Set-Content -Path ".\states.json" -Value $jsonResponseArray | Out-Null;
Write-Output "Інформацію записано у states.json";
}
else {
Write-Output "Інформацію уже записано до states.json. Обробляю..."
$responseArray = Get-Content "states.json" | ConvertFrom-Json
}
return $responseArray;