-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorder.js
43 lines (37 loc) · 1.16 KB
/
order.js
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
const fs = require("fs");
const fetch = require("node-fetch");
const BASE_URL = "https://www.ogd.stadt-zuerich.ch/geoportal_order/";
const ORDER_CHECK = 20000;
let { order } = JSON.parse(fs.readFileSync(process.argv[2]));
let destination = process.argv[3];
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
(async function () {
let res = await fetch(BASE_URL, {
method: "POST",
body: JSON.stringify({ order }),
headers: { "Content-Type": "application/json" },
});
let ordered = await res.json();
console.log(ordered);
let statusUrl = BASE_URL + ordered.job_id;
while (true) {
console.log(statusUrl);
let statusRes = await fetch(statusUrl);
let { status } = await statusRes.json();
if (status == "SUCCESS") {
break;
} else if (status == "PULLED") {
await sleep(ORDER_CHECK);
} else {
throw new Error("Unknown status: " + status);
}
}
let downloadUrl = ordered.download_url;
console.log(downloadUrl, destination);
await fetch(downloadUrl).then((res) => {
res.body.pipe(fs.createWriteStream(destination));
});
})().catch((err) => {
console.log(err);
process.exit(1);
});